Serialize startup database migrations#13
Merged
Conversation
Member
Author
|
Review complete. The advisory-lock scope and cleanup paths look correct, including migration discovery/history reads being inside the session lock. No correctness fix needed. One operational follow-up: |
kasnder
marked this pull request as ready for review
July 13, 2026 14:59
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.
Summary
schema_migrations, discovering SQL files, or reading migration historyfinallycleanupRoot cause
Production startup runs the migration script automatically. Concurrent instances could each create/read
schema_migrationsand snapshot the same unapplied files before either instance recorded its work. The existing per-file transactions protected individual statements but did not serialize migration discovery or application across processes.Impact
Concurrent app starts now wait on one stable advisory-lock key within the target database. After the previous runner releases the lock, the next runner creates/checks the migration table and discovers migration state afresh, so already-applied files are skipped rather than applied twice. Session cleanup also releases the lock automatically if the connection fails while explicit unlock is attempted on normal script failure paths.
Validation
node --test test/migrate.test.js— 5/5 passednpm test— 27/27 passednode --check scripts/migrate.jsgit diff --checkThe focused tests use a mocked PostgreSQL client and temporary migration directories; they do not require a production database.