-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathScriptingUpgrader.cs
More file actions
152 lines (132 loc) · 4.88 KB
/
Copy pathScriptingUpgrader.cs
File metadata and controls
152 lines (132 loc) · 4.88 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using DbUp.Builder;
using DbUp.Engine;
using DbUp.Engine.Output;
using DbUp.Support.SqlServer.Scripting;
using System.Reflection;
namespace DbUp;
public class ScriptingUpgrader
{
UpgradeEngine m_engine;
Options m_options;
public ScriptingUpgrader(
string connectionString,
UpgradeEngine engine,
Options options
)
{
m_engine = engine;
m_options = options;
m_connectionString = connectionString;
}
public ScriptingUpgrader(
string connectionString,
UpgradeEngine engine
)
: this(connectionString, engine, new Options())
{
}
UpgradeConfiguration m_configuration;
UpgradeConfiguration UpgradeConfiguration
{
get
{
if (m_configuration == null)
{
//configuration field on UpgradeEngine is private so we need to use reflection to break in
var field = typeof(UpgradeEngine).GetField("configuration",
BindingFlags.NonPublic | BindingFlags.Instance);
m_configuration = (UpgradeConfiguration)field.GetValue(m_engine);
}
return m_configuration;
}
}
IUpgradeLog m_log;
IUpgradeLog Log
{
get
{
if (m_log == null)
{
m_log = this.UpgradeConfiguration.Log;
}
return m_log;
}
}
readonly string m_connectionString;
string ConnectionString
{
get
{
/* if (m_connectionString == null)
{
var connectionManager = this.UpgradeConfiguration.ConnectionManager;
if (connectionManager is SqlConnectionManager)
{
// this does not work in dbup 3.2.4
//connectionString field is private on SqlConnectionManager so we need to use reflection to break in
var field = typeof(SqlConnectionManager).GetField("connectionString", BindingFlags.NonPublic | BindingFlags.Instance);
m_connectionString = (string)field.GetValue((SqlConnectionManager)connectionManager);
}
}
*/
return m_connectionString;
}
}
public DatabaseUpgradeResult ScriptAll()
{
this.Log.LogInformation("Scripting all database object definitions...");
if (this.ConnectionString == null)
{
return new DatabaseUpgradeResult([], false,
new Exception("connectionString could not be determined"), null);
}
var scripter = new DbObjectScripter(this.ConnectionString, m_options, this.Log);
scripter.ScriptAll();
return new DatabaseUpgradeResult(new List<SqlScript>(), true, null, null);
}
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([], true, null, null);
this.Log.LogWarning("WHATIF Mode!");
this.Log.LogWarning("The following scripts would have been executed:");
scriptsToExecute.ForEach(r => this.Log.LogWarning(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.LogInformation("Scripting changed database objects...");
var scriptorResult = scripter.ScriptMigrationTargets(scriptsToExecute);
}
else
{
this.Log.LogInformation("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;
}
}