Skip to content

fix(database): keep each migration's version and name, and stop when the files don't match - #10

Merged
Pedro77h merged 2 commits into
mainfrom
fix/migrations-match-files
Oct 27, 2026
Merged

Pedro77h merged 2 commits into
mainfrom
fix/migrations-match-files

Conversation

@Pedro77h

@Pedro77h Pedro77h commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

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_migrations stored the version as a number (1, not 0.0.1) and no name, so two files with the same version couldn't be told apart.

  • prea_migrations now 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.
  • Before migrating, every applied row is checked against its file. A missing file, a different name for the same version or changed SQL stops the app and prea migrate, listing every mismatch at once with how to fix it.
  • Migrations only run forward. A pending migration older than the highest applied version (e.g. after merging two branches) stops the app until it gets a higher version. prea migrate down undoes from the highest version.
  • The migrations run on pgx directly, without goose. A session advisory lock keeps several instances from migrating at once.
  • Fix: with from_models, startup no longer holds the lock's connection while diffing the models, so a pool of one connection doesn't deadlock.
  • Docs: site/guide/migrations.md explains 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 prea v0.0.5 and database v0.0.4 were tagged by mistake and then removed, the next release will be v0.0.6 for the root module, the CLI and database.

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 and 0.0.9/0.0.10 ordering), generated names and startup.

  • Heavier tests in database/migrate_stress_test.go:

    • a pool of one connection (found the startup deadlock, fixed);
    • ten instances starting at once;
    • eight converting goose's table at once;
    • goose's up/down history (found a wrong conversion, fixed);
    • failures with and without a transaction;
    • numeric version order;
    • waiting for the lock and canceling;
    • connections always released;
    • 300 migrations;
    • checksum rules;
    • 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, up and down work, and deleting a file stops the app.

  • gofmt, go vet github.com/prea-framework/prea/... and go test github.com/prea-framework/prea/... pass

  • the docs in site/ are updated, if the change is visible to users

Copilot AI balanced review requested due to automatic review settings October 26, 2026 19:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 20:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Pedro77h Pedro77h changed the title fix(database): stop when an applied migration's file is missing or changed fix(database): keep each migration's version and name, and stop when the files don't match Oct 26, 2026
…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.
@Pedro77h
Pedro77h force-pushed the fix/migrations-match-files branch from 48f62d6 to 7e92891 Compare October 26, 2026 20:31
Copilot AI balanced review requested due to automatic review settings October 26, 2026 20:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 20:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI balanced review requested due to automatic review settings October 27, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI balanced review requested due to automatic review settings October 27, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Pedro77h
Pedro77h force-pushed the fix/migrations-match-files branch from 98a6925 to 1faddec Compare October 27, 2026 14:24
Copilot AI balanced review requested due to automatic review settings October 27, 2026 14:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Pedro77h
Pedro77h merged commit 9249dbf into main Oct 27, 2026
6 checks passed
@Pedro77h Pedro77h mentioned this pull request Oct 27, 2026
2 tasks done
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.

2 participants