Skip to content

Rebuild indexes when altering sparse SQL Server columns - #38765

Open
AndriySvyryd with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-unable-to-make-column-sparse
Open

Rebuild indexes when altering sparse SQL Server columns#38765
AndriySvyryd with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-unable-to-make-column-sparse

Conversation

Copilot AI commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Altering a column to SPARSE fails when SQL Server indexes depend on that column. Migrations now recreate affected indexes around the column alteration.

  • Migration SQL generation
    • Treat sparse annotation changes like narrowing/type changes for index rebuild purposes.
    • Drop dependent indexes before ALTER COLUMN and recreate them afterward.
DROP INDEX [IX_People_SomeProperty] ON [People];
ALTER TABLE [People] ALTER COLUMN [SomeProperty] nvarchar(450) SPARSE NULL;
CREATE INDEX [IX_People_SomeProperty] ON [People] ([SomeProperty]);
  • Regression coverage
    • Added a SQL Server migration test for converting an indexed column to sparse.

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix unable to make a column sparse with an index Rebuild indexes when altering sparse SQL Server columns Aug 7, 2026
Copilot AI requested a review from AndriySvyryd August 7, 2026 20:46
@AndriySvyryd
AndriySvyryd requested a lite review from Copilot August 7, 2026 21:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates SQL Server migration SQL generation so that altering a column’s SPARSE setting will drop/recreate dependent indexes around ALTER COLUMN, avoiding SQL Server failures when indexes reference the altered column (fixing #38760).

Changes:

  • Treat SPARSE annotation changes similarly to narrowing/type changes for index rebuild purposes in SqlServerMigrationsSqlGenerator.
  • Generate DROP INDEX ... before ALTER TABLE ... ALTER COLUMN ... SPARSE ... and CREATE INDEX ... afterward.
  • Add a SQL Server functional migration test covering converting an indexed column to sparse.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Triggers index rebuilds when the SPARSE annotation changes during AlterColumn.
test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs Adds regression coverage asserting index drop/recreate around sparse column alteration.

Comment thread src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review August 7, 2026 22:02
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner August 7, 2026 22:02
@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) August 7, 2026 22:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs:325

  • The newly added sparseChanged lines contain stray carriage-return characters (shown as in the file view), which introduces mixed line endings in this LF file. Please normalize these lines to the repository’s standard line endings to avoid noisy diffs and potential style checks failing.
        var sparseChanged = ((bool?)operation[SqlServerAnnotationNames.Sparse] ?? false)

            != ((bool?)operation.OldColumn[SqlServerAnnotationNames.Sparse] ?? false);

Comment thread src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Outdated
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 7, 2026 23:27
auto-merge was automatically disabled August 7, 2026 23:27

Head branch was pushed to by a user without write access

Copilot AI requested a review from AndriySvyryd August 7, 2026 23:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@AndriySvyryd

Copy link
Copy Markdown
Member

@cincuranet "Hide whitespace" when reviewing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to make a column sparse (in SQL Server) if it has an index

3 participants