Skip to content

fix: refuse SQLite migrations whose table rebuild would drop complex indexes (#86) - #87

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/86-sqlite-rebuild
Oct 19, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/86-sqlite-rebuild

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #86

Problem

On SQLite, a migration that alters or drops a column, changes a key, foreign key, unique or check constraint, or renames a table (EF renames the primary key with it) runs as a table rebuild, and EF Core re-creates only the indexes in its own model. Every complex index on the table was dropped, unique ones included; the migration applied cleanly, and the snapshot still listed the index, so nothing ever re-created it. Reproduced on SQLite with the stock generator and the target model: after making an unrelated column optional, ux_members_email is gone, and duplicates are accepted. Present since the first release.

Change

The differ cannot fix this in the migration: SqliteMigrationsSqlGenerator.RewriteOperations (EF Core release/10.0) emits every table rebuild after all other operations, so a CREATE INDEX the differ adds still runs before the rebuild and is lost with the old table (tried: the SQL Server-style drop/create bracket of #85 leaves the index missing).

So on SQLite (detected from the type mapping source), migrations add now fails when a rebuild would hit a table carrying a complex index in the target model. The message names the table, the operation, the indexes, and the way out: three migrations, none of which rebuilds a table carrying a complex index. The operation list mirrors EF's RewriteOperations, and table renames are followed.

Re-creating the indexes after the rebuild needs a step that runs after EF's rewrite (a wrapper around the SQLite migrations SQL generator, registered by UseComplexIndexes() and guarded by the runtime-wiring sentinel). That is a new runtime component, so it is left for a later minor, noted in #86.

Docs: a paragraph in the root and core READMEs. Changelogs: a ### Changed answers row with an Upgrading note on finding the indexes earlier migrations already lost.

Merge note: #69, #71 and #73 add the same ### Changed answers heading to the 5.5.0 entry; whichever lands later needs a trivial changelog rebase.

Tests

  • SqliteTableRebuildTests: the refusal and its message; the three-migration way out applied on a real (in-memory) SQLite database keeps ux_members_email; a rebuild of a table without complex indexes is allowed; an unchanged model diffs to nothing.
  • IndexRenameTests: "a renamed table keeps its complex indexes without churn" ran on SQLite and asserted the loss as success. It now runs on PostgreSQL, where it is true, and a new SQLite row asserts the refusal. The loss itself was confirmed live on SQLite before the change.
  • verify-the-guard: with the differ reverted, both refusal tests fail.
  • Full suite 377/377 locally, integration included.

🤖 Generated with Claude Code

…indexes (#86)

SQLite runs ALTER COLUMN, DROP COLUMN, key, foreign key, unique and
check constraint changes by rebuilding the table, and EF Core re-creates
only the indexes in its own model. Every complex index on the table was
gone after the migration, unique ones included, the migration applied
cleanly, and the snapshot still listed the index. Applied for real on
SQLite: after making an unrelated column optional, ux_members_email no
longer exists. A table rename hits it too, since EF renames the primary
key along with it; the test that called that "no churn" asserted the
loss.

The differ cannot re-create the index in the same migration:
SqliteMigrationsSqlGenerator.RewriteOperations emits every rebuild after
all other operations, so a CREATE INDEX from here still runs first.
Verified: a drop/create bracket leaves the index missing. So on SQLite,
migrations add now fails when a rebuild would hit a table carrying a
complex index, naming the indexes and the three-migration way out, which
a test applies on SQLite and finds the index intact. Re-creating after
the rebuild needs a step that runs after EF's rewrite, a design for a
later minor.

Verified by reverting the differ: the refusal tests (alter/drop column,
table rename) fail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred severity/high Wrong results, crash or failed writes in a realistic scenario package/core Affects EFCore.ComplexIndexes area/indexes Complex-property, composite, unique and filtered indexes area/migrations The differs, snapshot format, EnsureCreated and design-time wiring labels Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 629ec76 into main Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/86-sqlite-rebuild branch October 19, 2026 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/indexes Complex-property, composite, unique and filtered indexes area/migrations The differs, snapshot format, EnsureCreated and design-time wiring bug Something isn't working package/core Affects EFCore.ComplexIndexes severity/high Wrong results, crash or failed writes in a realistic scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLite: a migration that rebuilds a table silently drops every complex index on it

1 participant