From 90dc7fe28a0f47daf380f78532d5853a9a55b9e5 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Tue, 9 Apr 2019 09:02:14 +0300 Subject: [PATCH] added generate migration script --- modules/docs/app/VoloDocs.Migrator/Program.cs | 100 +++++++++++------- 1 file changed, 64 insertions(+), 36 deletions(-) diff --git a/modules/docs/app/VoloDocs.Migrator/Program.cs b/modules/docs/app/VoloDocs.Migrator/Program.cs index 926e351260..566d0e0163 100644 --- a/modules/docs/app/VoloDocs.Migrator/Program.cs +++ b/modules/docs/app/VoloDocs.Migrator/Program.cs @@ -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"); } } -} +} \ No newline at end of file