Skip to content

Commit

Permalink
Fix to #27375 - EF Core 6.0 temporal tables - migration rollback does…
Browse files Browse the repository at this point in the history
…n't work

Problem was that we were incorrectly using old table schema rather than history table schema when we were creating DropTableOperation for the history table, after versioning has been disabled.
Fix is to use the correct value.

Fixes #27375
  • Loading branch information
maumar committed Feb 4, 2022
1 parent a170463 commit 4a1add4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema
new DropTableOperation
{
Name = historyTableName,
Schema = alterTableOperation.OldTable.Schema
Schema = historyTableSchema
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,61 @@ FROM [sys].[default_constraints] [d]
@"DROP TABLE [HistoryTable];");
}

[ConditionalFact]
public virtual async Task Convert_temporal_table_with_explicit_history_table_schema_to_normal_table()
{
await Test(
builder => builder.Entity(
"Customer", e =>
{
e.Property<int>("Id").ValueGeneratedOnAdd();
e.Property<string>("Name");
e.Property<DateTime>("PeriodStart").ValueGeneratedOnAddOrUpdate();
e.Property<DateTime>("PeriodEnd").ValueGeneratedOnAddOrUpdate();
e.HasKey("Id");
e.ToTable(tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("HistoryTable", "historySchema");
ttb.HasPeriodStart("PeriodStart");
ttb.HasPeriodEnd("PeriodEnd");
}));
}),
builder => builder.Entity(
"Customer", e =>
{
e.Property<int>("Id").ValueGeneratedOnAdd();
e.Property<string>("Name");
e.Property<DateTime>("PeriodStart");
e.Property<DateTime>("PeriodEnd");
e.HasKey("Id");
}),
model =>
{
var table = Assert.Single(model.Tables);
Assert.Equal("Customer", table.Name);
Assert.Null(table[SqlServerAnnotationNames.IsTemporal]);
Assert.Null(table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Collection(
table.Columns,
c => Assert.Equal("Id", c.Name),
c => Assert.Equal("Name", c.Name),
c => Assert.Equal("PeriodEnd", c.Name),
c => Assert.Equal("PeriodStart", c.Name));
Assert.Same(
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});

AssertSql(
@"ALTER TABLE [Customer] SET (SYSTEM_VERSIONING = OFF)",
//
@"ALTER TABLE [Customer] DROP PERIOD FOR SYSTEM_TIME",
//
@"DROP TABLE [historySchema].[HistoryTable];");
}

[ConditionalFact]
public virtual async Task Convert_normal_table_to_temporal_table_with_minimal_configuration()
{
Expand Down

0 comments on commit 4a1add4

Please sign in to comment.