Skip to content

Commit

Permalink
Working fully qualified table name and tables in different schema fro…
Browse files Browse the repository at this point in the history
…m function.
  • Loading branch information
Joe Andaverde committed Dec 13, 2017
1 parent 7e078ee commit 357c312
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions versioning_function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ BEGIN
history_table := TG_ARGV[1];

-- check if sys_period exists on original table
SELECT atttypid, attndims INTO holder FROM pg_attribute WHERE attrelid = TG_TABLE_NAME::regclass AND attname = sys_period AND NOT attisdropped;
SELECT atttypid, attndims INTO holder FROM pg_attribute WHERE attrelid = TG_RELID AND attname = sys_period AND NOT attisdropped;
IF NOT FOUND THEN
RAISE 'column "%" of relation "%" does not exist', sys_period, TG_TABLE_NAME USING
ERRCODE = 'undefined_column';
Expand Down Expand Up @@ -68,7 +68,7 @@ BEGIN
END IF;

-- check if history table exits
IF to_regclass(history_table) IS NULL THEN
IF history_table::regclass IS NULL THEN
RAISE 'relation "%" does not exist', history_table;
END IF;

Expand Down Expand Up @@ -108,7 +108,7 @@ BEGIN
main AS
(SELECT attname, atttypid
FROM pg_attribute
WHERE attrelid = TG_TABLE_NAME::regclass
WHERE attrelid = TG_RELID
AND attnum > 0
AND NOT attisdropped)
SELECT
Expand Down Expand Up @@ -138,7 +138,7 @@ BEGIN
main AS
(SELECT attname
FROM pg_attribute
WHERE attrelid = TG_TABLE_NAME::regclass
WHERE attrelid = TG_RELID
AND attnum > 0
AND NOT attisdropped)
SELECT array_agg(quote_ident(history.attname)) INTO commonColumns
Expand All @@ -148,7 +148,12 @@ BEGIN
AND history.attname != sys_period;

EXECUTE ('INSERT INTO ' ||
quote_ident(history_table) ||
CASE split_part(history_table, '.', 2)
WHEN '' THEN
quote_ident(history_table)
ELSE
quote_ident(split_part(history_table, '.', 1)) || '.' || quote_ident(split_part(history_table, '.', 2))
END ||
'(' ||
array_to_string(commonColumns , ',') ||
',' ||
Expand Down

0 comments on commit 357c312

Please sign in to comment.