forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemaUpdate.cs
226 lines (205 loc) · 6.02 KB
/
SchemaUpdate.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Common;
using NHibernate.Cfg;
using NHibernate.Util;
using Environment=NHibernate.Cfg.Environment;
using NHibernate.AdoNet.Util;
namespace NHibernate.Tool.hbm2ddl
{
using System.Threading.Tasks;
using System.Threading;
public partial class SchemaUpdate
{
private async Task InitializeAsync(CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
if (wasInitialized)
{
return;
}
string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, configuration.Properties, "not-defined");
if (autoKeyWordsImport == Hbm2DDLKeyWords.AutoQuote)
{
await (SchemaMetadataUpdater.UpdateAsync(configuration, dialect, cancellationToken)).ConfigureAwait(false);
SchemaMetadataUpdater.QuoteTableAndColumns(configuration, dialect);
}
wasInitialized = true;
}
public static async Task MainAsync(string[] args, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
try
{
var cfg = new Configuration();
bool script = true;
// If true then execute db updates, otherwise just generate and display updates
bool doUpdate = true;
//String propFile = null;
for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("--", StringComparison.Ordinal))
{
if (args[i].Equals("--quiet"))
{
script = false;
}
else if (args[i].StartsWith("--properties=", StringComparison.Ordinal))
{
throw new NotSupportedException("No properties file for .NET, use app.config instead");
//propFile = args[i].Substring( 13 );
}
else if (args[i].StartsWith("--config=", StringComparison.Ordinal))
{
cfg.Configure(args[i].Substring(9));
}
else if (args[i].StartsWith("--text", StringComparison.Ordinal))
{
doUpdate = false;
}
else if (args[i].StartsWith("--naming=", StringComparison.Ordinal))
{
cfg.SetNamingStrategy(
(INamingStrategy)
Environment.ObjectsFactory.CreateInstance(ReflectHelper.ClassForName(args[i].Substring(9))));
}
}
else
{
cfg.AddFile(args[i]);
}
}
/* NH: No props file for .NET
* if ( propFile != null ) {
Hashtable props = new Hashtable();
props.putAll( cfg.Properties );
props.load( new FileInputStream( propFile ) );
cfg.SetProperties( props );
}*/
await (new SchemaUpdate(cfg).ExecuteAsync(script, doUpdate, cancellationToken)).ConfigureAwait(false);
}
catch (OperationCanceledException) { throw; }
catch (Exception e)
{
log.Error(e, "Error running schema update");
Console.WriteLine(e);
}
}
/// <summary>
/// Execute the schema updates
/// </summary>
public Task ExecuteAsync(bool useStdOut, bool doUpdate, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (useStdOut)
{
return ExecuteAsync(Console.WriteLine, doUpdate, cancellationToken);
}
else
{
return ExecuteAsync(null, doUpdate, cancellationToken);
}
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
/// <summary>
/// Execute the schema updates
/// </summary>
/// <param name="scriptAction">The action to write the each schema line.</param>
/// <param name="doUpdate">Commit the script to DB</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
public async Task ExecuteAsync(Action<string> scriptAction, bool doUpdate, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
log.Info("Running hbm2ddl schema update");
await (InitializeAsync(cancellationToken)).ConfigureAwait(false);
DbConnection connection;
DbCommand stmt = null;
exceptions.Clear();
try
{
DatabaseMetadata meta;
try
{
log.Info("fetching database metadata");
await (connectionHelper.PrepareAsync(cancellationToken)).ConfigureAwait(false);
connection = connectionHelper.Connection;
meta = new DatabaseMetadata(connection, dialect);
stmt = connection.CreateCommand();
}
catch (OperationCanceledException) { throw; }
catch (Exception sqle)
{
exceptions.Add(sqle);
log.Error(sqle, "could not get database metadata");
throw;
}
log.Info("updating schema");
string[] createSQL = configuration.GenerateSchemaUpdateScript(dialect, meta);
for (int j = 0; j < createSQL.Length; j++)
{
string sql = createSQL[j];
string formatted = formatter.Format(sql);
try
{
if (scriptAction != null)
{
scriptAction(formatted);
}
if (doUpdate)
{
log.Debug(sql);
stmt.CommandText = sql;
await (stmt.ExecuteNonQueryAsync(cancellationToken)).ConfigureAwait(false);
}
}
catch (OperationCanceledException) { throw; }
catch (Exception e)
{
exceptions.Add(e);
log.Error(e, "Unsuccessful: {0}", sql);
}
}
log.Info("schema update complete");
}
catch (OperationCanceledException) { throw; }
catch (Exception e)
{
exceptions.Add(e);
log.Error(e, "could not complete schema update");
}
finally
{
try
{
if (stmt != null)
{
stmt.Dispose();
}
connectionHelper.Release();
}
catch (Exception e)
{
exceptions.Add(e);
log.Error(e, "Error closing connection");
}
}
}
}
}