Repository navigation
Fall back from a rename to drop-first, create-last when it would fail at apply (#141, #143) - #148
Merged
Merged
Conversation
… at apply Renames of complex indexes, exclusion constraints and temporal constraints run after EF Core's operations. Two shapes failed at apply: - #141 (SQL Server): the in-the-way check covered kept indexes only, so a renamed index stayed on a column an ALTER COLUMN changes (Msg 5074). - #143: a rename whose old name the target model gives to another object ran after EF created that object (PostgreSQL 42P07, a native index or key taking the name; also a complex-index name swap). A rename pair now stays a rename only when neither applies; otherwise it is a drop under the old name before the base operations and a create under the new one after them. A temporal constraint that falls back takes its temporal foreign keys with it, since dropping it under them fails with 2BP01. Names are checked against the target model: the core's IsIndexNameInUse (complex and native indexes, keys, under IndexNameScope), and in the PostgreSQL satellite the inventory ValidateNamesAcrossKinds already built, now TargetNames. Closes #141 Closes #143 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 #141
Closes #143
Problem
Renames of complex indexes, exclusion constraints and temporal constraints run after EF Core's operations, and two shapes scaffolded cleanly and failed at apply:
ALTER COLUMNon its column (an explicit rename, or a default name that follows a table rename) stayed in place:ALTER TABLE [users] ALTER COLUMN [Email_Value] nvarchar(100)→ Msg 5074.CREATE INDEX ix_a …thenALTER INDEX ix_a RENAME TO ix_b→ 42P07. Exclusion and temporal constraint renames failed the same way against a native unique constraint (verified on PostgreSQL 18, 42P07). Two complex indexes swapping names failed too:RENAME ix_c TO ix_bwhileix_bstill exists.Change
A rename pair now stays a rename only when nothing that runs before it needs the old object gone. Otherwise it becomes a drop under the old name before the base operations and a create/add under the new one after them.
CustomMigrationsModelDiffer.GetDifferences: it collects rename pairs first, then runs the in-the-way check (RequiresIndexRecreation) over them as well as over kept indexes (SQL Server: a renamed complex index stays in the way of ALTER COLUMN, and the migration fails at apply (Msg 5074) #141). It also checks the old name with a newinternal virtual IsIndexNameInUse: complex indexes (the renamed index itself excluded, so a case-only rename stays a rename), native indexes and keys of the target, underIndexNameScope/IndexNameComparer(A complex-index rename runs after a native CREATE INDEX that takes its old name (42P07) #143).NpgsqlComplexIndexMigrationsModelDiffer: the target-name inventoryValidateNamesAcrossKindsbuilt is nowTargetNames, and both checks use it.IsIndexNameInUseadds PostgreSQL's relation namespace (tables, views, sequences, constraint-backed indexes). Exclusion, temporal and temporal-FK renames fall back when the old name is taken: a constraint of the same table, or a relation of the same schema for the index-backed kinds. A temporal constraint that falls back takes its dependent temporal foreign keys with it, because dropping it under them fails with 2BP01 (verified live).ColumnsReadBy. An object created after the renames that takes the old name costs one needless rebuild. A miss costs a failed apply.### Fixed. SQL Server: a renamed complex index stays in the way of ALTER COLUMN, and the migration fails at apply (Msg 5074) #141 widens the existing SQL Server: ALTER COLUMN on a column under a complex index fails at apply (Msg 5074) #84 line, since SQL Server: ALTER COLUMN on a column under a complex index fails at apply (Msg 5074) #84 is new in 5.5.0. A complex-index rename runs after a native CREATE INDEX that takes its old name (42P07) #143 gets its own line. It is not a changed answer: every affected migration failed at apply before. CLAUDE.md's "Diff polish" section describes the fallback.Tests
AlterColumnUnderIndexTests: SQL Server, a default-named index on a renamed table and an explicitly renamed index, each under a shrinking column. NoRenameIndexOperation; the drop comes before theAlterColumnOperationand the create after it. On PostgreSQL the same model stays a rename.RenameOldNameTakenTests: a native index taking the old name (PostgreSQL and SQL Server), a name swap between two complex indexes, an exclusion constraint against a native unique constraint and against a complex index, and a temporal constraint against a native unique constraint (FK dropped first and re-added last). The index cases walk the operations by name the way the database would. Over-correction guards: a plain rename, a case-only rename on SQL Server, and a plain temporal rename that keeps its FK.PostgresIntegrationTests.Renames.cs: applies the index, exclusion and temporal A complex-index rename runs after a native CREATE INDEX that takes its old name (42P07) #143 migrations on PostgreSQL 18 in one transaction, asdatabase updatedoes.SqlServerIntegrationTests.Alter_column_under_a_renamed_index_applies: applies the SQL Server: a renamed complex index stays in the way of ALTER COLUMN, and the migration fails at apply (Msg 5074) #141 migration (a table rename plus an explicit rename, both under shrinking columns) and checks uniqueness still holds. Not run locally: this Apple Silicon machine has no Rosetta in OrbStack, so the SQL Server container exits. TheIntegration (SQL Server 2022)CI lane runs it.Counterfactuals (verify-the-guard), reverting one mechanism at a time:
42P07: relation "ra_ix_a" / "ra_ex_a" / "ra_uq_a" already exists2BP01: cannot drop constraint ra_uq_a … because other objects depend on itIsIndexNameInUseUnit suite: 581/581 (
TestCategory!=Integration&TestCategory!=SqlServer).🤖 Generated with Claude Code