Skip to content

Commit

Permalink
Fix the no-error reporting on failed migration applications
Browse files Browse the repository at this point in the history
We need to return the initial error and the error about whether the
reversal of the transaction was successful.
  • Loading branch information
gsamokovarov committed Sep 20, 2018
1 parent be5060b commit 6069fe9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
10 changes: 2 additions & 8 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,13 @@ type SQLExecutor struct {

// Up applies a migration.
func (e *SQLExecutor) Up(migration *Migration, store Store) error {
tx, err := e.db.Begin()
if err != nil {
return err
}

return e.exec(migration.Options.Transaction, func(tx SQLExecer) error {
if _, err := tx.Exec(string(migration.UpSQL)); err != nil {
return err
}

return store.Insert(migration, tx)
})

return tx.Commit()
}

// Down reverses a migrations.
Expand Down Expand Up @@ -70,7 +63,8 @@ func (e *SQLExecutor) exec(transaction bool, action func(SQLExecer) error) error
}

if err := action(tx); err != nil {
return tx.Rollback()
defer tx.Rollback()
return err
}

return tx.Commit()
Expand Down
14 changes: 14 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ func TestSQLExecutor_Up(t *testing.T) {
})
}

func TestSQLExecutor_Up_Broken(t *testing.T) {
td := filepath.Join(dbSrc, "20180920181906_migration_with_an_error")

exe := NewSQLExecutor(db)

migration, err := MigrationFromBytes(td, ioutil.ReadFile)
assert.Nil(t, err)

cleanState(func() {
err := exe.Up(migration, new(testingStore))
assert.Error(t, err)
})
}

func TestSQLExecutor_Down(t *testing.T) {
td := filepath.Join(dbSrc, "20170329154959_introduce_domain_model")

Expand Down
13 changes: 8 additions & 5 deletions gloat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
var (
gl Gloat

db *sql.DB
dbURL string
dbSrc string
dbDriver string
db *sql.DB
dbURL string
dbSrc string
dbSrcBroken string
dbDriver string
)

type testingStore struct{ applied Migrations }
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestUnapplied(t *testing.T) {

migrations, err := gl.Unapplied()
assert.Nil(t, err)
assert.Len(t, 3, migrations)
assert.Len(t, 4, migrations)

assert.Equal(t, 20170329154959, migrations[0].Version)
}
Expand All @@ -100,6 +101,7 @@ func TestUnapplied_Empty(t *testing.T) {
&Migration{Version: 20170329154959},
&Migration{Version: 20170511172647},
&Migration{Version: 20180905150724},
&Migration{Version: 20180920181906},
},
}

Expand All @@ -115,6 +117,7 @@ func TestUnapplied_MissingInSource(t *testing.T) {
&Migration{Version: 20170329154959},
&Migration{Version: 20170511172647},
&Migration{Version: 20180905150724},
&Migration{Version: 20180920181906},
},
}

Expand Down
5 changes: 4 additions & 1 deletion source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func TestFileSystemSourceCollect(t *testing.T) {
m3, err := MigrationFromBytes(filepath.Join(td, "20180905150724_concurrent_migration"), ioutil.ReadFile)
assert.Nil(t, err)

expectedMigrations := Migrations{m1, m2, m3}
m4, err := MigrationFromBytes(filepath.Join(td, "20180920181906_migration_with_an_error"), ioutil.ReadFile)
assert.Nil(t, err)

expectedMigrations := Migrations{m1, m2, m3, m4}
assert.Equal(t, migrations, expectedMigrations)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABL users;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABL users (
id bigserial PRIMARY KEY NOT NULL
);

0 comments on commit 6069fe9

Please sign in to comment.