mirror of https://github.com/abpframework/abp.git
23 changed files with 300 additions and 30 deletions
@ -0,0 +1,13 @@ |
|||||
|
namespace Volo.Abp.Cli.ProjectBuilding.Building |
||||
|
{ |
||||
|
public enum DatabaseManagementSystem |
||||
|
{ |
||||
|
NotSpecified, |
||||
|
SQLServer, |
||||
|
MySQL, |
||||
|
PostgreSQL, |
||||
|
Oracle, |
||||
|
OracleDevart, |
||||
|
SQLite |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,97 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using Volo.Abp.Cli.ProjectBuilding.Files; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps |
||||
|
{ |
||||
|
public class DatabaseManagementSystemChangeStep : 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"); |
||||
|
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 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)); |
||||
|
|
||||
|
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)); |
||||
|
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 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, newUseMethodForEfModule); |
||||
|
|
||||
|
var dbContextFactoryFile = context.Files.First(f => f.Name.EndsWith("MigrationsDbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); |
||||
|
dbContextFactoryFile.ReplaceText(oldUseMethod, newUseMethodForDbContext); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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<EfCoreMigrationRecreater> Logger { get; set; } |
||||
|
|
||||
|
public EfCoreMigrationRecreater() |
||||
|
{ |
||||
|
Logger = NullLogger<EfCoreMigrationRecreater>.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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue