Fix #4179: Snowflake parser handles nested IF/FOR/CASE in BEGIN blocks #4194
+280
−3
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.
Problem
Flyway was generating invalid SQL when parsing Snowflake migrations containing nested
IFstatements withinBEGIN...ENDblocks. The parser prematurely terminated at the firstEND IF, causing it to miss the semicolon and the finalENDof the block.Related
Fixes #4179
Issue Reproduction
What Flyway Was Executing (INVALID)
Snowflake Error:
Solution
Updated
SnowflakeParser.readBetweenRecursive()to properly distinguish between:END IF,END FOR,END CASE(continue reading)END(stop reading)Changes/Modified
flyway-database/flyway-database-snowflake/src/main/java/org/flywaydb/database/snowflake/SnowflakeParser.javaModified Method:
readBetweenRecursive()END IF/FOR/CASEBEFORE checking for blockENDEND, peek ahead to see if it's followed byIF/FOR/CASEImplementation Details
The fix uses a boolean flag
blockEndedto control loop termination and delegates logic to helper methods:handleEndKeyword(): Checks if END is followed by IF/FOR/CASE keywordshandleGeneralContent(): Processes regular content and nested BEGIN blocksThis approach eliminates nested conditionals and multiple break/continue statements while maintaining the same parsing logic.
After Fix (Valid)
Testing
Test Cases
Test Results
All tests pass:
Test Output Example (Nested IF)