From 1ba8c7f148baeac3ab96c05464637838042ea9fd Mon Sep 17 00:00:00 2001 From: tsaluszewski Date: Tue, 2 Aug 2016 19:20:31 +0200 Subject: [PATCH] TableCreationScripts for PostgreSQL do not handle schema name properly #260. Schema creation script added. --- .../PostgreSQLDatabase.cs | 7 ++-- .../TableCreationScripts.cs | 35 ++++++++++--------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/product/roundhouse.databases.postgresql/PostgreSQLDatabase.cs b/product/roundhouse.databases.postgresql/PostgreSQLDatabase.cs index 6db65c9b..bd097831 100644 --- a/product/roundhouse.databases.postgresql/PostgreSQLDatabase.cs +++ b/product/roundhouse.databases.postgresql/PostgreSQLDatabase.cs @@ -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); diff --git a/product/roundhouse.databases.postgresql/TableCreationScripts.cs b/product/roundhouse.databases.postgresql/TableCreationScripts.cs index 7745a0fc..8b49fa01 100644 --- a/product/roundhouse.databases.postgresql/TableCreationScripts.cs +++ b/product/roundhouse.databases.postgresql/TableCreationScripts.cs @@ -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 $$