Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: just delete migrations instead of recreating when -dbms is used #6795

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/en/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**. Supported DBMS's:
* `SqlServer`
* `MySQL`
* `SQLite`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ namespace Volo.Abp.Cli.Commands
{
public class NewCommand : IConsoleCommand, ITransientDependency
{
private readonly EfCoreMigrationRecreater _efCoreMigrationRecreater;
private readonly EfCoreMigrationManager _efCoreMigrationManager;
public ILogger<NewCommand> Logger { get; set; }

protected TemplateProjectBuilder TemplateProjectBuilder { get; }
public ITemplateInfoProvider TemplateInfoProvider { get; }

public NewCommand(TemplateProjectBuilder templateProjectBuilder
, ITemplateInfoProvider templateInfoProvider,
EfCoreMigrationRecreater efCoreMigrationRecreater)
EfCoreMigrationManager efCoreMigrationManager)
{
_efCoreMigrationRecreater = efCoreMigrationRecreater;
_efCoreMigrationManager = efCoreMigrationManager;
TemplateProjectBuilder = templateProjectBuilder;
TemplateInfoProvider = templateInfoProvider;

Expand Down Expand Up @@ -101,13 +101,6 @@ public async Task ExecuteAsync(CommandLineArgs commandLineArgs)
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)
{
Expand Down Expand Up @@ -215,7 +208,7 @@ public async Task ExecuteAsync(CommandLineArgs commandLineArgs)
}
}

ReCreateMigrationsIfNeeded(databaseProvider, databaseManagementSystem, outputFolder);
DeleteMigrationsIfNeeded(databaseProvider, databaseManagementSystem, outputFolder);

Logger.LogInformation($"'{projectName}' has been successfully created to '{outputFolder}'");

Expand All @@ -226,7 +219,7 @@ public async Task ExecuteAsync(CommandLineArgs commandLineArgs)
}
}

private void ReCreateMigrationsIfNeeded(DatabaseProvider databaseProvider, DatabaseManagementSystem databaseManagementSystem, string outputFolder)
private void DeleteMigrationsIfNeeded(DatabaseProvider databaseProvider, DatabaseManagementSystem databaseManagementSystem, string outputFolder)
{
if (databaseManagementSystem == DatabaseManagementSystem.NotSpecified || databaseManagementSystem == DatabaseManagementSystem.SQLServer)
{
Expand All @@ -238,9 +231,9 @@ private void ReCreateMigrationsIfNeeded(DatabaseProvider databaseProvider, Datab
return;
}

Logger.LogInformation($"Re-creating migrations... ({databaseManagementSystem})");
Logger.LogInformation($"Deleting migrations...");

_efCoreMigrationRecreater.Recreate(outputFolder);
_efCoreMigrationManager.RemoveAllMigrations(outputFolder);
}

private void OpenThanksPage(UiFramework uiFramework, DatabaseProvider databaseProvider, bool tiered, bool commercial)
Expand Down Expand Up @@ -305,7 +298,7 @@ public string GetUsageInfo()
sb.AppendLine("-ts|--template-source <template-source> (your local or network abp template source)");
sb.AppendLine("-csf|--create-solution-folder (default: true)");
sb.AppendLine("-cs|--connection-string <connection-string> (your database connection string)");
sb.AppendLine("--dbms <database-management-system> (your database management system. Requires --connection-string to be set)");
sb.AppendLine("--dbms <database-management-system> (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)");
Expand All @@ -328,7 +321,7 @@ public string GetUsageInfo()
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");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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}";
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SolutionModuleAdder : ITransientDependency
protected IJsonSerializer JsonSerializer { get; }
protected ProjectNugetPackageAdder ProjectNugetPackageAdder { get; }
protected DbContextFileBuilderConfigureAdder DbContextFileBuilderConfigureAdder { get; }
protected EfCoreMigrationAdder EfCoreMigrationAdder { get; }
protected EfCoreMigrationManager EfCoreMigrationManager { get; }
protected DerivedClassFinder DerivedClassFinder { get; }
protected ProjectNpmPackageAdder ProjectNpmPackageAdder { get; }
protected NpmGlobalPackagesChecker NpmGlobalPackagesChecker { get; }
Expand All @@ -42,7 +42,7 @@ public SolutionModuleAdder(
IJsonSerializer jsonSerializer,
ProjectNugetPackageAdder projectNugetPackageAdder,
DbContextFileBuilderConfigureAdder dbContextFileBuilderConfigureAdder,
EfCoreMigrationAdder efCoreMigrationAdder,
EfCoreMigrationManager efCoreMigrationManager,
DerivedClassFinder derivedClassFinder,
ProjectNpmPackageAdder projectNpmPackageAdder,
NpmGlobalPackagesChecker npmGlobalPackagesChecker,
Expand All @@ -57,7 +57,7 @@ public SolutionModuleAdder(
JsonSerializer = jsonSerializer;
ProjectNugetPackageAdder = projectNugetPackageAdder;
DbContextFileBuilderConfigureAdder = dbContextFileBuilderConfigureAdder;
EfCoreMigrationAdder = efCoreMigrationAdder;
EfCoreMigrationManager = efCoreMigrationManager;
DerivedClassFinder = derivedClassFinder;
ProjectNpmPackageAdder = projectNpmPackageAdder;
NpmGlobalPackagesChecker = npmGlobalPackagesChecker;
Expand Down Expand Up @@ -454,7 +454,7 @@ protected void ModifyDbContext(string[] projectFiles, ModuleInfo module, string
{
if (addedNewBuilder)
{
EfCoreMigrationAdder.AddMigration(dbMigrationsProject, module.Name, startupProject);
EfCoreMigrationManager.AddMigration(dbMigrationsProject, module.Name, startupProject);
}

RunMigrator(projectFiles);
Expand Down