diff --git a/build/environment.sh b/build/environment.sh index 0f3d19f..49e4584 100755 --- a/build/environment.sh +++ b/build/environment.sh @@ -2,10 +2,22 @@ export BUILD=`dirname $0` export APP_ROOT=`dirname $BUILD` -export PGOPTIONS="--client-min-messages=warning" -export PSQL_CMD="psql -q -P pager=off " -export PSQL="$PSQL_CMD -v VERBOSITY=terse -v ON_ERROR_STOP=on" +if [ "x$DEBUG" = "x" ]; then + # Production & Default Options. + export PGOPTIONS="--client-min-messages=warning" + VERBOSE=terse + Q=-q +else + # Debug enabled + echo "Enabling Debug" + export PGOPTIONS="--client-min-messages=notice" + VERBOSE=verbose + Q="" +fi + +export PSQL_CMD="psql $Q -P pager=off " +export PSQL="$PSQL_CMD -v VERBOSITY=$VERBOSE -v ON_ERROR_STOP=on" export PGHOST=${PGHOST:-localhost} export PGPORT=${PGPORT:-5432} diff --git a/build/test_database.sh b/build/test_database.sh index 254cea4..7d9d563 100755 --- a/build/test_database.sh +++ b/build/test_database.sh @@ -12,7 +12,6 @@ PGTAP_TEST=$? echo "Test if load_all is reloadable." # TODO: make new 'create type" reloadable -echo "There will be an ERROR for type bitemporal_pg_constraint" echo "\\set CI $CI %\\ir $D/_load_all.sql" |tr '%' '\n' | $PSQL RELOAD_TEST=$? diff --git a/sql/ll_create_bitemporal_table.sql b/sql/ll_create_bitemporal_table.sql index fb2465d..c518755 100644 --- a/sql/ll_create_bitemporal_table.sql +++ b/sql/ll_create_bitemporal_table.sql @@ -6,27 +6,53 @@ CREATE OR REPLACE FUNCTION bitemporal_internal.ll_create_bitemporal_table( p_business_key text) RETURNS boolean AS $BODY$ -DECLARE -v_business_key_name text; -v_business_key_gist text; -v_serial_key_name text; -v_serial_key text; -v_pk_constraint_name text; -v_table_definition text; -v_error text; -v_business_key_array text[]; -i int; +DECLARE + v_sql text; + v_error text; BEGIN -v_serial_key :=p_table||'_key'; -v_serial_key_name :=v_serial_key ||' serial'; -v_pk_constraint_name:= p_table||'_pk'; -v_business_key_name :=p_table||'_'||translate(p_business_key, ', ','_')||'_assert_eff_excl'; -v_business_key_gist :=replace(p_business_key, ',',' WITH =,')||' WITH =, asserted WITH &&, effective WITH &&'; ---raise notice 'gist %',v_business_key_gist; -v_table_definition :=replace (p_table_definition, ' serial', ' integer'); -v_business_key_array :=string_to_array(p_business_key, ','); + v_sql := bitemporal_internal.ll_create_bitemporal_table_sql( + p_schema, + p_table, + p_table_definition, + p_business_key ); + EXECUTE v_sql; + RETURN ('true'); + EXCEPTION WHEN OTHERS THEN + GET STACKED DIAGNOSTICS v_error = MESSAGE_TEXT; + RAISE NOTICE '%', v_error; + RETURN ('false'); +END; +$BODY$ + LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION bitemporal_internal.ll_create_bitemporal_table_sql( + p_schema text, + p_table text, + p_table_definition text, + p_business_key text) + RETURNS text AS +$BODY$ +DECLARE + v_business_key_name text; + v_business_key_gist text; + v_serial_key_name text; + v_serial_key text; + v_pk_constraint_name text; + v_table_definition text; + v_business_key_array text[]; + i int; + v_sql text DEFAULT ''; +BEGIN + v_serial_key :=p_table||'_key'; + v_serial_key_name :=v_serial_key ||' serial'; + v_pk_constraint_name:= p_table||'_pk'; + v_business_key_name :=p_table||'_'||translate(p_business_key, ', ','_')||'_assert_eff_excl'; + v_business_key_gist :=replace(p_business_key, ',',' WITH =,')||' WITH =, asserted WITH &&, effective WITH &&'; -EXECUTE format($create$ + v_table_definition :=replace (p_table_definition, ' serial', ' integer'); + v_business_key_array :=string_to_array(p_business_key, ','); + + v_sql := format($create$ CREATE TABLE %s.%s ( %s ,%s @@ -34,7 +60,7 @@ CREATE TABLE %s.%s ( ,asserted temporal_relationships.timeperiod NOT NULL ,row_created_at timestamptz NOT NULL DEFAULT now() ,CONSTRAINT %s PRIMARY KEY (%s) - ,CONSTRAINT %s EXCLUDE + ,CONSTRAINT %s EXCLUDE USING gist (%s) ) $create$ @@ -47,22 +73,18 @@ CREATE TABLE %s.%s ( ,v_business_key_name ,v_business_key_gist ) ; - i:=1; - while v_business_key_array[i] is not null loop - execute format($alter$ - ALTER TABLE %s.%s ALTER %s SET NOT NULL - $alter$ + i:=1; + while v_business_key_array[i] is not null loop + v_sql := v_sql || ';' || + format($alter$ + ALTER TABLE %s.%s ALTER %s SET NOT NULL $alter$ ,p_schema - ,p_table + ,p_table ,v_business_key_array[i] - ) ; - i:=i+1; - end loop; - RETURN ('true'); - EXCEPTION WHEN OTHERS THEN -GET STACKED DIAGNOSTICS v_error = MESSAGE_TEXT; -raise notice '%', v_error; -RETURN ('false'); + ) ; + i:=i+1; + end loop; + return v_sql; END; $BODY$ - LANGUAGE plpgsql; \ No newline at end of file + LANGUAGE plpgsql; diff --git a/sql/metadata.sql b/sql/metadata.sql index 9f243c9..0b708cb 100644 --- a/sql/metadata.sql +++ b/sql/metadata.sql @@ -116,6 +116,7 @@ BEGIN END; $f$; +drop type if exists bitemporal_internal.bitemporal_pg_constraint cascade; create type bitemporal_internal.bitemporal_pg_constraint as