Repository navigation
Expand file tree
/
Copy pathSqlScriptExecutor.cs
More file actions
54 lines (48 loc) · 2.3 KB
/
Copy pathSqlScriptExecutor.cs
File metadata and controls
54 lines (48 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using DbUp.Engine;
using DbUp.Engine.Output;
using DbUp.Engine.Transactions;
using DbUp.Support;
namespace DbUp.SqlServer
{
/// <summary>
/// An implementation of ScriptExecutor that executes against a SQL Server database.
/// </summary>
public class SqlScriptExecutor : ScriptExecutor
{
/// <summary>
/// Initializes an instance of the <see cref="SqlScriptExecutor"/> class.
/// </summary>
/// <param name="connectionManagerFactory"></param>
/// <param name="log">The logging mechanism.</param>
/// <param name="schema">The schema that contains the table.</param>
/// <param name="variablesEnabled">Function that returns <c>true</c> if variables should be replaced, <c>false</c> otherwise.</param>
/// <param name="scriptPreprocessors">Script Preprocessors in addition to variable substitution</param>
/// <param name="journalFactory">Database journal</param>
public SqlScriptExecutor(Func<IConnectionManager> connectionManagerFactory, Func<IUpgradeLog> log, string schema, Func<bool> variablesEnabled,
IEnumerable<IScriptPreprocessor> scriptPreprocessors, Func<IJournal> journalFactory)
: base(connectionManagerFactory, new SqlServerObjectParser(), log, schema, variablesEnabled, scriptPreprocessors, journalFactory)
{
}
protected override string GetVerifySchemaSql(string schema)
{
return string.Format(@"IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'{0}') Exec('CREATE SCHEMA [{0}]')", Schema);
}
protected override void ExecuteCommandsWithinExceptionHandler(int index, SqlScript script, Action excuteCommand)
{
try
{
excuteCommand();
}
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;
}
}
}
}