Skip to content

Commit

Permalink
Allow custom DB creation script to be passed.
Browse files Browse the repository at this point in the history
  • Loading branch information
jez9999 authored and droyad committed Oct 22, 2024
1 parent 32c112f commit 1363fa3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 62 deletions.
21 changes: 11 additions & 10 deletions src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[assembly: System.CLSCompliantAttribute(false)]
[assembly: System.CLSCompliantAttribute(false)]
[assembly: System.Runtime.InteropServices.ComVisibleAttribute(false)]
[assembly: System.Runtime.InteropServices.GuidAttribute("8190b40b-ac5b-414f-8a00-9b6a2c12b010")]

Expand All @@ -9,23 +9,24 @@ public static DbUp.Builder.UpgradeEngineBuilder AzureSqlDatabaseWithIntegratedSe
public static class SqlServerExtensions
{
public static DbUp.Builder.UpgradeEngineBuilder JournalToSqlTable(this DbUp.Builder.UpgradeEngineBuilder builder, string schema, string table) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, string collation) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, string collation) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition, string collation) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition, string collation) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout = -1, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition = 0, string collation = null, System.Collections.Generic.IList<string> createDbSqlCommands = null, bool checkOnly = false) { }
public static bool SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition = 0, string collation = null, System.Collections.Generic.IList<string> createDbSqlCommands = null, bool checkOnly = false) { }
public static DbUp.Builder.UpgradeEngineBuilder SqlDatabase(this DbUp.Builder.SupportedDatabases supported, string connectionString) { }
public static DbUp.Builder.UpgradeEngineBuilder SqlDatabase(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema) { }
public static DbUp.Builder.UpgradeEngineBuilder SqlDatabase(this DbUp.Builder.SupportedDatabases supported, DbUp.Engine.Transactions.IConnectionManager connectionManager, string schema = null) { }
[System.ObsoleteAttribute("Use "AzureSqlDatabaseWithIntegratedSecurity(this SupportedDatabases, string, string)" if passing "true" to "useAzureSqlIntegratedSecurity".")]
public static DbUp.Builder.UpgradeEngineBuilder SqlDatabase(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema, bool useAzureSqlIntegratedSecurity) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, string collation) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString, int commandTimeout) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, string collation) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition, string collation) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition, string collation) { }
public static void SqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1, DbUp.SqlServer.AzureDatabaseEdition azureDatabaseEdition = 0, string collation = null) { }
}
namespace DbUp.SqlServer
{
Expand Down
139 changes: 87 additions & 52 deletions src/dbup-sqlserver/SqlServerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Data.SqlClient;
using DbUp;
Expand Down Expand Up @@ -110,10 +111,10 @@ public static UpgradeEngineBuilder JournalToSqlTable(this UpgradeEngineBuilder b
/// </summary>
/// <param name="supported">Fluent helper type.</param>
/// <param name="connectionString">The connection string.</param>
/// <returns></returns>
public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString)
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString)
{
SqlDatabase(supported, connectionString, new ConsoleUpgradeLog());
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog());
}

/// <summary>
Expand All @@ -122,10 +123,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
/// <param name="supported">Fluent helper type.</param>
/// <param name="connectionString">The connection string.</param>
/// <param name="azureDatabaseEdition">Azure edition to Create</param>
/// <returns></returns>
public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, AzureDatabaseEdition azureDatabaseEdition)
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, AzureDatabaseEdition azureDatabaseEdition)
{
SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), -1, azureDatabaseEdition);
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), -1, azureDatabaseEdition);
}

/// <summary>
Expand All @@ -134,10 +135,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
/// <param name="supported">Fluent helper type.</param>
/// <param name="connectionString">The connection string.</param>
/// <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>
/// <returns></returns>
public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout)
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout)
{
SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout);
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout);
}

/// <summary>
Expand All @@ -146,10 +147,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
/// <param name="supported">Fluent helper type.</param>
/// <param name="connectionString">The connection string.</param>
/// <param name="collation">The collation name to set during database creation</param>
/// <returns></returns>
public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, string collation)
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, string collation)
{
SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), collation: collation);
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), collation: collation);
}

/// <summary>
Expand All @@ -159,10 +160,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
/// <param name="connectionString">The connection string.</param>
/// <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>
/// <param name="azureDatabaseEdition">Azure edition to Create</param>
/// <returns></returns>
public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, AzureDatabaseEdition azureDatabaseEdition)
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, AzureDatabaseEdition azureDatabaseEdition)
{
SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout, azureDatabaseEdition);
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout, azureDatabaseEdition);
}

/// <summary>
Expand All @@ -172,10 +173,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
/// <param name="connectionString">The connection string.</param>
/// <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>
/// <param name="collation">The collation name to set during database creation</param>
/// <returns></returns>
public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, string collation)
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, string collation)
{
SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout, collation: collation);
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout, collation: collation);
}

