Skip to content

Commit

Permalink
Add test coverage for index recreation (#26657)
Browse files Browse the repository at this point in the history
Closes #26656
  • Loading branch information
roji authored Nov 13, 2021
1 parent fa3cea4 commit a15bcc3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
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 @@ FROM [sys].[default_constraints] [d]
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

0 comments on commit a15bcc3

Please sign in to comment.