Skip to content

Commit

Permalink
adding in a default restore option
Browse files Browse the repository at this point in the history
  • Loading branch information
ferventcoder committed Jul 8, 2011
1 parent 35a907f commit d1a25ec
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
48 changes: 48 additions & 0 deletions product/roundhouse.databases.sqlserver/SqlServerDatabase.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -175,5 +199,29 @@ DROP DATABASE [{0}]
database_name);
}

/// <summary>
/// Low level hit to query the database for a restore
/// </summary>
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];
}

}
}
Original file line number Diff line number Diff line change
@@ -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'");
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<Link>Properties\SolutionVersion.cs</Link>
</Compile>
<Compile Include="databases\MySqlDatabaseSpecs.cs" />
<Compile Include="databases\SqlServerDatabaseSpecs.cs" />
<Compile Include="EmptyTestContainer.cs" />
<Compile Include="infrastructure\persistence\NHibernateSessionFactorySpecs.cs" />
<Compile Include="MigrateSpecs.cs" />
Expand Down

0 comments on commit d1a25ec

Please sign in to comment.