Skip to content

A column rename keeps the indexes and constraints on the column (#182) - #183

Merged
CaffeinatedCoder merged 3 commits into
release/5.xfrom
fix/182-column-renames
Oct 23, 2026
Merged

CaffeinatedCoder merged 3 commits into
release/5.xfrom
fix/182-column-renames

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Oct 23, 2026 •

Copy link
Copy Markdown
Owner

Closes #182

Into release/5.x directly: SchemaKit's differ is new code, and design/schemakit-api.md §9.4 needs the same rule (noted on #182). Closes acts only on main, so #182 is closed by hand after the merge. The first commit opens 5.6.0 (5.5.1 had no entry yet), since a change to what a migration scaffolds ships in a minor.

Problem

Renaming a column dropped every complex index, exclusion constraint and temporal constraint that read it before the RENAME COLUMN, and created them again after it. PostgreSQL, SQL Server and SQLite all keep them through a rename, and EF Core emits only the RenameColumnOperation for its own indexes. So a rename that should take milliseconds rebuilt each index under an exclusive lock held until commit.

The differs compare resolved descriptors. The source resolved to the old column name and the target to the new one, so the pair never matched. Table renames were normalized; column renames were not.

Change

  • CustomMigrationsModelDiffer: ResolveSource resolves the source model under the names the base RenameColumnOperations give its columns, and RenderedColumn applies them. The map is keyed by the source table: EF names the operation's table as the target has it, which the table-and-column test confirms. The new name reaches column parts, placeholders (filters and typed expressions), INCLUDE lists (ResolveIncludeColumns) and the implicit filter (RenderedImplicitFilter, which reads nullability from the source and renames the quoted columns in one pass). Default names still come from the source's own columns (ResolvedIndexPart.NameToken), so a default name that follows the column becomes a RenameIndexOperation, as EF Core's do. Verbatim SQL is compared as written.
  • Npgsql differ: the same for JSON container columns, exclusion parts (with a NameToken for default names), temporal constraints and temporal foreign keys. Source exclusion and temporal descriptors resolve inside ResolveSource.
  • SQL Server differ: INCLUDE lists go through ResolveIncludeColumns. sp_rename rejects a column that a filtered index's predicate reads (CI: "The index 'ix_name_email_active' is dependent on column 'revoked_at'"). So an internal RequiresIndexRecreation overload also gets the filter's columns, and the SQL Server differ drops such an index before the rename and creates it after it, the implicit IS NOT NULL filter included. EF Core rebuilds its own filtered indexes on a rename too. Key and included columns are renamed under the index.
  • Changelogs (### Changed answers), docs/upgrading.md (Changed answers in 5.6.0), CLAUDE.md.

Tests

  • ColumnRenameTests (unit). PostgreSQL with every declaration kind: only the five RenameColumnOperations, plus a rename of the default-named index and exclusion constraint. Table and columns renamed together. A real change beside the rename still drops before it and creates after it. A verbatim expression left unchanged gives no operation. SQL Server, with the implicit IS NOT NULL filter and INCLUDE, and SQLite.
  • PostgresIntegrationTests.Column_rename_keeps_indexes_and_constraints: applies the rename on PostgreSQL 18 and asserts every index's and constraint's pg_class OID is unchanged, so the objects were kept, not re-created under the same name. It also checks the definitions name the new columns.
  • SqlServerIntegrationTests.Column_rename_keeps_the_indexes: on SQL Server 2022, the covering index survives the rename of its key and included columns, and the two filtered ones are rebuilt around it. The first push showed the sp_rename rejection; this lane runs only in CI (the image crashes locally without Rosetta).
  • With the source fix reverted, all six differ tests and the PostgreSQL integration test fail ("'cr_ix_code_email' was rebuilt."). The verbatim test passes either way, on purpose. With only the implicit-filter renaming broken, or the SQL Server filter rule disabled, the SQL Server unit test fails.
  • Unit suite: 669 passed. PostgreSQL 18 and 16 integration suites: 35 passed.

🤖 Generated with Claude Code

CaffeinatedCoder and others added 2 commits October 23, 2026 09:57
#182 changes what a column rename scaffolds, so it ships in a minor.
5.5.1 had no change yet; its heading becomes 5.6.0 in the commit that
adds the first entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The differs compared the source under its old column names and the
target under the new ones, so every complex index, exclusion constraint
and temporal constraint on a renamed column was dropped before the
rename and created again after it, a full rebuild under an exclusive
lock. PostgreSQL, SQL Server and SQLite all keep them through a rename.

The source model is now resolved under the names the base
RenameColumnOperations give its columns (ResolveSource, RenderedColumn).
Only SQL this package renders from a column takes the new name: column
parts, placeholders, JSON extractions, INCLUDE entries and the implicit
SQL Server filter. Names still come from the source's own columns, so a
default name that follows the column becomes a rename, as EF Core's do.
Verbatim SQL is compared as written.

Closes #182

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.6.0 milestone Oct 23, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working severity/medium Inconsistency or failure in a narrow scenario package/core Affects EFCore.ComplexIndexes package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL package/sqlserver Affects EFCore.ComplexIndexes.SqlServer area/indexes Complex-property, composite, unique and filtered indexes area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints area/migrations The differs, snapshot format, EnsureCreated and design-time wiring labels Oct 23, 2026
…umn it filters on

CI's SQL Server lane showed sp_rename rejecting a column a filtered
index's predicate reads: "The index 'ix_name_email_active' is dependent
on column 'revoked_at'. RENAME COLUMN revoked_at failed because one or
more objects access this column." A key or included column is renamed
under the index.

The in-the-way check now also passes the columns the filter names to an
internal RequiresIndexRecreation overload, and the SQL Server differ
drops such an index before a RenameColumnOperation of one of them and
creates it after, the implicit IS NOT NULL filter included. EF Core
rebuilds its own filtered indexes on a rename for the same reason.

Refs #182

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder
CaffeinatedCoder merged commit d28c938 into release/5.x Oct 23, 2026
14 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/182-column-renames branch October 23, 2026 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints 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 package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL package/sqlserver Affects EFCore.ComplexIndexes.SqlServer severity/medium Inconsistency or failure in a narrow scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant