diff --git a/internal/migration/repair/repair.go b/internal/migration/repair/repair.go index c0670000a..a7dfee274 100644 --- a/internal/migration/repair/repair.go +++ b/internal/migration/repair/repair.go @@ -71,7 +71,7 @@ func UpdateMigrationTable(ctx context.Context, conn *pgx.Conn, version []string, if err != nil { return err } - batch.Queue(migration.INSERT_MIGRATION_VERSION, f.Version, f.Name, f.Statements) + batch.Queue(migration.UPSERT_MIGRATION_VERSION, f.Version, f.Name, f.Statements) } case Reverted: if !repairAll { diff --git a/internal/migration/repair/repair_test.go b/internal/migration/repair/repair_test.go index 4dea63279..f8eb33cce 100644 --- a/internal/migration/repair/repair_test.go +++ b/internal/migration/repair/repair_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "strings" "testing" "github.com/jackc/pgconn" @@ -37,7 +38,7 @@ func TestRepairCommand(t *testing.T) { conn := pgtest.NewConn() defer conn.Close(t) helper.MockMigrationHistory(conn). - Query(migration.INSERT_MIGRATION_VERSION, "0", "test", []string{"select 1"}). + Query(migration.UPSERT_MIGRATION_VERSION, "0", "test", []string{"select 1"}). Reply("INSERT 0 1") // Run test err := Run(context.Background(), dbConfig, []string{"0"}, Applied, fsys, conn.Intercept) @@ -87,7 +88,7 @@ func TestRepairCommand(t *testing.T) { conn := pgtest.NewConn() defer conn.Close(t) helper.MockMigrationHistory(conn). - Query(migration.INSERT_MIGRATION_VERSION, "0", "test", nil). + Query(migration.UPSERT_MIGRATION_VERSION, "0", "test", nil). ReplyError(pgerrcode.DuplicateObject, `relation "supabase_migrations.schema_migrations" does not exist`) // Run test err := Run(context.Background(), dbConfig, []string{"0"}, Applied, fsys, conn.Intercept) @@ -107,7 +108,10 @@ func TestRepairAll(t *testing.T) { conn := pgtest.NewConn() defer conn.Close(t) helper.MockMigrationHistory(conn). - Query(migration.TRUNCATE_VERSION_TABLE + `;INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES( '0' , 'test' , '{select 1}' )`). + Query(strings.Join([]string{ + migration.TRUNCATE_VERSION_TABLE, + strings.ReplaceAll(migration.UPSERT_MIGRATION_VERSION, "$1, $2, $3", " '0' , 'test' , '{select 1}' "), + }, ";")). Reply("TRUNCATE TABLE"). Reply("INSERT 0 1") // Run test diff --git a/pkg/migration/history.go b/pkg/migration/history.go index db0359c27..9f156faa4 100644 --- a/pkg/migration/history.go +++ b/pkg/migration/history.go @@ -16,6 +16,7 @@ const ( ADD_STATEMENTS_COLUMN = "ALTER TABLE supabase_migrations.schema_migrations ADD COLUMN IF NOT EXISTS statements text[]" ADD_NAME_COLUMN = "ALTER TABLE supabase_migrations.schema_migrations ADD COLUMN IF NOT EXISTS name text" INSERT_MIGRATION_VERSION = "INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES($1, $2, $3)" + UPSERT_MIGRATION_VERSION = INSERT_MIGRATION_VERSION + " ON CONFLICT (version) DO UPDATE SET name = EXCLUDED.name, statements = EXCLUDED.statements" DELETE_MIGRATION_VERSION = "DELETE FROM supabase_migrations.schema_migrations WHERE version = ANY($1)" DELETE_MIGRATION_BEFORE = "DELETE FROM supabase_migrations.schema_migrations WHERE version <= $1" TRUNCATE_VERSION_TABLE = "TRUNCATE supabase_migrations.schema_migrations"