Implement rollback command

This commit is contained in:
Adrian Macneil 2015-11-25 12:26:57 -08:00
parent ece5d3cf0e
commit 1c4cf2c122
4 changed files with 138 additions and 25 deletions

View file

@ -14,7 +14,7 @@ type Driver interface {
CreateDatabase(*url.URL) error
DropDatabase(*url.URL) error
CreateMigrationsTable(*sql.DB) error
SelectMigrations(*sql.DB) (map[string]struct{}, error)
SelectMigrations(*sql.DB, int) (map[string]struct{}, error)
InsertMigration(shared.Transaction, string) error
DeleteMigration(shared.Transaction, string) error
}

View file

@ -67,8 +67,13 @@ func (postgres Driver) CreateMigrationsTable(db *sql.DB) error {
}
// SelectMigrations returns a list of applied migrations
func (postgres Driver) SelectMigrations(db *sql.DB) (map[string]struct{}, error) {
rows, err := db.Query("SELECT version FROM schema_migrations")
// with an optional limit (in descending order)
func (postgres Driver) SelectMigrations(db *sql.DB, limit int) (map[string]struct{}, error) {
query := "SELECT version FROM schema_migrations ORDER BY version DESC"
if limit >= 0 {
query = fmt.Sprintf("%s LIMIT %d", query, limit)
}
rows, err := db.Query(query)
if err != nil {
return nil, err
}