Skip to content

Commit

Permalink
fix for chucknorris/roundhouse#20 - RunAfterCreateDatabase folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ferventcoder committed Sep 28, 2011
1 parent 95e6b32 commit ef4ccc0
Show file tree
Hide file tree
Showing 21 changed files with 166 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE SCHEMA [rh]
GO
8 changes: 6 additions & 2 deletions product/roundhouse.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,14 @@ private static void parse_arguments_and_set_up_configuration(ConfigurationProper
ApplicationParameters.default_version_x_path),
option => configuration.VersionXPath = option)
// folders
.Add("ad=|alterdatabase=|alterdatabasefolder=|alterdatabasefoldername=",
.Add("ad=|alterdatabase=|alterdatabasefolder=|alterdatabasefoldername=",
string.Format("AlterDatabaseFolderName - The name of the folder where you keep your alter database scripts. Read up on token replacement. You will want to use {{DatabaseName}} here instead of specifying a database name. Will recurse through subfolders. Defaults to \"{0}\".",
ApplicationParameters.default_alter_database_folder_name),
option => configuration.AlterDatabaseFolderName = option)
.Add("racd=|runaftercreatedatabase=|runaftercreatedatabasefolder=|runaftercreatedatabasefoldername=",
string.Format("RunAfterCreateDatabaseFolderName - The name of the folder where you will keep scripts that ONLY run after a database is created. Will recurse through subfolders. Defaults to \"{0}\".",
ApplicationParameters.default_run_after_create_database_folder_name),
option => configuration.RunAfterCreateDatabaseFolderName = option)
.Add("u=|up=|upfolder=|upfoldername=",
string.Format("UpFolderName - The name of the folder where you keep your update scripts. Will recurse through subfolders. Defaults to \"{0}\".",
ApplicationParameters.default_up_folder_name),
Expand Down Expand Up @@ -279,7 +283,7 @@ private static void parse_arguments_and_set_up_configuration(ConfigurationProper
"/c[onnection]s[tring]a[dministration] VALUE " +
"/c[ommand]t[imeout] VALUE /c[ommand]t[imeout]a[dmin] VALUE " +
"/r[epositorypath] VALUE /v[ersion]f[ile] VALUE /v[ersion]x[path] VALUE " +
"/a[lter]d[atabasefoldername] VALUE /u[pfoldername] VALUE /do[wnfoldername] VALUE " +
"/a[lter]d[atabasefoldername] /r[un]a[fter]c[reate]d[atabasefoldername] VALUE VALUE /u[pfoldername] VALUE /do[wnfoldername] VALUE " +
"/r[un]f[irstafterupdatefoldername] VALUE /fu[nctionsfoldername] VALUE /v[ie]w[sfoldername] VALUE " +
"/sp[rocsfoldername] VALUE /i[nde]x[foldername] VALUE /p[ermissionsfoldername] VALUE " +
"/sc[hemaname] VALUE /v[ersion]t[ablename] VALUE /s[cripts]r[un]t[ablename] VALUE /s[cripts]r[un]e[rrors]t[ablename] VALUE " +
Expand Down
2 changes: 1 addition & 1 deletion product/roundhouse.databases.oracle/OracleDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public override void run_sql(string sql_to_run, ConnectionType connection_type)
base.run_sql(sql_to_run.Replace("\r\n", "\n"), connection_type);
}

private object run_sql_scalar(string sql_to_run, ConnectionType connection_type, IList<IParameter<IDbDataParameter>> parameters)
protected override object run_sql_scalar(string sql_to_run, ConnectionType connection_type, IList<IParameter<IDbDataParameter>> parameters)
{
Log.bound_to(this).log_a_debug_event_containing("Replacing \r\n with \n to be compliant with Oracle.");
//http://www.barrydobson.com/2009/02/17/pls-00103-encountered-the-symbol-when-expecting-one-of-the-following/
Expand Down
6 changes: 5 additions & 1 deletion product/roundhouse.databases.sqlserver/SqlServerDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,15 @@ IF NOT EXISTS(SELECT * FROM sys.schemas WHERE [name] = '{0}')
public override string create_database_script()
{
return string.Format(
@"USE master
@" DECLARE @Created bit
SET @Created = 0
IF NOT EXISTS(SELECT * FROM sys.databases WHERE [name] = '{0}')
BEGIN
CREATE DATABASE [{0}]
SET @Created = 1
END
SELECT @Created
",
database_name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ public override void run_database_specific_tasks()
public override string create_database_script()
{
return string.Format(
@"USE master
IF NOT EXISTS(SELECT * FROM sysdatabases WHERE [name] = '{0}')
@"
DECLARE @Created bit
SET @Created = 0
IF NOT EXISTS(SELECT * FROM sys.databases WHERE [name] = '{0}')
BEGIN
CREATE DATABASE [{0}]
SET @Created = 1
END
--SELECT @Created
",
database_name);
}
Expand Down
2 changes: 2 additions & 0 deletions product/roundhouse.tasks/Roundhouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ bool ITask.Execute()

public string AlterDatabaseFolderName { get; set; }

public string RunAfterCreateDatabaseFolderName { get; set; }

public string UpFolderName { get; set; }

public string DownFolderName { get; set; }
Expand Down
1 change: 1 addition & 0 deletions product/roundhouse/consoles/DefaultConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class DefaultConfiguration : ConfigurationPropertyHolder
public string VersionFile { get; set; }
public string VersionXPath { get; set; }
public string AlterDatabaseFolderName { get; set; }
public string RunAfterCreateDatabaseFolderName { get; set; }
public string UpFolderName { get; set; }
public string DownFolderName { get; set; }
public string RunFirstAfterUpFolderName { get; set; }
Expand Down
14 changes: 14 additions & 0 deletions product/roundhouse/databases/AdoNetDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ private void run_command_with(string sql_to_run,ConnectionType connection_type,
}
}

protected override object run_sql_scalar(string sql_to_run, ConnectionType connection_type, IList<IParameter<IDbDataParameter>> parameters)
{
object return_value = new object();
if (string.IsNullOrEmpty(sql_to_run)) return return_value;

using (IDbCommand command = setup_database_command(sql_to_run, connection_type, null))
{
return_value = command.ExecuteScalar();
command.Dispose();
}

return return_value;
}

protected IDbCommand setup_database_command(string sql_to_run, ConnectionType connection_type, IEnumerable<IParameter<IDbDataParameter>> parameters)
{
IDbCommand command = null;
Expand Down
3 changes: 2 additions & 1 deletion product/roundhouse/databases/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ public interface Database : IDisposable
void close_admin_connection();
void rollback();

void create_database_if_it_doesnt_exist(string custom_create_database_script);
bool 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);
void delete_database_if_it_exists();
void run_database_specific_tasks();
void create_or_update_roundhouse_tables();
void run_sql(string sql_to_run,ConnectionType connection_type);
object run_sql_scalar(string sql_to_run, ConnectionType connection_type);
void insert_script_run(string script_name, string sql_to_run, string sql_to_run_hash, bool run_this_script_once, long version_id);
void insert_script_run_error(string script_name, string sql_to_run, string sql_erroneous_part, string error_message, string repository_version, string repository_path);

Expand Down
18 changes: 16 additions & 2 deletions product/roundhouse/databases/DefaultDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ 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(string custom_create_database_script)
public bool create_database_if_it_doesnt_exist(string custom_create_database_script)
{
bool database_was_created = false;
try
{
string create_script = create_database_script();
Expand All @@ -96,14 +97,21 @@ public void create_database_if_it_doesnt_exist(string custom_create_database_scr
create_script = TokenReplacer.replace_tokens(configuration, create_script);
}
}
run_sql(create_script, ConnectionType.Admin);

var return_value = run_sql_scalar(create_script, ConnectionType.Admin);
if (return_value !=null)
{
database_was_created = (bool) return_value;
}
}
catch (Exception ex)
{
Log.bound_to(this).log_a_warning_event_containing(
"{0} with provider {1} does not provide a facility for creating a database at this time.{2}{3}",
GetType(), provider, Environment.NewLine, ex.Message);
}

return database_was_created;
}

public void set_recovery_mode(bool simple)
Expand Down Expand Up @@ -175,7 +183,13 @@ public virtual void run_sql(string sql_to_run, ConnectionType connection_type)
run_sql(sql_to_run, connection_type, null);
}

public virtual object run_sql_scalar(string sql_to_run,ConnectionType connection_type)
{
return run_sql_scalar(sql_to_run, connection_type, null);
}

protected abstract void run_sql(string sql_to_run, ConnectionType connection_type, IList<IParameter<IDbDataParameter>> parameters);
protected abstract object run_sql_scalar(string sql_to_run, ConnectionType connection_type, IList<IParameter<IDbDataParameter>> parameters);

public void insert_script_run(string script_name, string sql_to_run, string sql_to_run_hash, bool run_this_script_once, long version_id)
{
Expand Down
11 changes: 10 additions & 1 deletion product/roundhouse/databases/MockDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,12 @@ public void rollback()
database.rollback();
}

public void create_database_if_it_doesnt_exist(string custom_create_database_script)
public bool 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
return true;
//return database.
}

public void set_recovery_mode(bool simple)
Expand Down Expand Up @@ -206,6 +208,13 @@ public void run_sql(string sql_to_run,ConnectionType connection_type)
Log.bound_to(this).log_an_info_event_containing("Running statemtent: {0}{1}", Environment.NewLine, sql_to_run);
//database.run_sql(sql_to_run);
}

