|
|
|
@ -1,4 +1,6 @@ |
|
|
|
using System; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Volo.Abp; |
|
|
|
using VoloDocs.EntityFrameworkCore; |
|
|
|
@ -7,6 +9,8 @@ namespace VoloDocs.Migrator |
|
|
|
{ |
|
|
|
class Program |
|
|
|
{ |
|
|
|
private const string ScriptFile = "Script.txt"; |
|
|
|
|
|
|
|
static void Main(string[] args) |
|
|
|
{ |
|
|
|
Console.WriteLine("Initializing VoloDocs Migrator ... "); |
|
|
|
@ -21,48 +25,72 @@ namespace VoloDocs.Migrator |
|
|
|
|
|
|
|
Console.Clear(); |
|
|
|
|
|
|
|
Console.Write("\nThis program updates an existing database or creates a new one if not exists.\n" + |
|
|
|
"The following connection string will be used:\n\n" + |
|
|
|
new string('=', 70) + "\n" + |
|
|
|
connectionString + "\n" + |
|
|
|
new string('=', 70) + "\n\n" + |
|
|
|
"Do you confirm to run the migration? (y/n) "); |
|
|
|
if (args != null && args.Contains("-script")) |
|
|
|
{ |
|
|
|
GenerateMigrationScript(dbContext); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
RunMigrations(connectionString, dbContext); |
|
|
|
} |
|
|
|
|
|
|
|
Console.WriteLine("\n\nPress ENTER to exit..."); |
|
|
|
Console.ReadLine(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static void RunMigrations(string connectionString, VoloDocsDbContext dbContext) |
|
|
|
{ |
|
|
|
Console.Write("\nThis program updates an existing database or creates a new one if not exists.\n" + |
|
|
|
"The following connection string will be used:\n\n" + |
|
|
|
new string('=', 70) + "\n" + |
|
|
|
connectionString + "\n" + |
|
|
|
new string('=', 70) + "\n\n" + |
|
|
|
"Are you sure you want to run the migration? (y/n) "); |
|
|
|
|
|
|
|
if (Console.ReadKey().Key == ConsoleKey.Y) |
|
|
|
{ |
|
|
|
Console.WriteLine("\nMigrating..."); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
dbContext.Database.Migrate(); |
|
|
|
|
|
|
|
Console.WriteLine("\nCompleted."); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Console.WriteLine(ex.Message); |
|
|
|
|
|
|
|
while (ex.InnerException != null) |
|
|
|
{ |
|
|
|
ex = ex.InnerException; |
|
|
|
Console.WriteLine(ex.Message); |
|
|
|
} |
|
|
|
|
|
|
|
Console.Write("\nThere was problem while applying migrations. " + |
|
|
|
"Do you want to create the migration script? (y/n) "); |
|
|
|
|
|
|
|
if (Console.ReadKey().Key == ConsoleKey.Y) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
Console.WriteLine("\nMigrating..."); |
|
|
|
|
|
|
|
dbContext.Database.Migrate(); |
|
|
|
|
|
|
|
Console.WriteLine("\nCompleted."); |
|
|
|
|
|
|
|
Console.Write("\nDo you want to see the applied migrations? (y/n) "); |
|
|
|
if (Console.ReadKey().Key == ConsoleKey.Y) |
|
|
|
{ |
|
|
|
Console.WriteLine("\n\n-- start of migrations --"); |
|
|
|
|
|
|
|
var appliedMigrations = dbContext.Database.GetAppliedMigrations(); |
|
|
|
foreach (var appliedMigration in appliedMigrations) |
|
|
|
{ |
|
|
|
Console.WriteLine(appliedMigration); |
|
|
|
} |
|
|
|
|
|
|
|
Console.WriteLine("-- end of migrations --\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Console.Write("\nAn error occured while migrating the database:\n"); |
|
|
|
Console.Write(ex.ToString()); |
|
|
|
} |
|
|
|
GenerateMigrationScript(dbContext); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Console.WriteLine("Press ENTER to exit..."); |
|
|
|
Console.ReadLine(); |
|
|
|
private static void GenerateMigrationScript(VoloDocsDbContext dbContext) |
|
|
|
{ |
|
|
|
if (File.Exists(ScriptFile)) |
|
|
|
{ |
|
|
|
File.Delete(ScriptFile); |
|
|
|
} |
|
|
|
|
|
|
|
Console.Write("\nGenerating migration scripts..."); |
|
|
|
|
|
|
|
File.WriteAllText(ScriptFile, dbContext.Database.GenerateCreateScript()); |
|
|
|
|
|
|
|
Console.Write("\nMigration script has been created to Script.txt file"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |