-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.sql
26 lines (23 loc) · 968 Bytes
/
util.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
CREATE OR REPLACE FUNCTION
util.denorm(src_schema TEXT, src_table TEXT, src_id_col TEXT,
dst_schema TEXT, dst_table TEXT, dst_id_col TEXT,
dst_col_prefix TEXT, src_cols TEXT[])
AS $$
BEGIN
/*
* Idea:
*
* for each col in src_cols[]
* EXECUTE format('ALTER TABLE {dst_schema}.{dst_tbl}
* ADD COLUMN {dst_col_prefix}{col} TEXT', ...);
*
* EXECUTE format('CREATE TRIGGER ... AFTER UPDATE OR DELETE
* ON {src_schema}.{src_tbl}...
-- update/delete dst_schema.dst_tbl to copy over the
-- denormalized columns');
* EXECUTE format('CREATE TRIGGER ... BEFORE INSERT
* ON {dst_schema}.{dst_tbl}...
-- set NEW.{dst_col_prefix}{col} for every dst_cols[]
-- by looking that up in the source table');
*/
END; $$ LANGUAGE PlPGSQL;