Skip to content

fix: support DROP INDEX CONCURRENTLY in migration script splitting - #5847

Open
0xturner wants to merge 1 commit into
prisma:mainfrom
0xturner:fix/drop-index-concurrently-splitting
Open

fix: support DROP INDEX CONCURRENTLY in migration script splitting#5847
0xturner wants to merge 1 commit into
prisma:mainfrom
0xturner:fix/drop-index-concurrently-splitting

Conversation

@0xturner

Copy link
Copy Markdown

Follow-up to #5767 and #5799.

Problem

A migration containing more than one DROP INDEX CONCURRENTLY statement — or a single one mixed with any other statement — fails to apply:

Error: P3018

Migration name: 20260103000000_drop_multiple_indexes

Database error code: 25001

Database error:
ERROR: DROP INDEX CONCURRENTLY cannot run inside a transaction block

A migration with multiple CREATE INDEX CONCURRENTLY statements works fine since #5767, so this is surprising and asymmetric. A migration whose only statement is a single DROP INDEX CONCURRENTLY also happens to work, which makes the failure look nondeterministic to users.

Root cause

split_script_into_statements relies on sqlparser successfully parsing every statement in the script to find statement boundaries. sqlparser (up to and including 0.62) supports the CONCURRENTLY keyword in CREATE INDEX but not in DROP INDEX:

sql parser error: Expected: end of statement, found: "idx1" at Line: 1, Column: 25

When parsing fails, the splitter falls back to submitting the whole script as a single simple-query batch. PostgreSQL wraps a multi-statement simple-query batch in an implicit transaction, and DROP INDEX CONCURRENTLY cannot run inside a transaction block. (A single-statement script survives the fallback only because a one-statement batch is not wrapped.)

Note the parse failure poisons the entire script: one DROP INDEX CONCURRENTLY in a migration also breaks CREATE INDEX CONCURRENTLY statements that would otherwise be handled.

Fix

Teach detect_statement_terminators to recognize the DROP INDEX CONCURRENTLY token sequence and consume the statement's tokens up to the next semicolon manually instead of calling Parser::parse_statement. This is safe because a DROP INDEX CONCURRENTLY statement cannot contain a semicolon anywhere except as its terminator (quoted identifiers and string literals are single tokens).

Everything else is unchanged, including the whole-script fallback for genuinely unparseable scripts.

If sqlparser gains support for DROP INDEX CONCURRENTLY upstream, this special case can be removed.

Tests

  • 4 new unit tests for split_script_into_statements (multiple drops, mixed with other statements, IF EXISTS, no trailing semicolon, plain DROP INDEX unaffected).
  • New integration test postgres_apply_migrations_works_with_multiple_drop_index_concurrently_statements, which fails with 25001 before this change and passes after.
  • Verified end to end with the Prisma CLI 7.8.0 against PostgreSQL 15: a previously failing migrate deploy with two DROP INDEX CONCURRENTLY statements succeeds with a patched schema-engine binary.

This fixes the DROP INDEX counterpart of prisma/prisma#14456.

@CLAassistant

CLAassistant commented Jul 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 5b347ad2-5763-45a2-af0d-a304b47e970f

📥 Commits

Reviewing files that changed from the base of the PR and between 059a7b2 and db2ef7a.

📒 Files selected for processing (2)
  • schema-engine/connectors/sql-schema-connector/src/flavour/postgres/connector/native/mod.rs
  • schema-engine/sql-migration-tests/tests/migrations/postgres.rs

Summary by CodeRabbit

  • Bug Fixes

    • PostgreSQL migrations now correctly split scripts containing one or more DROP INDEX CONCURRENTLY statements.
    • Supports mixed SQL statements and statements without a trailing semicolon.
    • Regular DROP INDEX statements continue to work as expected.
  • Tests

    • Added coverage for multiple and mixed concurrent index drops in PostgreSQL migrations.

Walkthrough

Updated Postgres SQL script splitting to recognize DROP INDEX CONCURRENTLY statements that sqlparser cannot parse, consuming tokens through the next semicolon or end-of-file. Added unit tests for multiple, mixed, unterminated, and regular DROP INDEX statements. Added a migration test covering multiple concurrent index drops in one migration.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: supporting DROP INDEX CONCURRENTLY in migration script splitting.
Description check ✅ Passed The description directly explains the migration-splitting bug, root cause, fix, and tests for DROP INDEX CONCURRENTLY.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants