Browse Source

Merge pull request #6779 from abpframework/CreateMigrationAndRunMigrator

DbMigrator creates the initial migration if no migration exists
pull/7053/head
Alper Ebicoglu 6 years ago
committed by GitHub
parent
commit
bebc87baf0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
  2. 61
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs
  3. 93
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Data/MyProjectNameDbMigrationService.cs

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

@ -40,6 +40,7 @@ namespace Volo.Abp.Cli
options.Commands["translate"] = typeof(TranslateCommand); options.Commands["translate"] = typeof(TranslateCommand);
options.Commands["build"] = typeof(BuildCommand); options.Commands["build"] = typeof(BuildCommand);
options.Commands["bundle"] = typeof(BundleCommand); options.Commands["bundle"] = typeof(BundleCommand);
options.Commands["create-migration-and-run-migrator"] = typeof(CreateMigrationAndRunMigrator);
}); });
} }
} }

61
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs

@ -0,0 +1,61 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.Commands
{
public class CreateMigrationAndRunMigrator : IConsoleCommand, ITransientDependency
{
public virtual async Task ExecuteAsync(CommandLineArgs commandLineArgs)
{
if (commandLineArgs.Target.IsNullOrEmpty())
{
throw new CliUsageException(
"DbMigrations folder path is missing!"
);
}
var dbMigratorProjectPath = GetDbMigratorProjectPath(commandLineArgs.Target);
if (dbMigratorProjectPath == null)
{
throw new Exception("DbMigrator is not found!");
}
var output = CmdHelper.RunCmdAndGetOutput("cd \"" + commandLineArgs.Target + "\" && dotnet ef migrations add Initial -s " + dbMigratorProjectPath);
if (output.Contains("Done.") && output.Contains("To undo this action") && output.Contains("ef migrations remove")) // Migration added successfully
{
CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(dbMigratorProjectPath) + "\" && dotnet run");
}
}
private string GetDbMigratorProjectPath(string dbMigrationsFolderPath)
{
var srcFolder = Directory.GetParent(dbMigrationsFolderPath);
var dbMigratorFolderPath = Directory.GetDirectories(srcFolder.FullName).FirstOrDefault(d => d.EndsWith(".DbMigrator"));
if (dbMigratorFolderPath == null)
{
return null;
}
return Directory.GetFiles(dbMigratorFolderPath).FirstOrDefault(f => f.EndsWith(".csproj"));
}
public string GetUsageInfo()
{
return string.Empty;
}
public string GetShortDescription()
{
return string.Empty;
}
}
}

93
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Data/MyProjectNameDbMigrationService.cs

@ -1,5 +1,9 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
@ -35,6 +39,19 @@ namespace MyCompanyName.MyProjectName.Data
public async Task MigrateAsync() public async Task MigrateAsync()
{ {
try
{
if (!await MigrationsFolderExists())
{
await AddInitialMigration();
return;
}
}
catch (Exception e)
{
Logger.LogWarning("Couldn't determinate if any migrations exist : " + e.Message);
}
Logger.LogInformation("Started database migrations..."); Logger.LogInformation("Started database migrations...");
await MigrateDatabaseSchemaAsync(); await MigrateDatabaseSchemaAsync();
@ -69,7 +86,8 @@ namespace MyCompanyName.MyProjectName.Data
Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations."); Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations.");
} }
Logger.LogInformation("Successfully completed database migrations."); Logger.LogInformation("Successfully completed all database migrations.");
Logger.LogInformation("You can safely end this process...");
} }
private async Task MigrateDatabaseSchemaAsync(Tenant tenant = null) private async Task MigrateDatabaseSchemaAsync(Tenant tenant = null)
@ -89,5 +107,76 @@ namespace MyCompanyName.MyProjectName.Data
await _dataSeeder.SeedAsync(tenant?.Id); await _dataSeeder.SeedAsync(tenant?.Id);
} }
private async Task<bool> MigrationsFolderExists()
{
var dbMigrationsProjectFolder = GetDbMigrationsProjectFolderPath();
return Directory.Exists(Path.Combine(dbMigrationsProjectFolder, "migrations"));
}
private async Task AddInitialMigration()
{
Logger.LogInformation("Creating initial migration...");
string argumentPrefix;
string fileName;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
argumentPrefix = "-c";
fileName = "/bin/bash";
}
else
{
argumentPrefix = "/C";
fileName = "cmd.exe";
}
var procStartInfo = new ProcessStartInfo( fileName,
$"{argumentPrefix} \"abp create-migration-and-run-migrator {GetDbMigrationsProjectFolderPath()}\""
);
try
{
Process.Start(procStartInfo);
}
catch (Exception)
{
throw new Exception("Couldn't run ABP CLI...");
}
}
private static string GetDbMigrationsProjectFolderPath()
{
var slnDirectoryPath = GetSolutionDirectoryPath();
if (slnDirectoryPath == null)
{
throw new Exception("Solution folder not found!");
}
var srcDirectoryPath = Path.Combine(slnDirectoryPath, "src");
return Directory.GetDirectories(srcDirectoryPath)
.FirstOrDefault(d => d.EndsWith(".DbMigrations"));
}
private static string GetSolutionDirectoryPath()
{
var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
while (Directory.GetParent(currentDirectory.FullName) != null)
{
currentDirectory = Directory.GetParent(currentDirectory.FullName);
if (Directory.GetFiles(currentDirectory.FullName).FirstOrDefault(f => f.EndsWith(".sln")) != null)
{
return currentDirectory.FullName;
}
}
return null;
}
} }
} }

Loading…
Cancel
Save