Repository navigation
fix(database): keep each migration's version and name, and stop when the files don't match - #10
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
…the files don't match prea_migrations was goose's table: a bigint per version (0.0.2 was 2) and no name, so Preá couldn't tell which migration ran. Deleting, renaming or replacing an applied migration went unnoticed, and a new file that took a deleted one's version never ran. Run the migrations on pgx directly, under a session advisory lock, and keep one row per applied migration: the version as written in the file name, the name, the checksum of its up and when it ran. Before every command and at startup, the rows are checked against the files: a missing file, another name or a changed up stops them. Migrations only run forward: a pending one older than the highest applied version stops them too, and down undoes from the highest version. The first run converts goose's table, taking the names from the files. goose is no longer a dependency.
48f62d6 to
7e92891
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
generateMigration kept the lock's connection while diff asked the pool for another, so a pool of one connection waited forever at startup. Release it once the pending check is done. Converting goose's table now reads only each version's last row: goose's CLI added a row per up and down, so a version rolled back there counted as applied, and one applied twice broke the conversion. Add heavier tests: a pool of one, ten instances starting at once, eight converting at once, goose's up/down history, failures with and without a transaction, version order, waiting for the lock, released connections, 300 migrations, the checksum rules, and a fuzz test of the file parser.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
98a6925 to
1faddec
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
What changed and why
Migrations could silently diverge from the database. Deleting or renaming an applied migration did nothing, and a new file that reused a deleted file's version never ran, without any error. The users thought they had undone a migration when they hadn't. Also,
prea_migrationsstored the version as a number (1, not0.0.1) and no name, so two files with the same version couldn't be told apart.prea_migrationsnow keeps, for each applied migration, its version as written (0.0.1), its name, a sha256 checksum of its SQL (the part above-- prea:down), and when it ran. The first run converts goose's table from earlier versions in one transaction, taking the names from the files. It reads goose's up/down history correctly, keeping only the last row of each version.prea migrate, listing every mismatch at once with how to fix it.prea migrate downundoes from the highest version.from_models, startup no longer holds the lock's connection while diffing the models, so a pool of one connection doesn't deadlock.site/guide/migrations.mdexplains the table, the checks, the forward-only rule and how to change an applied migration. The changelog has an Unreleased entry.Not released yet. Because
preav0.0.5 anddatabasev0.0.4 were tagged by mistake and then removed, the next release will be v0.0.6 for the root module, the CLI anddatabase.How it was tested
New tests in
database/migrate_test.go: the table, timestamp versions, applied rows against the files, the forward-only rule, no-transaction migrations, conversion from goose (including out-of-order history and0.0.9/0.0.10ordering), generated names and startup.Heavier tests in
database/migrate_stress_test.go:FuzzParseMigration(60s, 5.8M execs).Concurrency and lock tests run 10× under
-race.Manual mutation testing on the runner (checksum, ordering, lock, down order, target): every mutation is caught.
End to end: a database migrated by the published database v0.0.3, with an up/down/up history, then opened with this code. The conversion,
status,upanddownwork, and deleting a file stops the app.gofmt,go vet github.com/prea-framework/prea/...andgo test github.com/prea-framework/prea/...passthe docs in
site/are updated, if the change is visible to users