JiShe.IOT.Admin/src/JiShe.IoT.Domain/Data/IoTDbMigrationService.cs

222 lines
7.2 KiB
C#
Raw Normal View History

2025-06-04 17:09:21 +08:00
using JiShe.ServicePro.IoTDBManagement.SessionPools;
2025-05-27 14:27:50 +08:00
namespace JiShe.IoT.Data
2025-03-11 10:24:11 +08:00
{
2025-05-27 14:27:50 +08:00
public class IoTDbMigrationService : ITransientDependency
2025-03-11 10:24:11 +08:00
{
2025-05-27 14:27:50 +08:00
public ILogger<IoTDbMigrationService> Logger { get; set; }
2025-03-11 10:24:11 +08:00
private readonly IDataSeeder _dataSeeder;
2025-05-27 14:27:50 +08:00
private readonly IEnumerable<IIoTDbSchemaMigrator> _dbSchemaMigrators;
2025-03-11 10:24:11 +08:00
private readonly ITenantRepository _tenantRepository;
private readonly ICurrentTenant _currentTenant;
2025-06-04 17:09:21 +08:00
private readonly IIoTDBSessionPoolProvider _ioTDBSessionPoolProvider;
2025-03-11 10:24:11 +08:00
2025-05-27 14:27:50 +08:00
public IoTDbMigrationService(
2025-03-11 10:24:11 +08:00
IDataSeeder dataSeeder,
2025-05-27 14:27:50 +08:00
IEnumerable<IIoTDbSchemaMigrator> dbSchemaMigrators,
2025-03-11 10:24:11 +08:00
ITenantRepository tenantRepository,
2025-06-04 17:09:21 +08:00
ICurrentTenant currentTenant,
IIoTDBSessionPoolProvider ioTDBSessionPoolProvider)
2025-03-11 10:24:11 +08:00
{
_dataSeeder = dataSeeder;
_dbSchemaMigrators = dbSchemaMigrators;
_tenantRepository = tenantRepository;
_currentTenant = currentTenant;
2025-06-04 17:09:21 +08:00
_ioTDBSessionPoolProvider = ioTDBSessionPoolProvider;
2025-03-11 10:24:11 +08:00
2025-05-27 14:27:50 +08:00
Logger = NullLogger<IoTDbMigrationService>.Instance;
2025-03-11 10:24:11 +08:00
}
public async Task MigrateAsync()
{
var initialMigrationAdded = AddInitialMigrationIfNotExist();
if (initialMigrationAdded)
{
return;
}
Logger.LogInformation("Started database migrations...");
await MigrateDatabaseSchemaAsync();
await SeedDataAsync();
2025-06-04 17:09:21 +08:00
await InitIoTDBTable();
2025-03-11 10:24:11 +08:00
Logger.LogInformation($"Successfully completed host database migrations.");
var tenants = await _tenantRepository.GetListAsync(includeDetails: true);
var migratedDatabaseSchemas = new HashSet<string>();
foreach (var tenant in tenants)
{
using (_currentTenant.Change(tenant.Id))
{
if (tenant.ConnectionStrings.Any())
{
var tenantConnectionStrings = tenant.ConnectionStrings
.Select(x => x.Value)
.ToList();
if (!migratedDatabaseSchemas.IsSupersetOf(tenantConnectionStrings))
{
await MigrateDatabaseSchemaAsync(tenant);
migratedDatabaseSchemas.AddIfNotContains(tenantConnectionStrings);
}
}
await SeedDataAsync(tenant);
}
Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations.");
}
Logger.LogInformation("Successfully completed all database migrations.");
Logger.LogInformation("You can safely end this process...");
}
private async Task MigrateDatabaseSchemaAsync(Tenant tenant = null)
{
Logger.LogInformation(
$"Migrating schema for {(tenant == null ? "host" : tenant.Name + " tenant")} database...");
foreach (var migrator in _dbSchemaMigrators)
{
await migrator.MigrateAsync();
}
}
2025-06-04 17:09:21 +08:00
/// <summary>
/// <20><>ʼ<EFBFBD><CABC>IoTDB<44><42>ģ<EFBFBD><C4A3>
/// </summary>
/// <returns></returns>
private async Task InitIoTDBTable()
{
//<2F><>ʼ<EFBFBD><CABC>IoTDB<44><42>ģ<EFBFBD><C4A3>
await _ioTDBSessionPoolProvider.GetSessionPool(true).InitTableSessionModelAsync();
}
2025-03-11 10:24:11 +08:00
private async Task SeedDataAsync(Tenant tenant = null)
{
Logger.LogInformation($"Executing {(tenant == null ? "host" : tenant.Name + " tenant")} database seed...");
await _dataSeeder.SeedAsync(new DataSeedContext(tenant?.Id)
.WithProperty(IdentityDataSeedContributor.AdminEmailPropertyName, IdentityDataSeedContributor.AdminEmailDefaultValue)
.WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName, IdentityDataSeedContributor.AdminPasswordDefaultValue)
);
}
private bool AddInitialMigrationIfNotExist()
{
try
{
if (!DbMigrationsProjectExists())
{
return false;
}
}
catch (Exception)
{
return false;
}
try
{
if (!MigrationsFolderExists())
{
AddInitialMigration();
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
Logger.LogWarning("Couldn't determinate if any migrations exist : " + e.Message);
return false;
}
}
private bool DbMigrationsProjectExists()
{
var dbMigrationsProjectFolder = GetDbMigrationsProjectFolderPath();
return dbMigrationsProjectFolder != null;
}
private bool MigrationsFolderExists()
{
var dbMigrationsProjectFolder = GetDbMigrationsProjectFolderPath();
return Directory.Exists(Path.Combine(dbMigrationsProjectFolder, "EntityFrameworkCore"));
}
private void AddInitialMigration()
{
Logger.LogInformation("Creating initial migration...");
string argumentPrefix;
string fileName;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
argumentPrefix = "-c";
fileName = "/bin/bash";
}
else
{
argumentPrefix = "/C";
fileName = "cmd.exe";
}
var procStartInfo = new ProcessStartInfo(fileName,
$"{argumentPrefix} \"abp create-migration-and-run-migrator \"{GetDbMigrationsProjectFolderPath()}\"\""
);
try
{
Process.Start(procStartInfo);
}
catch (Exception)
{
throw new Exception("Couldn't run ABP CLI...");
}
}
private string GetDbMigrationsProjectFolderPath()
{
var slnDirectoryPath = GetSolutionDirectoryPath();
if (slnDirectoryPath == null)
{
throw new Exception("Solution folder not found!");
}
var srcDirectoryPath = Path.Combine(slnDirectoryPath, "src");
return Directory.GetDirectories(srcDirectoryPath)
.FirstOrDefault(d => d.EndsWith(".DbMigrations"));
}
private string GetSolutionDirectoryPath()
{
var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
while (Directory.GetParent(currentDirectory.FullName) != null)
{
currentDirectory = Directory.GetParent(currentDirectory.FullName);
if (Directory.GetFiles(currentDirectory.FullName).FirstOrDefault(f => f.EndsWith(".sln")) != null)
{
return currentDirectory.FullName;
}
}
return null;
}
}
}