diff --git a/pkg/gofr/migration/clickhouse.go b/pkg/gofr/migration/clickhouse.go index 5cb139518..c0398a2d5 100644 --- a/pkg/gofr/migration/clickhouse.go +++ b/pkg/gofr/migration/clickhouse.go @@ -99,7 +99,7 @@ func (ch clickHouseMigrator) commitMigration(c *container.Container, data transa } func (ch clickHouseMigrator) rollback(c *container.Container, data transactionData) { - c.Errorf("Migration %v failed", data.MigrationNumber) - ch.migrator.rollback(c, data) + + c.Fatalf("migration %v failed and rolled back", data.MigrationNumber) } diff --git a/pkg/gofr/migration/clickhouse_test.go b/pkg/gofr/migration/clickhouse_test.go index e26946422..444883d82 100644 --- a/pkg/gofr/migration/clickhouse_test.go +++ b/pkg/gofr/migration/clickhouse_test.go @@ -108,12 +108,3 @@ func Test_ClickHouseBeginTransaction(t *testing.T) { assert.Contains(t, logs, "Clickhouse Migrator begin successfully") } - -func Test_ClickHouseRollback(t *testing.T) { - logs := testutil.StderrOutputForFunc(func() { - mg, _, mockContainer := clickHouseSetup(t) - mg.rollback(mockContainer, transactionData{MigrationNumber: 0}) - }) - - assert.Contains(t, logs, "Migration 0 failed") -} diff --git a/pkg/gofr/migration/migration.go b/pkg/gofr/migration/migration.go index aa9e30732..c644a81ce 100644 --- a/pkg/gofr/migration/migration.go +++ b/pkg/gofr/migration/migration.go @@ -48,7 +48,7 @@ func Run(migrationsMap map[int64]Migrate, c *container.Container) { err := mg.checkAndCreateMigrationTable(c) if err != nil { - c.Errorf("failed to create gofr_migration table, err: %v", err) + c.Fatalf("failed to create gofr_migration table, err: %v", err) return } diff --git a/pkg/gofr/migration/migration_test.go b/pkg/gofr/migration/migration_test.go index 5a7d3c20c..86bcd6737 100644 --- a/pkg/gofr/migration/migration_test.go +++ b/pkg/gofr/migration/migration_test.go @@ -103,7 +103,9 @@ func TestMigrationRunClickhouseSuccess(t *testing.T) { } func TestMigrationRunClickhouseMigrationFailure(t *testing.T) { - logs := testutil.StderrOutputForFunc(func() { + mockClickHouse, mockContainer := initialiseClickHouseRunMocks(t) + + testutil.StderrOutputForFunc(func() { migrationMap := map[int64]Migrate{ 1: {UP: func(d Datasource) error { err := d.Clickhouse.Exec(context.Background(), "SELECT * FROM users") @@ -115,8 +117,6 @@ func TestMigrationRunClickhouseMigrationFailure(t *testing.T) { }}, } - mockClickHouse, mockContainer := initialiseClickHouseRunMocks(t) - mockClickHouse.EXPECT().Exec(gomock.Any(), CheckAndCreateChMigrationTable).Return(nil) mockClickHouse.EXPECT().Select(gomock.Any(), gomock.Any(), getLastChGoFrMigration).Return(nil) mockClickHouse.EXPECT().Exec(gomock.Any(), "SELECT * FROM users").Return(sql.ErrConnDone) @@ -124,11 +124,13 @@ func TestMigrationRunClickhouseMigrationFailure(t *testing.T) { Run(migrationMap, mockContainer) }) - assert.Contains(t, logs, "Migration 1 failed") + assert.True(t, mockClickHouse.ctrl.Satisfied()) } func TestMigrationRunClickhouseMigrationFailureWhileCheckingTable(t *testing.T) { - logs := testutil.StderrOutputForFunc(func() { + mockClickHouse, mockContainer := initialiseClickHouseRunMocks(t) + + testutil.StderrOutputForFunc(func() { migrationMap := map[int64]Migrate{ 1: {UP: func(d Datasource) error { err := d.Clickhouse.Exec(context.Background(), "SELECT * FROM users") @@ -140,14 +142,12 @@ func TestMigrationRunClickhouseMigrationFailureWhileCheckingTable(t *testing.T) }}, } - mockClickHouse, mockContainer := initialiseClickHouseRunMocks(t) - mockClickHouse.EXPECT().Exec(gomock.Any(), CheckAndCreateChMigrationTable).Return(sql.ErrConnDone) Run(migrationMap, mockContainer) }) - assert.Contains(t, logs, "failed to create gofr_migration table, err: sql: connection is already closed") + assert.True(t, mockClickHouse.ctrl.Satisfied()) } func TestMigrationRunClickhouseCurrentMigrationEqualLastMigration(t *testing.T) { diff --git a/pkg/gofr/migration/redis.go b/pkg/gofr/migration/redis.go index 3e1960242..430cc7ed2 100644 --- a/pkg/gofr/migration/redis.go +++ b/pkg/gofr/migration/redis.go @@ -121,6 +121,7 @@ func (m redisMigrator) commitMigration(c *container.Container, data transactionD func (m redisMigrator) rollback(c *container.Container, data transactionData) { data.RedisTx.Discard() - c.Errorf("Migration %v for Redis failed and rolled back", data.MigrationNumber) m.migrator.rollback(c, data) + + c.Fatalf("Migration %v for Redis failed and rolled back", data.MigrationNumber) } diff --git a/pkg/gofr/migration/sql.go b/pkg/gofr/migration/sql.go index a711cbf2b..d1b629486 100644 --- a/pkg/gofr/migration/sql.go +++ b/pkg/gofr/migration/sql.go @@ -128,7 +128,7 @@ func (d sqlMigrator) rollback(c *container.Container, data transactionData) { c.Error("unable to rollback transaction: %v", err) } - c.Errorf("Migration %v failed and rolled back", data.MigrationNumber) - d.migrator.rollback(c, data) + + c.Fatalf("Migration %v failed and rolled back", data.MigrationNumber) }