|
|
|
@ -19,12 +19,35 @@ namespace Volo.Abp.Cli.ProjectModification |
|
|
|
|
|
|
|
public void AddMigration(string dbMigrationsCsprojFile, string module, string startupProject) |
|
|
|
{ |
|
|
|
var dbMigrationsProjectFolder = Path.GetDirectoryName(dbMigrationsCsprojFile); |
|
|
|
var moduleName = ParseModuleName(module); |
|
|
|
var migrationName = "Added_" + moduleName + "_Module" + GetUniquePostFix(); |
|
|
|
|
|
|
|
CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(dbMigrationsCsprojFile) + |
|
|
|
"\" && dotnet ef migrations add " + migrationName + |
|
|
|
GetStartupProjectOption(startupProject)); |
|
|
|
var tenantDbContextName = FindTenantDbContextName(dbMigrationsProjectFolder); |
|
|
|
var dbContextName = tenantDbContextName != null ? |
|
|
|
FindDbContextName(dbMigrationsProjectFolder) |
|
|
|
: null; |
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tenantDbContextName)) |
|
|
|
{ |
|
|
|
RunAddMigrationCommand(dbMigrationsProjectFolder, migrationName, startupProject, tenantDbContextName); |
|
|
|
} |
|
|
|
|
|
|
|
RunAddMigrationCommand(dbMigrationsProjectFolder, migrationName, startupProject, dbContextName); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual void RunAddMigrationCommand( |
|
|
|
string dbMigrationsProjectFolder, |
|
|
|
string migrationName, |
|
|
|
string startupProject, |
|
|
|
string dbContext) |
|
|
|
{ |
|
|
|
var dbContextOption = string.IsNullOrWhiteSpace(dbContext) |
|
|
|
? string.Empty |
|
|
|
: $"--context {dbContext}"; |
|
|
|
|
|
|
|
CmdHelper.RunCmd($"cd \"{dbMigrationsProjectFolder}\" && dotnet ef migrations add {migrationName}" + |
|
|
|
$" {GetStartupProjectOption(startupProject)} {dbContextOption}"); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual string ParseModuleName(string fullModuleName) |
|
|
|
@ -47,5 +70,33 @@ namespace Volo.Abp.Cli.ProjectModification |
|
|
|
{ |
|
|
|
return startupProject.IsNullOrWhiteSpace() ? "" : $" -s {startupProject}"; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual string FindDbContextName(string dbMigrationsFolder) |
|
|
|
{ |
|
|
|
var dbContext = Directory |
|
|
|
.GetFiles(dbMigrationsFolder, "*MigrationsDbContext.cs", SearchOption.AllDirectories) |
|
|
|
.FirstOrDefault(fp => !fp.EndsWith("TenantMigrationsDbContext.cs")); |
|
|
|
|
|
|
|
if (dbContext == null) |
|
|
|
{ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
return Path.GetFileName(dbContext).RemovePostFix(".cs"); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual string FindTenantDbContextName(string dbMigrationsFolder) |
|
|
|
{ |
|
|
|
var tenantDbContext = Directory |
|
|
|
.GetFiles(dbMigrationsFolder, "*TenantMigrationsDbContext.cs", SearchOption.AllDirectories) |
|
|
|
.FirstOrDefault(); |
|
|
|
|
|
|
|
if (tenantDbContext == null) |
|
|
|
{ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
return Path.GetFileName(tenantDbContext).RemovePostFix(".cs"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|