Skip to content

fix: Restore two-way table comparison in the database integrity check - #5800

Merged
FXschwartz merged 2 commits into
serverpod:mainfrom
BiAksoy:fix/integrity-check-missing-objects
Oct 1, 2026
Merged

FXschwartz merged 2 commits into
serverpod:mainfrom
BiAksoy:fix/integrity-check-missing-objects

Conversation

@BiAksoy

@BiAksoy BiAksoy commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Before #2778, TableDefinition.like returned a bool and compared the number of columns, indexes and foreign keys on both sides before matching them by name, so an object present on only one side made it fail. #2778 replaced it with the detailed List<ComparisonWarning> version and dropped those counts. Since then like only walks the entries of this, and because verifyDatabaseIntegrity calls liveTable.like(table), a column, index or foreign key declared in the models but missing from the database is never reported and the server starts normally.

This PR restores the two-way comparison inside TableComparisons.like: after the existing loops, it reports the columns, indexes and foreign keys that only exist in other, using the existing isAdded state and the same name matching as the forward loops (exact for columns, case-insensitive for indexes and foreign keys). The call sites and the labels are unchanged, as agreed in #5797; the inverted expected/found wording is handled separately.

Because the live table is still the receiver, an object missing from the database is reported as added until that wording is fixed. With the test server schema after dropping address.street, blocking_blocked_unique_idx and blocking_fk_1, and adding cat_extra_idx:

Before:

WARNING: The database does not match the target database:
 - Table "cat" is not like the target database:
 - Missing Index "cat_extra_idx".

After:

WARNING: The database does not match the target database:
 - Table "address" is not like the target database:
 - New Column "street" added.
 - Table "blocking" is not like the target database:
 - New Index "blocking_blocked_unique_idx" added.
 - New Foreign key "blocking_fk_1" added.
 - Table "cat" is not like the target database:
 - Missing Index "cat_extra_idx".

DatabaseBulkData.exportTableData uses the same comparison, so the Insights export now also rejects a table that is missing a declared column, index or foreign key. Before, a missing column made it return the target definition next to rows selected from the live columns.

Tests cover the added direction in the column, index and foreign key comparison tests (including index names that only differ in casing), declared columns, indexes and foreign keys missing from the database on both SQLite and PostgreSQL, matching declared indexes and foreign keys that must not warn, and the bulk data export with a missing and a matching table. The integrity and export tests only assert that the object is named, not the missing/added wording, so they hold when the labels are fixed. The full test server schemas boot without integrity warnings on PostgreSQL (179 tables, 63 foreign keys, 260 indexes including GIN, GiST, SP-GiST, BRIN, HNSW and IVFFlat) and SQLite (166 tables, 70 foreign keys, 47 indexes).

Fixes #5797.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

Breaking changes

A database that is missing a column, index or foreign key its models declare now fails the integrity check, as it did before 2.1.4. In development the server exits at startup, and since #5719 a maintenance migration run exits with code 1. The Insights bulk data export also rejects such tables.

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@BiAksoy

BiAksoy commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@FXschwartz FXschwartz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for picking this up! I went through the change and the reverse loops look great to me!

Just one small recommendation

@BiAksoy
BiAksoy requested a review from FXschwartz October 1, 2026 17:46

@FXschwartz FXschwartz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM! Great job @BiAksoy and thanks again for the quick fix.

@FXschwartz
FXschwartz merged commit b5e77ac into serverpod:main Oct 1, 2026
481 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database integrity check passes when a declared column or index is missing from the database

2 participants