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

Create database custom script to handle file path #17

Merged
merged 2 commits into from
Sep 22, 2011
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions db/SQLServer/TestRoundhousE/custom_db_create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
USE master
IF NOT EXISTS(SELECT * FROM sys.databases WHERE [name] = '{{DatabaseName}}')
BEGIN
CREATE DATABASE {{DatabaseName}}
END
15 changes: 15 additions & 0 deletions deployment/templates/DBDeployment.DropCustomCreate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off

SET DIR=%~d0%~p0%

SET database.name="${database.name}"
SET sql.files.directory="%DIR%${dirs.db}\SQLServer\${database.name}"
SET server.database="${server.database}"
SET repository.path="${repository.path}"
SET version.file="${file.version}"
SET version.xpath="//buildInfo/version"
SET environment=${environment}
SET custom.create.script="USE master;IF NOT EXISTS(SELECT * FROM sys.databases WHERE [name] = '{{DatabaseName}}')BEGIN; CREATE DATABASE {{DatabaseName}}; END;"

"%DIR%Console\rh.exe" /d=%database.name% /f=%sql.files.directory% /s=%server.database% /vf=%version.file% /vx=%version.xpath% /r=%repository.path% /env=%environment% /drop
"%DIR%Console\rh.exe" /d=%database.name% /f=%sql.files.directory% /s=%server.database% /cds=%custom.create.script% /vf=%version.file% /vx=%version.xpath% /r=%repository.path% /env=%environment% /simple
14 changes: 14 additions & 0 deletions deployment/templates/DBDeployment.DropCustomFileCreate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@echo off

SET DIR=%~d0%~p0%

SET database.name="${database.name}"
SET sql.files.directory="%DIR%${dirs.db}\SQLServer\${database.name}"
SET server.database="${server.database}"
SET repository.path="${repository.path}"
SET version.file="${file.version}"
SET version.xpath="//buildInfo/version"
SET environment=${environment}

"%DIR%Console\rh.exe" /d=%database.name% /f=%sql.files.directory% /s=%server.database% /vf=%version.file% /vx=%version.xpath% /r=%repository.path% /env=%environment% /drop
"%DIR%Console\rh.exe" /d=%database.name% /f=%sql.files.directory% /s=%server.database% /cds=%sql.files.directory%\custom_db_create.sql /vf=%version.file% /vx=%version.xpath% /r=%repository.path% /env=%environment% /simple
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public class when_getting_the_default_restore_move_options_for_SqlServer_prior_t
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;
Expand Down
3 changes: 1 addition & 2 deletions product/roundhouse/databases/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public interface Database : IDisposable
string scripts_run_errors_table_name { get; set; }
string user_name { get; set; }
string sql_statement_separator_regex_pattern { get; }
string custom_create_database_script { get; set; }
int command_timeout { get; set; }
int admin_command_timeout { get; set; }
int restore_timeout { get; set; }
Expand All @@ -93,7 +92,7 @@ public interface Database : IDisposable
void close_admin_connection();
void rollback();

void create_database_if_it_doesnt_exist();
void create_database_if_it_doesnt_exist(string custom_create_database_script);
void set_recovery_mode(bool simple);
void backup_database(string output_path_minus_database);
void restore_database(string restore_from_path, string custom_restore_options);
Expand Down
3 changes: 1 addition & 2 deletions product/roundhouse/databases/DefaultDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public virtual string sql_statement_separator_regex_pattern
get { return @"(?<KEEP1>^(?:.)*(?:-{2}).*$)|(?<KEEP1>/{1}\*{1}[\S\s]*?\*{1}/{1})|(?<KEEP1>'{1}(?:[^']|\n[^'])*?'{1})|(?<KEEP1>\s)(?<BATCHSPLITTER>\;)(?<KEEP2>\s)|(?<KEEP1>\s)(?<BATCHSPLITTER>\;)(?<KEEP2>$)"; }
}

public string custom_create_database_script { get; set; }
public int command_timeout { get; set; }
public int admin_command_timeout { get; set; }
public int restore_timeout { get; set; }
Expand Down Expand Up @@ -84,7 +83,7 @@ public void set_repository()
public abstract string restore_database_script(string restore_from_path, string custom_restore_options);
public abstract string delete_database_script();

