17
17
using NHibernate . AdoNet . Util ;
18
18
using NHibernate . Cfg ;
19
19
using NHibernate . Connection ;
20
+ using NHibernate . MultiTenancy ;
20
21
using NHibernate . Util ;
21
22
using Environment = NHibernate . Cfg . Environment ;
22
23
@@ -46,9 +47,11 @@ public partial class SchemaExport
46
47
dropSQL = cfg . GenerateDropSchemaScript ( dialect ) ;
47
48
createSQL = cfg . GenerateSchemaCreationScript ( dialect ) ;
48
49
formatter = ( PropertiesHelper . GetBoolean ( Environment . FormatSql , configProperties , true ) ? FormatStyle . Ddl : FormatStyle . None ) . Formatter ;
50
+ _requireTenantConnection = PropertiesHelper . GetEnum ( Environment . MultiTenant , configProperties , MultiTenancyStrategy . None ) == MultiTenancyStrategy . Database ;
49
51
wasInitialized = true ;
50
52
}
51
53
54
+ //TODO 6.0: Remove (replaced by method with optional connection parameter)
52
55
/// <summary>
53
56
/// Run the schema creation script
54
57
/// </summary>
@@ -65,9 +68,38 @@ public partial class SchemaExport
65
68
{
66
69
return Task . FromCanceled < object > ( cancellationToken ) ;
67
70
}
68
- return ExecuteAsync ( useStdOut , execute , false , cancellationToken ) ;
71
+ return CreateAsync ( useStdOut , execute , null , cancellationToken ) ;
69
72
}
70
73
74
+ /// <summary>
75
+ /// Run the schema creation script
76
+ /// </summary>
77
+ /// <param name="useStdOut"><see langword="true" /> if the ddl should be outputted in the Console.</param>
78
+ /// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
79
+ /// <param name="connection"> Optional explicit connection. Required for multi-tenancy.
80
+ /// Must be an opened connection. The method doesn't close the connection. </param>
81
+ /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
82
+ /// <remarks>
83
+ /// This is a convenience method that calls <see cref="ExecuteAsync(bool, bool, bool,CancellationToken)"/> and sets
84
+ /// the justDrop parameter to false.
85
+ /// </remarks>
86
+ public Task CreateAsync ( bool useStdOut , bool execute , DbConnection connection = null , CancellationToken cancellationToken = default ( CancellationToken ) )
87
+ {
88
+ if ( cancellationToken . IsCancellationRequested )
89
+ {
90
+ return Task . FromCanceled < object > ( cancellationToken ) ;
91
+ }
92
+ try
93
+ {
94
+ return InitConnectionAndExecuteAsync ( GetAction ( useStdOut ) , execute , false , connection , null , cancellationToken ) ;
95
+ }
96
+ catch ( Exception ex )
97
+ {
98
+ return Task . FromException < object > ( ex ) ;
99
+ }
100
+ }
101
+
102
+ //TODO 6.0: Remove (replaced by method with optional connection parameter)
71
103
/// <summary>
72
104
/// Run the schema creation script
73
105
/// </summary>
@@ -84,9 +116,31 @@ public partial class SchemaExport
84
116
{
85
117
return Task . FromCanceled < object > ( cancellationToken ) ;
86
118
}
87
- return ExecuteAsync ( scriptAction , execute , false , cancellationToken ) ;
119
+ return CreateAsync ( scriptAction , execute , null , cancellationToken ) ;
88
120
}
89
121
122
+ /// <summary>
123
+ /// Run the schema creation script
124
+ /// </summary>
125
+ /// <param name="scriptAction"> an action that will be called for each line of the generated ddl.</param>
126
+ /// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
127
+ /// <param name="connection"> Optional explicit connection. Required for multi-tenancy.
128
+ /// Must be an opened connection. The method doesn't close the connection. </param>
129
+ /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
130
+ /// <remarks>
131
+ /// This is a convenience method that calls <see cref="ExecuteAsync(bool, bool, bool,CancellationToken)"/> and sets
132
+ /// the justDrop parameter to false.
133
+ /// </remarks>
134
+ public Task CreateAsync ( Action < string > scriptAction , bool execute , DbConnection connection = null , CancellationToken cancellationToken = default ( CancellationToken ) )
135
+ {
136
+ if ( cancellationToken . IsCancellationRequested )
137
+ {
138
+ return Task . FromCanceled < object > ( cancellationToken ) ;
139
+ }
140
+ return InitConnectionAndExecuteAsync ( scriptAction , execute , false , connection , null , cancellationToken ) ;
141
+ }
142
+
143
+ //TODO 6.0: Remove (replaced by method with optional connection parameter)
90
144
/// <summary>
91
145
/// Run the schema creation script
92
146
/// </summary>
@@ -103,9 +157,31 @@ public partial class SchemaExport
103
157
{
104
158
return Task . FromCanceled < object > ( cancellationToken ) ;
105
159
}
106
- return ExecuteAsync ( null , execute , false , exportOutput , cancellationToken ) ;
160
+ return CreateAsync ( exportOutput , execute , null , cancellationToken ) ;
107
161
}
108
162
163
+ /// <summary>
164
+ /// Run the schema creation script
165
+ /// </summary>
166
+ /// <param name="exportOutput"> if non-null, the ddl will be written to this TextWriter.</param>
167
+ /// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
168
+ /// <param name="connection"> Optional explicit connection. Required for multi-tenancy.
169
+ /// Must be an opened connection. The method doesn't close the connection. </param>
170
+ /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
171
+ /// <remarks>
172
+ /// This is a convenience method that calls <see cref="ExecuteAsync(bool, bool, bool,CancellationToken)"/> and sets
173
+ /// the justDrop parameter to false.
174
+ /// </remarks>
175
+ public Task CreateAsync ( TextWriter exportOutput , bool execute , DbConnection connection = null , CancellationToken cancellationToken = default ( CancellationToken ) )
176
+ {
177
+ if ( cancellationToken . IsCancellationRequested )
178
+ {
179
+ return Task . FromCanceled < object > ( cancellationToken ) ;
180
+ }
181
+ return InitConnectionAndExecuteAsync ( null , execute , false , connection , exportOutput , cancellationToken ) ;
182
+ }
183
+
184
+ //TODO 6.0: Remove (replaced by method with optional connection parameter)
109
185
/// <summary>
110
186
/// Run the drop schema script
111
187
/// </summary>
@@ -122,9 +198,38 @@ public partial class SchemaExport
122
198
{
123
199
return Task . FromCanceled < object > ( cancellationToken ) ;
124
200
}
125
- return ExecuteAsync ( useStdOut , execute , true , cancellationToken ) ;
201
+ return DropAsync ( useStdOut , execute , null , cancellationToken ) ;
202
+ }
203
+
204
+ /// <summary>
205
+ /// Run the drop schema script
206
+ /// </summary>
207
+ /// <param name="useStdOut"><see langword="true" /> if the ddl should be outputted in the Console.</param>
208
+ /// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
209
+ /// <param name="connection"> Optional explicit connection. Required for multi-tenancy.
210
+ /// Must be an opened connection. The method doesn't close the connection. </param>
211
+ /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
212
+ /// <remarks>
213
+ /// This is a convenience method that calls <see cref="ExecuteAsync(bool, bool, bool,CancellationToken)"/> and sets
214
+ /// the justDrop parameter to true.
215
+ /// </remarks>
216
+ public Task DropAsync ( bool useStdOut , bool execute , DbConnection connection = null , CancellationToken cancellationToken = default ( CancellationToken ) )
217
+ {
218
+ if ( cancellationToken . IsCancellationRequested )
219
+ {
220
+ return Task . FromCanceled < object > ( cancellationToken ) ;
221
+ }
222
+ try
223
+ {
224
+ return InitConnectionAndExecuteAsync ( GetAction ( useStdOut ) , execute , true , connection , null , cancellationToken ) ;
225
+ }
226
+ catch ( Exception ex )
227
+ {
228
+ return Task . FromException < object > ( ex ) ;
229
+ }
126
230
}
127
231
232
+ //TODO 6.0: Remove (replaced by method with optional connection parameter)
128
233
/// <summary>
129
234
/// Run the drop schema script
130
235
/// </summary>
@@ -141,7 +246,28 @@ public partial class SchemaExport
141
246
{
142
247
return Task . FromCanceled < object > ( cancellationToken ) ;
143
248
}
144
- return ExecuteAsync ( null , execute , true , exportOutput , cancellationToken ) ;
249
+ return DropAsync ( exportOutput , execute , null , cancellationToken ) ;
250
+ }
251
+
252
+ /// <summary>
253
+ /// Run the drop schema script
254
+ /// </summary>
255
+ /// <param name="exportOutput"> if non-null, the ddl will be written to this TextWriter.</param>
256
+ /// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
257
+ /// <param name="connection"> Optional explicit connection. Required for multi-tenancy.
258
+ /// Must be an opened connection. The method doesn't close the connection. </param>
259
+ /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
260
+ /// <remarks>
261
+ /// This is a convenience method that calls <see cref="ExecuteAsync(Action<string>, bool, bool, TextWriter,CancellationToken)"/> and sets
262
+ /// the justDrop parameter to true.
263
+ /// </remarks>
264
+ public Task DropAsync ( TextWriter exportOutput , bool execute , DbConnection connection = null , CancellationToken cancellationToken = default ( CancellationToken ) )
265
+ {
266
+ if ( cancellationToken . IsCancellationRequested )
267
+ {
268
+ return Task . FromCanceled < object > ( cancellationToken ) ;
269
+ }
270
+ return InitConnectionAndExecuteAsync ( null , execute , true , connection , exportOutput , cancellationToken ) ;
145
271
}
146
272
147
273
private async Task ExecuteInitializedAsync ( Action < string > scriptAction , bool execute , bool throwOnError , TextWriter exportOutput ,
@@ -228,14 +354,7 @@ public Task ExecuteAsync(bool useStdOut, bool execute, bool justDrop, DbConnecti
228
354
}
229
355
try
230
356
{
231
- if ( useStdOut )
232
- {
233
- return ExecuteAsync ( Console . WriteLine , execute , justDrop , connection , exportOutput , cancellationToken ) ;
234
- }
235
- else
236
- {
237
- return ExecuteAsync ( null , execute , justDrop , connection , exportOutput , cancellationToken ) ;
238
- }
357
+ return ExecuteAsync ( GetAction ( useStdOut ) , execute , justDrop , connection , exportOutput , cancellationToken ) ;
239
358
}
240
359
catch ( Exception ex )
241
360
{
@@ -319,14 +438,7 @@ public async Task ExecuteAsync(Action<string> scriptAction, bool execute, bool j
319
438
}
320
439
try
321
440
{
322
- if ( useStdOut )
323
- {
324
- return ExecuteAsync ( Console . WriteLine , execute , justDrop , cancellationToken ) ;
325
- }
326
- else
327
- {
328
- return ExecuteAsync ( null , execute , justDrop , cancellationToken ) ;
329
- }
441
+ return InitConnectionAndExecuteAsync ( GetAction ( useStdOut ) , execute , justDrop , null , null , cancellationToken ) ;
330
442
}
331
443
catch ( Exception ex )
332
444
{
@@ -343,11 +455,19 @@ public async Task ExecuteAsync(Action<string> scriptAction, bool execute, bool j
343
455
return ExecuteAsync ( scriptAction , execute , justDrop , null , cancellationToken ) ;
344
456
}
345
457
346
- public async Task ExecuteAsync ( Action < string > scriptAction , bool execute , bool justDrop , TextWriter exportOutput , CancellationToken cancellationToken = default ( CancellationToken ) )
458
+ public Task ExecuteAsync ( Action < string > scriptAction , bool execute , bool justDrop , TextWriter exportOutput , CancellationToken cancellationToken = default ( CancellationToken ) )
459
+ {
460
+ if ( cancellationToken . IsCancellationRequested )
461
+ {
462
+ return Task . FromCanceled < object > ( cancellationToken ) ;
463
+ }
464
+ return InitConnectionAndExecuteAsync ( scriptAction , execute , justDrop , null , exportOutput , cancellationToken ) ;
465
+ }
466
+
467
+ private async Task InitConnectionAndExecuteAsync ( Action < string > scriptAction , bool execute , bool justDrop , DbConnection connection , TextWriter exportOutput , CancellationToken cancellationToken = default ( CancellationToken ) )
347
468
{
348
469
cancellationToken . ThrowIfCancellationRequested ( ) ;
349
470
await ( InitializeAsync ( cancellationToken ) ) . ConfigureAwait ( false ) ;
350
- DbConnection connection = null ;
351
471
TextWriter fileOutput = exportOutput ;
352
472
IConnectionProvider connectionProvider = null ;
353
473
@@ -358,8 +478,13 @@ public async Task ExecuteAsync(Action<string> scriptAction, bool execute, bool j
358
478
fileOutput = new StreamWriter ( outputFile ) ;
359
479
}
360
480
361
- if ( execute )
481
+ if ( execute && connection == null )
362
482
{
483
+ if ( _requireTenantConnection )
484
+ {
485
+ throw new ArgumentException ( "When Database multi-tenancy is enabled you need to provide explicit connection. Please use overload with connection parameter." ) ;
486
+ }
487
+
363
488
var props = new Dictionary < string , string > ( ) ;
364
489
foreach ( var de in dialect . DefaultProperties )
365
490
{
@@ -393,7 +518,7 @@ public async Task ExecuteAsync(Action<string> scriptAction, bool execute, bool j
393
518
}
394
519
finally
395
520
{
396
- if ( connection != null )
521
+ if ( connectionProvider != null )
397
522
{
398
523
connectionProvider . CloseConnection ( connection ) ;
399
524
connectionProvider . Dispose ( ) ;
0 commit comments