Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for index recreation #26657

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,38 @@ public virtual Task Alter_column_change_computed()
}
});

[ConditionalFact]
public virtual Task Alter_column_change_computed_recreates_indexes()
=> Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.Property<int>("X");
e.Property<int>("Y");
e.Property<int>("Sum");

e.HasIndex("Sum");
}),
builder => builder.Entity("People").Property<int>("Sum")
.HasComputedColumnSql($"{DelimitIdentifier("X")} + {DelimitIdentifier("Y")}"),
builder => builder.Entity("People").Property<int>("Sum")
.HasComputedColumnSql($"{DelimitIdentifier("X")} - {DelimitIdentifier("Y")}"),
model =>
{
var table = Assert.Single(model.Tables);
var sumColumn = Assert.Single(table.Columns, c => c.Name == "Sum");
if (AssertComputedColumns)
{
Assert.Contains("X", sumColumn.ComputedColumnSql);
Assert.Contains("Y", sumColumn.ComputedColumnSql);
Assert.Contains("-", sumColumn.ComputedColumnSql);
}

var sumIndex = Assert.Single(table.Indexes);
Assert.Collection(sumIndex.Columns, c => Assert.Equal("Sum", c.Name));
});

[ConditionalFact]
public virtual Task Alter_column_change_computed_type()
=> Test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,23 @@ public override async Task Alter_column_change_computed()
ALTER TABLE [People] ADD [Sum] AS [X] - [Y];");
}

public override async Task Alter_column_change_computed_recreates_indexes()
{
await base.Alter_column_change_computed_recreates_indexes();

AssertSql(
@"DROP INDEX [IX_People_Sum] ON [People];
DECLARE @var0 sysname;
SELECT @var0 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[People]') AND [c].[name] = N'Sum');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [People] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [People] DROP COLUMN [Sum];
ALTER TABLE [People] ADD [Sum] AS [X] - [Y];",
@"CREATE INDEX [IX_People_Sum] ON [People] ([Sum]);");
}

public override async Task Alter_column_change_computed_type()
{
await base.Alter_column_change_computed_type();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,27 @@ public override async Task Alter_column_change_computed()
@"PRAGMA foreign_keys = 1;");
}

public override async Task Alter_column_change_computed_recreates_indexes()
{
await base.Alter_column_change_computed_recreates_indexes();

AssertSql(
@"CREATE TABLE ""ef_temp_People"" (
""Id"" INTEGER NOT NULL,
""Sum"" AS (""X"" - ""Y""),
""X"" INTEGER NOT NULL,
""Y"" INTEGER NOT NULL
);",
@"INSERT INTO ""ef_temp_People"" (""Id"", ""X"", ""Y"")
SELECT ""Id"", ""X"", ""Y""
FROM ""People"";",
@"PRAGMA foreign_keys = 0;",
@"DROP TABLE ""People"";",
@"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";",
@"PRAGMA foreign_keys = 1;",
@"CREATE INDEX ""IX_People_Sum"" ON ""People"" (""Sum"");");
}

public override async Task Alter_column_change_computed_type()
{
await base.Alter_column_change_computed_type();
Expand Down