From af3881ba80238272f732008cf125735b1bbe016a Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 1 Mar 2021 09:46:49 +0300 Subject: [PATCH] Update CreateMigrationAndRunMigrator to support --separate-tenant-schema --- .../CreateMigrationAndRunMigratorCommand.cs | 76 ++++++++++++++++--- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs index 68fae779ce..5152e7af2e 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs @@ -26,7 +26,9 @@ namespace Volo.Abp.Cli.Commands throw new CliUsageException("DbMigrations folder path is missing!"); } - var dbMigratorProjectPath = GetDbMigratorProjectPath(commandLineArgs.Target); + var dbMigrationsFolder = commandLineArgs.Target; + + var dbMigratorProjectPath = GetDbMigratorProjectPath(dbMigrationsFolder); if (dbMigratorProjectPath == null) { throw new Exception("DbMigrator is not found!"); @@ -37,13 +39,17 @@ namespace Volo.Abp.Cli.Commands InstallDotnetEfTool(); } - var addMigrationCmd = $"cd \"{commandLineArgs.Target}\" && " + - $"dotnet ef migrations add Initial -s \"{dbMigratorProjectPath}\""; + var tenantDbContextName = FindTenantDbContextName(dbMigrationsFolder); + var dbContextName = tenantDbContextName != null ? + FindDbContextName(dbMigrationsFolder) + : null; + + var migrationOutput = AddMigrationAndGetOutput(dbMigrationsFolder, dbMigratorProjectPath, dbContextName); + var tenantMigrationOutput = tenantDbContextName != null ? + AddMigrationAndGetOutput(dbMigrationsFolder, dbMigratorProjectPath, tenantDbContextName) + : null; - var output = CmdHelper.RunCmdAndGetOutput(addMigrationCmd); - if (output.Contains("Done.") && - output.Contains("To undo this action") && - output.Contains("ef migrations remove")) + if (CheckMigrationOutput(migrationOutput) && CheckMigrationOutput(tenantMigrationOutput)) { // Migration added successfully CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(dbMigratorProjectPath) + "\" && dotnet run"); @@ -51,22 +57,70 @@ namespace Volo.Abp.Cli.Commands } else { - var exceptionMsg = "Migrations failed! The following command didn't run successfully:" + + var exceptionMsg = "Migrations failed! A migration command didn't run successfully:" + Environment.NewLine + - addMigrationCmd + - Environment.NewLine + output; + Environment.NewLine + migrationOutput + + Environment.NewLine + + Environment.NewLine + tenantMigrationOutput; Logger.LogError(exceptionMsg); throw new Exception(exceptionMsg); } } + private string FindTenantDbContextName(string dbMigrationsFolder) + { + var tenantDbContext = Directory + .GetFiles(dbMigrationsFolder, "*TenantMigrationsDbContext.cs", SearchOption.AllDirectories) + .FirstOrDefault(); + + if (tenantDbContext == null) + { + return null; + } + + return Path.GetFileName(tenantDbContext).RemovePostFix(".cs"); + } + + private string FindDbContextName(string dbMigrationsFolder) + { + var dbContext = Directory + .GetFiles(dbMigrationsFolder, "*MigrationsDbContext.cs", SearchOption.AllDirectories) + .FirstOrDefault(fp => !fp.EndsWith("TenantMigrationsDbContext.cs")); + + if (dbContext == null) + { + return null; + } + + return Path.GetFileName(dbContext).RemovePostFix(".cs"); + } + + private static string AddMigrationAndGetOutput(string dbMigrationsFolder, string dbMigratorProjectPath, string dbContext) + { + var dbContextOption = string.IsNullOrWhiteSpace(dbContext) + ? string.Empty + : $"--context {dbContext}"; + + var addMigrationCmd = $"cd \"{dbMigrationsFolder}\" && " + + $"dotnet ef migrations add Initial -s \"{dbMigratorProjectPath}\" {dbContextOption}"; + + return CmdHelper.RunCmdAndGetOutput(addMigrationCmd); + } + private static bool IsDotNetEfToolInstalled() { var output = CmdHelper.RunCmdAndGetOutput("dotnet tool list -g"); return output.Contains("dotnet-ef"); } + private static bool CheckMigrationOutput(string output) + { + return output == null || (output.Contains("Done.") && + output.Contains("To undo this action") && + output.Contains("ef migrations remove")); + } + private void InstallDotnetEfTool() { Logger.LogInformation("Installing dotnet-ef tool..."); @@ -95,4 +149,4 @@ namespace Volo.Abp.Cli.Commands return string.Empty; } } -} \ No newline at end of file +}