forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationsInfrastructureNpgsqlTest.cs
More file actions
223 lines (177 loc) · 7.64 KB
/
MigrationsInfrastructureNpgsqlTest.cs
File metadata and controls
223 lines (177 loc) · 7.64 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
namespace Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
public class MigrationsInfrastructureNpgsqlTest(MigrationsInfrastructureNpgsqlTest.MigrationsInfrastructureNpgsqlFixture fixture)
: MigrationsInfrastructureTestBase<MigrationsInfrastructureNpgsqlTest.MigrationsInfrastructureNpgsqlFixture>(fixture)
{
public override void Can_get_active_provider()
{
base.Can_get_active_provider();
Assert.Equal("Npgsql.EntityFrameworkCore.PostgreSQL", ActiveProvider);
}
// See #3407
public override void Can_apply_two_migrations_in_transaction()
=> Assert.ThrowsAny<Exception>(() => base.Can_apply_two_migrations_in_transaction());
// See #3407
public override Task Can_apply_two_migrations_in_transaction_async()
=> Assert.ThrowsAnyAsync<Exception>(() => base.Can_apply_two_migrations_in_transaction_async());
// This tests uses Fixture.CreateEmptyContext(), which does not go through MigrationsInfrastructureNpgsqlFixture.CreateContext()
// and therefore does not set the PostgresVersion in the context options. As a result, we try to drop the database with
// WITH (FORCE), which is only supported starting with PG 13.
[MinimumPostgresVersion(13, 0)]
public override Task Can_generate_no_migration_script()
=> base.Can_generate_no_migration_script();
[ConditionalFact(Skip = "https://github.com/dotnet/efcore/issues/33056")]
public override void Can_apply_all_migrations()
=> base.Can_apply_all_migrations();
[ConditionalFact(Skip = "https://github.com/dotnet/efcore/issues/33056")]
public override void Can_apply_range_of_migrations()
=> base.Can_apply_range_of_migrations();
[ConditionalFact(Skip = "https://github.com/dotnet/efcore/issues/33056")]
public override void Can_revert_all_migrations()
=> base.Can_revert_all_migrations();
[ConditionalFact(Skip = "https://github.com/dotnet/efcore/issues/33056")]
public override void Can_revert_one_migrations()
=> base.Can_revert_one_migrations();
[ConditionalFact(Skip = "https://github.com/dotnet/efcore/issues/33056")]
public override Task Can_apply_all_migrations_async()
=> base.Can_apply_all_migrations_async();
[ConditionalFact]
public async Task Empty_Migration_Creates_Database()
{
await using var context = new BloggingContext(
Fixture.TestStore.AddProviderOptions(
new DbContextOptionsBuilder().EnableServiceProviderCaching(false))
.ConfigureWarnings(e => e.Log(RelationalEventId.PendingModelChangesWarning)).Options);
var creator = (NpgsqlDatabaseCreator)context.GetService<IRelationalDatabaseCreator>();
creator.RetryTimeout = TimeSpan.FromMinutes(10);
await context.Database.MigrateAsync();
Assert.True(creator.Exists());
}
private class BloggingContext(DbContextOptions options) : DbContext(options)
{
// ReSharper disable once UnusedMember.Local
public DbSet<Blog> Blogs { get; set; }
// ReSharper disable once ClassNeverInstantiated.Local
public class Blog
{
// ReSharper disable UnusedMember.Local
public int Id { get; set; }
public string Name { get; set; }
// ReSharper restore UnusedMember.Local
}
}
[DbContext(typeof(BloggingContext))]
[Migration("00000000000000_Empty")]
public class EmptyMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
}
public override void Can_diff_against_2_2_model()
{
using var context = new Migrations.BloggingContext();
DiffSnapshot(new BloggingContextModelSnapshot22(), context);
}
public class BloggingContextModelSnapshot22 : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation(
"Npgsql:ValueGenerationStrategy",
NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
modelBuilder.Entity(
"ModelSnapshot22.Blog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasAnnotation(
"Npgsql:ValueGenerationStrategy",
NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name");
b.HasKey("Id");
b.ToTable("Blogs");
});
modelBuilder.Entity(
"ModelSnapshot22.Post", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasAnnotation(
"Npgsql:ValueGenerationStrategy",
NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int?>("BlogId");
b.Property<string>("Content");
b.Property<DateTime>("EditDate");
b.Property<string>("Title");
b.HasKey("Id");
b.HasIndex("BlogId");
b.ToTable("Post");
});
modelBuilder.Entity(
"ModelSnapshot22.Post", b =>
{
b.HasOne("ModelSnapshot22.Blog", "Blog")
.WithMany("Posts")
.HasForeignKey("BlogId");
});
#pragma warning restore 612, 618
}
}
public override void Can_diff_against_3_0_ASP_NET_Identity_model()
{
// TODO: Implement
}
public override void Can_diff_against_2_2_ASP_NET_Identity_model()
{
// TODO: Implement
}
public override void Can_diff_against_2_1_ASP_NET_Identity_model()
{
// TODO: Implement
}
protected override Task ExecuteSqlAsync(string value)
=> ((NpgsqlTestStore)Fixture.TestStore).ExecuteNonQueryAsync(value);
public class MigrationsInfrastructureNpgsqlFixture : MigrationsInfrastructureFixtureBase
{
protected override ITestStoreFactory TestStoreFactory
=> NpgsqlTestStoreFactory.Instance;
public override MigrationsContext CreateContext()
{
var options = AddOptions(
TestStore.AddProviderOptions(new DbContextOptionsBuilder())
.UseNpgsql(
TestStore.ConnectionString, b => b.ApplyConfiguration()
.SetPostgresVersion(TestEnvironment.PostgresVersion)))
.UseInternalServiceProvider(ServiceProvider)
.Options;
return new MigrationsContext(options);
}
}
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime EditDate { get; set; }
public Blog Blog { get; set; }
}
public class BloggingContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql(TestEnvironment.ConnectionString);
public DbSet<Blog> Blogs { get; set; }
}