fix: support DROP INDEX CONCURRENTLY in migration script splitting - #5847
Open
0xturner wants to merge 1 commit into
Open
fix: support DROP INDEX CONCURRENTLY in migration script splitting#58470xturner wants to merge 1 commit into
DROP INDEX CONCURRENTLY in migration script splitting#58470xturner wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Summary by CodeRabbit
WalkthroughUpdated Postgres SQL script splitting to recognize 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify 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. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #5767 and #5799.
Problem
A migration containing more than one
DROP INDEX CONCURRENTLYstatement — or a single one mixed with any other statement — fails to apply:A migration with multiple
CREATE INDEX CONCURRENTLYstatements works fine since #5767, so this is surprising and asymmetric. A migration whose only statement is a singleDROP INDEX CONCURRENTLYalso happens to work, which makes the failure look nondeterministic to users.Root cause
split_script_into_statementsrelies on sqlparser successfully parsing every statement in the script to find statement boundaries. sqlparser (up to and including 0.62) supports theCONCURRENTLYkeyword inCREATE INDEXbut not inDROP INDEX: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 CONCURRENTLYcannot 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 CONCURRENTLYin a migration also breaksCREATE INDEX CONCURRENTLYstatements that would otherwise be handled.Fix
Teach
detect_statement_terminatorsto recognize theDROP INDEX CONCURRENTLYtoken sequence and consume the statement's tokens up to the next semicolon manually instead of callingParser::parse_statement. This is safe because aDROP INDEX CONCURRENTLYstatement 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 CONCURRENTLYupstream, this special case can be removed.Tests
split_script_into_statements(multiple drops, mixed with other statements,IF EXISTS, no trailing semicolon, plainDROP INDEXunaffected).postgres_apply_migrations_works_with_multiple_drop_index_concurrently_statements, which fails with25001before this change and passes after.migrate deploywith twoDROP INDEX CONCURRENTLYstatements succeeds with a patchedschema-enginebinary.This fixes the
DROP INDEXcounterpart of prisma/prisma#14456.