Support to change dbms on module template.

Resolve #17667
pull/17856/head
maliming 1 year ago
parent 99806b7621
commit b5fb03cc29
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

@ -450,7 +450,7 @@ public abstract class ProjectCreationCommandBase
var efCoreProjectPath = string.Empty;
bool isLayeredTemplate;
var isModuleTemplate = false;
switch (projectArgs.TemplateName)
{
case AppTemplate.TemplateName:
@ -464,11 +464,16 @@ public abstract class ProjectCreationCommandBase
?? Directory.GetFiles(projectArgs.OutputFolder, "*.csproj", SearchOption.AllDirectories).FirstOrDefault();
isLayeredTemplate = false;
break;
case ModuleTemplate.TemplateName:
case ModuleProTemplate.TemplateName:
isModuleTemplate = true;
isLayeredTemplate = false;
break;
default:
return;
}
if (string.IsNullOrWhiteSpace(efCoreProjectPath))
if (string.IsNullOrWhiteSpace(efCoreProjectPath) && !isModuleTemplate)
{
Logger.LogWarning("Couldn't find the project to create initial migrations!");
return;
@ -479,7 +484,20 @@ public abstract class ProjectCreationCommandBase
Message = "Creating the initial DB migration"
}, false);
await InitialMigrationCreator.CreateAsync(Path.GetDirectoryName(efCoreProjectPath), isLayeredTemplate);
if (!isModuleTemplate)
{
await InitialMigrationCreator.CreateAsync(Path.GetDirectoryName(efCoreProjectPath), isLayeredTemplate);
}
else
{
var hostProjectsWithEfCore = Directory.GetFiles(projectArgs.OutputFolder, "*.csproj", SearchOption.AllDirectories)
.Where(x => File.ReadAllText(x).Contains("Microsoft.EntityFrameworkCore.Tools"))
.ToList();
foreach (var project in hostProjectsWithEfCore)
{
await InitialMigrationCreator.CreateAsync(Path.GetDirectoryName(project));
}
}
}
protected Task CreateOpenIddictPfxFilesAsync(ProjectBuildArgs projectArgs)

@ -0,0 +1,113 @@
using System;
using System.Linq;
using Volo.Abp.Cli.ProjectBuilding.Files;
using Volo.Abp.Cli.ProjectBuilding.Templates.App;
namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps;
public class AppModuleDatabaseManagementSystemChangeStep : ProjectBuildPipelineStep
{
public override void Execute(ProjectBuildContext context)
{
switch (context.BuildArgs.DatabaseManagementSystem)
{
case DatabaseManagementSystem.MySQL:
ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.MySQL",
"Volo.Abp.EntityFrameworkCore.MySQL",
"AbpEntityFrameworkCoreMySQLModule");
AddMySqlServerVersion(context);
ChangeUseSqlServer(context, "UseMySQL", "UseMySql");
break;
case DatabaseManagementSystem.PostgreSQL:
ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.PostgreSql",
"Volo.Abp.EntityFrameworkCore.PostgreSql",
"AbpEntityFrameworkCorePostgreSqlModule");
ChangeUseSqlServer(context, "UseNpgsql");
break;
case DatabaseManagementSystem.Oracle:
ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.Oracle",
"Volo.Abp.EntityFrameworkCore.Oracle",
"AbpEntityFrameworkCoreOracleModule");
AdjustOracleDbContextOptionsBuilder(context);
ChangeUseSqlServer(context, "UseOracle");
break;
case DatabaseManagementSystem.OracleDevart:
ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.Oracle.Devart",
"Volo.Abp.EntityFrameworkCore.Oracle.Devart",
"AbpEntityFrameworkCoreOracleDevartModule");
AdjustOracleDbContextOptionsBuilder(context);
ChangeUseSqlServer(context, "UseOracle");
break;
case DatabaseManagementSystem.SQLite:
ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.Sqlite",
"Volo.Abp.EntityFrameworkCore.Sqlite",
"AbpEntityFrameworkCoreSqliteModule");
ChangeUseSqlServer(context, "UseSqlite");
break;
default:
return;
}
}
private void AdjustOracleDbContextOptionsBuilder(ProjectBuildContext context)
{
var dbContextFactoryFiles = context.Files.Where(f => f.Name.EndsWith("DbContextFactory.cs", StringComparison.OrdinalIgnoreCase));
foreach (var dbContextFactoryFile in dbContextFactoryFiles)
{
dbContextFactoryFile?.ReplaceText("new DbContextOptionsBuilder",
$"(DbContextOptionsBuilder<{context.BuildArgs.SolutionName.ProjectName}{(false ? "Migrations" : string.Empty)}DbContext>) new DbContextOptionsBuilder");
}
}
private void AddMySqlServerVersion(ProjectBuildContext context)
{
var dbContextFactoryFiles = context.Files.Where(f => f.Name.EndsWith("DbContextFactory.cs", StringComparison.OrdinalIgnoreCase));
foreach (var dbContextFactoryFile in dbContextFactoryFiles)
{
dbContextFactoryFile?.ReplaceText("configuration.GetConnectionString(\"Default\")", "configuration.GetConnectionString(\"Default\"), MySqlServerVersion.LatestSupportedServerVersion");
}
}
private void ChangeEntityFrameworkCoreDependency(ProjectBuildContext context, string newPackageName, string newModuleNamespace, string newModuleClass)
{
var efCoreProjectFiles = context.Files.Where(f => f.Name.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase));
foreach (var efCoreProjectFile in efCoreProjectFiles)
{
efCoreProjectFile?.ReplaceText("Volo.Abp.EntityFrameworkCore.SqlServer", newPackageName);
}
var efCoreModuleClasses = context.Files.Where(f => f.Name.EndsWith("Module.cs", StringComparison.OrdinalIgnoreCase));
foreach (var efCoreModuleClass in efCoreModuleClasses)
{
efCoreModuleClass?.ReplaceText("Volo.Abp.EntityFrameworkCore.SqlServer", newModuleNamespace);
efCoreModuleClass?.ReplaceText("AbpEntityFrameworkCoreSqlServerModule", newModuleClass);
}
}
private void ChangeUseSqlServer(ProjectBuildContext context, string newUseMethodForEfModule, string newUseMethodForDbContext = null)
{
if (newUseMethodForDbContext == null)
{
newUseMethodForDbContext = newUseMethodForEfModule;
}
const string oldUseMethod = "UseSqlServer";
var efCoreModuleClasses = context.Files.Where(f => f.Name.EndsWith("Module.cs", StringComparison.OrdinalIgnoreCase));
foreach (var efCoreModuleClass in efCoreModuleClasses)
{
efCoreModuleClass.ReplaceText(oldUseMethod, newUseMethodForEfModule);
}
var dbContextFactoryFiles = context.Files.Where(f => f.Name.EndsWith("DbContextFactory.cs", StringComparison.OrdinalIgnoreCase));
foreach (var dbContextFactoryFile in dbContextFactoryFiles)
{
dbContextFactoryFile?.ReplaceText(oldUseMethod, newUseMethodForDbContext);
}
}
}

