Browse Source

Merge pull request #24685 from abpframework/BuildBeforeAddMigrations

Run `dotnet build` command before creating EF Core migrations
pull/24690/head
Engincan VESKE 2 weeks ago
committed by GitHub
parent
commit
f8daaa1805
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 8
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Internal/RecreateInitialMigrationCommand.cs
  2. 16
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Services/InitialMigrationCreator.cs
  3. 9
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationManager.cs

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

@ -53,6 +53,14 @@ public class RecreateInitialMigrationCommand : IConsoleCommand, ITransientDepend
Directory.Delete(Path.Combine(projectDir, "TenantMigrations"), true); Directory.Delete(Path.Combine(projectDir, "TenantMigrations"), true);
separateDbContext = true; separateDbContext = true;
} }
CmdHelper.RunCmd("dotnet build", workingDirectory: projectDir, exitCode: out var exitCode);
if (exitCode != 0)
{
Logger.LogError("Build failed for project {Project}. Skipping migration recreation.", csprojFile);
continue;
}
if (!separateDbContext) if (!separateDbContext)
{ {
CmdHelper.RunCmd($"dotnet ef migrations add Initial", workingDirectory: projectDir); CmdHelper.RunCmd($"dotnet ef migrations add Initial", workingDirectory: projectDir);

16
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Services/InitialMigrationCreator.cs

@ -14,7 +14,7 @@ public class InitialMigrationCreator : ITransientDependency
public ICmdHelper CmdHelper { get; } public ICmdHelper CmdHelper { get; }
public DotnetEfToolManager DotnetEfToolManager { get; } public DotnetEfToolManager DotnetEfToolManager { get; }
public ILogger<InitialMigrationCreator> Logger { get; set; } public ILogger<InitialMigrationCreator> Logger { get; set; }
public InitialMigrationCreator(ICmdHelper cmdHelper, DotnetEfToolManager dotnetEfToolManager) public InitialMigrationCreator(ICmdHelper cmdHelper, DotnetEfToolManager dotnetEfToolManager)
{ {
CmdHelper = cmdHelper; CmdHelper = cmdHelper;
@ -30,11 +30,11 @@ public class InitialMigrationCreator : ITransientDependency
Logger.LogError($"This path doesn't exist: {targetProjectFolder}"); Logger.LogError($"This path doesn't exist: {targetProjectFolder}");
return false; return false;
} }
Logger.LogInformation("Creating initial migrations..."); Logger.LogInformation("Creating initial migrations...");
await DotnetEfToolManager.BeSureInstalledAsync(); await DotnetEfToolManager.BeSureInstalledAsync();
var tenantDbContextName = FindTenantDbContextName(targetProjectFolder); var tenantDbContextName = FindTenantDbContextName(targetProjectFolder);
var dbContextName = tenantDbContextName != null ? var dbContextName = tenantDbContextName != null ?
FindDbContextName(targetProjectFolder) FindDbContextName(targetProjectFolder)
@ -60,7 +60,7 @@ public class InitialMigrationCreator : ITransientDependency
return migrationSuccess; return migrationSuccess;
} }
private string FindTenantDbContextName(string projectFolder) private string FindTenantDbContextName(string projectFolder)
{ {
var tenantDbContext = Directory.GetFiles(projectFolder, "*TenantMigrationsDbContext.cs", SearchOption.AllDirectories) var tenantDbContext = Directory.GetFiles(projectFolder, "*TenantMigrationsDbContext.cs", SearchOption.AllDirectories)
@ -93,6 +93,12 @@ public class InitialMigrationCreator : ITransientDependency
private string AddMigrationAndGetOutput(string dbMigrationsFolder, string dbContext, string outputDirectory) private string AddMigrationAndGetOutput(string dbMigrationsFolder, string dbContext, string outputDirectory)
{ {
var output = CmdHelper.RunCmdAndGetOutput("dotnet build", out int buildExitCode, dbMigrationsFolder);
if (buildExitCode != 0)
{
return output;
}
var dbContextOption = string.IsNullOrWhiteSpace(dbContext) var dbContextOption = string.IsNullOrWhiteSpace(dbContext)
? string.Empty ? string.Empty
: $"--context {dbContext}"; : $"--context {dbContext}";
@ -108,4 +114,4 @@ public class InitialMigrationCreator : ITransientDependency
output.Contains("To undo this action") && output.Contains("To undo this action") &&
output.Contains("ef migrations remove")); output.Contains("ef migrations remove"));
} }
} }

9
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationManager.cs

@ -44,13 +44,20 @@ public class EfCoreMigrationManager : ITransientDependency
string dbContext, string dbContext,
string outputDirectory) string outputDirectory)
{ {
CmdHelper.RunCmd($"dotnet build", workingDirectory: dbMigrationsProjectFolder, exitCode: out var buildExitCode);
if (buildExitCode != 0)
{
Logger.LogWarning("Dotnet build failed for project folder {ProjectFolder}. Skipping EF Core migration command.", dbMigrationsProjectFolder);
return;
}
var dbContextOption = string.IsNullOrWhiteSpace(dbContext) var dbContextOption = string.IsNullOrWhiteSpace(dbContext)
? string.Empty ? string.Empty
: $"--context {dbContext}"; : $"--context {dbContext}";
CmdHelper.RunCmd($"dotnet ef migrations add {migrationName}" + CmdHelper.RunCmd($"dotnet ef migrations add {migrationName}" +
$" --output-dir {outputDirectory}" + $" --output-dir {outputDirectory}" +
$" {dbContextOption}", $" {dbContextOption}",
workingDirectory: dbMigrationsProjectFolder); workingDirectory: dbMigrationsProjectFolder);
} }

Loading…
Cancel
Save