Skip to content

Commit

Permalink
Add support for DropDatabase (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
scorixear authored Oct 22, 2024
1 parent 00a6082 commit 1250c8c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ public static DbUp.Builder.UpgradeEngineBuilder MySqlDatabase(this DbUp.Builder.
public static DbUp.Builder.UpgradeEngineBuilder MySqlDatabase(DbUp.Engine.Transactions.IConnectionManager connectionManager, string schema) { }
public static DbUp.Builder.UpgradeEngineBuilder MySqlDatabase(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, string collation) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString, int commandTimeout) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, int commandTimeout, string collation) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForDropDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1) { }
public static void MySqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger, int timeout = -1, string collation = null) { }
}
namespace DbUp.MySql
Expand Down
3 changes: 3 additions & 0 deletions src/dbup-mysql.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SQ/@EntryIndexedValue">SQ</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=4a98fdf6_002D7d98_002D4f5a_002Dafeb_002Dea44ad98c70c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=f9fce829_002De6f4_002D4cb2_002D80f1_002D5497c44f51df/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EPredefinedNamingRulesToUserRulesUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters /&gt;&lt;/data&gt;</s:String>
<s:String x:Key="/Default/FilterSettingsManager/AttributeFilterXml/@EntryValue">&lt;data /&gt;</s:String></wpf:ResourceDictionary>
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.LogInformation("Dropped database {0}", databaseName);
}
}

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

0 comments on commit 1250c8c

Please sign in to comment.