|
|
|
@ -1,5 +1,4 @@ |
|
|
|
using LINGYUN.Abp.Saas.Features; |
|
|
|
using LINGYUN.Abp.Saas.Tenants; |
|
|
|
using LINGYUN.Abp.Saas.Tenants; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
@ -53,37 +52,31 @@ public class PlatformDbMigrationEventHandler : |
|
|
|
var hostDefaultConnectionString = Configuration.GetConnectionString(ConnectionStrings.DefaultConnectionStringName); |
|
|
|
using (CurrentTenant.Change(eventData.Id)) |
|
|
|
{ |
|
|
|
// 租户删除时的资源回收策略
|
|
|
|
var strategyFeature = await FeatureChecker.GetOrNullAsync(SaasFeatureNames.Tenant.RecycleStrategy); |
|
|
|
if (!strategyFeature.IsNullOrWhiteSpace() && Enum.TryParse<RecycleStrategy>(strategyFeature, out var strategy)) |
|
|
|
// 需要回收策略为回收且存在默认连接字符串且默认连接字符串与宿主不同
|
|
|
|
if (eventData.Strategy == RecycleStrategy.Recycle && !eventData.DefaultConnectionString.IsNullOrWhiteSpace()) |
|
|
|
{ |
|
|
|
// 需要回收策略为回收且存在默认连接字符串且默认连接字符串与宿主不同
|
|
|
|
if (strategy == RecycleStrategy.Recycle && !eventData.DefaultConnectionString.IsNullOrWhiteSpace()) |
|
|
|
var hostConnection = new DbConnectionStringBuilder() |
|
|
|
{ |
|
|
|
var hostConnection = new DbConnectionStringBuilder() |
|
|
|
{ |
|
|
|
ConnectionString = hostDefaultConnectionString, |
|
|
|
}; |
|
|
|
var tenantConnection = new DbConnectionStringBuilder() |
|
|
|
{ |
|
|
|
ConnectionString = eventData.DefaultConnectionString, |
|
|
|
}; |
|
|
|
if (hostConnection.EquivalentTo(tenantConnection)) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
ConnectionString = hostDefaultConnectionString, |
|
|
|
}; |
|
|
|
var tenantConnection = new DbConnectionStringBuilder() |
|
|
|
{ |
|
|
|
ConnectionString = eventData.DefaultConnectionString, |
|
|
|
}; |
|
|
|
if (hostConnection.EquivalentTo(tenantConnection)) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
using var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true); |
|
|
|
var buildr = new DbContextOptionsBuilder(); |
|
|
|
buildr.UseMySql(eventData.DefaultConnectionString, ServerVersion.AutoDetect(eventData.DefaultConnectionString)); |
|
|
|
await using var dbConnection = new DbContext(buildr.Options); |
|
|
|
if ((await dbConnection.Database.GetAppliedMigrationsAsync()).Any()) |
|
|
|
{ |
|
|
|
await dbConnection.Database.EnsureDeletedAsync(); |
|
|
|
} |
|
|
|
using var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true); |
|
|
|
var buildr = new DbContextOptionsBuilder(); |
|
|
|
buildr.UseMySql(eventData.DefaultConnectionString, ServerVersion.AutoDetect(eventData.DefaultConnectionString)); |
|
|
|
await using var dbConnection = new DbContext(buildr.Options); |
|
|
|
if ((await dbConnection.Database.GetAppliedMigrationsAsync()).Any()) |
|
|
|
{ |
|
|
|
await dbConnection.Database.EnsureDeletedAsync(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|