Skip to content

Commit

Permalink
added SupportedDatabasesForDropDatabase extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
scorixear committed Jul 28, 2024
1 parent 461bd38 commit 58837ad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase s
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, string collation) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, string collation) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1, string collation = null) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString, int commandTimeout) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1) { }
}
namespace DbUp.MySql
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase s
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, string collation) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, string collation) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1, string collation = null) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString, int commandTimeout) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1) { }
}
namespace DbUp.MySql
{
Expand Down
63 changes: 63 additions & 0 deletions src/dbup-mysql/MySqlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,69 @@ public static void MySqlDatabase(
}
}

/// <summary>
/// Drop the database specified in the connection string.
/// </summary>
/// <param name="supported">Fluent helper type.</param>
/// <param name="connectionString">The connection string.</param>
public static void MySqlDatabase(
this SupportedDatabasesForDropDatabase supported,
string connectionString)
{
MySqlDatabase(supported, connectionString, new ConsoleUpgradeLog());
}

/// <summary>
/// Drop the database specified in the connection string.
/// </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 dropping a database in case you're encountering a time out in this operation.</param>
public static void MySqlDatabase(
this SupportedDatabasesForDropDatabase supported,
string connectionString,
int commandTimeout)
{
MySqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout);
}

/// <summary>
/// Drop the database specified in the connection string.
/// </summary>
/// <param name="supported">Fluent helper type.</param>
/// <param name="connectionString">The connection string.</param>
/// <param name="logger">The <see cref="DbUp.Engine.Output.IUpgradeLog"/> used to record actions.</param>
/// <param name="timeout">Use this to set the command time out for dropping a database in case you're encountering a time out in this operation.</param>
public static void MySqlDatabase(
this SupportedDatabasesForDropDatabase supported,
string connectionString,
IUpgradeLog logger,
int timeout = -1)
{
GetMysqlConnectionStringBuilder(connectionString, logger, out var masterConnectionString, out var databaseName);
using (var connection = new MySqlConnection(masterConnectionString))
{
connection.Open();
if (!DatabaseExists(connection, databaseName))
return;
var dropDatabaseCommand = new MySqlCommand($"DROP DATABASE @databaseName;", connection)
{
CommandType = CommandType.Text
};
dropDatabaseCommand.Parameters.AddWithValue("@databaseName", databaseName);
using (var command = dropDatabaseCommand)
{
if (timeout >= 0)
{
command.CommandTimeout = timeout;
}

command.ExecuteNonQuery();
}
logger.WriteInformation(@"Dropped database {0}", databaseName);
}
}

static bool DatabaseExists(MySqlConnection connection, string databaseName)
{
var sqlCommandText = string.Format
Expand Down

0 comments on commit 58837ad

Please sign in to comment.