Skip to content

fix(migrate): name the statement a migration failed on, without a position - #337

Open
lx3133584 wants to merge 4 commits into
skudra-go:mainfrom
lx3133584:fix/migration-failure-line
Open

lx3133584 wants to merge 4 commits into
skudra-go:mainfrom
lx3133584:fix/migration-failure-line

Conversation

@lx3133584

@lx3133584 lx3133584 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

up ran a file migration as one string inside its transaction, so a failure said failed and was rolled back and nothing else unless PostgreSQL sent a position. It sends one for expression and cast errors but not for most DDL and constraint errors, which are the common failures of a generated file. A file with twenty statements then named neither the statement nor the line:

Error: ant: migration 0012_hand failed and was rolled back: ERROR: relation "usrs" does not exist (SQLSTATE 42P01)

A transactional file migration now runs its statements one at a time inside the same transaction. The all-or-nothing rollback is unchanged; the cost is one round trip per statement instead of one per file, which a large data migration notices (5,000 statements over a 1 ms link take about 5 s longer), and the statement that raised the error gives both the line and the text:

Error: ant: migration 0012_hand failed and was rolled back, at line 5: ERROR: relation "usrs" does not exist (SQLSTATE 42P01)

  5 | ALTER TABLE usrs ADD COLUMN nick text;

A comment before a statement stays with it, so the line is the first that carries code, not the comment above it. That holds for a -- line and for a block comment, over however many lines it runs and whatever it nests. When the driver does send a position it is relative to the statement, so that statement's first line is added.

Two paths are unchanged. A Go migration and a -- ant:no-transaction file run as before — the first is code, the second has no transaction to run inside. A file whose statements Split cannot place in the script runs whole, as it did, so no line is named there unless the driver sends a position.

Testing

tests/features/migrations/up_test.go gains these tests:

  • TestFailedMigrationWithoutAPosition runs a file whose second statement names a relation that does not exist, which PostgreSQL reports with no position, and checks the error names line 3 and the statement. It failed before the change with no line at all.
  • TestFailedMigrationUnderAComment puts a comment above the failing statement and checks the line is the statement's own, not the comment's.
  • TestFailedMigrationUnderABlockComment does the same under a three-line block comment with one nested in it: line 3, the comment's, before lineOfCode skipped block comments, line 6 after. TestFailedMigrationAfterABlockCommentOnItsLine and TestFailedMigrationWithAPositionUnderABlockComment pin code on the comment's own line and a driver position under a block comment.

TestFailedMigration, which already covered a position case, still reports line 3, so a position within a statement maps back to the right line in the file. Each new behaviour was checked against a mutation that undoes it, and each turned red.

  • make fmt, make lint → 0 issues
  • make test → no failures
  • ANT_TEST_DATABASES=postgres go test ./tests/features/migrations/ ./migrate/... → all pass except TestCommandsSayWhichMigrations/postgres/a_relative_folder,_from_the_working_directory, which fails the same way on a clean b1e7568 checkout
  • go test ./tests/docs → passes, with the new decision 162

Fixes #303

@lx3133584
lx3133584 force-pushed the fix/migration-failure-line branch from 9cbda2f to e3eb061 Compare November 24, 2026 21:14
…ition

up ran a file migration as one string inside its transaction, so a failure
said "failed and was rolled back" and nothing else unless PostgreSQL sent a
position. It sends one for expression and cast errors but not for most DDL
and constraint errors, the common failures of a generated file. A file with
twenty statements then named neither the statement nor the line:

    migration 0012_hand failed and was rolled back: relation "usrs" does not exist

A transactional file migration now runs its statements one at a time inside
the same transaction. It costs the same, the all-or-nothing rollback is
unchanged, and the statement that raised the error gives both the line and
its text.

A comment before a statement stays with it, so the line is the first that
carries code, not the comment above it. When the driver does send a position,
it is counted from the statement's text, comments included, so the lines
skipped before its code come off before the file's offset is added.

A Go migration and a -- ant:no-transaction file run as before. A file whose
statements Split cannot place in the script runs whole, as it did.

Signed-off-by: Liang Xu <24490353+lx3133584@users.noreply.github.com>
@lx3133584
lx3133584 force-pushed the fix/migration-failure-line branch from e3eb061 to 761f27d Compare November 24, 2026 21:41
@borderss

Copy link
Copy Markdown
Contributor

Thanks, this works, and the tests cover the cases with and without a position. Three small things:

  1. The ran() doc comment describes a branch that does not exist (migrate/engine.go:669): "A file that already ends its own transaction, which checkBody refuses, is run whole so that refusal is the error rather than a half-run file." ran() has no such case. checkBody refuses the file at engine.go:523, before applyInTx runs. Drop the paragraph, or say that checkBody has refused such a file before this runs.

  2. "Costs the same" isn't accurate, both in the ran() comment and in decision 162 (line 22). The old path made one round trip per file, and this makes one per statement. A 5,000-statement data migration on a 1 ms link takes about 5 s longer. That's probably a fair price for naming the line, but the decision should record the cost rather than deny it, for example: "one round trip per statement instead of one per file, which a large data migration notices".

  3. Minor: block comments. lineOfCode skips only blank lines and -- lines. A statement under /* why */ is reported at the comment's line, because Split keeps a block comment with the statement too. Either skip lines that are only a block comment, or say in the comment that the line can be a block comment's.

…d what running by statement costs

lineOfCode skipped blank lines and -- lines only, so a statement under a
block comment was reported at the comment's line. It now passes a block
comment to where it closes, over the lines it runs and the comments nested
in it, as PostgreSQL reads them and as Split keeps them with the statement.

The comment on ran() described a branch it does not have: checkBody has
refused a file that ends its own transaction before the transaction begins.
It and decision 162 also said running statement by statement costs the same.
It is one round trip per statement instead of one per file, which a large
data migration notices, and both now say so.
@lx3133584

Copy link
Copy Markdown
Contributor Author

Thanks, all three are in.

  1. The ran() comment no longer describes that branch. It now says checkBody has refused such a file before the transaction began.
  2. Both the comment and decision 162 record the cost instead of denying it: one round trip per statement instead of one per file, which a large data migration notices. The decision keeps your figure (5,000 statements over a 1 ms link, about 5 s).
  3. lineOfCode skips block comments too: to where the comment closes, over the lines it runs and whatever it nests, since PostgreSQL nests them and Split already reads them that way. Code after a block comment on the same line counts as that line. TestFailedMigrationUnderABlockComment reported line 3, the comment's, before the change and reports line 6 now; two more specs pin code on the comment's own line and a driver position under a block comment.

make lint is clean and ./tests/features/migrations, ./tests/docs and ./migrate/... pass on PostgreSQL.

@lx3133584

Copy link
Copy Markdown
Contributor Author

All three are in on the head.

  1. The doc comment on ran() no longer describes a branch that does not exist. It says the file never gets there because checkBody refused it before the transaction began.
  2. The cost is recorded rather than denied: "one round trip per statement instead of one per file, which a large data migration notices" — in both the ran() comment and decision 162.
  3. lineOfCode skips a block comment now, not just blank and -- lines, and there is a case for a statement written under /* why */ so the line is the statement's own.

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.

A failed migration doesn't say which statement failed unless PostgreSQL sends a position

2 participants