From d1a25ec9a61af034bab9aa7172682f3807b872f9 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Fri, 8 Jul 2011 16:21:05 -0500 Subject: [PATCH] adding in a default restore option --- .../SqlServerDatabase.cs | 48 ++++++++ .../databases/SqlServerDatabaseSpecs.cs | 110 ++++++++++++++++++ .../roundhouse.tests.integration.csproj | 1 + 3 files changed, 159 insertions(+) create mode 100644 product/roundhouse.tests.integration/databases/SqlServerDatabaseSpecs.cs diff --git a/product/roundhouse.databases.sqlserver/SqlServerDatabase.cs b/product/roundhouse.databases.sqlserver/SqlServerDatabase.cs index 5bbb82f5..9d599e31 100644 --- a/product/roundhouse.databases.sqlserver/SqlServerDatabase.cs +++ b/product/roundhouse.databases.sqlserver/SqlServerDatabase.cs @@ -1,6 +1,9 @@ namespace roundhouse.databases.sqlserver { using System; + using System.Data; + using System.Data.SqlClient; + using System.Text; using System.Text.RegularExpressions; using infrastructure.app; using infrastructure.extensions; @@ -124,6 +127,8 @@ CREATE DATABASE [{0}] END ", database_name); + + // ALTER DATABASE [{0}] MODIFY FILE ( NAME = N'{0}', FILEGROWTH = 10240KB ) } public override string set_recovery_mode_script(bool simple) @@ -142,6 +147,10 @@ public override string restore_database_script(string restore_from_path, string { restore_options = custom_restore_options.to_lower().StartsWith(",") ? custom_restore_options : ", " + custom_restore_options; } + else + { + restore_options = get_default_restore_move_options(); + } return string.Format( @"USE master @@ -162,6 +171,21 @@ WITH NOUNLOAD ); } + public string get_default_restore_move_options() + { + StringBuilder restore_options = new StringBuilder(); + DataTable dt = execute_datatable("select [name],[physical_name] from sys.database_files"); + if (dt != null && dt.Rows.Count != 0) + { + foreach (DataRow row in dt.Rows) + { + restore_options.AppendFormat(", MOVE '{0}' TO '{1}'", row["name"], row["physical_name"]); + } + } + + return restore_options.ToString(); + } + public override string delete_database_script() { return string.Format( @@ -175,5 +199,29 @@ DROP DATABASE [{0}] database_name); } + /// + /// Low level hit to query the database for a restore + /// + private DataTable execute_datatable(string sql_to_run) + { + DataSet result = new DataSet(); + + using (IDbCommand command = setup_database_command(sql_to_run,ConnectionType.Default,null)) + { + using (IDataReader data_reader = command.ExecuteReader()) + { + DataTable data_table = new DataTable(); + data_table.Load(data_reader); + data_reader.Close(); + data_reader.Dispose(); + + result.Tables.Add(data_table); + } + command.Dispose(); + } + + return result.Tables.Count == 0 ? null : result.Tables[0]; + } + } } \ No newline at end of file diff --git a/product/roundhouse.tests.integration/databases/SqlServerDatabaseSpecs.cs b/product/roundhouse.tests.integration/databases/SqlServerDatabaseSpecs.cs new file mode 100644 index 00000000..f49017cf --- /dev/null +++ b/product/roundhouse.tests.integration/databases/SqlServerDatabaseSpecs.cs @@ -0,0 +1,110 @@ +namespace roundhouse.tests.integration.databases +{ + using bdddoc.core; + using developwithpassion.bdd.contexts; + using developwithpassion.bdd.mbunit; + using developwithpassion.bdd.mbunit.standard; + using developwithpassion.bdd.mbunit.standard.observations; + using roundhouse.databases.mysql; + using roundhouse.databases.sqlserver; + using roundhouse.infrastructure.app; + using roundhouse.infrastructure.logging.custom; + + public class SqlServerDatabaseSpecs + { + public abstract class concern_for_SqlServerDatabase : observations_for_a_static_sut + { + protected static string database_name = "TestRoundhousE"; + protected static string sql_files_folder = @"..\..\..\..\db\SqlServer\TestRoundhousE"; + + private after_all_observations after = () => + { + new Migrate().Set(p => + { + p.DatabaseName = database_name; + p.SqlFilesDirectory = sql_files_folder; + p.Drop = true; + p.Silent = true; + }).Run(); + }; + } + + [Concern(typeof(SqlServerDatabase))] + public class when_running_the_migrator_with_sqlserver : concern_for_SqlServerDatabase + { + protected static object result; + + private context c = () => { }; + + private because b = () => + { + new Migrate().Set(p => + { + p.Logger = new ConsoleLogger(); + p.DatabaseName = database_name; + p.SqlFilesDirectory = sql_files_folder; + p.Silent = true; + }).Run(); + }; + + [Observation] + public void should_successfully_run() + { + //nothing needed here + } + } + + [Concern(typeof(SqlServerDatabase))] + public class when_getting_the_default_restore_move_options_for_SqlServer_prior_to_a_restore : concern_for_SqlServerDatabase + { + protected static SqlServerDatabase db = new SqlServerDatabase(); + protected static Migrate migrator; + + protected static string result; + + private context c = () => { + migrator = new Migrate().Set(p => { + p.Logger = new ConsoleLogger(); + p.DatabaseName = database_name; + p.SqlFilesDirectory = sql_files_folder; + p.Silent = true; + }); + migrator.Run(); + ConfigurationPropertyHolder configuration_property_holder = migrator.GetConfiguration(); + ApplicationConfiguraton.set_defaults_if_properties_are_not_set(configuration_property_holder); + db.configuration = configuration_property_holder; + db.server_name = configuration_property_holder.ServerName ?? string.Empty; + db.database_name = configuration_property_holder.DatabaseName ?? string.Empty; + db.connection_string = configuration_property_holder.ConnectionString; + db.admin_connection_string = configuration_property_holder.ConnectionStringAdmin; + db.roundhouse_schema_name = configuration_property_holder.SchemaName; + db.version_table_name = configuration_property_holder.VersionTableName; + db.scripts_run_table_name = configuration_property_holder.ScriptsRunTableName; + db.scripts_run_errors_table_name = configuration_property_holder.ScriptsRunErrorsTableName; + db.custom_create_database_script = configuration_property_holder.CreateDatabaseCustomScript; + db.command_timeout = configuration_property_holder.CommandTimeout; + db.admin_command_timeout = configuration_property_holder.CommandTimeoutAdmin; + db.restore_timeout = configuration_property_holder.RestoreTimeout; + db.initialize_connections(configuration_property_holder); + }; + + private because b = () => { + result = db.get_default_restore_move_options(); + }; + + [Observation] + public void should_successfully_run() + { + //nothing needed here + } + + [Observation] + public void should_have_the_correct_default_restore_options() + { + //NOTE: this is not conclusive since this could vary from system to system depending on where you store stuff. This test needs some work to make it go to the database. + result.should_be_equal_to(@", MOVE 'TestRoundhousE' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestRoundhousE.mdf', MOVE 'TestRoundhousE_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestRoundhousE_log.LDF'"); + } + + } + } +} \ No newline at end of file diff --git a/product/roundhouse.tests.integration/roundhouse.tests.integration.csproj b/product/roundhouse.tests.integration/roundhouse.tests.integration.csproj index 1186b61e..c804cbfc 100644 --- a/product/roundhouse.tests.integration/roundhouse.tests.integration.csproj +++ b/product/roundhouse.tests.integration/roundhouse.tests.integration.csproj @@ -125,6 +125,7 @@ Properties\SolutionVersion.cs +