From 58837ad55513df36632d9a308da70dd1edc94e26 Mon Sep 17 00:00:00 2001 From: Paul Keller Date: Sun, 28 Jul 2024 09:40:10 +0200 Subject: [PATCH] added SupportedDatabasesForDropDatabase extensions --- .../NoPublicApiChanges.Run.DotNet.verified.cs | 3 + .../NoPublicApiChanges.Run.Net.verified.cs | 3 + src/dbup-mysql/MySqlExtensions.cs | 63 +++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.DotNet.verified.cs b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.DotNet.verified.cs index b25c715..bfc7d4a 100644 --- a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.DotNet.verified.cs +++ b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.DotNet.verified.cs @@ -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 { diff --git a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs index b25c715..bfc7d4a 100644 --- a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs +++ b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs @@ -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 { diff --git a/src/dbup-mysql/MySqlExtensions.cs b/src/dbup-mysql/MySqlExtensions.cs index 478c6e9..843210e 100644 --- a/src/dbup-mysql/MySqlExtensions.cs +++ b/src/dbup-mysql/MySqlExtensions.cs @@ -199,6 +199,69 @@ public static void MySqlDatabase( } } + /// + /// Drop the database specified in the connection string. + /// + /// Fluent helper type. + /// The connection string. + public static void MySqlDatabase( + this SupportedDatabasesForDropDatabase supported, + string connectionString) + { + MySqlDatabase(supported, connectionString, new ConsoleUpgradeLog()); + } + + /// + /// Drop the database specified in the connection string. + /// + /// Fluent helper type. + /// The connection string. + /// Use this to set the command time out for dropping a database in case you're encountering a time out in this operation. + public static void MySqlDatabase( + this SupportedDatabasesForDropDatabase supported, + string connectionString, + int commandTimeout) + { + MySqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), commandTimeout); + } + + /// + /// Drop the database specified in the connection string. + /// + /// Fluent helper type. + /// The connection string. + /// The used to record actions. + /// Use this to set the command time out for dropping a database in case you're encountering a time out in this operation. + 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