public object run_sql_scalar(string sql_to_run,ConnectionType connection_type)
{
Log.bound_to(this).log_an_info_event_containing("Running statemtent: {0}{1}", Environment.NewLine, sql_to_run);
//database.run_sql(sql_to_run);
return new object();
}

public void insert_script_run(string script_name, string sql_to_run, string sql_to_run_hash, bool run_this_script_once, long version_id)
{
Expand Down
9 changes: 7 additions & 2 deletions product/roundhouse/databases/SqlServerLiteSpeedDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ public void rollback()
database.rollback();
}

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

public void set_recovery_mode(bool simple)
Expand Down Expand Up @@ -209,6 +209,11 @@ public void create_or_update_roundhouse_tables()
public void run_sql(string sql_to_run,ConnectionType connection_type)
{
database.run_sql(sql_to_run,connection_type);
}

public object run_sql_scalar(string sql_to_run,ConnectionType connection_type)
{
return database.run_sql_scalar(sql_to_run,connection_type);
}

public void insert_script_run(string script_name, string sql_to_run, string sql_to_run_hash, bool run_this_script_once, long version_id)
Expand Down
89 changes: 46 additions & 43 deletions product/roundhouse/folders/DefaultKnownFolders.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
namespace roundhouse.folders
{
public sealed class DefaultKnownFolders : KnownFolders
{
public DefaultKnownFolders(
MigrationsFolder alter_database,
MigrationsFolder up,
MigrationsFolder down,
MigrationsFolder run_first_after_up,
MigrationsFolder functions,
MigrationsFolder views,
MigrationsFolder sprocs,
MigrationsFolder indexes,
MigrationsFolder runAfterOtherAnyTimeScripts,
MigrationsFolder permissions,
Folder change_drop
)
{
this.alter_database = alter_database;
this.up = up;
this.down = down;
this.run_first_after_up = run_first_after_up;
this.functions = functions;
this.views = views;
namespace roundhouse.folders
{
public sealed class DefaultKnownFolders : KnownFolders
{
public DefaultKnownFolders(
MigrationsFolder alter_database,
MigrationsFolder run_after_create_database,
MigrationsFolder up,
MigrationsFolder down,
MigrationsFolder run_first_after_up,
MigrationsFolder functions,
MigrationsFolder views,
MigrationsFolder sprocs,
MigrationsFolder indexes,
MigrationsFolder runAfterOtherAnyTimeScripts,
MigrationsFolder permissions,
Folder change_drop
)
{
this.alter_database = alter_database;
this.run_after_create_database = run_after_create_database;
this.up = up;
this.down = down;
this.run_first_after_up = run_first_after_up;
this.functions = functions;
this.views = views;
this.sprocs = sprocs;
this.indexes = indexes;
this.runAfterOtherAnyTimeScripts = runAfterOtherAnyTimeScripts;
this.permissions = permissions;
this.change_drop = change_drop;
}

public MigrationsFolder alter_database { get; private set; }
public MigrationsFolder up { get; private set; }
public MigrationsFolder down { get; private set; }
public MigrationsFolder run_first_after_up { get; private set; }
public MigrationsFolder functions { get; private set; }
public MigrationsFolder views { get; private set; }
this.indexes = indexes;
this.run_after_other_any_time_scripts = runAfterOtherAnyTimeScripts;
this.permissions = permissions;
this.change_drop = change_drop;
}

public MigrationsFolder alter_database { get; private set; }
public MigrationsFolder run_after_create_database { get; private set; }
public MigrationsFolder up { get; private set; }
public MigrationsFolder down { get; private set; }
public MigrationsFolder run_first_after_up { get; private set; }
public MigrationsFolder functions { get; private set; }
public MigrationsFolder views { get; private set; }
public MigrationsFolder sprocs { get; private set; }
public MigrationsFolder indexes { get; private set; }
public MigrationsFolder runAfterOtherAnyTimeScripts { get; private set; }
public MigrationsFolder permissions { get; private set; }

public Folder change_drop{get; private set;}

}
public MigrationsFolder indexes { get; private set; }
public MigrationsFolder run_after_other_any_time_scripts { get; private set; }
public MigrationsFolder permissions { get; private set; }

public Folder change_drop{get; private set;}

}
}
31 changes: 16 additions & 15 deletions product/roundhouse/folders/KnownFolders.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
namespace roundhouse.folders
{
public interface KnownFolders
{
MigrationsFolder alter_database { get; }
MigrationsFolder up { get; }
MigrationsFolder down { get; }
MigrationsFolder run_first_after_up { get; }
MigrationsFolder functions { get; }
MigrationsFolder views { get; }
namespace roundhouse.folders
{
public interface KnownFolders
{
MigrationsFolder alter_database { get; }
MigrationsFolder run_after_create_database { get; }
MigrationsFolder up { get; }
MigrationsFolder down { get; }
MigrationsFolder run_first_after_up { get; }
MigrationsFolder functions { get; }
MigrationsFolder views { get; }
MigrationsFolder sprocs { get; }
MigrationsFolder indexes { get; }
MigrationsFolder runAfterOtherAnyTimeScripts { get; }
MigrationsFolder permissions { get; }
Folder change_drop { get; }
}
MigrationsFolder indexes { get; }
MigrationsFolder run_after_other_any_time_scripts { get; }
MigrationsFolder permissions { get; }
Folder change_drop { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static void set_defaults_if_properties_are_not_set(ConfigurationPropertyH
{
configuration_property_holder.AlterDatabaseFolderName = ApplicationParameters.default_alter_database_folder_name;
}
if (string.IsNullOrEmpty(configuration_property_holder.RunAfterCreateDatabaseFolderName))
{
configuration_property_holder.RunAfterCreateDatabaseFolderName = ApplicationParameters.default_run_after_create_database_folder_name;
}
if (string.IsNullOrEmpty(configuration_property_holder.UpFolderName))
{
configuration_property_holder.UpFolderName = ApplicationParameters.default_up_folder_name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface ConfigurationPropertyHolder
string VersionFile { get; set; }
string VersionXPath { get; set; }
string AlterDatabaseFolderName { get; set; }
string RunAfterCreateDatabaseFolderName { get; set; }
string UpFolderName { get; set; }
string DownFolderName { get; set; }
string RunFirstAfterUpFolderName { get; set; }
Expand Down
Loading

0 comments on commit ef4ccc0

Please sign in to comment.