Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DropDatabase #17

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/test-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Test Report
run-name: Generate Test Report for workflow ${{ github.event.workflow_run.name }} run ${{ github.event.workflow_run.run_number }} branch ${{ github.event.workflow_run.head_branch }}

on:
workflow_run:
workflows: ["CI", "build"]
types: [completed]

jobs:
report:
name: Test Report 🧪
uses: DbUp/Universe/.github/workflows/test-report.yml@main
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
Loading