Skip to content

Commit

Permalink
TableCreationScripts for PostgreSQL do not handle schema name properly
Browse files Browse the repository at this point in the history
…#260.

Schema creation script added.
  • Loading branch information
tsaluszewski committed Aug 2, 2016
1 parent 11c0f11 commit 1ba8c7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
7 changes: 5 additions & 2 deletions product/roundhouse.databases.postgresql/PostgreSQLDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@ public override string delete_database_script()

public override void create_or_update_roundhouse_tables()
{
//Log.bound_to(this).log_an_info_event_containing("Creating schema [{0}].", roundhouse_schema_name);
//run_sql(TableCreationScripts.create_roundhouse_schema(roundhouse_schema_name), ConnectionType.Default);
Log.bound_to(this).log_an_info_event_containing("Creating schema [{0}].", roundhouse_schema_name);
run_sql(TableCreationScripts.create_roundhouse_schema(roundhouse_schema_name), ConnectionType.Default);

Log.bound_to(this).log_an_info_event_containing("Creating table [{0}].[{1}].", roundhouse_schema_name, version_table_name);
run_sql(TableCreationScripts.create_roundhouse_version_table(roundhouse_schema_name, version_table_name), ConnectionType.Default);

Log.bound_to(this).log_an_info_event_containing("Creating table [{0}].[{1}].", roundhouse_schema_name, scripts_run_table_name);
run_sql(TableCreationScripts.create_roundhouse_scripts_run_table(roundhouse_schema_name, version_table_name, scripts_run_table_name),
ConnectionType.Default);

Log.bound_to(this).log_an_info_event_containing("Creating table [{0}].[{1}].", roundhouse_schema_name, scripts_run_errors_table_name);
run_sql(TableCreationScripts.create_roundhouse_scripts_run_errors_table(roundhouse_schema_name, scripts_run_errors_table_name),
ConnectionType.Default);
Expand Down
35 changes: 18 additions & 17 deletions product/roundhouse.databases.postgresql/TableCreationScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
{
public sealed class TableCreationScripts
{
// public static string create_roundhouse_schema(string roundhouse_schema_name)
// {
// return string.Format(@"
//CREATE FUNCTION CreateRoundHouseSchema(in schemaName varchar) RETURNS void AS $$
//DECLARE t_exists integer;
//BEGIN
// SELECT INTO t_exists COUNT(*) FROM information_schema.schemata WHERE schema_name = lower($1);
// IF t_exists = 0 THEN
// EXECUTE 'CREATE SCHEMA ' || lower(schemaName);
// END IF;
//END;
//$$ LANGUAGE 'plpgsql';
//SELECT CreateRoundHouseSchema('{0}');
//DROP FUNCTION CreateRoundHouseSchema(in schemaName varchar);
//", roundhouse_schema_name);
// }
public static string create_roundhouse_schema(string roundhouse_schema_name)
{
return string.Format(@"
CREATE OR REPLACE FUNCTION CreateRoundHouseSchema(in schemaName varchar) RETURNS void AS $$
DECLARE t_exists integer;
BEGIN
SELECT INTO t_exists COUNT(*) FROM information_schema.schemata WHERE schema_name = lower(schemaName);
IF t_exists = 0 THEN
EXECUTE 'CREATE SCHEMA ' || lower(schemaName);
END IF;
END;
$$ LANGUAGE 'plpgsql';
SELECT CreateRoundHouseSchema('{0}');
DROP FUNCTION CreateRoundHouseSchema(in schemaName varchar);
", roundhouse_schema_name);
}

public static string create_roundhouse_version_table(string roundhouse_schema_name, string version_table_name)
public static string create_roundhouse_version_table(string roundhouse_schema_name, string version_table_name)
{
return string.Format(@"
CREATE OR REPLACE FUNCTION CreateRoundHouseVersionTable(in schName varchar, in tblName varchar) RETURNS void AS $$
Expand Down

0 comments on commit 1ba8c7f

Please sign in to comment.