11using System ;
2+ using System . Collections . Generic ;
23using System . Data ;
34using Microsoft . Data . SqlClient ;
45using DbUp ;
@@ -110,10 +111,10 @@ public static UpgradeEngineBuilder JournalToSqlTable(this UpgradeEngineBuilder b
110111 /// </summary>
111112 /// <param name="supported">Fluent helper type.</param>
112113 /// <param name="connectionString">The connection string.</param>
113- /// <returns></returns>
114- public static void SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString )
114+ /// <returns>True if the database was (or would've been) created; otherwise false. </returns>
115+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString )
115116 {
116- SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) ) ;
117+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) ) ;
117118 }
118119
119120 /// <summary>
@@ -122,10 +123,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
122123 /// <param name="supported">Fluent helper type.</param>
123124 /// <param name="connectionString">The connection string.</param>
124125 /// <param name="azureDatabaseEdition">Azure edition to Create</param>
125- /// <returns></returns>
126- public static void SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , AzureDatabaseEdition azureDatabaseEdition )
126+ /// <returns>True if the database was (or would've been) created; otherwise false. </returns>
127+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , AzureDatabaseEdition azureDatabaseEdition )
127128 {
128- SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , - 1 , azureDatabaseEdition ) ;
129+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , - 1 , azureDatabaseEdition ) ;
129130 }
130131
131132 /// <summary>
@@ -134,10 +135,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
134135 /// <param name="supported">Fluent helper type.</param>
135136 /// <param name="connectionString">The connection string.</param>
136137 /// <param name="commandTimeout">Use this to set the command time out for creating a database in case you're encountering a time out in this operation.</param>
137- /// <returns></returns>
138- public static void SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout )
138+ /// <returns>True if the database was (or would've been) created; otherwise false. </returns>
139+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout )
139140 {
140- SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout ) ;
141+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout ) ;
141142 }
142143
143144 /// <summary>
@@ -146,10 +147,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
146147 /// <param name="supported">Fluent helper type.</param>
147148 /// <param name="connectionString">The connection string.</param>
148149 /// <param name="collation">The collation name to set during database creation</param>
149- /// <returns></returns>
150- public static void SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , string collation )
150+ /// <returns>True if the database was (or would've been) created; otherwise false. </returns>
151+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , string collation )
151152 {
152- SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , collation : collation ) ;
153+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , collation : collation ) ;
153154 }
154155
155156 /// <summary>
@@ -159,10 +160,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
159160 /// <param name="connectionString">The connection string.</param>
160161 /// <param name="commandTimeout">Use this to set the command time out for creating a database in case you're encountering a time out in this operation.</param>
161162 /// <param name="azureDatabaseEdition">Azure edition to Create</param>
162- /// <returns></returns>
163- public static void SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout , AzureDatabaseEdition azureDatabaseEdition )
163+ /// <returns>True if the database was (or would've been) created; otherwise false. </returns>
164+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout , AzureDatabaseEdition azureDatabaseEdition )
164165 {
165- SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout , azureDatabaseEdition ) ;
166+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout , azureDatabaseEdition ) ;
166167 }
167168
168169 /// <summary>
@@ -172,10 +173,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
172173 /// <param name="connectionString">The connection string.</param>
173174 /// <param name="commandTimeout">Use this to set the command time out for creating a database in case you're encountering a time out in this operation.</param>
174175 /// <param name="collation">The collation name to set during database creation</param>
175- /// <returns></returns>
176- public static void SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout , string collation )
176+ /// <returns>True if the database was (or would've been) created; otherwise false. </returns>
177+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout , string collation )
177178 {
178- SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout , collation : collation ) ;
179+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout , collation : collation ) ;
179180 }
180181
181182 /// <summary>
@@ -185,10 +186,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
185186 /// <param name="connectionString">The connection string.</param>
186187 /// <param name="azureDatabaseEdition">Azure edition to Create</param>
187188 /// <param name="collation">The collation name to set during database creation</param>
188- /// <returns></returns>
189- public static void SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , AzureDatabaseEdition azureDatabaseEdition , string collation )
189+ /// <returns>True if the database was (or would've been) created; otherwise false. </returns>
190+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , AzureDatabaseEdition azureDatabaseEdition , string collation )
190191 {
191- SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , azureDatabaseEdition : azureDatabaseEdition , collation : collation ) ;
192+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , azureDatabaseEdition : azureDatabaseEdition , collation : collation ) ;
192193 }
193194
194195 /// <summary>
@@ -199,10 +200,26 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
199200 /// <param name="commandTimeout">Use this to set the command time out for creating a database in case you're encountering a time out in this operation.</param>
200201 /// <param name="azureDatabaseEdition">Azure edition to Create</param>
201202 /// <param name="collation">The collation name to set during database creation</param>
202- /// <returns></returns>
203- public static void SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout , AzureDatabaseEdition azureDatabaseEdition , string collation )
203+ /// <returns>True if the database was (or would've been) created; otherwise false. </returns>
204+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout , AzureDatabaseEdition azureDatabaseEdition , string collation )
204205 {
205- SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout , azureDatabaseEdition , collation ) ;
206+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout , azureDatabaseEdition , collation ) ;
207+ }
208+
209+ /// <summary>
210+ /// Ensures that the database specified in the connection string exists.
211+ /// </summary>
212+ /// <param name="supported">Fluent helper type.</param>
213+ /// <param name="connectionString">The connection string.</param>
214+ /// <param name="commandTimeout">Use this to set the command time out for creating a database in case you're encountering a time out in this operation.</param>
215+ /// <param name="azureDatabaseEdition">Azure edition to Create</param>
216+ /// <param name="collation">The collation name to set during database creation</param>
217+ /// <param name="createDbSqlCommands">A list of custom SQL commands that will be used to create the database.</param>
218+ /// <param name="checkOnly">If true, only checks whether the database would've been created but does not perform the creation.</param>
219+ /// <returns>True if the database was (or would've been) created; otherwise false.</returns>
220+ public static bool SqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , int commandTimeout = - 1 , AzureDatabaseEdition azureDatabaseEdition = AzureDatabaseEdition . None , string collation = null , IList < string > createDbSqlCommands = null , bool checkOnly = false )
221+ {
222+ return SqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , commandTimeout , azureDatabaseEdition , collation , createDbSqlCommands , checkOnly ) ;
206223 }
207224
208225 /// <summary>
@@ -214,14 +231,18 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
214231 /// <param name="timeout">Use this to set the command time out for creating a database in case you're encountering a time out in this operation.</param>
215232 /// <param name="azureDatabaseEdition">Use to indicate that the SQL server database is in Azure</param>
216233 /// <param name="collation">The collation name to set during database creation</param>
217- /// <returns></returns>
218- public static void SqlDatabase (
234+ /// <param name="createDbSqlCommands">A list of custom SQL commands that will be used to create the database.</param>
235+ /// <param name="checkOnly">If true, only checks whether the database would've been created but does not perform the creation.</param>
236+ /// <returns>True if the database was (or would've been) created; otherwise false.</returns>
237+ public static bool SqlDatabase (
219238 this SupportedDatabasesForEnsureDatabase supported ,
220239 string connectionString ,
221240 IUpgradeLog logger ,
222241 int timeout = - 1 ,
223242 AzureDatabaseEdition azureDatabaseEdition = AzureDatabaseEdition . None ,
224- string collation = null )
243+ string collation = null ,
244+ IList < string > createDbSqlCommands = null ,
245+ bool checkOnly = false )
225246 {
226247 GetMasterConnectionStringBuilder ( connectionString , logger , out var masterConnectionString , out var databaseName ) ;
227248
@@ -235,48 +256,62 @@ public static void SqlDatabase(
235256 {
236257 // Failed to connect to master, lets try direct
237258 if ( DatabaseExistsIfConnectedToDirectly ( logger , connectionString , databaseName ) )
238- return ;
259+ return false ;
239260
240261 throw ;
241262 }
242263
243264 if ( DatabaseExists ( connection , databaseName ) )
244- return ;
265+ return false ;
245266
246- var collationString = string . IsNullOrEmpty ( collation ) ? "" : $@ " COLLATE { collation } " ;
247- var sqlCommandText = $@ "create database [ { databaseName } ] { collationString } " ;
267+ if ( checkOnly )
268+ return true ;
248269
249- switch ( azureDatabaseEdition )
270+ if ( createDbSqlCommands == null )
250271 {
251- case AzureDatabaseEdition . None :
252- sqlCommandText += ";" ;
253- break ;
254- case AzureDatabaseEdition . Basic :
255- sqlCommandText += " ( EDITION = ''basic'' );" ;
256- break ;
257- case AzureDatabaseEdition . Standard :
258- sqlCommandText += " ( EDITION = ''standard'' );" ;
259- break ;
260- case AzureDatabaseEdition . Premium :
261- sqlCommandText += " ( EDITION = ''premium'' );" ;
262- break ;
272+ createDbSqlCommands = new List < string > ( ) ;
273+ var collationString = string . IsNullOrEmpty ( collation ) ? "" : $@ " COLLATE { collation } ";
274+ var sqlCommandText = $@ "create database [{ databaseName } ]{ collationString } ";
275+
276+ switch ( azureDatabaseEdition )
277+ {
278+ case AzureDatabaseEdition . None :
279+ sqlCommandText += ";" ;
280+ break ;
281+ case AzureDatabaseEdition . Basic :
282+ sqlCommandText += " ( EDITION = ''basic'' );" ;
283+ break ;
284+ case AzureDatabaseEdition . Standard :
285+ sqlCommandText += " ( EDITION = ''standard'' );" ;
286+ break ;
287+ case AzureDatabaseEdition . Premium :
288+ sqlCommandText += " ( EDITION = ''premium'' );" ;
289+ break ;
290+ }
291+
292+ createDbSqlCommands . Add ( sqlCommandText ) ;
263293 }
264294
265295 // Create the database...
266- using ( var command = new SqlCommand ( sqlCommandText , connection )
296+ foreach ( var sqlCommandText in createDbSqlCommands )
267297 {
268- CommandType = CommandType . Text
269- } )
270- {
271- if ( timeout >= 0 )
298+ using ( var command = new SqlCommand ( sqlCommandText , connection )
272299 {
273- command . CommandTimeout = timeout ;
274- }
300+ CommandType = CommandType . Text
301+ } )
302+ {
303+ if ( timeout >= 0 )
304+ {
305+ command . CommandTimeout = timeout ;
306+ }
275307
276- command . ExecuteNonQuery ( ) ;
308+ command . ExecuteNonQuery ( ) ;
309+ }
277310 }
278311
279312 logger . LogInformation ( @"Created database {0}" , databaseName ) ;
313+
314+ return true ;
280315 }
281316 }
282317
0 commit comments