fix(cli): run DROP INDEX CONCURRENTLY outside the migration transaction (CLI-2218) by avallete · Pull Request #6276 · supabase/cli · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/cli-go/pkg/migration/file.go
15 changes: 15 additions & 0 deletions apps/cli-go/pkg/migration/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ func TestIsPipelineIncompatible(t *testing.T) {
sql: "-- cannot run in a transaction\n/* generated */\nCREATE INDEX CONCURRENTLY widgets_id_idx ON public.widgets(id)",
want: true,
},
{
name: "drop index concurrently",
sql: "DROP INDEX CONCURRENTLY public.widgets_id_idx",
want: true,
},
{
name: "drop index concurrently if exists",
sql: "drop index concurrently if exists api.idx_rx_orders_clinic_id",
want: true,
},
{
name: "reindex table concurrently",
sql: "REINDEX TABLE CONCURRENTLY public.widgets",
Expand Down Expand Up @@ -275,6 +285,11 @@ func TestIsPipelineIncompatible(t *testing.T) {
sql: "CREATE INDEX widgets_id_idx ON public.widgets(id)",
want: false,
},
{
name: "ordinary drop index",
sql: "DROP INDEX IF EXISTS public.widgets_id_idx",
want: false,
},
{
name: "concurrently in string literal",
sql: "SELECT 'CREATE INDEX CONCURRENTLY widgets_id_idx ON public.widgets(id)'",
Expand Down
7 changes: 5 additions & 2 deletions apps/cli/src/legacy/shared/legacy-migration-apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const BOM_CODE_POINT = 0xfeff;
// pipeline-incompatible here but wouldn't under that narrower definition. Not worth
// changing behaviour over — flagging so a future review doesn't rediscover it.
const CREATE_INDEX_CONCURRENTLY_PATTERN = /^CREATE\s+(?:UNIQUE\s+)?INDEX\s+CONCURRENTLY(?:\s|$)/u;
const DROP_INDEX_CONCURRENTLY_PATTERN = /^DROP\s+INDEX\s+CONCURRENTLY(?:\s|$)/u;
const REINDEX_CONCURRENTLY_PATTERN = /^REINDEX(?:\s|\().*\sCONCURRENTLY(?:\s|$)/u;
const VACUUM_PATTERN = /^VACUUM(?:\s|\(|$)/u;
const ALTER_SYSTEM_PATTERN = /^ALTER\s+SYSTEM(?:\s|$)/u;
Expand Down Expand Up @@ -95,15 +96,17 @@ const legacyTrimLeadingSqlComments = (sql: string): string => {

/**
* Whether a migration statement cannot run inside a transaction block — `CREATE
* [UNIQUE] INDEX CONCURRENTLY`, `REINDEX … CONCURRENTLY`, `VACUUM`, `ALTER SYSTEM`,
* `CLUSTER`. Such statements fail with SQLSTATE 25001 inside the implicit transaction
* [UNIQUE] INDEX CONCURRENTLY`, `DROP INDEX CONCURRENTLY`, `REINDEX … CONCURRENTLY`,
* `VACUUM`, `ALTER SYSTEM`, `CLUSTER`. Such statements fail with SQLSTATE 25001
* inside the implicit transaction
* created by a migration batch, so `execMigrationBatch` runs them standalone.
* Port of `isPipelineIncompatible` (`pkg/migration/file.go`, supabase/cli#5156).
*/
export const legacyIsPipelineIncompatible = (sql: string): boolean => {
const upper = legacyTrimLeadingSqlComments(sql).toUpperCase();
return (
CREATE_INDEX_CONCURRENTLY_PATTERN.test(upper) ||
DROP_INDEX_CONCURRENTLY_PATTERN.test(upper) ||
REINDEX_CONCURRENTLY_PATTERN.test(upper) ||
VACUUM_PATTERN.test(upper) ||
ALTER_SYSTEM_PATTERN.test(upper) ||
Expand Down
Loading