From cc3515cd7f4119be0d47459bb210fa27b8309b83 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 21 Dec 2020 11:25:39 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=C4=B0nitial=20DatabaseManagementSystemChan?= =?UTF-8?q?geStep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Volo/Abp/Cli/Commands/NewCommand.cs | 40 +++++++++++++++++++ .../Services/SourceCodeDownloadService.cs | 1 + .../Building/DatabaseManagementSystem.cs | 12 ++++++ .../DatabaseManagementSystemChangeStep.cs | 40 +++++++++++++++++++ .../TemplateProjectBuildPipelineBuilder.cs | 2 + .../Cli/ProjectBuilding/ProjectBuildArgs.cs | 6 ++- 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/DatabaseManagementSystem.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs 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 75d2651cbc..f998a6c09e 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 @@ -84,6 +84,12 @@ namespace Volo.Abp.Cli.Commands Logger.LogInformation("Database provider: " + databaseProvider); } + var databaseManagementSystem = GetDatabaseManagementSystem(commandLineArgs); + if (databaseManagementSystem != DatabaseManagementSystem.NotSpecified) + { + Logger.LogInformation("DBSM: " + databaseManagementSystem); + } + var uiFramework = GetUiFramework(commandLineArgs); if (uiFramework != UiFramework.NotSpecified) { @@ -147,6 +153,7 @@ namespace Volo.Abp.Cli.Commands template, version, databaseProvider, + databaseManagementSystem, uiFramework, mobileApp, gitHubAbpLocalRepositoryPath, @@ -267,6 +274,7 @@ namespace Volo.Abp.Cli.Commands 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)"); 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)"); @@ -289,6 +297,7 @@ namespace Volo.Abp.Cli.Commands 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"); 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"); @@ -315,6 +324,32 @@ namespace Volo.Abp.Cli.Commands } } + protected virtual DatabaseManagementSystem GetDatabaseManagementSystem(CommandLineArgs commandLineArgs) + { + var optionValue = commandLineArgs.Options.GetOrNull(Options.DatabaseManagementSystem.Long); + + if (optionValue == null) + { + return DatabaseManagementSystem.NotSpecified; + } + + switch (optionValue.ToLowerInvariant()) + { + case "sqlserver": + return DatabaseManagementSystem.SQLServer; + case "mysql": + return DatabaseManagementSystem.MySQL; + case "postgresql": + return DatabaseManagementSystem.PostgreSQL; + case "oracle": + return DatabaseManagementSystem.Oracle; + case "sqlite": + return DatabaseManagementSystem.SQLite; + default: + return DatabaseManagementSystem.NotSpecified; + } + } + protected virtual UiFramework GetUiFramework(CommandLineArgs commandLineArgs) { var optionValue = commandLineArgs.Options.GetOrNull(Options.UiFramework.Short, Options.UiFramework.Long); @@ -362,6 +397,11 @@ namespace Volo.Abp.Cli.Commands public const string Long = "database-provider"; } + public static class DatabaseManagementSystem + { + public const string Long = "dbsm"; + } + public static class OutputFolder { public const string Short = "o"; diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Services/SourceCodeDownloadService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Services/SourceCodeDownloadService.cs index 78a95996bd..28f41e55d8 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Services/SourceCodeDownloadService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Services/SourceCodeDownloadService.cs @@ -37,6 +37,7 @@ namespace Volo.Abp.Cli.Commands.Services moduleName, version, DatabaseProvider.NotSpecified, + DatabaseManagementSystem.NotSpecified, UiFramework.NotSpecified, null, gitHubAbpLocalRepositoryPath, diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/DatabaseManagementSystem.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/DatabaseManagementSystem.cs new file mode 100644 index 0000000000..edf30abce1 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/DatabaseManagementSystem.cs @@ -0,0 +1,12 @@ +namespace Volo.Abp.Cli.ProjectBuilding.Building +{ + public enum DatabaseManagementSystem + { + NotSpecified, + SQLServer, + MySQL, + PostgreSQL, + Oracle, + SQLite + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs new file mode 100644 index 0000000000..cbbaa8d64a --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Volo.Abp.Cli.ProjectBuilding.Files; +using Volo.Abp.Text; + +namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps +{ + public class DatabaseManagementSystemChangeStep : ProjectBuildPipelineStep + { + public override void Execute(ProjectBuildContext context) + { + switch (context.BuildArgs.DatabaseManagementSystem) + { + case DatabaseManagementSystem.NotSpecified: + return; + + case DatabaseManagementSystem.SQLServer: + return; + + case DatabaseManagementSystem.MySQL: + return; + + case DatabaseManagementSystem.PostgreSQL: + return; + + case DatabaseManagementSystem.Oracle: + return; + + case DatabaseManagementSystem.SQLite: + return; + } + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs index ad5da58a8e..c7cf29de20 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs @@ -18,6 +18,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building pipeline.Steps.Add(new ProjectReferenceReplaceStep()); pipeline.Steps.Add(new TemplateCodeDeleteStep()); pipeline.Steps.Add(new SolutionRenameStep()); + pipeline.Steps.Add(new DatabaseManagementSystemChangeStep()); if (context.Template.Name == AppProTemplate.TemplateName || context.Template.Name == ModuleProTemplate.TemplateName) @@ -31,6 +32,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building pipeline.Steps.Add(new RemoveRootFolderStep()); } + if (context.BuildArgs.ConnectionString != null) { pipeline.Steps.Add(new ConnectionStringChangeStep()); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ProjectBuildArgs.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ProjectBuildArgs.cs index 52fa76e551..1022f5f10e 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ProjectBuildArgs.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ProjectBuildArgs.cs @@ -17,6 +17,8 @@ namespace Volo.Abp.Cli.ProjectBuilding public DatabaseProvider DatabaseProvider { get; set; } + public DatabaseManagementSystem DatabaseManagementSystem { get; set; } + public UiFramework UiFramework { get; set; } public MobileApp? MobileApp { get; set; } @@ -41,6 +43,7 @@ namespace Volo.Abp.Cli.ProjectBuilding [CanBeNull] string templateName = null, [CanBeNull] string version = null, DatabaseProvider databaseProvider = DatabaseProvider.NotSpecified, + DatabaseManagementSystem databaseManagementSystem = DatabaseManagementSystem.NotSpecified, UiFramework uiFramework = UiFramework.NotSpecified, MobileApp? mobileApp = null, [CanBeNull] string abpGitHubLocalRepositoryPath = null, @@ -53,6 +56,7 @@ namespace Volo.Abp.Cli.ProjectBuilding TemplateName = templateName; Version = version; DatabaseProvider = databaseProvider; + DatabaseManagementSystem = databaseManagementSystem; UiFramework = uiFramework; MobileApp = mobileApp; AbpGitHubLocalRepositoryPath = abpGitHubLocalRepositoryPath; @@ -62,4 +66,4 @@ namespace Volo.Abp.Cli.ProjectBuilding ConnectionString = connectionString; } } -} \ No newline at end of file +} From 83f9d34120fe14c15ae77c3820ea0841324656b4 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 21 Dec 2020 15:57:57 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=C4=B0mplement=20DatabaseManagementSystemCh?= =?UTF-8?q?angeStep=20&=20migration=20recreater?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Volo/Abp/Cli/Commands/NewCommand.cs | 58 ++++++++++++---- .../Building/DatabaseManagementSystem.cs | 1 + .../DatabaseManagementSystemChangeStep.cs | 68 ++++++++++++++----- .../TemplateProjectBuildPipelineBuilder.cs | 1 - .../EfCoreMigrationRecreater.cs | 46 +++++++++++++ 5 files changed, 144 insertions(+), 30 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationRecreater.cs 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 f998a6c09e..c3b9302d3b 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 @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; @@ -16,6 +17,7 @@ using Volo.Abp.Cli.ProjectBuilding; using Volo.Abp.Cli.ProjectBuilding.Building; using Volo.Abp.Cli.ProjectBuilding.Templates.App; using Volo.Abp.Cli.ProjectBuilding.Templates.Console; +using Volo.Abp.Cli.ProjectModification; using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; using Volo.Abp.Threading; @@ -24,14 +26,17 @@ namespace Volo.Abp.Cli.Commands { public class NewCommand : IConsoleCommand, ITransientDependency { + private readonly EfCoreMigrationRecreater _efCoreMigrationRecreater; public ILogger Logger { get; set; } protected TemplateProjectBuilder TemplateProjectBuilder { get; } public ITemplateInfoProvider TemplateInfoProvider { get; } public NewCommand(TemplateProjectBuilder templateProjectBuilder - , ITemplateInfoProvider templateInfoProvider) + , ITemplateInfoProvider templateInfoProvider, + EfCoreMigrationRecreater efCoreMigrationRecreater) { + _efCoreMigrationRecreater = efCoreMigrationRecreater; TemplateProjectBuilder = templateProjectBuilder; TemplateInfoProvider = templateInfoProvider; @@ -84,22 +89,29 @@ namespace Volo.Abp.Cli.Commands Logger.LogInformation("Database provider: " + databaseProvider); } + var connectionString = GetConnectionString(commandLineArgs); + if (connectionString != null) + { + Logger.LogInformation("Connection string: " + connectionString); + } + var databaseManagementSystem = GetDatabaseManagementSystem(commandLineArgs); if (databaseManagementSystem != DatabaseManagementSystem.NotSpecified) { - Logger.LogInformation("DBSM: " + databaseManagementSystem); + Logger.LogInformation("DBMS: " + databaseManagementSystem); } - var uiFramework = GetUiFramework(commandLineArgs); - if (uiFramework != UiFramework.NotSpecified) + if (databaseManagementSystem != DatabaseManagementSystem.NotSpecified + && databaseManagementSystem != DatabaseManagementSystem.SQLServer + && connectionString == null) { - Logger.LogInformation("UI Framework: " + uiFramework); + 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 connectionString = GetConnectionString(commandLineArgs); - if (connectionString != null) + var uiFramework = GetUiFramework(commandLineArgs); + if (uiFramework != UiFramework.NotSpecified) { - Logger.LogInformation("Connection string: " + connectionString); + Logger.LogInformation("UI Framework: " + uiFramework); } var mobileApp = GetMobilePreference(commandLineArgs); @@ -203,6 +215,8 @@ namespace Volo.Abp.Cli.Commands } } + ReCreateMigrationsIfNeeded(databaseProvider, databaseManagementSystem, outputFolder); + Logger.LogInformation($"'{projectName}' has been successfully created to '{outputFolder}'"); if (AppTemplateBase.IsAppTemplate(template ?? (await TemplateInfoProvider.GetDefaultAsync()).Name)) @@ -212,6 +226,23 @@ namespace Volo.Abp.Cli.Commands } } + private void ReCreateMigrationsIfNeeded(DatabaseProvider databaseProvider, DatabaseManagementSystem databaseManagementSystem, string outputFolder) + { + if (databaseManagementSystem == DatabaseManagementSystem.NotSpecified || databaseManagementSystem == DatabaseManagementSystem.SQLServer) + { + return; + } + + if (databaseProvider != DatabaseProvider.NotSpecified && databaseProvider != DatabaseProvider.EntityFrameworkCore) + { + return; + } + + Logger.LogInformation($"Re-creating migrations... ({databaseManagementSystem})"); + + _efCoreMigrationRecreater.Recreate(outputFolder); + } + private void OpenThanksPage(UiFramework uiFramework, DatabaseProvider databaseProvider, bool tiered, bool commercial) { uiFramework = uiFramework == UiFramework.NotSpecified || uiFramework == UiFramework.None ? UiFramework.Mvc : uiFramework; @@ -274,7 +305,7 @@ namespace Volo.Abp.Cli.Commands 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)"); + sb.AppendLine("--dbms (your database management system. Requires --connection-string to be set)"); 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)"); @@ -297,7 +328,7 @@ namespace Volo.Abp.Cli.Commands 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"); + 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 --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"); @@ -326,7 +357,7 @@ namespace Volo.Abp.Cli.Commands protected virtual DatabaseManagementSystem GetDatabaseManagementSystem(CommandLineArgs commandLineArgs) { - var optionValue = commandLineArgs.Options.GetOrNull(Options.DatabaseManagementSystem.Long); + var optionValue = commandLineArgs.Options.GetOrNull(Options.DatabaseManagementSystem.Short, Options.DatabaseManagementSystem.Long); if (optionValue == null) { @@ -343,6 +374,8 @@ namespace Volo.Abp.Cli.Commands return DatabaseManagementSystem.PostgreSQL; case "oracle": return DatabaseManagementSystem.Oracle; + case "oracle-devart": + return DatabaseManagementSystem.OracleDevart; case "sqlite": return DatabaseManagementSystem.SQLite; default: @@ -399,7 +432,8 @@ namespace Volo.Abp.Cli.Commands public static class DatabaseManagementSystem { - public const string Long = "dbsm"; + public const string Short = "dbms"; + public const string Long = "database-management-system"; } public static class OutputFolder diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/DatabaseManagementSystem.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/DatabaseManagementSystem.cs index edf30abce1..fcb3130854 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/DatabaseManagementSystem.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/DatabaseManagementSystem.cs @@ -7,6 +7,7 @@ MySQL, PostgreSQL, Oracle, + OracleDevart, SQLite } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs index cbbaa8d64a..f0f7c12872 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs @@ -1,13 +1,6 @@ using System; -using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using Volo.Abp.Cli.ProjectBuilding.Files; -using Volo.Abp.Text; namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps { @@ -17,24 +10,65 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps { switch (context.BuildArgs.DatabaseManagementSystem) { - case DatabaseManagementSystem.NotSpecified: - return; - - case DatabaseManagementSystem.SQLServer: - return; - case DatabaseManagementSystem.MySQL: - return; + ChangeEntityFrameworkCoreDependency(context,"Volo.Abp.EntityFrameworkCore.MySQL", + "Volo.Abp.EntityFrameworkCore.MySQL", + "AbpEntityFrameworkCoreMySQLModule"); + ChangeUseSqlServer(context,"UseMySql"); + break; case DatabaseManagementSystem.PostgreSQL: - return; + ChangeEntityFrameworkCoreDependency(context,"Volo.Abp.EntityFrameworkCore.PostgreSql", + "Volo.Abp.EntityFrameworkCore.PostgreSql", + "AbpEntityFrameworkCorePostgreSqlModule"); + ChangeUseSqlServer(context,"UseNpgsql"); + break; case DatabaseManagementSystem.Oracle: - return; + ChangeEntityFrameworkCoreDependency(context,"Volo.Abp.EntityFrameworkCore.Oracle", + "Volo.Abp.EntityFrameworkCore.Oracle", + "AbpEntityFrameworkCoreOracleModule"); + ChangeUseSqlServer(context,"UseOracle"); + break; + + case DatabaseManagementSystem.OracleDevart: + ChangeEntityFrameworkCoreDependency(context,"Volo.Abp.EntityFrameworkCore.Oracle.Devart", + "Volo.Abp.EntityFrameworkCore.Oracle.Devart", + "AbpEntityFrameworkCoreOracleDevartModule"); + ChangeUseSqlServer(context,"UseOracle"); + break; case DatabaseManagementSystem.SQLite: - return; + ChangeEntityFrameworkCoreDependency(context,"Volo.Abp.EntityFrameworkCore.Sqlite", + "Volo.Abp.EntityFrameworkCore.Sqlite", + "AbpEntityFrameworkCoreSqliteModule"); + ChangeUseSqlServer(context,"UseSqlite"); + break; + + default: + return; } } + + private void ChangeEntityFrameworkCoreDependency(ProjectBuildContext context, string newPackageName, string newModuleNamespace, string newModuleClass) + { + var efCoreProjectFile = context.Files.First(f => f.Name.EndsWith("EntityFrameworkCore.csproj", StringComparison.OrdinalIgnoreCase)); + efCoreProjectFile.ReplaceText("Volo.Abp.EntityFrameworkCore.SqlServer", newPackageName); + + var efCoreModuleClass = context.Files.First(f => f.Name.EndsWith("EntityFrameworkCoreModule.cs", StringComparison.OrdinalIgnoreCase)); + efCoreModuleClass.ReplaceText("Volo.Abp.EntityFrameworkCore.SqlServer", newModuleNamespace); + efCoreModuleClass.ReplaceText("AbpEntityFrameworkCoreSqlServerModule", newModuleClass); + } + + private void ChangeUseSqlServer(ProjectBuildContext context, string newUseMethod) + { + var oldUseMethod = "UseSqlServer"; + + var efCoreModuleClass = context.Files.First(f => f.Name.EndsWith("EntityFrameworkCoreModule.cs", StringComparison.OrdinalIgnoreCase)); + efCoreModuleClass.ReplaceText(oldUseMethod, newUseMethod); + + var dbContextFactoryFile = context.Files.First(f => f.Name.EndsWith("MigrationsDbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); + dbContextFactoryFile.ReplaceText(oldUseMethod, newUseMethod); + } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs index c7cf29de20..40ba54a3c0 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs @@ -32,7 +32,6 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building pipeline.Steps.Add(new RemoveRootFolderStep()); } - if (context.BuildArgs.ConnectionString != null) { pipeline.Steps.Add(new ConnectionStringChangeStep()); 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 new file mode 100644 index 0000000000..1f9dc7d93d --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationRecreater.cs @@ -0,0 +1,46 @@ +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; + } + } + } +} From a8be0b76111f186cebb1cedebbafa9fd8f47da7f Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 21 Dec 2020 16:25:28 +0300 Subject: [PATCH 3/7] DatabaseManagementSystemChangeStep improvements & remove connection string requirement --- .../Volo/Abp/Cli/Commands/NewCommand.cs | 11 ++-------- .../DatabaseManagementSystemChangeStep.cs | 22 +++++++++++++++---- .../EfCoreMigrationRecreater.cs | 2 +- 3 files changed, 21 insertions(+), 14 deletions(-) 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 c3b9302d3b..0fd3360f15 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 @@ -101,13 +101,6 @@ namespace Volo.Abp.Cli.Commands 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) { @@ -305,7 +298,7 @@ namespace Volo.Abp.Cli.Commands 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 @@ namespace Volo.Abp.Cli.Commands 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/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs index f0f7c12872..f215d37954 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs @@ -14,7 +14,8 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps ChangeEntityFrameworkCoreDependency(context,"Volo.Abp.EntityFrameworkCore.MySQL", "Volo.Abp.EntityFrameworkCore.MySQL", "AbpEntityFrameworkCoreMySQLModule"); - ChangeUseSqlServer(context,"UseMySql"); + AddMySqlServerVersion(context); + ChangeUseSqlServer(context,"UseMySQL", "UseMySql"); break; case DatabaseManagementSystem.PostgreSQL: @@ -50,6 +51,14 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps } } + private void AddMySqlServerVersion(ProjectBuildContext context) + { + var dbContextFactoryFile = context.Files.First(f => f.Name.EndsWith("MigrationsDbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); + + dbContextFactoryFile.ReplaceText("configuration.GetConnectionString(\"Default\")", + "configuration.GetConnectionString(\"Default\"), MySqlServerVersion.LatestSupportedServerVersion"); + } + private void ChangeEntityFrameworkCoreDependency(ProjectBuildContext context, string newPackageName, string newModuleNamespace, string newModuleClass) { var efCoreProjectFile = context.Files.First(f => f.Name.EndsWith("EntityFrameworkCore.csproj", StringComparison.OrdinalIgnoreCase)); @@ -60,15 +69,20 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps efCoreModuleClass.ReplaceText("AbpEntityFrameworkCoreSqlServerModule", newModuleClass); } - private void ChangeUseSqlServer(ProjectBuildContext context, string newUseMethod) + private void ChangeUseSqlServer(ProjectBuildContext context, string newUseMethodForEfModule, string newUseMethodForDbContext = null) { + if (newUseMethodForDbContext == null) + { + newUseMethodForDbContext = newUseMethodForEfModule; + } + var oldUseMethod = "UseSqlServer"; var efCoreModuleClass = context.Files.First(f => f.Name.EndsWith("EntityFrameworkCoreModule.cs", StringComparison.OrdinalIgnoreCase)); - efCoreModuleClass.ReplaceText(oldUseMethod, newUseMethod); + efCoreModuleClass.ReplaceText(oldUseMethod, newUseMethodForEfModule); var dbContextFactoryFile = context.Files.First(f => f.Name.EndsWith("MigrationsDbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); - dbContextFactoryFile.ReplaceText(oldUseMethod, newUseMethod); + dbContextFactoryFile.ReplaceText(oldUseMethod, newUseMethodForDbContext); } } } 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 index 1f9dc7d93d..3abdaf9392 100644 --- 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 @@ -38,7 +38,7 @@ namespace Volo.Abp.Cli.ProjectModification } catch (Exception e) { - Logger.LogWarning($"\"Re-creating migrations process failed."); + Logger.LogWarning("Re-creating migrations process failed."); throw e; } } From 9e00d351c8243062d73d5e539bf709a53a0b454e Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 21 Dec 2020 16:57:43 +0300 Subject: [PATCH 4/7] DatabaseManagementSystemChangeStep improvements & revert connectionString requirement revert --- .../Volo/Abp/Cli/Commands/NewCommand.cs | 15 +++++++++++---- .../Steps/DatabaseManagementSystemChangeStep.cs | 9 +++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) 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 0fd3360f15..94a690deb9 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 @@ -101,6 +101,13 @@ namespace Volo.Abp.Cli.Commands 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) { @@ -298,7 +305,7 @@ namespace Volo.Abp.Cli.Commands 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)"); + sb.AppendLine("--dbms (your database management system. Requires --connection-string to be set)"); 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)"); @@ -321,7 +328,7 @@ namespace Volo.Abp.Cli.Commands 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"); + 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 --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"); @@ -365,12 +372,12 @@ namespace Volo.Abp.Cli.Commands return DatabaseManagementSystem.MySQL; case "postgresql": return DatabaseManagementSystem.PostgreSQL; - case "oracle": - return DatabaseManagementSystem.Oracle; case "oracle-devart": return DatabaseManagementSystem.OracleDevart; case "sqlite": return DatabaseManagementSystem.SQLite; + case "oracle": // Currently disabled. See https://github.com/abpframework/abp/issues/6513 + // return DatabaseManagementSystem.Oracle; default: return DatabaseManagementSystem.NotSpecified; } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs index f215d37954..4286710899 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/DatabaseManagementSystemChangeStep.cs @@ -36,6 +36,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps ChangeEntityFrameworkCoreDependency(context,"Volo.Abp.EntityFrameworkCore.Oracle.Devart", "Volo.Abp.EntityFrameworkCore.Oracle.Devart", "AbpEntityFrameworkCoreOracleDevartModule"); + AdjustOracleDbContextOptionsBuilder(context); ChangeUseSqlServer(context,"UseOracle"); break; @@ -51,6 +52,14 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps } } + private void AdjustOracleDbContextOptionsBuilder(ProjectBuildContext context) + { + var dbContextFactoryFile = context.Files.First(f => f.Name.EndsWith("MigrationsDbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); + + dbContextFactoryFile.ReplaceText("new DbContextOptionsBuilder", + $"(DbContextOptionsBuilder<{context.BuildArgs.SolutionName.ProjectName}MigrationsDbContext>) new DbContextOptionsBuilder"); + } + private void AddMySqlServerVersion(ProjectBuildContext context) { var dbContextFactoryFile = context.Files.First(f => f.Name.EndsWith("MigrationsDbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); From f6f7c7ca7081078f7b6a0941989296681c39bc4c Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 21 Dec 2020 17:06:22 +0300 Subject: [PATCH 5/7] Cli DBMS selection documentation --- docs/en/CLI.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/en/CLI.md b/docs/en/CLI.md index 2e7b117799..4f4da1d90a 100644 --- a/docs/en/CLI.md +++ b/docs/en/CLI.md @@ -106,6 +106,12 @@ 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: + * `SqlServer` + * `MySQL` + * `SQLite` + * `Oracle-Devart` + * `PostgreSQL` * `--local-framework-ref --abp-path`: Uses local projects references to the ABP framework instead of using the NuGet packages. This can be useful if you download the ABP Framework source code and have a local reference to the framework from your application. * `--no-random-port`: Uses template's default ports. From 2f824c20449752ba9500cba19d841d198a840908 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 22 Dec 2020 11:33:05 +0300 Subject: [PATCH 6/7] Update TemplateProjectBuildPipelineBuilder.cs --- .../Building/TemplateProjectBuildPipelineBuilder.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs index 40ba54a3c0..9590001ea4 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs @@ -18,7 +18,6 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building pipeline.Steps.Add(new ProjectReferenceReplaceStep()); pipeline.Steps.Add(new TemplateCodeDeleteStep()); pipeline.Steps.Add(new SolutionRenameStep()); - pipeline.Steps.Add(new DatabaseManagementSystemChangeStep()); if (context.Template.Name == AppProTemplate.TemplateName || context.Template.Name == ModuleProTemplate.TemplateName) @@ -26,6 +25,12 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building pipeline.Steps.Add(new LicenseCodeReplaceStep()); } + if (context.Template.Name == AppTemplate.TemplateName || + context.Template.Name == AppProTemplate.TemplateName) + { + pipeline.Steps.Add(new DatabaseManagementSystemChangeStep()); + } + if ((context.BuildArgs.UiFramework == UiFramework.Mvc || context.BuildArgs.UiFramework == UiFramework.Blazor) && context.BuildArgs.MobileApp == MobileApp.None) { From fe3628cca02c1ef363179e384d2a3afeb9a374ef Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 23 Dec 2020 12:15:38 +0300 Subject: [PATCH 7/7] Update CLI.md --- docs/en/CLI.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/CLI.md b/docs/en/CLI.md index 4f4da1d90a..373b07e3d7 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**. `--connection-string` parameter should be set along with this parameter if you want to set any DBMS other than **SQL Server**. Supported DBMS's: * `SqlServer` * `MySQL` * `SQLite` @@ -425,4 +425,4 @@ abp bundle [options] * ```--working-directory``` or ```-wd```: Specifies the working directory. This option is useful when executing directory doesn't contain a Blazor project file. * ```--force``` or ```-f```: Forces to build project before generating references. -`bundle` command reads the `appsettings.json` file inside the Blazor project for bundling options. For more details about managing style and script references in Blazor apps, see [Managing Global Scripts & Styles](UI/Blazor/Global-Scripts-Styles.md) \ No newline at end of file +`bundle` command reads the `appsettings.json` file inside the Blazor project for bundling options. For more details about managing style and script references in Blazor apps, see [Managing Global Scripts & Styles](UI/Blazor/Global-Scripts-Styles.md)