Skip to content

Commit

Permalink
Fix to #26271 - Temporal history table name always based on entity ty…
Browse files Browse the repository at this point in the history
…pe name, not table name

When deciding on history table name use entityType.GetTableName() function instead of entity type name directly.

Fixes #26271
  • Loading branch information
maumar committed Oct 8, 2021
1 parent 458569e commit 6898d78
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static void SetPeriodEndPropertyName(this IMutableEntityType entityType,
: entityType[SqlServerAnnotationNames.TemporalHistoryTableName] is string historyTableName
? historyTableName
: entityType[SqlServerAnnotationNames.IsTemporal] as bool? == true
? entityType.ShortName() + DefaultHistoryTableNameSuffix
? entityType.GetTableName() + DefaultHistoryTableNameSuffix
: null;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,7 @@ await Test(
Assert.Equal("Customers", table.Name);
Assert.Equal("mySchema", table.Schema);
Assert.Equal(true, table[SqlServerAnnotationNames.IsTemporal]);
Assert.Equal("CustomerHistory", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Equal("CustomersHistory", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Equal("SystemTimeStart", table[SqlServerAnnotationNames.TemporalPeriodStartPropertyName]);
Assert.Equal("SystemTimeEnd", table[SqlServerAnnotationNames.TemporalPeriodEndPropertyName]);

Expand All @@ -2109,7 +2109,7 @@ [Name] nvarchar(max) NULL,
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id]),
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[CustomerHistory]));");
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[CustomersHistory]));");
}

[ConditionalFact]
Expand Down Expand Up @@ -2142,7 +2142,7 @@ await Test(
Assert.Equal("Customers", table.Name);
Assert.Equal("myDefaultSchema", table.Schema);
Assert.Equal(true, table[SqlServerAnnotationNames.IsTemporal]);
Assert.Equal("CustomerHistory", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Equal("CustomersHistory", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Equal("SystemTimeStart", table[SqlServerAnnotationNames.TemporalPeriodStartPropertyName]);
Assert.Equal("SystemTimeEnd", table[SqlServerAnnotationNames.TemporalPeriodEndPropertyName]);

Expand All @@ -2165,7 +2165,7 @@ [Name] nvarchar(max) NULL,
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id]),
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [myDefaultSchema].[CustomerHistory]));");
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [myDefaultSchema].[CustomersHistory]));");
}

[ConditionalFact]
Expand Down Expand Up @@ -2198,7 +2198,7 @@ await Test(
Assert.Equal("Customers", table.Name);
Assert.Equal("mySchema", table.Schema);
Assert.Equal(true, table[SqlServerAnnotationNames.IsTemporal]);
Assert.Equal("CustomerHistory", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Equal("CustomersHistory", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
Assert.Equal("SystemTimeStart", table[SqlServerAnnotationNames.TemporalPeriodStartPropertyName]);
Assert.Equal("SystemTimeEnd", table[SqlServerAnnotationNames.TemporalPeriodEndPropertyName]);

Expand All @@ -2221,7 +2221,7 @@ [Name] nvarchar(max) NULL,
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id]),
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[CustomerHistory]));");
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[CustomersHistory]));");
}

[ConditionalFact]
Expand Down Expand Up @@ -3849,7 +3849,7 @@ [Name] nvarchar(max) NULL,
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id]),
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[CustomerHistory]));",
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[CustomersHistory]));",
//
@"CREATE TABLE [mySchema].[Orders] (
[Id] int NOT NULL,
Expand All @@ -3858,7 +3858,7 @@ [Name] nvarchar(max) NULL,
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY ([Id]),
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[OrderHistory]));");
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[OrdersHistory]));");
}

[ConditionalFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ protected override void Seed(ComplexNavigationsContext context)

var historyTableInfos = new List<(string table, string historyTable)>()
{
("LevelOne", "Level1History"),
("LevelTwo", "Level2History"),
("LevelThree", "Level3History"),
("LevelFour", "Level4History"),
("LevelOne", "LevelOneHistory"),
("LevelTwo", "LevelTwoHistory"),
("LevelThree", "LevelThreeHistory"),
("LevelFour", "LevelFourHistory"),
};

// clean up intermittent history since in the Seed method we do fixup in multiple stages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ protected override void Seed(InheritanceContext context)

var historyTableInfos = new List<(string table, string historyTable)>()
{
("Animals", "AnimalHistory"),
("Plants", "PlantHistory"),
("Countries", "CountryHistory"),
("Drinks", "DrinkHistory"),
("Animals", "AnimalsHistory"),
("Plants", "PlantsHistory"),
("Countries", "CountriesHistory"),
("Drinks", "DrinksHistory"),
};

foreach (var historyTableInfo in historyTableInfos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ protected override void Seed(GearsOfWarContext context)
//// clean up intermittent history - we do the data fixup in 2 steps (due to cycle)
//// so we want to remove the temporary states, so that further manipulation is easier
context.Database.ExecuteSqlRaw("ALTER TABLE [LocustLeaders] SET (SYSTEM_VERSIONING = OFF)");
context.Database.ExecuteSqlRaw("DELETE FROM [LocustLeaderHistory]");
context.Database.ExecuteSqlRaw("ALTER TABLE [LocustLeaders] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[LocustLeaderHistory]))");
context.Database.ExecuteSqlRaw("DELETE FROM [LocustLeadersHistory]");
context.Database.ExecuteSqlRaw("ALTER TABLE [LocustLeaders] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[LocustLeadersHistory]))");

context.Database.ExecuteSqlRaw("ALTER TABLE [Missions] SET (SYSTEM_VERSIONING = OFF)");
context.Database.ExecuteSqlRaw("DELETE FROM [MissionHistory]");
context.Database.ExecuteSqlRaw("ALTER TABLE [Missions] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MissionHistory]))");
context.Database.ExecuteSqlRaw("DELETE FROM [MissionsHistory]");
context.Database.ExecuteSqlRaw("ALTER TABLE [Missions] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MissionsHistory]))");

context.RemoveRange(context.ChangeTracker.Entries().Where(e => e.Entity is City).Select(e => e.Entity));
context.RemoveRange(context.ChangeTracker.Entries().Where(e => e.Entity is CogTag).Select(e => e.Entity));
Expand All @@ -62,22 +62,22 @@ protected override void Seed(GearsOfWarContext context)

// clean up Faction history
context.Database.ExecuteSqlRaw("ALTER TABLE [Factions] SET (SYSTEM_VERSIONING = OFF)");
context.Database.ExecuteSqlRaw("DELETE FROM [FactionHistory] WHERE CommanderName IS NULL");
context.Database.ExecuteSqlRaw("ALTER TABLE [Factions] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[FactionHistory]))");
context.Database.ExecuteSqlRaw("DELETE FROM [FactionsHistory] WHERE CommanderName IS NULL");
context.Database.ExecuteSqlRaw("ALTER TABLE [Factions] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[FactionsHistory]))");

var historyTableInfos = new List<(string table, string historyTable)>()
{
("Cities", "CityHistory"),
("Tags", "CogTagHistory"),
("Gears", "GearHistory"),
("LocustHighCommands", "LocustHighCommandHistory"),
("Missions", "MissionHistory"),
("Squads", "SquadHistory"),
("SquadMissions", "SquadMissionHistory"),
("Weapons", "WeaponHistory"),

("LocustLeaders", "LocustLeaderHistory"),
("Factions", "FactionHistory"),
("Cities", "CitiesHistory"),
("Tags", "TagsHistory"),
("Gears", "GearsHistory"),
("LocustHighCommands", "LocustHighCommandsHistory"),
("Missions", "MissionsHistory"),
("Squads", "SquadsHistory"),
("SquadMissions", "SquadMissionsHistory"),
("Weapons", "WeaponsHistory"),

("LocustLeaders", "LocustLeadersHistory"),
("Factions", "FactionsHistory"),
};

foreach (var historyTableInfo in historyTableInfos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ protected override void Seed(ManyToManyContext context)

var historyTableInfos = new List<(string table, string historyTable)>()
{
("EntityCompositeKeys", "EntityCompositeKeyHistory"),
("EntityCompositeKeys", "EntityCompositeKeysHistory"),
("EntityOneEntityTwo", "EntityOneEntityTwoHistory"),
("EntityOnes", "EntityOneHistory"),
("EntityTwos", "EntityTwoHistory"),
("EntityThrees", "EntityThreeHistory"),
("EntityRoots", "EntityRootHistory"),
("EntityOnes", "EntityOnesHistory"),
("EntityTwos", "EntityTwosHistory"),
("EntityThrees", "EntityThreesHistory"),
("EntityRoots", "EntityRootsHistory"),
("EntityRootEntityThree", "EntityRootEntityThreeHistory"),

("JoinCompositeKeyToLeaf", "JoinCompositeKeyToLeafHistory"),
Expand Down

0 comments on commit 6898d78

Please sign in to comment.