private static bool FinishUpgrade(UpgradeEngine upgrader, ILog log) { var toExecute = upgrader.GetScriptsToExecute(); log.WriteLine("Upgrading File..."); log.WriteLine($"Applying {toExecute.Count} scripts."); var result = upgrader.PerformUpgrade(); if (!result.Successful) { log.WriteLine("Upgrage failed!", LogLevel.Error); log.WriteLine(result.Error.ToString(), LogLevel.Error); return(false); } log.WriteLine("Upgrade successful!"); foreach (SqlScript script in result.Scripts) { log.WriteLine($"Applied Upgrade Script {script.Name}."); } log.WriteLine(); return(true); }
public IList <IUpgradeName> GetPendingUpgrades() { return(upgradeEngine .GetScriptsToExecute() .Select(x => new SimpleUpgradeName { Name = x.Name }) .Cast <IUpgradeName>() .ToList()); }
} // Run private static void GenerateScriptFile(string destinationDir, UpgradeEngine upgrader) { // Generate full script to apply var scriptPath = Path.Combine(destinationDir, "UpgradeScript.sql"); Console.WriteLine($"Generating the script {scriptPath}"); File.WriteAllLines(scriptPath, upgrader.GetScriptsToExecute().Select(x => $"-- ### {x.Name}{Environment.NewLine}{x.Contents}"), Encoding.UTF8); }
public static string DryRun(this UpgradeEngine upgradeEngine, IDictionary <string, string> variables) { var buffer = new StringBuilder($"-- DbUp generated script at {DateTimeOffset.Now}\n"); var variableReplacer = new VariableSubstitutionPreprocessor(variables); upgradeEngine.GetScriptsToExecute().ForEach(s => { buffer.AppendLine($"-- Script name: {s.Name}"); buffer.AppendLine(variableReplacer.Process(s.Contents)); }); return(buffer.ToString()); }
private void Upgrade(UpgradeEngine upgradeEngine) { var scripts = upgradeEngine.GetScriptsToExecute() .Select(s => s.Name) .ToList(); _log.Information( "Going to migrate the SQL database by executing these scripts: {0}", string.Join(", ", scripts)); var result = upgradeEngine.PerformUpgrade(); if (!result.Successful) { throw new SqlMigrationException(scripts, result.Error.Message, result.Error); } }
public DatabaseUpgradeResult Run(string[] args) { DatabaseUpgradeResult result = null; if (args.Any(a => "--scriptAllDefinitions".Equals(a.Trim(), StringComparison.InvariantCultureIgnoreCase))) { result = ScriptAll(); } else { var scriptsToExecute = m_engine.GetScriptsToExecute(); if (args.Any(a => "--whatIf".Equals(a.Trim(), StringComparison.InvariantCultureIgnoreCase))) { result = new DatabaseUpgradeResult(Enumerable.Empty <SqlScript>(), true, null); this.Log.WriteWarning("WHATIF Mode!"); this.Log.WriteWarning("The following scripts would have been executed:"); scriptsToExecute.ForEach(r => this.Log.WriteWarning(r.Name)); } else { var executedScriptsBeforeUpgrade = this.m_engine.GetExecutedScripts(); result = m_engine.PerformUpgrade(); if (args.Any(a => "--fromconsole".Equals(a.Trim(), StringComparison.InvariantCultureIgnoreCase))) { var scripter = new DbObjectScripter(this.ConnectionString, this.m_options, this.Log); if (result.Successful) { this.Log.WriteInformation("Scripting changed database objects..."); var scriptorResult = scripter.ScriptMigrationTargets(scriptsToExecute); } else { this.Log.WriteInformation("Scripting successfully changed database objects..."); var executedScriptsAfterUpgrade = this.m_engine.GetExecutedScripts(); var appliedScripts = scriptsToExecute.Where(s => executedScriptsAfterUpgrade.Except(executedScriptsBeforeUpgrade) .Contains(s.Name)); var scriptorResult = scripter.ScriptMigrationTargets(appliedScripts); } } } } return(result); }
/// <summary> /// This method will generate an HTML report which can be uploaded as an artifact to any deployment / build tool which supports artifacts. Useful for getting approvals and seeing a history of what ran when /// </summary> /// <param name="upgradeEngine">The upgrade engine</param> /// <param name="fullPath">The full path of the file which will be generated</param> /// <param name="serverName">The name of the server being connected to</param> /// <param name="databaseName">The name of the database being upgraded</param> public static void GenerateUpgradeHtmlReport(this UpgradeEngine upgradeEngine, string fullPath, string serverName, string databaseName) { var scriptsToRunList = upgradeEngine.GetScriptsToExecute(); var htmlReport = new StringBuilder(); htmlReport.Append(GetHtmlHeader(serverName, databaseName)); for (var i = 0; i < scriptsToRunList.Count; i++) { htmlReport.Append(GetHtmlForScript(scriptsToRunList[i], i)); } htmlReport.Append(GetHtmlFooter()); if (File.Exists(fullPath)) { File.Delete(fullPath); } File.WriteAllText(fullPath, htmlReport.ToString(), DbUpDefaults.DefaultEncoding); }
public DatabaseUpgradeResult Run(string[] args) { DatabaseUpgradeResult result = null; if (args.Any(a => "--scriptAllDefinitions".Equals(a.Trim(), StringComparison.InvariantCultureIgnoreCase))) { result = ScriptAll(); } else { var scriptsToExecute = m_engine.GetScriptsToExecute(); if (args.Any(a => "--whatIf".Equals(a.Trim(), StringComparison.InvariantCultureIgnoreCase))) { result = new DatabaseUpgradeResult(Enumerable.Empty <SqlScript>(), true, null); this.Log.WriteWarning("WHATIF Mode!"); this.Log.WriteWarning("The following scripts would have been executed:"); scriptsToExecute.ForEach(r => this.Log.WriteWarning(r.Name)); } else { var scripter = new DbObjectScripter(ConnectionString, m_options, Log); scripter.StartWatch(); result = m_engine.PerformUpgrade(); if (args.Any(a => "--fromconsole".Equals(a.Trim(), StringComparison.InvariantCultureIgnoreCase))) { this.Log.WriteInformation("Scripting changed database objects..."); scripter.ScriptWatched(); } } } return(result); }