Skip to content

Commit 6da773f

Browse files
Additional tests for Temporal tables (#26682)
1 parent 71fa384 commit 6da773f

File tree

1 file changed

+118
-2
lines changed

1 file changed

+118
-2
lines changed

test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs

+118-2
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,122 @@ PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
22882288
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [mySchema].[CustomersHistory]));");
22892289
}
22902290

2291+
[ConditionalFact]
2292+
public virtual async Task Create_temporal_table_with_default_schema_for_model_changed_and_explicit_history_table_schema_not_provided()
2293+
{
2294+
await Test(
2295+
builder => { },
2296+
builder =>
2297+
{
2298+
builder.HasDefaultSchema("myDefaultSchema");
2299+
builder.Entity(
2300+
"Customer", e =>
2301+
{
2302+
e.Property<int>("Id").ValueGeneratedOnAdd();
2303+
e.Property<string>("Name");
2304+
e.Property<DateTime>("SystemTimeStart").ValueGeneratedOnAddOrUpdate();
2305+
e.Property<DateTime>("SystemTimeEnd").ValueGeneratedOnAddOrUpdate();
2306+
e.HasKey("Id");
2307+
2308+
e.ToTable("Customers", tb => tb.IsTemporal(ttb =>
2309+
{
2310+
ttb.UseHistoryTable("HistoryTable");
2311+
ttb.HasPeriodStart("SystemTimeStart");
2312+
ttb.HasPeriodEnd("SystemTimeEnd");
2313+
}));
2314+
});
2315+
},
2316+
model =>
2317+
{
2318+
var table = Assert.Single(model.Tables);
2319+
Assert.Equal("Customers", table.Name);
2320+
Assert.Equal(true, table[SqlServerAnnotationNames.IsTemporal]);
2321+
Assert.Equal("HistoryTable", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
2322+
Assert.Equal("myDefaultSchema", table[SqlServerAnnotationNames.TemporalHistoryTableSchema]);
2323+
Assert.Equal("SystemTimeStart", table[SqlServerAnnotationNames.TemporalPeriodStartPropertyName]);
2324+
Assert.Equal("SystemTimeEnd", table[SqlServerAnnotationNames.TemporalPeriodEndPropertyName]);
2325+
2326+
Assert.Collection(
2327+
table.Columns,
2328+
c => Assert.Equal("Id", c.Name),
2329+
c => Assert.Equal("Name", c.Name));
2330+
Assert.Same(
2331+
table.Columns.Single(c => c.Name == "Id"),
2332+
Assert.Single(table.PrimaryKey!.Columns));
2333+
});
2334+
2335+
AssertSql(
2336+
@"IF SCHEMA_ID(N'myDefaultSchema') IS NULL EXEC(N'CREATE SCHEMA [myDefaultSchema];');",
2337+
//
2338+
@"CREATE TABLE [myDefaultSchema].[Customers] (
2339+
[Id] int NOT NULL,
2340+
[Name] nvarchar(max) NULL,
2341+
[SystemTimeEnd] datetime2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
2342+
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
2343+
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id]),
2344+
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
2345+
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [myDefaultSchema].[HistoryTable]));");
2346+
}
2347+
2348+
[ConditionalFact]
2349+
public virtual async Task Create_temporal_table_with_default_schema_for_model_changed_and_explicit_history_table_schema_provided()
2350+
{
2351+
await Test(
2352+
builder => { },
2353+
builder =>
2354+
{
2355+
builder.HasDefaultSchema("myDefaultSchema");
2356+
builder.Entity(
2357+
"Customer", e =>
2358+
{
2359+
e.Property<int>("Id").ValueGeneratedOnAdd();
2360+
e.Property<string>("Name");
2361+
e.Property<DateTime>("SystemTimeStart").ValueGeneratedOnAddOrUpdate();
2362+
e.Property<DateTime>("SystemTimeEnd").ValueGeneratedOnAddOrUpdate();
2363+
e.HasKey("Id");
2364+
2365+
e.ToTable("Customers", tb => tb.IsTemporal(ttb =>
2366+
{
2367+
ttb.UseHistoryTable("HistoryTable", "historySchema");
2368+
ttb.HasPeriodStart("SystemTimeStart");
2369+
ttb.HasPeriodEnd("SystemTimeEnd");
2370+
}));
2371+
});
2372+
},
2373+
model =>
2374+
{
2375+
var table = Assert.Single(model.Tables);
2376+
Assert.Equal("Customers", table.Name);
2377+
Assert.Equal(true, table[SqlServerAnnotationNames.IsTemporal]);
2378+
Assert.Equal("HistoryTable", table[SqlServerAnnotationNames.TemporalHistoryTableName]);
2379+
Assert.Equal("historySchema", table[SqlServerAnnotationNames.TemporalHistoryTableSchema]);
2380+
Assert.Equal("SystemTimeStart", table[SqlServerAnnotationNames.TemporalPeriodStartPropertyName]);
2381+
Assert.Equal("SystemTimeEnd", table[SqlServerAnnotationNames.TemporalPeriodEndPropertyName]);
2382+
2383+
Assert.Collection(
2384+
table.Columns,
2385+
c => Assert.Equal("Id", c.Name),
2386+
c => Assert.Equal("Name", c.Name));
2387+
Assert.Same(
2388+
table.Columns.Single(c => c.Name == "Id"),
2389+
Assert.Single(table.PrimaryKey!.Columns));
2390+
});
2391+
2392+
AssertSql(
2393+
@"IF SCHEMA_ID(N'myDefaultSchema') IS NULL EXEC(N'CREATE SCHEMA [myDefaultSchema];');",
2394+
//
2395+
@"IF SCHEMA_ID(N'historySchema') IS NULL EXEC(N'CREATE SCHEMA [historySchema];');",
2396+
//
2397+
@"CREATE TABLE [myDefaultSchema].[Customers] (
2398+
[Id] int NOT NULL,
2399+
[Name] nvarchar(max) NULL,
2400+
[SystemTimeEnd] datetime2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
2401+
[SystemTimeStart] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
2402+
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id]),
2403+
PERIOD FOR SYSTEM_TIME([SystemTimeStart], [SystemTimeEnd])
2404+
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [historySchema].[HistoryTable]));");
2405+
}
2406+
22912407
[ConditionalFact]
22922408
public virtual async Task Create_temporal_table_with_default_schema_for_table_and_explicit_history_table_schema_provided()
22932409
{
@@ -2783,8 +2899,8 @@ await Test(
27832899
e.Property<int>("Number");
27842900
}),
27852901
builder =>
2786-
{
2787-
},
2902+
{
2903+
},
27882904
model =>
27892905
{
27902906
var table = Assert.Single(model.Tables);

0 commit comments

Comments
 (0)