From 6d78dbe8cbc1b1ceb08aba5826edb50f7259ba01 Mon Sep 17 00:00:00 2001 From: Tom Quist Date: Wed, 28 May 2025 10:46:13 +0200 Subject: [PATCH] fix: support concurrently in DROP INDEX The `concurrently` flag was previously ignored in drop index statements. --- __fixtures__/drops.sql | 10 + .../__snapshots__/kitchen-sink.test.ts.snap | 329 +++++++++++++++++- packages/deparser/src/deparser.ts | 3 + .../__snapshots__/kitchen-sink.test.ts.snap | 287 ++++++++++++++- 4 files changed, 626 insertions(+), 3 deletions(-) diff --git a/__fixtures__/drops.sql b/__fixtures__/drops.sql index 56f1b9d9..dc565389 100644 --- a/__fixtures__/drops.sql +++ b/__fixtures__/drops.sql @@ -81,6 +81,16 @@ DROP DOMAIN IF EXISTS no_such_schema.foo; DROP FOREIGN TABLE IF EXISTS no_such_schema.foo; DROP INDEX IF EXISTS no_such_schema.foo; +DROP INDEX my_index; +DROP INDEX CONCURRENTLY my_index; +DROP INDEX IF EXISTS my_index; +DROP INDEX CONCURRENTLY IF EXISTS my_index; +DROP INDEX schema.my_index; +DROP INDEX CONCURRENTLY schema.my_index; +DROP INDEX CONCURRENTLY IF EXISTS schema.my_index; +DROP INDEX my_index CASCADE; +DROP INDEX CONCURRENTLY my_index CASCADE; +DROP INDEX CONCURRENTLY IF EXISTS my_index CASCADE; DROP MATERIALIZED VIEW IF EXISTS no_such_schema.foo; -- DROP OPERATOR IF EXISTS no_such_schema.+ (int, int); diff --git a/packages/deparser/__tests__/__snapshots__/kitchen-sink.test.ts.snap b/packages/deparser/__tests__/__snapshots__/kitchen-sink.test.ts.snap index c6550a86..82f3975e 100644 --- a/packages/deparser/__tests__/__snapshots__/kitchen-sink.test.ts.snap +++ b/packages/deparser/__tests__/__snapshots__/kitchen-sink.test.ts.snap @@ -8431,6 +8431,331 @@ exports[`drops 113`] = ` exports[`drops 114`] = `"DROP INDEX IF EXISTS no_such_schema.foo;"`; exports[`drops 115`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 20, + "stmt_location": 2764, + }, +} +`; + +exports[`drops 116`] = `"DROP INDEX my_index;"`; + +exports[`drops 117`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "concurrent": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 33, + "stmt_location": 2785, + }, +} +`; + +exports[`drops 118`] = `"DROP INDEX CONCURRENTLY my_index;"`; + +exports[`drops 119`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "missing_ok": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 30, + "stmt_location": 2819, + }, +} +`; + +exports[`drops 120`] = `"DROP INDEX IF EXISTS my_index;"`; + +exports[`drops 121`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "concurrent": true, + "missing_ok": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 43, + "stmt_location": 2850, + }, +} +`; + +exports[`drops 122`] = `"DROP INDEX CONCURRENTLY IF EXISTS my_index;"`; + +exports[`drops 123`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "schema", + }, + }, + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 27, + "stmt_location": 2894, + }, +} +`; + +exports[`drops 124`] = `"DROP INDEX schema.my_index;"`; + +exports[`drops 125`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "concurrent": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "schema", + }, + }, + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 40, + "stmt_location": 2922, + }, +} +`; + +exports[`drops 126`] = `"DROP INDEX CONCURRENTLY schema.my_index;"`; + +exports[`drops 127`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "concurrent": true, + "missing_ok": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "schema", + }, + }, + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 50, + "stmt_location": 2963, + }, +} +`; + +exports[`drops 128`] = `"DROP INDEX CONCURRENTLY IF EXISTS schema.my_index;"`; + +exports[`drops 129`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_CASCADE", + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 28, + "stmt_location": 3014, + }, +} +`; + +exports[`drops 130`] = `"DROP INDEX my_index CASCADE;"`; + +exports[`drops 131`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_CASCADE", + "concurrent": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 41, + "stmt_location": 3043, + }, +} +`; + +exports[`drops 132`] = `"DROP INDEX CONCURRENTLY my_index CASCADE;"`; + +exports[`drops 133`] = ` +{ + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_CASCADE", + "concurrent": true, + "missing_ok": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 51, + "stmt_location": 3085, + }, +} +`; + +exports[`drops 134`] = `"DROP INDEX CONCURRENTLY IF EXISTS my_index CASCADE;"`; + +exports[`drops 135`] = ` { "RawStmt": { "stmt": { @@ -8459,12 +8784,12 @@ exports[`drops 115`] = ` }, }, "stmt_len": 52, - "stmt_location": 2764, + "stmt_location": 3137, }, } `; -exports[`drops 116`] = `"DROP MATERIALIZED VIEW IF EXISTS no_such_schema.foo;"`; +exports[`drops 136`] = `"DROP MATERIALIZED VIEW IF EXISTS no_such_schema.foo;"`; exports[`kitchen sink alias 1`] = ` { diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index 221112f6..f42048d8 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -2525,6 +2525,9 @@ export default class Deparser { const output = []; output.push('DROP'); output.push(objtypeName(node.removeType)); + if (node.concurrent) { + output.push('CONCURRENTLY'); + } if (node.missing_ok) { output.push('IF EXISTS'); } diff --git a/packages/pgsql-parser/__tests__/__snapshots__/kitchen-sink.test.ts.snap b/packages/pgsql-parser/__tests__/__snapshots__/kitchen-sink.test.ts.snap index 21d595e1..e9fff310 100644 --- a/packages/pgsql-parser/__tests__/__snapshots__/kitchen-sink.test.ts.snap +++ b/packages/pgsql-parser/__tests__/__snapshots__/kitchen-sink.test.ts.snap @@ -7734,6 +7734,281 @@ exports[`drops 1`] = ` "stmt_location": 2722, }, }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 20, + "stmt_location": 2764, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "concurrent": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 33, + "stmt_location": 2785, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "missing_ok": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 30, + "stmt_location": 2819, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "concurrent": true, + "missing_ok": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 43, + "stmt_location": 2850, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "schema", + }, + }, + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 27, + "stmt_location": 2894, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "concurrent": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "schema", + }, + }, + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 40, + "stmt_location": 2922, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_RESTRICT", + "concurrent": true, + "missing_ok": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "schema", + }, + }, + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 50, + "stmt_location": 2963, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_CASCADE", + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 28, + "stmt_location": 3014, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_CASCADE", + "concurrent": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 41, + "stmt_location": 3043, + }, + }, + { + "RawStmt": { + "stmt": { + "DropStmt": { + "behavior": "DROP_CASCADE", + "concurrent": true, + "missing_ok": true, + "objects": [ + { + "List": { + "items": [ + { + "String": { + "str": "my_index", + }, + }, + ], + }, + }, + ], + "removeType": "OBJECT_INDEX", + }, + }, + "stmt_len": 51, + "stmt_location": 3085, + }, + }, { "RawStmt": { "stmt": { @@ -7762,7 +8037,7 @@ exports[`drops 1`] = ` }, }, "stmt_len": 52, - "stmt_location": 2764, + "stmt_location": 3137, }, }, ] @@ -7826,6 +8101,16 @@ DROP CONVERSION IF EXISTS no_such_schema.foo; DROP DOMAIN IF EXISTS no_such_schema.foo; DROP FOREIGN TABLE IF EXISTS no_such_schema.foo; DROP INDEX IF EXISTS no_such_schema.foo; +DROP INDEX my_index; +DROP INDEX CONCURRENTLY my_index; +DROP INDEX IF EXISTS my_index; +DROP INDEX CONCURRENTLY IF EXISTS my_index; +DROP INDEX schema.my_index; +DROP INDEX CONCURRENTLY schema.my_index; +DROP INDEX CONCURRENTLY IF EXISTS schema.my_index; +DROP INDEX my_index CASCADE; +DROP INDEX CONCURRENTLY my_index CASCADE; +DROP INDEX CONCURRENTLY IF EXISTS my_index CASCADE; DROP MATERIALIZED VIEW IF EXISTS no_such_schema.foo;" `;