public MySmoObjectBase(SmoObjectBase smoObject, string name, string parentName, params string[] extraParams) { SourceSmoObject = smoObject; Name = name; ParentName = parentName; ExtraParams = new string[extraParams.Length]; for (int i = 0; i < extraParams.Length; i++) { ExtraParams[i] = UpgradeScriptGenerator.TrimSymbols(extraParams[i]); } }
private static void GenerateUpdate( string productionDatabaseName) { // // Restore so called source database. // var dbEngine = new DatabaseEngine(); Trace.TraceInformation("Restore {0} database...\r\n", Configuration.Default.SourceDatabaseName); string sourceDatabaseBackupFilePath = PathsEngine.CorrectBackupPathAccordingToConfig( PathsEngine.ConvertRelativePath2RootedPathIfNeeded( Configuration.Default.SourceDatabaseBackupFilePath)); dbEngine.CreateDatabaseFromBackupFile( Configuration.Default.SourceDatabaseName, sourceDatabaseBackupFilePath); if ((Configuration.Default.IsTestModeEnabled) && (productionDatabaseName != "*")) { // // Restore production database from the backup file. // Needed just to simplify testing. // Trace.TraceInformation("Restore {0} database...\r\n", productionDatabaseName); string productionDatabaseBackupFilePath = PathsEngine.CorrectBackupPathAccordingToConfig( PathsEngine.ConvertRelativePath2RootedPathIfNeeded( Configuration.Default.ProductionDatabaseBackupFilePath)); dbEngine.CreateDatabaseFromBackupFile( productionDatabaseName, productionDatabaseBackupFilePath); } DateTime now = DateTime.Now; foreach (string databaseName in dbEngine.GetDatabasesForUpgrade(productionDatabaseName)) { string outputFolder = string.Format( "Output\\{0}\\{1}", now.ToString("yyyy.MM.dd HH.mm.ss"), databaseName); Configuration.Default.OutputFolder = Path.Combine( Configuration.Default.Path, outputFolder); // // Backup production database. For the case, // If update will do something wrong, then we can restore // original database from the backup. // Trace.TraceInformation("Backup production database {0}...\r\n", databaseName); dbEngine.CreateBackup( databaseName, PathsEngine.CorrectBackupPathAccordingToConfig( Path.Combine( Configuration.Default.OutputFolder, databaseName + ".bak"))); var scriptGenerator = new UpgradeScriptGenerator( databaseName); string dropScriptText; string createScriptText; string alterScriptText; scriptGenerator.GenerateUpgradeScript( databaseName, Configuration.Default.SourceDatabaseName, out dropScriptText, out createScriptText, out alterScriptText); SaveScriptText( dropScriptText, "DROP.sql"); SaveScriptText( createScriptText, "CREATE.sql"); SaveScriptText( alterScriptText, "UPDATE.sql"); }//foreach (string databaseName in dbEngine.GetDatabasesForUpgrade(productionDatabaseName)) if (Configuration.Default.ExecuteUpdateScript) { Console.WriteLine( "All scripts for update production databases was generated.\r\n" + "Press Y to continue or N to exit"); ConsoleKeyInfo key = Console.ReadKey(); Console.WriteLine(); if (key.KeyChar != 'y' && key.KeyChar != 'Y') { return; } foreach (string databaseName in dbEngine.GetDatabasesForUpgrade(productionDatabaseName)) { if (string.IsNullOrEmpty(Configuration.Default.ExternalPreUpdateScriptPath) == false) { Trace.TraceInformation("Execute PRE script for the database {0}...\r\n", databaseName); string externalUpdateScriptPath = PathsEngine.ConvertRelativePath2RootedPathIfNeeded( Configuration.Default.ExternalPreUpdateScriptPath); RunExternalScript(dbEngine, databaseName, externalUpdateScriptPath); } string outputFolder = string.Format( "Output\\{0}\\{1}", now.ToString("yyyy.MM.dd HH.mm.ss"), databaseName); string dropScriptText; using (var sr = new StreamReader(outputFolder + @"\DROP.sql")) { dropScriptText = sr.ReadToEnd(); } Trace.TraceInformation("Execute DROP script for the database {0}...\r\n", databaseName); dbEngine.ExecuteBatch( dropScriptText); string alterScriptText; using (var sr = new StreamReader(outputFolder + @"\UPDATE.sql")) { alterScriptText = sr.ReadToEnd(); } Trace.TraceInformation("Execute ALTER script for the database {0}...\r\n", databaseName); dbEngine.ExecuteBatch( alterScriptText); string createScriptText; using (var sr = new StreamReader(outputFolder + @"\CREATE.sql")) { createScriptText = sr.ReadToEnd(); } Trace.TraceInformation("Execute CREATE script for the database {0}...\r\n", databaseName); dbEngine.ExecuteBatch( createScriptText); if (string.IsNullOrEmpty(Configuration.Default.ExternalPostUpdateScriptPath) == false) { Trace.TraceInformation("Execute POST script for the database {0}...\r\n", databaseName); string externalUpdateScriptPath = PathsEngine.ConvertRelativePath2RootedPathIfNeeded( Configuration.Default.ExternalPostUpdateScriptPath); RunExternalScript(dbEngine, databaseName, externalUpdateScriptPath); } } //foreach (string databaseName in dbEngine.GetDatabasesForUpgrade(productionDatabaseName)) } //if (Configuration.Default.ExecuteUpdateScript == true) // // All databases successfully upgraded, now we need: // 1. Create new backup file if needed // 2. Drop source "upgrade" database created by utility // if (Configuration.Default.UpdateDefaultDatabaseBackup) { Trace.TraceInformation("Backuping default database {0}, path {1}...\r\n", Configuration.Default.DefaultDatabaseName, Configuration.Default.DefaultDatabaseBackupFilePath); dbEngine.CreateBackup( Configuration.Default.DefaultDatabaseName, Configuration.Default.DefaultDatabaseBackupFilePath); } dbEngine.DropDatabase( Configuration.Default.SourceDatabaseName); }