Skip to content

Commit 2bb6c35

Browse files
authored
fix: use upsert when repairing history table (#4493)
1 parent 2b7134a commit 2bb6c35

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

internal/migration/repair/repair.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func UpdateMigrationTable(ctx context.Context, conn *pgx.Conn, version []string,
7171
if err != nil {
7272
return err
7373
}
74-
batch.Queue(migration.INSERT_MIGRATION_VERSION, f.Version, f.Name, f.Statements)
74+
batch.Queue(migration.UPSERT_MIGRATION_VERSION, f.Version, f.Name, f.Statements)
7575
}
7676
case Reverted:
7777
if !repairAll {

internal/migration/repair/repair_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"os"
66
"path/filepath"
7+
"strings"
78
"testing"
89

910
"github.com/jackc/pgconn"
@@ -37,7 +38,7 @@ func TestRepairCommand(t *testing.T) {
3738
conn := pgtest.NewConn()
3839
defer conn.Close(t)
3940
helper.MockMigrationHistory(conn).
40-
Query(migration.INSERT_MIGRATION_VERSION, "0", "test", []string{"select 1"}).
41+
Query(migration.UPSERT_MIGRATION_VERSION, "0", "test", []string{"select 1"}).
4142
Reply("INSERT 0 1")
4243
// Run test
4344
err := Run(context.Background(), dbConfig, []string{"0"}, Applied, fsys, conn.Intercept)
@@ -87,7 +88,7 @@ func TestRepairCommand(t *testing.T) {
8788
conn := pgtest.NewConn()
8889
defer conn.Close(t)
8990
helper.MockMigrationHistory(conn).
90-
Query(migration.INSERT_MIGRATION_VERSION, "0", "test", nil).
91+
Query(migration.UPSERT_MIGRATION_VERSION, "0", "test", nil).
9192
ReplyError(pgerrcode.DuplicateObject, `relation "supabase_migrations.schema_migrations" does not exist`)
9293
// Run test
9394
err := Run(context.Background(), dbConfig, []string{"0"}, Applied, fsys, conn.Intercept)
@@ -107,7 +108,10 @@ func TestRepairAll(t *testing.T) {
107108
conn := pgtest.NewConn()
108109
defer conn.Close(t)
109110
helper.MockMigrationHistory(conn).
110-
Query(migration.TRUNCATE_VERSION_TABLE + `;INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES( '0' , 'test' , '{select 1}' )`).
111+
Query(strings.Join([]string{
112+
migration.TRUNCATE_VERSION_TABLE,
113+
strings.ReplaceAll(migration.UPSERT_MIGRATION_VERSION, "$1, $2, $3", " '0' , 'test' , '{select 1}' "),
114+
}, ";")).
111115
Reply("TRUNCATE TABLE").
112116
Reply("INSERT 0 1")
113117
// Run test

pkg/migration/history.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
ADD_STATEMENTS_COLUMN = "ALTER TABLE supabase_migrations.schema_migrations ADD COLUMN IF NOT EXISTS statements text[]"
1717
ADD_NAME_COLUMN = "ALTER TABLE supabase_migrations.schema_migrations ADD COLUMN IF NOT EXISTS name text"
1818
INSERT_MIGRATION_VERSION = "INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES($1, $2, $3)"
19+
UPSERT_MIGRATION_VERSION = INSERT_MIGRATION_VERSION + " ON CONFLICT (version) DO UPDATE SET name = EXCLUDED.name, statements = EXCLUDED.statements"
1920
DELETE_MIGRATION_VERSION = "DELETE FROM supabase_migrations.schema_migrations WHERE version = ANY($1)"
2021
DELETE_MIGRATION_BEFORE = "DELETE FROM supabase_migrations.schema_migrations WHERE version <= $1"
2122
TRUNCATE_VERSION_TABLE = "TRUNCATE supabase_migrations.schema_migrations"

0 commit comments

Comments
 (0)