Browse Source

Add `RecreateInitialMigrationCommand`.

pull/18735/head
maliming 3 years ago
parent
commit
16f32561d4
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 2
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
  2. 84
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Internal/RecreateInitialMigrationCommand.cs

2
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs

@ -1,6 +1,7 @@
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Cli.Commands;
using Volo.Abp.Cli.Commands.Internal;
using Volo.Abp.Cli.Http;
using Volo.Abp.Cli.ServiceProxying;
using Volo.Abp.Cli.ServiceProxying.Angular;
@ -68,6 +69,7 @@ public class AbpCliCoreModule : AbpModule
options.Commands[CleanCommand.Name] = typeof(CleanCommand);
options.Commands[CliCommand.Name] = typeof(CliCommand);
options.Commands[ClearDownloadCacheCommand.Name] = typeof(ClearDownloadCacheCommand);
options.Commands[RecreateInitialMigrationCommand.Name] = typeof(RecreateInitialMigrationCommand);
options.DisabledModulesToAddToSolution.Add("Volo.Abp.LeptonXTheme.Pro");
options.DisabledModulesToAddToSolution.Add("Volo.Abp.LeptonXTheme.Lite");

84
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Internal/RecreateInitialMigrationCommand.cs

@ -0,0 +1,84 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.Commands.Internal;
public class RecreateInitialMigrationCommand : IConsoleCommand, ITransientDependency
{
public const string Name = "recreate-initial-migration";
public ILogger<RecreateInitialMigrationCommand> Logger { get; set; }
protected CmdHelper CmdHelper { get; }
public RecreateInitialMigrationCommand(CmdHelper cmdHelper)
{
CmdHelper = cmdHelper;
Logger = NullLogger<RecreateInitialMigrationCommand>.Instance;
}
public virtual Task ExecuteAsync(CommandLineArgs commandLineArgs)
{
var csprojFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.csproj", SearchOption.AllDirectories)
.Where(x => x.Contains("templates") || x.Contains("test-app"))
.Where(x => File.ReadAllText(x).Contains("Microsoft.EntityFrameworkCore.Tools")).ToList();
foreach (var csprojFile in csprojFiles)
{
var projectDir = Path.GetDirectoryName(csprojFile)!;
if (!Directory.Exists(Path.Combine(projectDir, "Migrations")))
{
continue;
}
Logger.LogInformation($"Recreating migrations for {csprojFile}");
if (Directory.Exists(Path.Combine(projectDir, "Migrations")))
{
Directory.Delete(Path.Combine(projectDir, "Migrations"), true);
}
var separateDbContext = false;
if (Directory.Exists(Path.Combine(projectDir, "TenantMigrations")))
{
Directory.Delete(Path.Combine(projectDir, "TenantMigrations"), true);
separateDbContext = true;
}
if (!separateDbContext)
{
CmdHelper.RunCmd($"dotnet ef migrations add Initial", workingDirectory: projectDir);
}
else
{
CmdHelper.RunCmd($"dotnet ef migrations add Initial --context MyProjectNameDbContext", workingDirectory: projectDir);
CmdHelper.RunCmd($"dotnet ef migrations add Initial --context MyProjectNameTenantDbContext --output-dir TenantMigrations", workingDirectory: projectDir);
}
if (Directory.Exists(Path.Combine(projectDir, "Logs")))
{
Directory.Delete(Path.Combine(projectDir, "Logs"), true);
}
}
Logger.LogInformation("Done! All migrations recreated.");
return Task.CompletedTask;
}
public string GetUsageInfo()
{
return GetShortDescription();
}
public string GetShortDescription()
{
return "This is a internal command. Please run 'abp recreate-initial-migration' command in abp or volo root directory.";
}
}
Loading…
Cancel
Save