-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimescaleDbMigrationsInfrastructureTests.cs
More file actions
188 lines (161 loc) · 8.51 KB
/
TimescaleDbMigrationsInfrastructureTests.cs
File metadata and controls
188 lines (161 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using CmdScale.EntityFrameworkCore.TimescaleDB.FunctionalTests.Snapshots;
using CmdScale.EntityFrameworkCore.TimescaleDB.FunctionalTests.Utils;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Xunit.Abstractions;
namespace CmdScale.EntityFrameworkCore.TimescaleDB.FunctionalTests
{
public class TimescaleDbMigrationsInfrastructureTests : MigrationsInfrastructureTestBase<TimescaleMigrationsFixture>
{
private readonly ITestOutputHelper _testOutputHelper;
private class AspNetIdentityDbContext(DbContextOptions options)
: IdentityDbContext<IdentityUser>(options)
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<IdentityUser>(b =>
{
b.HasIndex(u => u.NormalizedUserName).HasDatabaseName("UserNameIndex").IsUnique();
b.HasIndex(u => u.NormalizedEmail).HasDatabaseName("EmailIndex");
b.ToTable("AspNetUsers");
});
builder.Entity<IdentityUserClaim<string>>(b =>
{
b.ToTable("AspNetUserClaims");
});
builder.Entity<IdentityUserLogin<string>>(b =>
{
b.ToTable("AspNetUserLogins");
b.Property(l => l.LoginProvider).HasMaxLength(128);
b.Property(l => l.ProviderKey).HasMaxLength(128);
});
builder.Entity<IdentityUserToken<string>>(b =>
{
b.ToTable("AspNetUserTokens");
b.Property(t => t.LoginProvider).HasMaxLength(128);
b.Property(t => t.Name).HasMaxLength(128);
});
builder.Entity<IdentityRole>(b =>
{
b.HasIndex(r => r.NormalizedName).HasDatabaseName("RoleNameIndex").IsUnique();
b.ToTable("AspNetRoles");
});
builder.Entity<IdentityRoleClaim<string>>(b =>
{
b.ToTable("AspNetRoleClaims");
});
builder.Entity<IdentityUserRole<string>>(b =>
{
b.ToTable("AspNetUserRoles");
});
}
}
public TimescaleDbMigrationsInfrastructureTests(TimescaleMigrationsFixture fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
{
_testOutputHelper = testOutputHelper;
Fixture.ListLoggerFactory.Clear();
}
protected override Task ExecuteSqlAsync(string value)
{
((TimescaleTestStore)Fixture.TestStore).ExecuteScript(value);
return Task.CompletedTask;
}
[ConditionalFact]
public override void Can_diff_against_2_1_ASP_NET_Identity_model()
{
using AspNetIdentityDbContext context = new(
Fixture.TestStore.AddProviderOptions(new DbContextOptionsBuilder()).Options);
DiffSnapshot(new AspNetIdentity21ModelSnapshot(), context);
}
[ConditionalFact]
public override void Can_diff_against_2_2_ASP_NET_Identity_model()
{
using AspNetIdentityDbContext context = new(
Fixture.TestStore.AddProviderOptions(new DbContextOptionsBuilder()).Options);
DiffSnapshot(new AspNetIdentity22ModelSnapshot(), context);
}
[ConditionalFact]
public override void Can_diff_against_2_2_model()
{
using MigrationsInfrastructureFixtureBase.MigrationsContext context = Fixture.CreateContext();
DiffSnapshot(new EfCore22ModelSnapshot(), context);
}
/// <summary>
/// PostgreSQL/Npgsql doesn't support applying migrations within an externally-managed transaction.
/// This is a known limitation - see https://github.com/npgsql/efcore.pg/issues/3407
/// </summary>
[ConditionalFact]
public override void Can_apply_two_migrations_in_transaction()
=> Assert.ThrowsAny<Exception>(() => base.Can_apply_two_migrations_in_transaction());
/// <summary>
/// PostgreSQL/Npgsql doesn't support applying migrations within an externally-managed transaction.
/// This is a known limitation - see https://github.com/npgsql/efcore.pg/issues/3407
/// </summary>
[ConditionalFact]
public override Task Can_apply_two_migrations_in_transaction_async()
=> Assert.ThrowsAnyAsync<Exception>(() => base.Can_apply_two_migrations_in_transaction_async());
[ConditionalFact]
public override void Can_diff_against_3_0_ASP_NET_Identity_model()
{
using AspNetIdentityDbContext context = new(
Fixture.TestStore.AddProviderOptions(new DbContextOptionsBuilder()).Options);
DiffSnapshot(new AspNetIdentity30ModelSnapshot(), context);
}
protected virtual void DiffSnapshotWithDebug(ModelSnapshot snapshot, DbContext context)
{
IModel sourceModel = context.GetService<IModelRuntimeInitializer>().Initialize(
snapshot.Model, designTime: true, validationLogger: null);
IMigrationsModelDiffer modelDiffer = context.GetService<IMigrationsModelDiffer>();
IReadOnlyList<MigrationOperation> operations = modelDiffer.GetDifferences(
sourceModel.GetRelationalModel(),
context.GetService<IDesignTimeModel>().Model.GetRelationalModel());
if (operations.Count > 0)
{
_testOutputHelper.WriteLine($"Found {operations.Count} differences:");
for (int i = 0; i < operations.Count; i++)
{
MigrationOperation op = operations[i];
_testOutputHelper.WriteLine($" {i + 1}. {op.GetType().Name}:");
if (op is AlterColumnOperation alterColumn)
{
_testOutputHelper.WriteLine($" Table: {alterColumn.Table}");
_testOutputHelper.WriteLine($" Column: {alterColumn.Name}");
_testOutputHelper.WriteLine($" OldColumn Type: {alterColumn.OldColumn?.ClrType?.Name} -> {alterColumn.OldColumn?.ColumnType}");
_testOutputHelper.WriteLine($" NewColumn Type: {alterColumn.ClrType?.Name} -> {alterColumn.ColumnType}");
_testOutputHelper.WriteLine($" OldColumn Nullable: {alterColumn.OldColumn?.IsNullable}");
_testOutputHelper.WriteLine($" NewColumn Nullable: {alterColumn.IsNullable}");
_testOutputHelper.WriteLine($" OldColumn DefaultValue: {alterColumn.OldColumn?.DefaultValue}");
_testOutputHelper.WriteLine($" NewColumn DefaultValue: {alterColumn.DefaultValue}");
_testOutputHelper.WriteLine($" OldColumn DefaultValueGenerated: {alterColumn.OldColumn?.DefaultValueSql}");
_testOutputHelper.WriteLine($" NewColumn DefaultValueGenerated: {alterColumn.DefaultValueSql}");
}
else if (op is CreateTableOperation createTable)
{
_testOutputHelper.WriteLine($" Table: {createTable.Name}");
_testOutputHelper.WriteLine($" Columns: {string.Join(", ", createTable.Columns.Select(c => $"{c.Name}:{c.ClrType?.Name}"))}");
}
else if (op is CreateIndexOperation createIndex)
{
_testOutputHelper.WriteLine($" Table: {createIndex.Table}");
_testOutputHelper.WriteLine($" Name: {createIndex.Name}");
_testOutputHelper.WriteLine($" Columns: {string.Join(", ", createIndex.Columns)}");
_testOutputHelper.WriteLine($" IsUnique: {createIndex.IsUnique}");
}
else
{
_testOutputHelper.WriteLine($" {op}");
}
_testOutputHelper.WriteLine("");
}
}
Assert.Empty(operations);
}
}
}