Skip to content

Commit b0082f9

Browse files
authored
fix: update migrations history separately from applying (#2018)
* Revert "fix(db): create history table before inserting migration" This reverts commit c3623a8. * chore: update unit tests
1 parent b2f476b commit b0082f9

File tree

8 files changed

+33
-24
lines changed

8 files changed

+33
-24
lines changed

internal/db/diff/diff_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ func TestMigrateShadow(t *testing.T) {
148148
conn.Query(utils.GlobalsSql).
149149
Reply("CREATE SCHEMA").
150150
Query(utils.InitialSchemaSql).
151-
Reply("CREATE SCHEMA").
152-
Query(sql).
153151
Reply("CREATE SCHEMA")
154152
pgtest.MockMigrationHistory(conn)
155-
conn.Query(history.INSERT_MIGRATION_VERSION, "0", "test", fmt.Sprintf("{%s}", sql)).
153+
conn.Query(sql).
154+
Reply("CREATE SCHEMA").
155+
Query(history.INSERT_MIGRATION_VERSION, "0", "test", fmt.Sprintf("{%s}", sql)).
156156
Reply("INSERT 0 1")
157157
// Run test
158158
err := MigrateShadowDatabase(context.Background(), "test-shadow-db", fsys, conn.Intercept)
@@ -313,11 +313,11 @@ At statement 0: create schema public`)
313313
conn.Query(utils.GlobalsSql).
314314
Reply("CREATE SCHEMA").
315315
Query(utils.InitialSchemaSql).
316-
Reply("CREATE SCHEMA").
317-
Query(sql).
318316
Reply("CREATE SCHEMA")
319317
pgtest.MockMigrationHistory(conn)
320-
conn.Query(history.INSERT_MIGRATION_VERSION, "0", "test", fmt.Sprintf("{%s}", sql)).
318+
conn.Query(sql).
319+
Reply("CREATE SCHEMA").
320+
Query(history.INSERT_MIGRATION_VERSION, "0", "test", fmt.Sprintf("{%s}", sql)).
321321
Reply("INSERT 0 1")
322322
// Run test
323323
diff, err := DiffDatabase(context.Background(), []string{"public"}, dbConfig, io.Discard, fsys, DiffSchemaMigra, conn.Intercept)

internal/db/push/push_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ func TestMigrationPush(t *testing.T) {
101101
err := Run(context.Background(), false, false, false, false, dbConfig, fsys, conn.Intercept)
102102
// Check error
103103
assert.ErrorContains(t, err, `ERROR: null value in column "version" of relation "schema_migrations" (SQLSTATE 23502)`)
104-
assert.ErrorContains(t, err, "At statement 4: "+history.INSERT_MIGRATION_VERSION)
104+
assert.ErrorContains(t, err, "At statement 0: "+history.INSERT_MIGRATION_VERSION)
105105
})
106106
}

internal/migration/apply/apply.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func SeedDatabase(ctx context.Context, conn *pgx.Conn, fsys afero.Fs) error {
3939
}
4040

4141
func MigrateUp(ctx context.Context, conn *pgx.Conn, pending []string, fsys afero.Fs) error {
42+
if len(pending) > 0 {
43+
if err := repair.CreateMigrationTable(ctx, conn); err != nil {
44+
return err
45+
}
46+
}
4247
for _, filename := range pending {
4348
if err := applyMigration(ctx, conn, filename, fsys); err != nil {
4449
return err

internal/migration/apply/apply_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ func TestMigrateDatabase(t *testing.T) {
2828
// Setup mock postgres
2929
conn := pgtest.NewConn()
3030
defer conn.Close(t)
31-
conn.Query(sql).
32-
Reply("CREATE SCHEMA")
3331
pgtest.MockMigrationHistory(conn)
34-
conn.Query(history.INSERT_MIGRATION_VERSION, "0", "test", fmt.Sprintf("{%s}", sql)).
32+
conn.Query(sql).
33+
Reply("CREATE SCHEMA").
34+
Query(history.INSERT_MIGRATION_VERSION, "0", "test", fmt.Sprintf("{%s}", sql)).
3535
Reply("INSERT 0 1")
3636
// Connect to mock
3737
ctx := context.Background()
@@ -122,6 +122,7 @@ func TestMigrateUp(t *testing.T) {
122122
// Setup mock postgres
123123
conn := pgtest.NewConn()
124124
defer conn.Close(t)
125+
pgtest.MockMigrationHistory(conn)
125126
// Connect to mock
126127
ctx := context.Background()
127128
mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept)

internal/migration/repair/repair.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ func CreateMigrationTable(ctx context.Context, conn *pgx.Conn) error {
100100
}
101101

102102
func InsertVersionSQL(batch *pgconn.Batch, version, name string, stats []string) {
103-
// Create history table if not exists
104-
history.AddCreateTableStatements(batch)
105103
encoded := []byte{'{'}
106104
for i, line := range stats {
107105
if i > 0 {

internal/migration/repair/repair_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ func TestMigrationFile(t *testing.T) {
126126
conn := pgtest.NewConn()
127127
defer conn.Close(t)
128128
conn.Query(migration.Lines[0]).
129-
ReplyError(pgerrcode.DuplicateSchema, `schema "public" already exists`)
130-
pgtest.MockMigrationHistory(conn)
131-
conn.Query(history.INSERT_MIGRATION_VERSION, "0", "", fmt.Sprintf("{%s}", migration.Lines[0])).
129+
ReplyError(pgerrcode.DuplicateSchema, `schema "public" already exists`).
130+
Query(history.INSERT_MIGRATION_VERSION, "0", "", fmt.Sprintf("{%s}", migration.Lines[0])).
132131
Reply("INSERT 0 1")
133132
// Connect to mock
134133
ctx := context.Background()

internal/migration/squash/squash.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ func baselineMigrations(ctx context.Context, config pgconn.Config, version strin
115115
return err
116116
}
117117
defer conn.Close(context.Background())
118+
if err := repair.CreateMigrationTable(ctx, conn); err != nil {
119+
return err
120+
}
118121
m, err := repair.NewMigrationFromVersion(version, fsys)
119122
if err != nil {
120123
return err

internal/migration/squash/squash_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ func TestSquashCommand(t *testing.T) {
5959
// Setup mock postgres
6060
conn := pgtest.NewConn()
6161
defer conn.Close(t)
62-
conn.Query(sql).
63-
Reply("CREATE SCHEMA")
64-
pgtest.MockMigrationHistory(conn)
65-
conn.Query(history.INSERT_MIGRATION_VERSION, "0", "init", fmt.Sprintf("{%s}", sql)).
66-
Reply("INSERT 0 1")
6762
pgtest.MockMigrationHistory(conn)
68-
conn.Query(history.INSERT_MIGRATION_VERSION, "1", "target", "{}").
63+
conn.Query(sql).
64+
Reply("CREATE SCHEMA").
65+
Query(history.INSERT_MIGRATION_VERSION, "0", "init", fmt.Sprintf("{%s}", sql)).
66+
Reply("INSERT 0 1").
67+
Query(history.INSERT_MIGRATION_VERSION, "1", "target", "{}").
6968
Reply("INSERT 0 1")
7069
// Run test
7170
err := Run(context.Background(), "", pgconn.Config{
@@ -93,6 +92,7 @@ func TestSquashCommand(t *testing.T) {
9392
// Setup mock postgres
9493
conn := pgtest.NewConn()
9594
defer conn.Close(t)
95+
pgtest.MockMigrationHistory(conn)
9696
conn.Query(fmt.Sprintf("DELETE FROM supabase_migrations.schema_migrations WHERE version <= ('0');INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES(('0'), ('init'), ('{%s}'))", sql)).
9797
Reply("INSERT 0 1")
9898
// Run test
@@ -227,10 +227,10 @@ func TestSquashMigrations(t *testing.T) {
227227
// Setup mock postgres
228228
conn := pgtest.NewConn()
229229
defer conn.Close(t)
230-
conn.Query(sql).
231-
Reply("CREATE SCHEMA")
232230
pgtest.MockMigrationHistory(conn)
233-
conn.Query(history.INSERT_MIGRATION_VERSION, "0", "init", fmt.Sprintf("{%s}", sql)).
231+
conn.Query(sql).
232+
Reply("CREATE SCHEMA").
233+
Query(history.INSERT_MIGRATION_VERSION, "0", "init", fmt.Sprintf("{%s}", sql)).
234234
Reply("INSERT 0 1")
235235
// Run test
236236
err := squashMigrations(context.Background(), []string{filepath.Base(path)}, afero.NewReadOnlyFs(fsys), conn.Intercept)
@@ -254,6 +254,7 @@ func TestBaselineMigration(t *testing.T) {
254254
// Setup mock postgres
255255
conn := pgtest.NewConn()
256256
defer conn.Close(t)
257+
pgtest.MockMigrationHistory(conn)
257258
conn.Query(fmt.Sprintf("DELETE FROM supabase_migrations.schema_migrations WHERE version <= ('0');INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES(('0'), ('init'), ('{%s}'))", sql)).
258259
Reply("INSERT 0 1")
259260
// Run test
@@ -279,6 +280,7 @@ func TestBaselineMigration(t *testing.T) {
279280
// Setup mock postgres
280281
conn := pgtest.NewConn()
281282
defer conn.Close(t)
283+
pgtest.MockMigrationHistory(conn)
282284
conn.Query(fmt.Sprintf("DELETE FROM supabase_migrations.schema_migrations WHERE version <= ('%[1]s');INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES(('%[1]s'), ('init'), (null))", "0")).
283285
ReplyError(pgerrcode.InsufficientPrivilege, "permission denied for relation supabase_migrations")
284286
// Run test
@@ -293,6 +295,7 @@ func TestBaselineMigration(t *testing.T) {
293295
// Setup mock postgres
294296
conn := pgtest.NewConn()
295297
defer conn.Close(t)
298+
pgtest.MockMigrationHistory(conn)
296299
// Run test
297300
err := baselineMigrations(context.Background(), dbConfig, "0", fsys, conn.Intercept)
298301
// Check error

0 commit comments

Comments
 (0)