From 6ea46257bd7f24c166c81f24704e82900d9b6526 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 24 Dec 2020 10:21:45 +0300 Subject: [PATCH] CLI: just delete migrations instead of recreating when -dbms is used --- docs/en/CLI.md | 2 +- .../Volo/Abp/Cli/Commands/NewCommand.cs | 25 +++---- .../EfCoreMigrationAdder.cs | 40 ---------- .../EfCoreMigrationManager.cs | 73 +++++++++++++++++++ .../EfCoreMigrationRecreater.cs | 46 ------------ .../SolutionModuleAdder.cs | 8 +- 6 files changed, 87 insertions(+), 107 deletions(-) delete mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationManager.cs delete mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationRecreater.cs diff --git a/docs/en/CLI.md b/docs/en/CLI.md index 373b07e3d70..daf86025c49 100644 --- a/docs/en/CLI.md +++ b/docs/en/CLI.md @@ -106,7 +106,7 @@ abp new Acme.BookStore * `--template-source` or `-ts`: Specifies a custom template source to use to build the project. Local and network sources can be used(Like `D:\local-template` or `https://.../my-template-file.zip`). * `--create-solution-folder` or `-csf`: Specifies if the project will be in a new folder in the output folder or directly the output folder. * `--connection-string` or `-cs`: Overwrites the default connection strings in all `appsettings.json` files. The default connection string is `Server=localhost;Database=MyProjectName;Trusted_Connection=True;MultipleActiveResultSets=true` for EF Core and it is configured to use the SQL Server. If you want to use the EF Core, but need to change the DBMS, you can change it as [described here](Entity-Framework-Core-Other-DBMS.md) (after creating the solution). -* `--database-management-system` or `-dbms`: Sets the database management system. Default is **SQL Server**. `--connection-string` parameter should be set along with this parameter if you want to set any DBMS other than **SQL Server**. Supported DBMS's: +* `--database-management-system` or `-dbms`: Sets the database management system. Default is **SQL Server**. Supported DBMS's: * `SqlServer` * `MySQL` * `SQLite` diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewCommand.cs index 94a690deb9f..f81290e1d32 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewCommand.cs @@ -26,7 +26,7 @@ namespace Volo.Abp.Cli.Commands { public class NewCommand : IConsoleCommand, ITransientDependency { - private readonly EfCoreMigrationRecreater _efCoreMigrationRecreater; + private readonly EfCoreMigrationManager _efCoreMigrationManager; public ILogger Logger { get; set; } protected TemplateProjectBuilder TemplateProjectBuilder { get; } @@ -34,9 +34,9 @@ public class NewCommand : IConsoleCommand, ITransientDependency public NewCommand(TemplateProjectBuilder templateProjectBuilder , ITemplateInfoProvider templateInfoProvider, - EfCoreMigrationRecreater efCoreMigrationRecreater) + EfCoreMigrationManager efCoreMigrationManager) { - _efCoreMigrationRecreater = efCoreMigrationRecreater; + _efCoreMigrationManager = efCoreMigrationManager; TemplateProjectBuilder = templateProjectBuilder; TemplateInfoProvider = templateInfoProvider; @@ -101,13 +101,6 @@ public async Task ExecuteAsync(CommandLineArgs commandLineArgs) Logger.LogInformation("DBMS: " + databaseManagementSystem); } - if (databaseManagementSystem != DatabaseManagementSystem.NotSpecified - && databaseManagementSystem != DatabaseManagementSystem.SQLServer - && connectionString == null) - { - throw new CliUsageException($"Connection string must be set if a Database Management System other than SQLServer is set. Use \"--{Options.ConnectionString.Long}\" parameter to set connection string"); - } - var uiFramework = GetUiFramework(commandLineArgs); if (uiFramework != UiFramework.NotSpecified) { @@ -215,7 +208,7 @@ public async Task ExecuteAsync(CommandLineArgs commandLineArgs) } } - ReCreateMigrationsIfNeeded(databaseProvider, databaseManagementSystem, outputFolder); + DeleteMigrationsIfNeeded(databaseProvider, databaseManagementSystem, outputFolder); Logger.LogInformation($"'{projectName}' has been successfully created to '{outputFolder}'"); @@ -226,7 +219,7 @@ public async Task ExecuteAsync(CommandLineArgs commandLineArgs) } } - private void ReCreateMigrationsIfNeeded(DatabaseProvider databaseProvider, DatabaseManagementSystem databaseManagementSystem, string outputFolder) + private void DeleteMigrationsIfNeeded(DatabaseProvider databaseProvider, DatabaseManagementSystem databaseManagementSystem, string outputFolder) { if (databaseManagementSystem == DatabaseManagementSystem.NotSpecified || databaseManagementSystem == DatabaseManagementSystem.SQLServer) { @@ -238,9 +231,9 @@ private void ReCreateMigrationsIfNeeded(DatabaseProvider databaseProvider, Datab return; } - Logger.LogInformation($"Re-creating migrations... ({databaseManagementSystem})"); + Logger.LogInformation($"Deleting migrations..."); - _efCoreMigrationRecreater.Recreate(outputFolder); + _efCoreMigrationManager.RemoveAllMigrations(outputFolder); } private void OpenThanksPage(UiFramework uiFramework, DatabaseProvider databaseProvider, bool tiered, bool commercial) @@ -305,7 +298,7 @@ public string GetUsageInfo() sb.AppendLine("-ts|--template-source (your local or network abp template source)"); sb.AppendLine("-csf|--create-solution-folder (default: true)"); sb.AppendLine("-cs|--connection-string (your database connection string)"); - sb.AppendLine("--dbms (your database management system. Requires --connection-string to be set)"); + sb.AppendLine("--dbms (your database management system)"); sb.AppendLine("--tiered (if supported by the template)"); sb.AppendLine("--no-ui (if supported by the template)"); sb.AppendLine("--no-random-port (Use template's default ports)"); @@ -328,7 +321,7 @@ public string GetUsageInfo() sb.AppendLine(" abp new Acme.BookStore -ts \"D:\\localTemplate\\abp\""); sb.AppendLine(" abp new Acme.BookStore -csf false"); sb.AppendLine(" abp new Acme.BookStore --local-framework-ref --abp-path \"D:\\github\\abp\""); - sb.AppendLine(" abp new Acme.BookStore --dbms mysql --connection-string \"Server=myServerName\\myInstanceName;Database=myDatabase;User Id=myUsername;Password=myPassword\""); + sb.AppendLine(" abp new Acme.BookStore --dbms mysql"); sb.AppendLine(" abp new Acme.BookStore --connection-string \"Server=myServerName\\myInstanceName;Database=myDatabase;User Id=myUsername;Password=myPassword\""); sb.AppendLine(""); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs deleted file mode 100644 index 280d4b8f349..00000000000 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.IO; -using Volo.Abp.Cli.Utils; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Cli.ProjectModification -{ - public class EfCoreMigrationAdder : ITransientDependency - { - public void AddMigration(string dbMigrationsCsprojFile, string module, string startupProject) - { - var moduleName = ParseModuleName(module); - var migrationName = "Added_" + moduleName + "_Module" + GetUniquePostFix(); - - CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(dbMigrationsCsprojFile) + "\" && dotnet ef migrations add " + migrationName + GetStartupProjectOption(startupProject)); - } - - protected virtual string ParseModuleName(string fullModuleName) - { - var words = fullModuleName?.Split('.'); - - if (words == null || words.Length <= 1) - { - return ""; - } - - return words[words.Length - 1]; - } - - protected virtual string GetUniquePostFix() - { - return "_" + new Random().Next(1,99999); - } - - protected virtual string GetStartupProjectOption(string startupProject) - { - return startupProject.IsNullOrWhiteSpace() ? "" : $" -s {startupProject}"; - } - } -} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationManager.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationManager.cs new file mode 100644 index 00000000000..b26ca1be908 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationManager.cs @@ -0,0 +1,73 @@ +using System; +using System.IO; +using System.Linq; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.Cli.Utils; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ProjectModification +{ + public class EfCoreMigrationManager : ITransientDependency + { + public ILogger Logger { get; set; } + + public EfCoreMigrationManager() + { + Logger = NullLogger.Instance; + } + + public void AddMigration(string dbMigrationsCsprojFile, string module, string startupProject) + { + var moduleName = ParseModuleName(module); + var migrationName = "Added_" + moduleName + "_Module" + GetUniquePostFix(); + + CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(dbMigrationsCsprojFile) + + "\" && dotnet ef migrations add " + migrationName + + GetStartupProjectOption(startupProject)); + } + + public void RemoveAllMigrations(string solutionFolder) + { + if (Directory.Exists(Path.Combine(solutionFolder, "aspnet-core"))) + { + solutionFolder = Path.Combine(solutionFolder, "aspnet-core"); + } + + var srcFolder = Path.Combine(solutionFolder, "src"); + + var migrationsFolder = Directory.GetDirectories(srcFolder) + .FirstOrDefault(d => d.EndsWith(".EntityFrameworkCore.DbMigrations")); + + if (migrationsFolder != null) + { + Directory.Delete(Path.Combine(migrationsFolder, "Migrations"), true); + } + else + { + Logger.LogWarning("No migration found to delete."); + } + } + + protected virtual string ParseModuleName(string fullModuleName) + { + var words = fullModuleName?.Split('.'); + if (words == null || words.Length <= 1) + { + return ""; + } + + return words[words.Length - 1]; + } + + protected virtual string GetUniquePostFix() + { + return "_" + new Random().Next(1, 99999); + } + + protected virtual string GetStartupProjectOption(string startupProject) + { + return startupProject.IsNullOrWhiteSpace() ? "" : $" -s {startupProject}"; + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationRecreater.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationRecreater.cs deleted file mode 100644 index 3abdaf93920..00000000000 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationRecreater.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using Volo.Abp.Cli.Utils; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Cli.ProjectModification -{ - public class EfCoreMigrationRecreater : ITransientDependency - { - public ILogger Logger { get; set; } - - public EfCoreMigrationRecreater() - { - Logger = NullLogger.Instance; - } - - public void Recreate(string solutionFolder) - { - if (Directory.Exists(Path.Combine(solutionFolder, "aspnet-core"))) - { - solutionFolder = Path.Combine(solutionFolder, "aspnet-core"); - } - - var srcFolder = Path.Combine(solutionFolder, "src"); - - try - { - var migrationsFolder = Directory.GetDirectories(srcFolder).First(d => d.EndsWith(".EntityFrameworkCore.DbMigrations")); - Directory.Delete(Path.Combine(migrationsFolder, "Migrations"), true); - - var migratorFolder = Directory.GetDirectories(srcFolder).First(d => d.EndsWith(".DbMigrator")); - var migratorProjectFile = Directory.GetFiles(migratorFolder).First(d => d.EndsWith(".DbMigrator.csproj")); - var addMigrationCommand = $"dotnet ef migrations add Initial --startup-project {migratorProjectFile}"; - CmdHelper.RunCmd($"cd {migrationsFolder} && {addMigrationCommand}"); - } - catch (Exception e) - { - Logger.LogWarning("Re-creating migrations process failed."); - throw e; - } - } - } -} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs index 4eae8ace8fe..07896b49a16 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs @@ -26,7 +26,7 @@ public class SolutionModuleAdder : ITransientDependency protected IJsonSerializer JsonSerializer { get; } protected ProjectNugetPackageAdder ProjectNugetPackageAdder { get; } protected DbContextFileBuilderConfigureAdder DbContextFileBuilderConfigureAdder { get; } - protected EfCoreMigrationAdder EfCoreMigrationAdder { get; } + protected EfCoreMigrationManager EfCoreMigrationManager { get; } protected DerivedClassFinder DerivedClassFinder { get; } protected ProjectNpmPackageAdder ProjectNpmPackageAdder { get; } protected NpmGlobalPackagesChecker NpmGlobalPackagesChecker { get; } @@ -42,7 +42,7 @@ public SolutionModuleAdder( IJsonSerializer jsonSerializer, ProjectNugetPackageAdder projectNugetPackageAdder, DbContextFileBuilderConfigureAdder dbContextFileBuilderConfigureAdder, - EfCoreMigrationAdder efCoreMigrationAdder, + EfCoreMigrationManager efCoreMigrationManager, DerivedClassFinder derivedClassFinder, ProjectNpmPackageAdder projectNpmPackageAdder, NpmGlobalPackagesChecker npmGlobalPackagesChecker, @@ -57,7 +57,7 @@ public SolutionModuleAdder( JsonSerializer = jsonSerializer; ProjectNugetPackageAdder = projectNugetPackageAdder; DbContextFileBuilderConfigureAdder = dbContextFileBuilderConfigureAdder; - EfCoreMigrationAdder = efCoreMigrationAdder; + EfCoreMigrationManager = efCoreMigrationManager; DerivedClassFinder = derivedClassFinder; ProjectNpmPackageAdder = projectNpmPackageAdder; NpmGlobalPackagesChecker = npmGlobalPackagesChecker; @@ -454,7 +454,7 @@ protected void ModifyDbContext(string[] projectFiles, ModuleInfo module, string { if (addedNewBuilder) { - EfCoreMigrationAdder.AddMigration(dbMigrationsProject, module.Name, startupProject); + EfCoreMigrationManager.AddMigration(dbMigrationsProject, module.Name, startupProject); } RunMigrator(projectFiles);