Skip to content

feat(migrations): refuse to start against a newer (downgraded) database - #1861

Merged
c-thiel merged 2 commits into
mainfrom
ct/lakekeeper-downgrade-guard
Jun 21, 2026
Merged

c-thiel merged 2 commits into
mainfrom
ct/lakekeeper-downgrade-guard

Conversation

@c-thiel

@c-thiel c-thiel commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

Guard against running an older binary against a database that a newer Lakekeeper has already migrated.

check_migration_status only checked one direction (are the binary's migrations applied?), so an older binary — whose migrations are a subset of what's applied — reported Complete and served against the newer schema. That risks runtime errors or silent data corruption when the binary writes against schema invariants it doesn't know about.

Add MigrationState::Ahead: any applied migration whose version this binary's migrator does not contain means the DB is newer. The serve gate (wait_for_db) now fails fast on Ahead instead of retrying — waiting never resolves a newer DB. Run serve --force-start to start anyway (emergency rollback, accepting the risk of schema incompatibility).

The migrate command already aborted on this via VersionMissing; this brings serve in line.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved migration safety: the app now detects when the database has been migrated by a newer binary and immediately stops with a clear, non-retryable error to prevent unsafe startup.
    • Migration checks are more accurate when the database contains applied migrations unknown to the running version, reporting this explicitly rather than treating it as complete.

Guard against running an older binary against a database that a newer
Lakekeeper has already migrated.

check_migration_status only checked one direction (are the binary's
migrations applied?), so an older binary — whose migrations are a subset
of what's applied — reported Complete and served against the newer
schema. That risks runtime errors or silent data corruption when the
binary writes against schema invariants it doesn't know about.

Add MigrationState::Ahead: any applied migration whose version this
binary's migrator does not contain means the DB is newer. The serve gate
(wait_for_db) now fails fast on Ahead instead of retrying — waiting never
resolves a newer DB. Run `serve --force-start` to start anyway (emergency
rollback, accepting the risk of schema incompatibility).

The migrate command already aborted on this via VersionMissing; this
brings serve in line.
@coderabbitai

coderabbitai Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b153f294-4de2-4c89-b4e0-aee02650a37b

📥 Commits

Reviewing files that changed from the base of the PR and between c0c1fd1 and 11d521b.

📒 Files selected for processing (1)
  • crates/lakekeeper-bin/src/wait_for_db.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/lakekeeper-bin/src/wait_for_db.rs

Walkthrough

Adds a new MigrationState::Ahead variant to signal that the database has been migrated by a newer Lakekeeper binary. Detection logic in check_migration_status compares applied _sqlx_migrations versions against the binary's known versions, returning Ahead when unknown versions are found. The startup wait loop in wait_for_db now fails immediately on this state using the primary database connection. A new integration test validates detection.

Changes

Downgrade Guard for DB-Ahead-of-Binary

Layer / File(s) Summary
MigrationState::Ahead variant and detection logic
crates/lakekeeper-storage-postgres/src/migrations/mod.rs
Adds the Ahead enum variant to MigrationState with documentation describing the unsafe downgrade scenario, extends check_migration_status to collect applied DB migration versions from _sqlx_migrations, compare them against the binary's known migrator versions, and return Ahead when unknown versions are present. Updates test imports and adds an integration test that inserts a fake future migration row and asserts Ahead is returned.
Fast-fail on Ahead in wait loop
crates/lakekeeper-bin/src/wait_for_db.rs
Swaps the pool import from get_reader_pool to get_writer_pool to use the primary database for migration checks. Adds an Ok(MigrationState::Ahead) match arm in the migration-check retry loop that logs an error and immediately bails via anyhow::bail!, stopping startup when the DB schema is newer than the binary.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐇 Sniff sniff, the database smells newer today,
A future migration has wandered astray!
I check all the versions with careful paws,
And bail right away if I spot unknown flaws.
No downgrade for me — I hop safe and sound! 🌿

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: preventing an older Lakekeeper binary from starting against a database migrated by a newer version, which directly corresponds to the addition of MigrationState::Ahead detection and the hard failure behavior in wait_for_db.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ct/lakekeeper-downgrade-guard

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/lakekeeper-bin/src/wait_for_db.rs`:
- Around line 47-60: The migration status check is using a read pool that may be
connected to a read replica, which can have stale data if a newer Lakekeeper
binary has committed migrations to the primary but the replica hasn't replicated
them yet. This causes the status check to incorrectly report Complete instead of
Ahead, allowing an older binary to start against an incompatible schema. Replace
the read_pool passed to the check_migration_status function with a writer pool
obtained from get_writer_pool instead of get_reader_pool to ensure the migration
status check reads from the primary database and detects newer migrations
correctly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c1809f2c-8309-42c6-9d20-24ec26e9b974

📥 Commits

Reviewing files that changed from the base of the PR and between 9af0a3f and c0c1fd1.

📒 Files selected for processing (2)
  • crates/lakekeeper-bin/src/wait_for_db.rs
  • crates/lakekeeper-storage-postgres/src/migrations/mod.rs

@c-thiel
c-thiel enabled auto-merge (squash) June 21, 2026 14:29
@github-actions

Copy link
Copy Markdown
Contributor

✅ PR Title Formatted Correctly

The title of this PR match the correct format. Thank you!

@c-thiel
c-thiel merged commit d8de0db into main Jun 21, 2026
78 of 80 checks passed
@c-thiel
c-thiel deleted the ct/lakekeeper-downgrade-guard branch June 21, 2026 18:07
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.

1 participant