diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs index 971deab5ca..a87436510b 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs @@ -1,6 +1,7 @@ using System.Text; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Cli.Commands; +using Volo.Abp.Cli.Commands.Internal; using Volo.Abp.Cli.Http; using Volo.Abp.Cli.ServiceProxying; using Volo.Abp.Cli.ServiceProxying.Angular; @@ -68,6 +69,7 @@ public class AbpCliCoreModule : AbpModule options.Commands[CleanCommand.Name] = typeof(CleanCommand); options.Commands[CliCommand.Name] = typeof(CliCommand); options.Commands[ClearDownloadCacheCommand.Name] = typeof(ClearDownloadCacheCommand); + options.Commands[RecreateInitialMigrationCommand.Name] = typeof(RecreateInitialMigrationCommand); options.DisabledModulesToAddToSolution.Add("Volo.Abp.LeptonXTheme.Pro"); options.DisabledModulesToAddToSolution.Add("Volo.Abp.LeptonXTheme.Lite"); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Internal/RecreateInitialMigrationCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Internal/RecreateInitialMigrationCommand.cs new file mode 100644 index 0000000000..ac1c8d6e61 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Internal/RecreateInitialMigrationCommand.cs @@ -0,0 +1,84 @@ +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.Cli.Args; +using Volo.Abp.Cli.Utils; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.Commands.Internal; + +public class RecreateInitialMigrationCommand : IConsoleCommand, ITransientDependency +{ + public const string Name = "recreate-initial-migration"; + + public ILogger Logger { get; set; } + + protected CmdHelper CmdHelper { get; } + + public RecreateInitialMigrationCommand(CmdHelper cmdHelper) + { + CmdHelper = cmdHelper; + Logger = NullLogger.Instance; + } + + public virtual Task ExecuteAsync(CommandLineArgs commandLineArgs) + { + var csprojFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.csproj", SearchOption.AllDirectories) + .Where(x => x.Contains("templates") || x.Contains("test-app")) + .Where(x => File.ReadAllText(x).Contains("Microsoft.EntityFrameworkCore.Tools")).ToList(); + + foreach (var csprojFile in csprojFiles) + { + var projectDir = Path.GetDirectoryName(csprojFile)!; + + if (!Directory.Exists(Path.Combine(projectDir, "Migrations"))) + { + continue; + } + + Logger.LogInformation($"Recreating migrations for {csprojFile}"); + + if (Directory.Exists(Path.Combine(projectDir, "Migrations"))) + { + Directory.Delete(Path.Combine(projectDir, "Migrations"), true); + } + + var separateDbContext = false; + if (Directory.Exists(Path.Combine(projectDir, "TenantMigrations"))) + { + Directory.Delete(Path.Combine(projectDir, "TenantMigrations"), true); + separateDbContext = true; + } + if (!separateDbContext) + { + CmdHelper.RunCmd($"dotnet ef migrations add Initial", workingDirectory: projectDir); + } + else + { + CmdHelper.RunCmd($"dotnet ef migrations add Initial --context MyProjectNameDbContext", workingDirectory: projectDir); + CmdHelper.RunCmd($"dotnet ef migrations add Initial --context MyProjectNameTenantDbContext --output-dir TenantMigrations", workingDirectory: projectDir); + } + + if (Directory.Exists(Path.Combine(projectDir, "Logs"))) + { + Directory.Delete(Path.Combine(projectDir, "Logs"), true); + } + } + + Logger.LogInformation("Done! All migrations recreated."); + + return Task.CompletedTask; + } + + public string GetUsageInfo() + { + return GetShortDescription(); + } + + public string GetShortDescription() + { + return "This is a internal command. Please run 'abp recreate-initial-migration' command in abp or volo root directory."; + } +}