Fix transaction on nil Go func migrations

pull/4/head
Vojtech Vitek (V-Teq) 2016-03-08 12:40:16 -05:00
parent ae64375f29
commit 946d2ad074
1 changed files with 10 additions and 12 deletions

View File

@ -81,23 +81,21 @@ func RunMigrations(db *sql.DB, dir string, target int64) (err error) {
} }
case ".go": case ".go":
fn := m.Up
if !direction {
fn = m.Down
}
if fn == nil {
continue
}
tx, err := db.Begin() tx, err := db.Begin()
if err != nil { if err != nil {
log.Fatal("db.Begin: ", err) log.Fatal("db.Begin: ", err)
} }
if err := fn(tx); err != nil { fn := m.Up
tx.Rollback() if !direction {
log.Fatalf("FAIL %s (%v), quitting migration.", filepath.Base(m.Source), err) fn = m.Down
return err }
if fn != nil {
if err := fn(tx); err != nil {
tx.Rollback()
log.Fatalf("FAIL %s (%v), quitting migration.", filepath.Base(m.Source), err)
return err
}
} }
if err = FinalizeMigration(tx, direction, m.Version); err != nil { if err = FinalizeMigration(tx, direction, m.Version); err != nil {