-
Notifications
You must be signed in to change notification settings - Fork 154
/
ShardingOnlyTenantChangeService.cs
197 lines (171 loc) · 8.09 KB
/
ShardingOnlyTenantChangeService.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
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
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using System.Data;
using AuthPermissions.AdminCode;
using AuthPermissions.AspNetCore.GetDataKeyCode;
using AuthPermissions.AspNetCore.ShardingServices;
using AuthPermissions.BaseCode.CommonCode;
using AuthPermissions.BaseCode.DataLayer.Classes;
using Example7.SingleLevelShardingOnly.EfCoreClasses;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
namespace Example7.SingleLevelShardingOnly.EfCoreCode;
/// <summary>
/// This is the <see cref="ITenantChangeService"/> service for a single-level tenant with Sharding turned on.
/// This is different to the non-sharding versions, as we have to create the the instance of the application's
/// DbContext because the connection string relies on the <see cref="Tenant.DatabaseInfoName"/> in the tenant -
/// see <see cref="GetShardingSingleDbContext"/> at the end of this class. This also allows the DataKey to be added
/// which removes the need for using the IgnoreQueryFilters method on any queries
/// </summary>
public class ShardingOnlyTenantChangeService : ITenantChangeService
{
private readonly Microsoft.EntityFrameworkCore.DbContextOptions<ShardingOnlyDbContext> _options;
private readonly IGetSetShardingEntries _shardingService;
private readonly ILogger _logger;
/// <summary>
/// This allows the tenantId of the deleted tenant to be returned.
/// This is useful if you want to soft delete the data
/// </summary>
public int DeletedTenantId { get; private set; }
public ShardingOnlyTenantChangeService(Microsoft.EntityFrameworkCore.DbContextOptions<ShardingOnlyDbContext> options,
IGetSetShardingEntries shardingService, ILogger<ShardingOnlyTenantChangeService> logger)
{
_options = options;
_shardingService = shardingService;
_logger = logger;
}
/// <summary>
/// This creates a <see cref="CompanyTenant"/> in the given database
/// </summary>
/// <param name="tenant">The tenant data used to create a new tenant</param>
/// <returns>Returns null if all OK, otherwise the create is rolled back and the return string is shown to the user</returns>
public async Task<string> CreateNewTenantAsync(Tenant tenant)
{
using var context = GetShardingSingleDbContext(tenant.DatabaseInfoName, tenant.GetTenantDataKey());
if (context == null)
return $"There is no connection string with the name {tenant.DatabaseInfoName}.";
var databaseError = await CheckDatabaseAndPossibleMigrate(context, tenant, true);
if (databaseError != null)
return databaseError;
var newCompanyTenant = new CompanyTenant
{
AuthPTenantId = tenant.TenantId,
CompanyName = tenant.TenantFullName
};
context.Add(newCompanyTenant);
await context.SaveChangesAsync();
return null;
}
public async Task<string> SingleTenantUpdateNameAsync(Tenant tenant)
{
using var context = GetShardingSingleDbContext(tenant.DatabaseInfoName, tenant.GetTenantDataKey());
if (context == null)
return $"There is no connection string with the name {tenant.DatabaseInfoName}.";
var companyTenant = await context.Companies
.SingleOrDefaultAsync(x => x.AuthPTenantId == tenant.TenantId);
if (companyTenant != null)
{
companyTenant.CompanyName = tenant.TenantFullName;
await context.SaveChangesAsync();
}
return null;
}
public async Task<string> SingleTenantDeleteAsync(Tenant tenant)
{
using var context = GetShardingSingleDbContext(tenant.DatabaseInfoName, tenant.GetTenantDataKey());
if (context == null)
return $"There is no connection string with the name {tenant.DatabaseInfoName}.";
//If the database doesn't exist then log it and return
if (!await context.Database.CanConnectAsync())
{
_logger.LogWarning("DeleteTenantData: asked to remove tenant data / database, but no database found. " +
$"Tenant name = {tenant?.TenantFullName ?? "- not available -"}");
return null;
}
//Now we delete it
DeletedTenantId = tenant.TenantId;
//The tenant its own database, then you should drop the database, but that depends on what SQL Server provider you use.
//In this case I can the database because it is on a local SqlServer server.
try
{
await context.Database.EnsureDeletedAsync();
}
catch (Exception e)
{
_logger.LogError(e, $"Failure when trying to delete the '{tenant.TenantFullName}' tenant.");
return "There was a system-level problem - see logs for more detail";
}
return null;
}
public Task<string> HierarchicalTenantUpdateNameAsync(List<Tenant> tenantsToUpdate)
{
throw new NotImplementedException();
}
public Task<string> HierarchicalTenantDeleteAsync(List<Tenant> tenantsInOrder)
{
throw new NotImplementedException();
}
public Task<string> MoveHierarchicalTenantDataAsync(List<(string oldDataKey, Tenant tenantToMove)> tenantToUpdate)
{
throw new NotImplementedException();
}
public Task<string> MoveToDifferentDatabaseAsync(string oldDatabaseInfoName, string oldDataKey, Tenant updatedTenant)
{
throw new NotImplementedException();
}
//--------------------------------------------------
//private methods / classes
/// <summary>
/// This check is a database is there
/// </summary>
/// <param name="context">The context for the new database</param>
/// <param name="tenant"></param>
/// <param name="migrateEvenIfNoDb">If using local SQL server, Migrate will create the database.
/// That doesn't work on Azure databases</param>
/// <returns></returns>
private static async Task<string?> CheckDatabaseAndPossibleMigrate(ShardingOnlyDbContext context, Tenant tenant,
bool migrateEvenIfNoDb)
{
//Thanks to https://stackoverflow.com/questions/33911316/entity-framework-core-how-to-check-if-database-exists
//There are various options to detect if a database is there - this seems the clearest
if (!await context.Database.CanConnectAsync())
{
//The database doesn't exist
if (migrateEvenIfNoDb)
await context.Database.MigrateAsync();
else
{
return $"The database defined by the connection string '{tenant.DatabaseInfoName}' doesn't exist.";
}
}
else if (!await context.Database.GetService<IRelationalDatabaseCreator>().HasTablesAsync())
//The database exists but needs migrating
await context.Database.MigrateAsync();
return null;
}
/// <summary>
/// This create a <see cref="ShardingOnlyDbContext"/> with the correct connection string and DataKey
/// </summary>
/// <param name="databaseDataName"></param>
/// <param name="dataKey"></param>
/// <returns><see cref="ShardingOnlyDbContext"/> or null if connectionName wasn't found in the appsetting file</returns>
private ShardingOnlyDbContext? GetShardingSingleDbContext(string databaseDataName, string dataKey)
{
var connectionString = _shardingService.FormConnectionString(databaseDataName);
if (connectionString == null)
return null;
return new ShardingOnlyDbContext(_options, new StubGetShardingDataFromUser(connectionString, dataKey));
}
private class StubGetShardingDataFromUser : IGetShardingDataFromUser
{
public StubGetShardingDataFromUser(string connectionString, string dataKey)
{
ConnectionString = connectionString;
DataKey = dataKey;
}
public string DataKey { get; }
public string ConnectionString { get; }
}
}