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

[release/6.0] Fix to #27375 - EF Core 6.0 temporal tables - migration rollback doesn't work #27379

Merged
merged 1 commit into from
Mar 2, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2573,12 +2573,25 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema

if (historyTableName != null)
{
operations.Add(
new DropTableOperation
{
Name = historyTableName,
Schema = alterTableOperation.OldTable.Schema
});
var useOldBehavior27375 = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27375", out var enabled27375) && enabled27375;
if (!useOldBehavior27375)
{
operations.Add(
new DropTableOperation
{
Name = historyTableName,
Schema = historyTableSchema
});
}
else
{
operations.Add(
new DropTableOperation
{
Name = historyTableName,
Schema = alterTableOperation.OldTable.Schema
});
}
}

operations.Add(operation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,233 @@ 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_temporal_table_with_explicit_schemas_same_schema_for_table_and_history_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("Customer", "mySchema", tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("HistoryTable", "mySchema");
ttb.HasPeriodStart("PeriodStart");
ttb.HasPeriodEnd("PeriodEnd");
}));
}),
builder => builder.Entity(
"Customer", e =>
{
e.ToTable("Customer", "mySchema");
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 [mySchema].[Customer] SET (SYSTEM_VERSIONING = OFF)",
//
@"ALTER TABLE [mySchema].[Customer] DROP PERIOD FOR SYSTEM_TIME",
//
@"DROP TABLE [mySchema].[HistoryTable];");
}

[ConditionalFact]
public virtual async Task Convert_temporal_table_using_custom_default_schema_to_normal_table()
{
await Test(

builder => builder.HasDefaultSchema("myDefaultSchema"),
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("Customer", tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("HistoryTable");
ttb.HasPeriodStart("PeriodStart");
ttb.HasPeriodEnd("PeriodEnd");
}));
}),
builder => builder.Entity(
"Customer", e =>
{
e.ToTable("Customer");
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 [myDefaultSchema].[Customer] SET (SYSTEM_VERSIONING = OFF)",
//
@"ALTER TABLE [myDefaultSchema].[Customer] DROP PERIOD FOR SYSTEM_TIME",
//
@"DROP TABLE [myDefaultSchema].[HistoryTable];");
}

[ConditionalFact]
public virtual async Task Convert_temporal_table_using_custom_default_schema_and_explicit_history_schema_to_normal_table()
{
await Test(

builder => builder.HasDefaultSchema("myDefaultSchema"),
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("Customer", tb => tb.IsTemporal(ttb =>
{
ttb.UseHistoryTable("HistoryTable", "mySchema");
ttb.HasPeriodStart("PeriodStart");
ttb.HasPeriodEnd("PeriodEnd");
}));
}),
builder => builder.Entity(
"Customer", e =>
{
e.ToTable("Customer");
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 [myDefaultSchema].[Customer] SET (SYSTEM_VERSIONING = OFF)",
//
@"ALTER TABLE [myDefaultSchema].[Customer] DROP PERIOD FOR SYSTEM_TIME",
//
@"DROP TABLE [mySchema].[HistoryTable];");
}

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