Repository navigation
fix(sqlparser): do not drop the statement after an empty StatementBegin block - #1164
Open
JayOfTheKeyboard wants to merge 1 commit into
Open
JayOfTheKeyboard wants to merge 1 commit into
JayOfTheKeyboard wants to merge 1 commit into
Conversation
…in block
When a StatementBegin/StatementEnd block holds only comments (or nothing),
the StatementEnd line reaches the parser with an empty buffer and is skipped
as an ordinary leading comment. The parser then stays in the StatementEnd
state, so the next line is discarded and an empty statement is stored in its
place. A migration such as
-- +goose StatementBegin
-- placeholder
-- +goose StatementEnd
CREATE TABLE users (id INTEGER PRIMARY KEY);
is applied and recorded without ever creating the table.
Never treat the StatementEnd line as a skippable comment, and do not store
an empty statement for a block that held no SQL.
JayOfTheKeyboard
added a commit
to JayOfTheKeyboard/JayOfTheKeyboard
that referenced
this pull request
Nov 8, 2026
Collaborator
|
Good find! |
This branch has not been deployed
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.
A
StatementBegin/StatementEndblock that holds only comments makes the parser throw away the line that follows it. goose then applies the migration, records the version, and reports success:goose statusshows 00001 as applied, but the only tables in the database aregoose_db_versionandsqlite_sequence.ParseSQLMigrationreturns[""]for the Up direction. If the next statement spans several lines, its first line is dropped and the rest is stored as a statement (["", "id INTEGER PRIMARY KEY\n);"]), which fails when run. The Down direction behaves the same way. Built from main at 2d285d2.The check that skips comments and blank lines before a statement runs whenever the buffer is empty, and after an empty block that includes the
-- +goose StatementEndline itself. That line is skipped before the code that stores the block and moves the state back togooseUp/gooseDown, so the parser stays ingooseStatementEndUp. In that state the next line is not written to the buffer, and an empty statement is stored in its place.The comment check no longer applies to the
-- +goose StatementEndline, so the parser always leaves the block. A block with no SQL in it now produces no statement instead of"", which matches how comment-only text outside a block is already handled. Blocks that contain SQL, plain semicolon splitting and annotation parsing are unchanged.valid-up/test10is a new golden file where theCREATE TABLEafter an empty block has to come through whole, andTestSplitStatementsgains a case with an empty block in each direction. On the parent both fail:TestSplitStatementswithincorrect number of up stmts. got 2 ([ id int NOT NULL\n);]), want 1, andTestValidUp/test10with 2 statements parsed where 1 is expected. Never setting the new StatementEnd flag fails the down count (got 0), and restoring eitherappendof an empty statement fails a count (got 2).Ran locally on Go 1.27.3, linux/amd64:
gofmt -s -l .,go vet ./...andgo build ./...are clean.go test -race -count=1over themake test-packagespackage list passes all 10 packages on both the parent and this branch. Aftermake add-gowork,go test ./internal/testing/integration -run='^TestPostgres$|TestMySQL'(Docker) passes on both. I didn't run the ClickHouse, YDB, StarRocks, Spanner or Turso suites. With the CLI built from this branch, the migration above createsusers, anddowndrops it.