fix(migrate): surface lost SQL migration finalizer errors (#1389) - #1390
Merged
vmihailenco merged 1 commit intoJul 17, 2026
Merged
Conversation
Yusufihsangorgel
force-pushed
the
fix/migrate-finalizer-errors
branch
from
July 14, 2026 11:37
a734787 to
7084b1f
Compare
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.
Fixes #1389.
SQL migrations run via an unnamed return, so the finalizer's error (
tx.Commit/tx.Rollback/bun.Conn.Close) was assigned to a localretErrinside the deferred function afterreturnhad already evaluated the result. In Go, the return expression is copied to the (unnamed) result before deferred functions run, so those finalizer errors were silently dropped:.tx.up.sql, aCommiterror was lost. WithWithMarkAppliedOnSuccess(true)the migrator could then mark the migration applied even though the commit's outcome was uncertain.Rollbackerror was lost.Conn.Closeerror was lost.This switches the migration closure to a named return and assigns the finalizer error to it. The execution error keeps priority: when the body fails, its error is preserved and joined with any rollback/close error via
errors.Join.Tests (
migrate/migration_finalizer_test.go) use a smalldatabase/sqldriver whose transactions fail to commit (and optionally fail exec):Commiterror is now returned from the migration (previously nil)