diff --git a/executor.go b/executor.go index 24f1e43..cb86f23 100644 --- a/executor.go +++ b/executor.go @@ -28,11 +28,6 @@ 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 @@ -40,8 +35,6 @@ func (e *SQLExecutor) Up(migration *Migration, store Store) error { return store.Insert(migration, tx) }) - - return tx.Commit() } // Down reverses a migrations. @@ -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() diff --git a/executor_test.go b/executor_test.go index 44ca2c1..a4a4ba8 100644 --- a/executor_test.go +++ b/executor_test.go @@ -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") diff --git a/gloat_test.go b/gloat_test.go index dc6dec8..a8c2bf2 100644 --- a/gloat_test.go +++ b/gloat_test.go @@ -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 } @@ -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) } @@ -100,6 +101,7 @@ func TestUnapplied_Empty(t *testing.T) { &Migration{Version: 20170329154959}, &Migration{Version: 20170511172647}, &Migration{Version: 20180905150724}, + &Migration{Version: 20180920181906}, }, } @@ -115,6 +117,7 @@ func TestUnapplied_MissingInSource(t *testing.T) { &Migration{Version: 20170329154959}, &Migration{Version: 20170511172647}, &Migration{Version: 20180905150724}, + &Migration{Version: 20180920181906}, }, } diff --git a/source_test.go b/source_test.go index 0c992ac..b5878df 100644 --- a/source_test.go +++ b/source_test.go @@ -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) } diff --git a/testdata/migrations/20180920181906_migration_with_an_error/down.sql b/testdata/migrations/20180920181906_migration_with_an_error/down.sql new file mode 100644 index 0000000..55a9f9f --- /dev/null +++ b/testdata/migrations/20180920181906_migration_with_an_error/down.sql @@ -0,0 +1 @@ +DROP TABL users; diff --git a/testdata/migrations/20180920181906_migration_with_an_error/up.sql b/testdata/migrations/20180920181906_migration_with_an_error/up.sql new file mode 100644 index 0000000..ef0bb38 --- /dev/null +++ b/testdata/migrations/20180920181906_migration_with_an_error/up.sql @@ -0,0 +1,4 @@ +CREATE TABL users ( + id bigserial PRIMARY KEY NOT NULL +); +