/// <summary>
Expand All @@ -185,10 +186,10 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
/// <param name="connectionString">The connection string.</param>
/// <param name="azureDatabaseEdition">Azure edition to Create</param>
/// <param name="collation">The collation name to set during database creation</param>
/// <returns></returns>
public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, AzureDatabaseEdition azureDatabaseEdition, string collation)
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, AzureDatabaseEdition azureDatabaseEdition, string collation)
{
SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), azureDatabaseEdition: azureDatabaseEdition, collation: collation);
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), azureDatabaseEdition: azureDatabaseEdition, collation: collation);
}

/// <summary>
Expand All @@ -199,10 +200,26 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
/// <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>
/// <param name="azureDatabaseEdition">Azure edition to Create</param>
/// <param name="collation">The collation name to set during database creation</param>
/// <returns></returns>
public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, AzureDatabaseEdition azureDatabaseEdition, string collation)
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, AzureDatabaseEdition azureDatabaseEdition, string collation)
{
SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout, azureDatabaseEdition, collation);
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout, azureDatabaseEdition, collation);
}

/// <summary>
/// Ensures that the database specified in the connection string exists.
/// </summary>
/// <param name="supported">Fluent helper type.</param>
/// <param name="connectionString">The connection string.</param>
/// <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>
/// <param name="azureDatabaseEdition">Azure edition to Create</param>
/// <param name="collation">The collation name to set during database creation</param>
/// <param name="createDbSqlCommands">A list of custom SQL commands that will be used to create the database.</param>
/// <param name="checkOnly">If true, only checks whether the database would've been created but does not perform the creation.</param>
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
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)
{
return SqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout, azureDatabaseEdition, collation, createDbSqlCommands, checkOnly);
}

/// <summary>
Expand All @@ -214,14 +231,18 @@ public static void SqlDatabase(this SupportedDatabasesForEnsureDatabase supporte
/// <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>
/// <param name="azureDatabaseEdition">Use to indicate that the SQL server database is in Azure</param>
/// <param name="collation">The collation name to set during database creation</param>
/// <returns></returns>
public static void SqlDatabase(
/// <param name="createDbSqlCommands">A list of custom SQL commands that will be used to create the database.</param>
/// <param name="checkOnly">If true, only checks whether the database would've been created but does not perform the creation.</param>
/// <returns>True if the database was (or would've been) created; otherwise false.</returns>
public static bool SqlDatabase(
this SupportedDatabasesForEnsureDatabase supported,
string connectionString,
IUpgradeLog logger,
int timeout = -1,
AzureDatabaseEdition azureDatabaseEdition = AzureDatabaseEdition.None,
string collation = null)
string collation = null,
IList<string> createDbSqlCommands = null,
bool checkOnly = false)
{
GetMasterConnectionStringBuilder(connectionString, logger, out var masterConnectionString, out var databaseName);

Expand All @@ -235,48 +256,62 @@ public static void SqlDatabase(
{
// Failed to connect to master, lets try direct
if (DatabaseExistsIfConnectedToDirectly(logger, connectionString, databaseName))
return;
return false;

throw;
}

if (DatabaseExists(connection, databaseName))
return;
return false;

var collationString = string.IsNullOrEmpty(collation) ? "" : $@" COLLATE {collation}";
var sqlCommandText = $@"create database [{databaseName}]{collationString}";
if (checkOnly)
return true;

switch (azureDatabaseEdition)
if (createDbSqlCommands == null)
{
case AzureDatabaseEdition.None:
sqlCommandText += ";";
break;
case AzureDatabaseEdition.Basic:
sqlCommandText += " ( EDITION = ''basic'' );";
break;
case AzureDatabaseEdition.Standard:
sqlCommandText += " ( EDITION = ''standard'' );";
break;
case AzureDatabaseEdition.Premium:
sqlCommandText += " ( EDITION = ''premium'' );";
break;
createDbSqlCommands = new List<string>();
var collationString = string.IsNullOrEmpty(collation) ? "" : $@" COLLATE {collation}";
var sqlCommandText = $@"create database [{databaseName}]{collationString}";

switch (azureDatabaseEdition)
{
case AzureDatabaseEdition.None:
sqlCommandText += ";";
break;
case AzureDatabaseEdition.Basic:
sqlCommandText += " ( EDITION = ''basic'' );";
break;
case AzureDatabaseEdition.Standard:
sqlCommandText += " ( EDITION = ''standard'' );";
break;
case AzureDatabaseEdition.Premium:
sqlCommandText += " ( EDITION = ''premium'' );";
break;
}

createDbSqlCommands.Add(sqlCommandText);
}

// Create the database...
using (var command = new SqlCommand(sqlCommandText, connection)
foreach (var sqlCommandText in createDbSqlCommands)
{
CommandType = CommandType.Text
})
{
if (timeout >= 0)
using (var command = new SqlCommand(sqlCommandText, connection)
{
command.CommandTimeout = timeout;
}
CommandType = CommandType.Text
})
{
if (timeout >= 0)
{
command.CommandTimeout = timeout;
}

command.ExecuteNonQuery();
command.ExecuteNonQuery();
}
}

logger.LogInformation(@"Created database {0}", databaseName);

return true;
}
}

Expand Down

0 comments on commit 1363fa3

Please sign in to comment.