-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add schema support infrastructure (#46300)
- Loading branch information
Showing
11 changed files
with
570 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#nullable enable | ||
virtual Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext<TUser, TKey, TUserClaim, TUserLogin, TUserToken>.SchemaVersion.get -> System.Version! |
49 changes: 49 additions & 0 deletions
49
src/Identity/EntityFrameworkCore/test/EF.Test/CustomSchemaTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test; | ||
|
||
public class CustomSchemaTest : IClassFixture<ScratchDatabaseFixture> | ||
{ | ||
private readonly ApplicationBuilder _builder; | ||
|
||
public CustomSchemaTest(ScratchDatabaseFixture fixture) | ||
{ | ||
var services = new ServiceCollection(); | ||
services | ||
.AddLogging() | ||
.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build()) | ||
.AddDbContext<CustomVersionDbContext>(o => | ||
o.UseSqlite(fixture.Connection) | ||
.ConfigureWarnings(b => b.Log(CoreEventId.ManyServiceProvidersCreatedWarning))) | ||
.AddIdentity<IdentityUser, IdentityRole>(o => | ||
{ | ||
// Versions >= 3 are custom | ||
o.Stores.SchemaVersion = new Version(3, 0); | ||
}) | ||
.AddEntityFrameworkStores<CustomVersionDbContext>(); | ||
|
||
_builder = new ApplicationBuilder(services.BuildServiceProvider()); | ||
using var scope = _builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
var db = scope.ServiceProvider.GetRequiredService<CustomVersionDbContext>(); | ||
db.Database.EnsureCreated(); | ||
} | ||
|
||
[Fact] | ||
public void CanAddCustomColumn() | ||
{ | ||
using var scope = _builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
var db = scope.ServiceProvider.GetRequiredService<CustomVersionDbContext>(); | ||
VersionTwoSchemaTest.VerifyVersion2Schema(db); | ||
using var sqlConn = (SqliteConnection)db.Database.GetDbConnection(); | ||
sqlConn.Open(); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "CustomColumns", "Id")); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Identity/EntityFrameworkCore/test/EF.Test/EmptySchemaTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test; | ||
|
||
public class EmptySchemaTest : IClassFixture<ScratchDatabaseFixture> | ||
{ | ||
private readonly ApplicationBuilder _builder; | ||
|
||
public EmptySchemaTest(ScratchDatabaseFixture fixture) | ||
{ | ||
var services = new ServiceCollection(); | ||
services | ||
.AddLogging() | ||
.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build()) | ||
.AddDbContext<EmptyDbContext>(o => | ||
o.UseSqlite(fixture.Connection) | ||
.ConfigureWarnings(b => b.Log(CoreEventId.ManyServiceProvidersCreatedWarning))) | ||
.AddIdentity<IdentityUser, IdentityRole>(o => | ||
{ | ||
// Versions >= 10 are empty | ||
o.Stores.SchemaVersion = new Version(11, 0); | ||
}) | ||
.AddEntityFrameworkStores<EmptyDbContext>(); | ||
|
||
_builder = new ApplicationBuilder(services.BuildServiceProvider()); | ||
using var scope = _builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
var db = scope.ServiceProvider.GetRequiredService<EmptyDbContext>(); | ||
db.Database.EnsureCreated(); | ||
} | ||
|
||
[Fact] | ||
public void CanIgnoreEverything() | ||
{ | ||
using var scope = _builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
var db = scope.ServiceProvider.GetRequiredService<EmptyDbContext>(); | ||
VerifyEmptySchema(db); | ||
} | ||
|
||
private static void VerifyEmptySchema(EmptyDbContext dbContext) | ||
{ | ||
using var sqlConn = (SqliteConnection)dbContext.Database.GetDbConnection(); | ||
sqlConn.Open(); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUsers")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetRoles")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserRoles")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserClaims")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserLogins")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserTokens")); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Identity/EntityFrameworkCore/test/EF.Test/VersionOneSchemaTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test; | ||
|
||
public class VersionOneSchemaTest : IClassFixture<ScratchDatabaseFixture> | ||
{ | ||
private readonly ApplicationBuilder _builder; | ||
|
||
public VersionOneSchemaTest(ScratchDatabaseFixture fixture) | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
services | ||
.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build()) | ||
.AddDbContext<VersionOneDbContext>(o => | ||
o.UseSqlite(fixture.Connection) | ||
.ConfigureWarnings(b => b.Log(CoreEventId.ManyServiceProvidersCreatedWarning))) | ||
.AddIdentity<IdentityUser, IdentityRole>(o => | ||
{ | ||
o.Stores.MaxLengthForKeys = 128; | ||
}) | ||
.AddEntityFrameworkStores<VersionOneDbContext>(); | ||
|
||
services.AddLogging(); | ||
|
||
_builder = new ApplicationBuilder(services.BuildServiceProvider()); | ||
|
||
using var scope = _builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
var db = scope.ServiceProvider.GetRequiredService<VersionOneDbContext>(); | ||
db.Database.EnsureCreated(); | ||
} | ||
|
||
[Fact] | ||
public void EnsureDefaultSchema() | ||
{ | ||
using var scope = _builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
var db = scope.ServiceProvider.GetRequiredService<VersionOneDbContext>(); | ||
VerifyVersion1Schema(db); | ||
} | ||
|
||
private static void VerifyVersion1Schema(VersionOneDbContext dbContext) | ||
{ | ||
using var sqlConn = (SqliteConnection)dbContext.Database.GetDbConnection(); | ||
sqlConn.Open(); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUsers", "Id", "UserName", "Email", "PasswordHash", "SecurityStamp", | ||
"EmailConfirmed", "PhoneNumber", "PhoneNumberConfirmed", "TwoFactorEnabled", "LockoutEnabled", | ||
"LockoutEnd", "AccessFailedCount", "ConcurrencyStamp", "NormalizedUserName", "NormalizedEmail")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetRoles", "Id", "Name", "NormalizedName", "ConcurrencyStamp")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserRoles", "UserId", "RoleId")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserClaims", "Id", "UserId", "ClaimType", "ClaimValue")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserLogins", "UserId", "ProviderKey", "LoginProvider", "ProviderDisplayName")); | ||
Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserTokens", "UserId", "LoginProvider", "Name", "Value")); | ||
|
||
Assert.True(DbUtil.VerifyMaxLength(dbContext, "AspNetUsers", 256, "UserName", "Email", "NormalizedUserName", "NormalizedEmail")); | ||
Assert.True(DbUtil.VerifyMaxLength(dbContext, "AspNetRoles", 256, "Name", "NormalizedName")); | ||
Assert.True(DbUtil.VerifyMaxLength(dbContext, "AspNetUserLogins", 128, "LoginProvider", "ProviderKey")); | ||
Assert.True(DbUtil.VerifyMaxLength(dbContext, "AspNetUserTokens", 128, "LoginProvider", "Name")); | ||
|
||
DbUtil.VerifyIndex(sqlConn, "AspNetRoles", "RoleNameIndex", isUnique: true); | ||
DbUtil.VerifyIndex(sqlConn, "AspNetUsers", "UserNameIndex", isUnique: true); | ||
DbUtil.VerifyIndex(sqlConn, "AspNetUsers", "EmailIndex"); | ||
} | ||
} |
Oops, something went wrong.