From 10307adf832efb9cbc8c610778330e85021d9073 Mon Sep 17 00:00:00 2001 From: ferventcoder Date: Thu, 22 Sep 2011 10:37:29 +0000 Subject: [PATCH] Create database custom script to handle file path - https://github.com/chucknorris/roundhouse/pull/17 - thanks Seif! --- .../TestRoundhousE/custom_db_create.sql | 5 +++++ .../templates/DBDeployment.DropCustomCreate.bat | 15 +++++++++++++++ .../DBDeployment.DropCustomFileCreate.bat | 14 ++++++++++++++ .../databases/SqlServerDatabaseSpecs.cs | 1 - product/roundhouse/databases/Database.cs | 3 +-- product/roundhouse/databases/DefaultDatabase.cs | 3 +-- product/roundhouse/databases/MockDatabase.cs | 8 +------- .../databases/SqlServerLiteSpeedDatabase.cs | 10 ++-------- .../builders/DatabaseBuilder.cs | 1 - .../roundhouse/migrators/DatabaseMigrator.cs | 2 +- .../migrators/DefaultDatabaseMigrator.cs | 4 ++-- .../runners/RoundhouseMigrationRunner.cs | 17 ++++++++++++++++- 12 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 db/SQLServer/TestRoundhousE/custom_db_create.sql create mode 100644 deployment/templates/DBDeployment.DropCustomCreate.bat create mode 100644 deployment/templates/DBDeployment.DropCustomFileCreate.bat diff --git a/db/SQLServer/TestRoundhousE/custom_db_create.sql b/db/SQLServer/TestRoundhousE/custom_db_create.sql new file mode 100644 index 0000000..29c8240 --- /dev/null +++ b/db/SQLServer/TestRoundhousE/custom_db_create.sql @@ -0,0 +1,5 @@ +USE master +IF NOT EXISTS(SELECT * FROM sys.databases WHERE [name] = '{{DatabaseName}}') +BEGIN + CREATE DATABASE {{DatabaseName}} +END \ No newline at end of file diff --git a/deployment/templates/DBDeployment.DropCustomCreate.bat b/deployment/templates/DBDeployment.DropCustomCreate.bat new file mode 100644 index 0000000..a6dd74b --- /dev/null +++ b/deployment/templates/DBDeployment.DropCustomCreate.bat @@ -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 \ No newline at end of file diff --git a/deployment/templates/DBDeployment.DropCustomFileCreate.bat b/deployment/templates/DBDeployment.DropCustomFileCreate.bat new file mode 100644 index 0000000..239780e --- /dev/null +++ b/deployment/templates/DBDeployment.DropCustomFileCreate.bat @@ -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 \ No newline at end of file diff --git a/product/roundhouse.tests.integration/databases/SqlServerDatabaseSpecs.cs b/product/roundhouse.tests.integration/databases/SqlServerDatabaseSpecs.cs index f49017c..6686a53 100644 --- a/product/roundhouse.tests.integration/databases/SqlServerDatabaseSpecs.cs +++ b/product/roundhouse.tests.integration/databases/SqlServerDatabaseSpecs.cs @@ -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; diff --git a/product/roundhouse/databases/Database.cs b/product/roundhouse/databases/Database.cs index e8b5f8d..16a86b9 100644 --- a/product/roundhouse/databases/Database.cs +++ b/product/roundhouse/databases/Database.cs @@ -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; } @@ -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); diff --git a/product/roundhouse/databases/DefaultDatabase.cs b/product/roundhouse/databases/DefaultDatabase.cs index 32f10e3..7ebdbaf 100644 --- a/product/roundhouse/databases/DefaultDatabase.cs +++ b/product/roundhouse/databases/DefaultDatabase.cs @@ -38,7 +38,6 @@ public virtual string sql_statement_separator_regex_pattern get { return @"(?^(?:.)*(?:-{2}).*$)|(?/{1}\*{1}[\S\s]*?\*{1}/{1})|(?'{1}(?:[^']|\n[^'])*?'{1})|(?\s)(?\;)(?\s)|(?\s)(?\;)(?$)"; } } - 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; } @@ -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 { diff --git a/product/roundhouse/databases/MockDatabase.cs b/product/roundhouse/databases/MockDatabase.cs index dfa05ae..fe48713 100644 --- a/product/roundhouse/databases/MockDatabase.cs +++ b/product/roundhouse/databases/MockDatabase.cs @@ -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; } @@ -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 diff --git a/product/roundhouse/databases/SqlServerLiteSpeedDatabase.cs b/product/roundhouse/databases/SqlServerLiteSpeedDatabase.cs index ae97187..c21a996 100644 --- a/product/roundhouse/databases/SqlServerLiteSpeedDatabase.cs +++ b/product/roundhouse/databases/SqlServerLiteSpeedDatabase.cs @@ -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; } @@ -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) diff --git a/product/roundhouse/infrastructure.app/builders/DatabaseBuilder.cs b/product/roundhouse/infrastructure.app/builders/DatabaseBuilder.cs index 03b57a8..93781d9 100644 --- a/product/roundhouse/infrastructure.app/builders/DatabaseBuilder.cs +++ b/product/roundhouse/infrastructure.app/builders/DatabaseBuilder.cs @@ -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; diff --git a/product/roundhouse/migrators/DatabaseMigrator.cs b/product/roundhouse/migrators/DatabaseMigrator.cs index 07f0f07..85cf8de 100644 --- a/product/roundhouse/migrators/DatabaseMigrator.cs +++ b/product/roundhouse/migrators/DatabaseMigrator.cs @@ -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(); diff --git a/product/roundhouse/migrators/DefaultDatabaseMigrator.cs b/product/roundhouse/migrators/DefaultDatabaseMigrator.cs index b955a85..f7debdb 100644 --- a/product/roundhouse/migrators/DefaultDatabaseMigrator.cs +++ b/product/roundhouse/migrators/DefaultDatabaseMigrator.cs @@ -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) { diff --git a/product/roundhouse/runners/RoundhouseMigrationRunner.cs b/product/roundhouse/runners/RoundhouseMigrationRunner.cs index d11b5a0..bf5c106 100644 --- a/product/roundhouse/runners/RoundhouseMigrationRunner.cs +++ b/product/roundhouse/runners/RoundhouseMigrationRunner.cs @@ -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(); } @@ -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);