public void create_database_if_it_doesnt_exist()
public void create_database_if_it_doesnt_exist(string custom_create_database_script)
{
try
{
Expand Down
8 changes: 1 addition & 7 deletions product/roundhouse/databases/MockDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ public string sql_statement_separator_regex_pattern
get { return database.sql_statement_separator_regex_pattern; }
}

public string custom_create_database_script
{
get { return database.custom_create_database_script; }
set { database.custom_create_database_script = value; }
}

public int command_timeout
{
get { return database.command_timeout; }
Expand Down Expand Up @@ -164,7 +158,7 @@ public void rollback()
database.rollback();
}

public void create_database_if_it_doesnt_exist()
public void create_database_if_it_doesnt_exist(string custom_create_database_script)
{
//TODO: Don't allow creation of the database - record everything from here on out as something that would run
//database_exists = database.database_exists
Expand Down
10 changes: 2 additions & 8 deletions product/roundhouse/databases/SqlServerLiteSpeedDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ public string sql_statement_separator_regex_pattern
get { return database.sql_statement_separator_regex_pattern; }
}

public string custom_create_database_script
{
get { return database.custom_create_database_script; }
set { database.custom_create_database_script = value; }
}

public int command_timeout
{
get { return database.command_timeout; }
Expand Down Expand Up @@ -154,9 +148,9 @@ public void rollback()
database.rollback();
}

public void create_database_if_it_doesnt_exist()
public void create_database_if_it_doesnt_exist(string custom_create_database_script)
{
database.create_database_if_it_doesnt_exist();
database.create_database_if_it_doesnt_exist(custom_create_database_script);
}

public void set_recovery_mode(bool simple)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public static Database build(FileSystemAccess file_system, ConfigurationProperty
database_to_migrate.scripts_run_table_name = configuration_property_holder.ScriptsRunTableName;
database_to_migrate.scripts_run_errors_table_name = configuration_property_holder.ScriptsRunErrorsTableName;
database_to_migrate.user_name = get_identity_of_person_running_roundhouse();
database_to_migrate.custom_create_database_script = configuration_property_holder.CreateDatabaseCustomScript;
database_to_migrate.command_timeout = configuration_property_holder.CommandTimeout;
database_to_migrate.admin_command_timeout = configuration_property_holder.CommandTimeoutAdmin;
database_to_migrate.restore_timeout = configuration_property_holder.RestoreTimeout;
Expand Down
2 changes: 1 addition & 1 deletion product/roundhouse/migrators/DatabaseMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface DatabaseMigrator
void open_connection(bool with_transaction);
void close_connection();
void backup_database_if_it_exists();
void create_or_restore_database();
void create_or_restore_database(string custom_create_database_script);
void set_recovery_mode(bool simple);
//void restore_database(string restore_from_path);
void delete_database();
Expand Down
4 changes: 2 additions & 2 deletions product/roundhouse/migrators/DefaultDatabaseMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public void close_connection()
database.close_connection();
}

public void create_or_restore_database()
public void create_or_restore_database(string custom_create_database_script)
{
Log.bound_to(this).log_an_info_event_containing("Creating {0} database on {1} server if it doesn't exist.", database.database_name, database.server_name);

database.create_database_if_it_doesnt_exist();
database.create_database_if_it_doesnt_exist(custom_create_database_script);

if (restoring_database)
{
Expand Down
17 changes: 16 additions & 1 deletion product/roundhouse/runners/RoundhouseMigrationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void run()
if (!dont_create_the_database)
{
database_migrator.open_admin_connection();
database_migrator.create_or_restore_database();
database_migrator.create_or_restore_database(get_custom_create_database_script());
database_migrator.set_recovery_mode(use_simple_recovery);
database_migrator.close_admin_connection();
}
Expand Down Expand Up @@ -216,6 +216,21 @@ public void run()
}
}

private string get_custom_create_database_script()
{
if (string.IsNullOrEmpty(configuration.CreateDatabaseCustomScript))
{
return configuration.CreateDatabaseCustomScript;
}

if(file_system.file_exists(configuration.CreateDatabaseCustomScript))
{
return file_system.read_file_text(configuration.CreateDatabaseCustomScript);
}

return configuration.CreateDatabaseCustomScript;
}

private void create_change_drop_folder()
{
file_system.create_directory(known_folders.change_drop.folder_full_path);
Expand Down