@ -43,6 +43,12 @@ public static class TemplateProjectBuildPipelineBuilder
pipeline.Steps.Add(new AppNoLayersDatabaseManagementSystemChangeStep()); // todo: move to custom steps?
}
if (context.Template.Name == ModuleTemplate.TemplateName ||
context.Template.Name == ModuleProTemplate.TemplateName)
{
pipeline.Steps.Add(new AppModuleDatabaseManagementSystemChangeStep()); // todo: move to custom steps?
}
if ((context.BuildArgs.UiFramework == UiFramework.Mvc || context.BuildArgs.UiFramework == UiFramework.Blazor || context.BuildArgs.UiFramework == UiFramework.BlazorServer)
&& context.BuildArgs.MobileApp == MobileApp.None && context.Template.Name != MicroserviceProTemplate.TemplateName
&& context.Template.Name != MicroserviceServiceProTemplate.TemplateName)

@ -27,6 +27,7 @@ public abstract class ModuleTemplateBase : TemplateInfo
DeleteUnrelatedProjects(context, steps);
RandomizeSslPorts(context, steps);
UpdateNuGetConfig(context, steps);
RemoveMigrations(context, steps);
ChangeConnectionString(context, steps);
CleanupFolderHierarchy(context, steps);
@ -103,6 +104,17 @@ public abstract class ModuleTemplateBase : TemplateInfo
steps.Add(new UpdateNuGetConfigStep("/NuGet.Config"));
}
protected void RemoveMigrations(ProjectBuildContext context, List<ProjectBuildPipelineStep> steps)
{
steps.Add(new RemoveFolderStep("/aspnet-core/host/MyCompanyName.MyProjectName.AuthServer/Migrations"));
steps.Add(new RemoveFolderStep("/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Migrations"));
steps.Add(new RemoveFolderStep("/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Migrations"));
if (context.BuildArgs.TemplateName == ModuleProTemplate.TemplateName)
{
steps.Add(new RemoveFolderStep("/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Migrations"));
}
}
private void ChangeConnectionString(ProjectBuildContext context, List<ProjectBuildPipelineStep> steps)
{
if (context.BuildArgs.ConnectionString != null)

Loading…
Cancel
Save