Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration error #934

Merged
merged 10 commits into from
Aug 22, 2024
4 changes: 2 additions & 2 deletions pkg/gofr/migration/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 0 additions & 9 deletions pkg/gofr/migration/clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,3 @@ func Test_ClickHouseBeginTransaction(t *testing.T) {

assert.Contains(t, logs, "Clickhouse Migrator begin successfully")
}

func Test_ClickHouseRollback(t *testing.T) {
srijan-27 marked this conversation as resolved.
Show resolved Hide resolved
logs := testutil.StderrOutputForFunc(func() {
mg, _, mockContainer := clickHouseSetup(t)
mg.rollback(mockContainer, transactionData{MigrationNumber: 0})
})

assert.Contains(t, logs, "Migration 0 failed")
}
2 changes: 1 addition & 1 deletion pkg/gofr/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/gofr/migration/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -115,20 +117,20 @@ 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)

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")
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/gofr/migration/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions pkg/gofr/migration/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading