Repository navigation
fix: refuse SQLite migrations whose table rebuild would drop complex indexes (#86) - #87
Merged
Merged
Conversation
…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]>
This was referenced Oct 19, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_emailis gone, and duplicates are accepted. Present since the first release.Change
The differ cannot fix this in the migration:
SqliteMigrationsSqlGenerator.RewriteOperations(EF Corerelease/10.0) emits every table rebuild after all other operations, so aCREATE INDEXthe 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 addnow 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'sRewriteOperations, 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 answersrow with an Upgrading note on finding the indexes earlier migrations already lost.Merge note: #69, #71 and #73 add the same
### Changed answersheading 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 keepsux_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.🤖 Generated with Claude Code