Skip to content

SQL Server: check the clustered slot and key names on the whole target table - #105

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/90-sqlserver-clustered-and-key-names
Oct 20, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/90-sqlserver-clustered-and-key-names

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Closes #90

Was stacked on #103 (#91), now merged; rebased onto main.

Problem

Three gaps in the SQL Server satellite's validation:

  • A second clustered complex index in a later migration. With cx_one (clustered) already applied, a migration adding cx_two with .IsClustered() scaffolded CREATE CLUSTERED INDEX [cx_two] ON [things] (...) and failed at apply. ValidateCreatedIndexes counted clustered indexes among one migration's creates only, while docs/sqlserver.md promised a rejection at migrations add.
  • The clustered-slot check ran on the source model. It lived in TransformIndexAnnotation, which runs for both sides of the diff, so a snapshot holding a clustered key plus a clustered complex index (reachable only from snapshots scaffolded before 5.0.2) refused even the migration that fixes it. Every other check validates the target only.
  • A complex index named like its table's key. indexName: "PK_things" scaffolded CREATE INDEX [PK_things] ON [things] and failed at apply: the core's collision check covers native HasIndex declarations, not keys.

Change

  • CustomMigrationsModelDiffer: a new internal hook, ValidateTargetIndexes(IRelationalModel target, IReadOnlyCollection<IndexDescriptor>), called once with every complex index the target model declares, after the existing target-only checks. It is internal because IndexDescriptor is; the satellites see it through InternalsVisibleTo, so the public surface is unchanged.
  • SqlServerComplexIndexMigrationsModelDiffer overrides it, per target table:
    • Clustered slot. More than one clustered complex index fails. A single one fails against a clustered primary key (clustered unless IsClustered(false)), a unique constraint declared clustered (new), or a clustered native index.
    • Key names. A complex index named like the table's primary key or one of its unique constraints fails. The comparison ignores case, as the default case-insensitive collation does; a database with a case-sensitive collation pays at most a rename. Another table's key name is still allowed, since SQL Server scopes index names per table.
    • The ValidateCreatedIndexes override and the clustered check in TransformIndexAnnotation are removed. Pack shows no ApiCompat break.
  • docs/sqlserver.md: the deliberate-rejections list covers both.
  • Changelog: one ### Fixed line in the root and SQL Server changelogs.

Tests

SqlServerDifferTests gains eight tests: a second clustered index in a later migration (rejected), the clustered slot moving between two complex indexes in one migration (allowed), a source snapshot holding a conflict (diffable), a clustered native index, a clustered unique constraint, a name equal to the primary key's in a different case, a name equal to a unique constraint's (all rejected), and another table's key name (allowed).

Verified by reverting src/: six fail. Five are the fixes (later migration, snapshot, primary-key name, unique-constraint name, clustered unique constraint), and the sixth is the native-index test, failing on its message only. That branch existed before with no test, one of the seeds #96 lists; this is its first. Full unit suite: 403 passed.

The SQL Server lane (#100) applies the clustered cases to a live server.

🤖 Generated with Claude Code

@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working stacked Base is another open PR; merge in order, CI does not run until it targets main severity/low Cosmetic, message or documentation-level difference package/core Affects EFCore.ComplexIndexes package/sqlserver Affects EFCore.ComplexIndexes.SqlServer area/indexes Complex-property, composite, unique and filtered indexes labels Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the branch fix/91-guard-in-constructors October 20, 2026 06:08
@CaffeinatedCoder
CaffeinatedCoder changed the base branch from fix/91-guard-in-constructors to main October 20, 2026 06:08
@CaffeinatedCoder CaffeinatedCoder removed the stacked Base is another open PR; merge in order, CI does not run until it targets main label Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder force-pushed the fix/90-sqlserver-clustered-and-key-names branch from a678e57 to 2a0060b Compare October 20, 2026 06:09
…arget table (#90)

Three gaps in the SQL Server satellite's validation, each failing only at
apply or refusing a valid migration:

- A second clustered complex index added in a later migration scaffolded
  CREATE CLUSTERED INDEX [cx_two] and failed at apply: ValidateCreatedIndexes
  counted clustered indexes among one migration's creates only.
- The clustered-slot check ran from TransformIndexAnnotation, so on the
  source (snapshot) model too: a snapshot holding a clustered key plus a
  clustered complex index refused even the migration that fixes it.
- A complex index named like its table's key (PK_things) scaffolded and
  failed at apply: the core's name check covers native indexes, not keys.

The core differ gains an internal hook, ValidateTargetIndexes, called with
every complex index of the target model. The SQL Server differ overrides it
to count clustered declarations per table (complex indexes, the primary key,
clustered unique constraints and native indexes) and to compare complex
index names with the table's primary key and unique constraints, ignoring
case as the default collation does. The ValidateCreatedIndexes override and
the check in TransformIndexAnnotation are gone.

Verified: six new SqlServerDifferTests; with the source change reverted,
the later-migration, snapshot, primary-key name, unique-constraint name and
clustered-unique-constraint tests fail (and the native-index one on its
message). Full unit suite green; pack shows no ApiCompat break.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder
CaffeinatedCoder force-pushed the fix/90-sqlserver-clustered-and-key-names branch from 2a0060b to dd59a41 Compare October 20, 2026 06:26
@CaffeinatedCoder
CaffeinatedCoder merged commit 6d9412d into main Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/90-sqlserver-clustered-and-key-names branch October 20, 2026 06:33
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 bug Something isn't working package/core Affects EFCore.ComplexIndexes package/sqlserver Affects EFCore.ComplexIndexes.SqlServer severity/low Cosmetic, message or documentation-level difference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL Server: a second clustered complex index in a later migration, and an index named like the table's key, fail only at apply

1 participant