mirror of https://github.com/abpframework/abp.git
6 changed files with 87 additions and 107 deletions
@ -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}"; |
|||
} |
|||
} |
|||
} |
|||
@ -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<EfCoreMigrationManager> Logger { get; set; } |
|||
|
|||
public EfCoreMigrationManager() |
|||
{ |
|||
Logger = NullLogger<EfCoreMigrationManager>.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}"; |
|||
} |
|||
} |
|||
} |
|||
@ -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<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