protected virtual string PreprocessScriptContents(SqlScript script, IDictionary <string, string> variables) { if (variables == null) { variables = new Dictionary <string, string>(); } if (Schema != null && !variables.ContainsKey("schema")) { variables.Add("schema", QuoteSqlObjectName(Schema)); } var contents = script.Contents; if (string.IsNullOrEmpty(Schema)) { contents = new StripSchemaPreprocessor().Process(contents); } if (variablesEnabled()) { contents = new VariableSubstitutionPreprocessor(variables).Process(contents); } contents = (scriptPreprocessors ?? new IScriptPreprocessor[0]) .Aggregate(contents, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); return(contents); }
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 string Preprocess(string query) { if (string.IsNullOrEmpty(Schema)) { query = new StripSchemaPreprocessor().Process(query); } if (!string.IsNullOrEmpty(Schema) && !variables.ContainsKey("schema")) { variables.Add("schema", SqlObjectParser.QuoteSqlObjectName(Schema)); } if (variablesEnabled()) { query = new VariableSubstitutionPreprocessor(variables).Process(query); } query = additionalScriptPreprocessors.Aggregate(query, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); return(query); }
/// <summary> /// Executes the specified script against a database at a given connection string. /// </summary> /// <param name="script">The script.</param> /// <param name="variables">Variables to replace in the script</param> public void Execute(SqlScript script, IDictionary <string, string> variables) { if (variables == null) { variables = new Dictionary <string, string>(); } if (Schema != null && !variables.ContainsKey("schema")) { variables.Add("schema", SqlObjectParser.QuoteSqlObjectName(Schema)); } log().WriteInformation("Executing SQL Server script '{0}'", script.Name); var contents = script.Contents; if (string.IsNullOrEmpty(Schema)) { contents = new StripSchemaPreprocessor().Process(contents); } if (variablesEnabled()) { contents = new VariableSubstitutionPreprocessor(variables).Process(contents); } contents = (scriptPreprocessors ?? new IScriptPreprocessor[0]) .Aggregate(contents, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); var scriptStatements = connectionManager().SplitScriptIntoCommands(contents); var index = -1; try { connectionManager().ExecuteCommandsWithManagedConnection(dbCommandFactory => { foreach (var statement in scriptStatements) { index++; using (var command = dbCommandFactory()) { command.CommandText = statement; if (ExecutionTimeoutSeconds != null) { command.CommandTimeout = ExecutionTimeoutSeconds.Value; } command.ExecuteNonQuery(); } } }); } catch (SqlException sqlException) { log().WriteInformation("SQL exception has occured in script: '{0}'", script.Name); log().WriteError("Script block number: {0}; Block line {1}; Message: {2}", index, sqlException.LineNumber, sqlException.Procedure, sqlException.Number, sqlException.Message); log().WriteError(sqlException.ToString()); throw; } catch (DbException sqlException) { log().WriteInformation("DB exception has occured in script: '{0}'", script.Name); log().WriteError("Script block number: {0}; Error code {1}; Message: {2}", index, sqlException.ErrorCode, sqlException.Message); log().WriteError(sqlException.ToString()); throw; } catch (Exception ex) { log().WriteInformation("Exception has occured in script: '{0}'", script.Name); log().WriteError(ex.ToString()); throw; } }
/// <summary> /// Executes the specified script against a database at a given connection string. /// </summary> /// <param name="script">The script.</param> /// <param name="variables">Variables to replace in the script</param> public void Execute(SqlScript script, IDictionary <string, string> variables) { if (variables == null) { variables = new Dictionary <string, string>(); } Logger.WriteInformation("Executing SQL Server script '{0}'", script.Name); var contents = script.Contents; if (VariablesEnabled) { contents = new VariableSubstitutionPreprocessor(variables).Process(contents); } contents = (ScriptPreprocessors ?? new IScriptPreprocessor[0]) .Aggregate(contents, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); var scriptStatements = ConnectionManager.SplitScriptIntoCommands(contents); var statementIndex = journalTable.GetFailedStatementIndex(script); var successfullStatements = scriptStatements.Take(statementIndex).ToList(); var index = -1; if (statementIndex > 0) { if (!journalTable.ValidateExecutedScript(script, successfullStatements)) { throw new Exception( $"Invalid change of script {script.Name}. Successfully executed parts of previously failed script has been changed."); } scriptStatements = scriptStatements.Skip(statementIndex); index = statementIndex - 1; } var executingStatement = string.Empty; try { ConnectionManager.ExecuteCommandsWithManagedConnection(dbCommandFactory => { foreach (var statement in scriptStatements) { executingStatement = statement; index++; using (var command = dbCommandFactory()) { command.CommandText = statement; if (ExecutionTimeoutSeconds != null) { command.CommandTimeout = ExecutionTimeoutSeconds.Value; } if (ConnectionManager.IsScriptOutputLogged) { using (var reader = command.ExecuteReader()) { Log(reader); } } else { command.ExecuteNonQuery(); } } } }); } catch (SqlException sqlException) { Logger.WriteInformation("SQL exception has occured in script: '{0}'", script.Name); Logger.WriteError("Script block number: {0}; Block line: {1}; Procedure: {2};{5}SQL Exception Number: {3}; Message: {4}{5}", index, sqlException.LineNumber, sqlException.Procedure, sqlException.Number, sqlException.Message, Environment.NewLine); if (ConnectionManager.IsScriptOutputLogged) { Logger.WriteInformation(executingStatement + Environment.NewLine); } journalTable.StoreExecutedScript(script, index, sqlException.Message); throw; } catch (DbException sqlException) { Logger.WriteInformation("DB exception has occured in script: '{0}'", script.Name); Logger.WriteError("Script block number: {0}; Error code {1}; Message: {2}", index, sqlException.ErrorCode, sqlException.Message); if (ConnectionManager.IsScriptOutputLogged) { Logger.WriteInformation(executingStatement + Environment.NewLine); } journalTable.StoreExecutedScript(script, index, sqlException.Message); throw; } catch (Exception ex) { Logger.WriteInformation("Exception has occured in script: '{0}'", script.Name); Logger.WriteError(ex.ToString()); if (ConnectionManager.IsScriptOutputLogged) { Logger.WriteInformation(executingStatement + Environment.NewLine); } throw; } }