-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
DatabaseExtensions.cs
113 lines (99 loc) · 7.63 KB
/
DatabaseExtensions.cs
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
// Copyright (c) Jan Škoruba. All Rights Reserved.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Reflection;
using Duende.IdentityServer.EntityFramework.Storage;
using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Skoruba.AuditLogging.EntityFramework.DbContexts;
using Skoruba.AuditLogging.EntityFramework.Entities;
using Skoruba.Duende.IdentityServer.Admin.EntityFramework.Configuration.Configuration;
using Skoruba.Duende.IdentityServer.Admin.EntityFramework.Interfaces;
namespace Skoruba.Duende.IdentityServer.Admin.EntityFramework.Configuration.MySql
{
public static class DatabaseExtensions
{
/// <summary>
/// Register DbContexts for IdentityServer ConfigurationStore and PersistedGrants, Identity and Logging
/// Configure the connection strings in AppSettings.json
/// </summary>
/// <typeparam name="TConfigurationDbContext"></typeparam>
/// <typeparam name="TPersistedGrantDbContext"></typeparam>
/// <typeparam name="TLogDbContext"></typeparam>
/// <typeparam name="TIdentityDbContext"></typeparam>
/// <typeparam name="TAuditLoggingDbContext"></typeparam>
/// <typeparam name="TDataProtectionDbContext"></typeparam>
/// <typeparam name="TAuditLog"></typeparam>
/// <param name="services"></param>
/// <param name="connectionStrings"></param>
/// <param name="databaseMigrations"></param>
public static void RegisterMySqlDbContexts<TIdentityDbContext, TConfigurationDbContext,
TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(this IServiceCollection services,
ConnectionStringsConfiguration connectionStrings,
DatabaseMigrationsConfiguration databaseMigrations)
where TIdentityDbContext : DbContext
where TPersistedGrantDbContext : DbContext, IAdminPersistedGrantDbContext
where TConfigurationDbContext : DbContext, IAdminConfigurationDbContext
where TLogDbContext : DbContext, IAdminLogDbContext
where TAuditLoggingDbContext : DbContext, IAuditLoggingDbContext<TAuditLog>
where TDataProtectionDbContext : DbContext, IDataProtectionKeyContext
where TAuditLog : AuditLog
{
var migrationsAssembly = typeof(DatabaseExtensions).GetTypeInfo().Assembly.GetName().Name;
// Config DB for identity
services.AddDbContext<TIdentityDbContext>(options =>
options.UseMySql(connectionStrings.IdentityDbConnection, ServerVersion.AutoDetect(connectionStrings.IdentityDbConnection), sql => sql.MigrationsAssembly(databaseMigrations.IdentityDbMigrationsAssembly ?? migrationsAssembly)));
// Config DB from existing connection
services.AddConfigurationDbContext<TConfigurationDbContext>(options =>
options.ConfigureDbContext = b =>
b.UseMySql(connectionStrings.ConfigurationDbConnection, ServerVersion.AutoDetect(connectionStrings.ConfigurationDbConnection), sql => sql.MigrationsAssembly(databaseMigrations.ConfigurationDbMigrationsAssembly ?? migrationsAssembly)));
// Operational DB from existing connection
services.AddOperationalDbContext<TPersistedGrantDbContext>(options => options.ConfigureDbContext = b =>
b.UseMySql(connectionStrings.PersistedGrantDbConnection, ServerVersion.AutoDetect(connectionStrings.PersistedGrantDbConnection), sql => sql.MigrationsAssembly(databaseMigrations.PersistedGrantDbMigrationsAssembly ?? migrationsAssembly)));
// Log DB from existing connection
services.AddDbContext<TLogDbContext>(options => options.UseMySql(connectionStrings.AdminLogDbConnection, ServerVersion.AutoDetect(connectionStrings.AdminLogDbConnection),
optionsSql => optionsSql.MigrationsAssembly(databaseMigrations.AdminLogDbMigrationsAssembly ?? migrationsAssembly)));
// Audit logging connection
services.AddDbContext<TAuditLoggingDbContext>(options => options.UseMySql(connectionStrings.AdminAuditLogDbConnection, ServerVersion.AutoDetect(connectionStrings.AdminAuditLogDbConnection),
optionsSql => optionsSql.MigrationsAssembly(databaseMigrations.AdminAuditLogDbMigrationsAssembly ?? migrationsAssembly)));
// DataProtectionKey DB from existing connection
if(!string.IsNullOrEmpty(connectionStrings.DataProtectionDbConnection))
services.AddDbContext<TDataProtectionDbContext>(options => options.UseMySql(connectionStrings.DataProtectionDbConnection, ServerVersion.AutoDetect(connectionStrings.DataProtectionDbConnection),
optionsSql => optionsSql.MigrationsAssembly(databaseMigrations.DataProtectionDbMigrationsAssembly ?? migrationsAssembly)));
}
/// <summary>
/// Register DbContexts for IdentityServer ConfigurationStore and PersistedGrants and Identity
/// Configure the connection strings in AppSettings.json
/// </summary>
/// <typeparam name="TConfigurationDbContext"></typeparam>
/// <typeparam name="TPersistedGrantDbContext"></typeparam>
/// <typeparam name="TIdentityDbContext"></typeparam>
/// <typeparam name="TDataProtectionDbContext"></typeparam>
/// <param name="services"></param>
/// <param name="identityConnectionString"></param>
/// <param name="configurationConnectionString"></param>
/// <param name="persistedGrantConnectionString"></param>
/// <param name="dataProtectionConnectionString"></param>
public static void RegisterMySqlDbContexts<TIdentityDbContext, TConfigurationDbContext,
TPersistedGrantDbContext, TDataProtectionDbContext>(this IServiceCollection services,
string identityConnectionString, string configurationConnectionString,
string persistedGrantConnectionString, string dataProtectionConnectionString)
where TIdentityDbContext : DbContext
where TPersistedGrantDbContext : DbContext, IAdminPersistedGrantDbContext
where TConfigurationDbContext : DbContext, IAdminConfigurationDbContext
where TDataProtectionDbContext : DbContext, IDataProtectionKeyContext
{
var migrationsAssembly = typeof(DatabaseExtensions).GetTypeInfo().Assembly.GetName().Name;
// Config DB for identity
services.AddDbContext<TIdentityDbContext>(options => options.UseMySql(identityConnectionString, ServerVersion.AutoDetect(identityConnectionString), sql => sql.MigrationsAssembly(migrationsAssembly)));
// Config DB from existing connection
services.AddConfigurationDbContext<TConfigurationDbContext>(options => options.ConfigureDbContext = b => b.UseMySql(configurationConnectionString, ServerVersion.AutoDetect(configurationConnectionString), sql => sql.MigrationsAssembly(migrationsAssembly)));
// Operational DB from existing connection
services.AddOperationalDbContext<TPersistedGrantDbContext>(options => options.ConfigureDbContext = b => b.UseMySql(persistedGrantConnectionString, ServerVersion.AutoDetect(persistedGrantConnectionString), sql => sql.MigrationsAssembly(migrationsAssembly)));
// DataProtectionKey DB from existing connection
services.AddDbContext<TDataProtectionDbContext>(options => options.UseMySql(dataProtectionConnectionString, ServerVersion.AutoDetect(dataProtectionConnectionString),
optionsSql => optionsSql.MigrationsAssembly(migrationsAssembly)));
}
}
}