-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbitemporal.sql.template
40 lines (33 loc) · 1.41 KB
/
bitemporal.sql.template
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
\echo 'Code or Die (Bitemporality)'
\echo 'James Powell <[email protected]>'
\set VERBOSITY terse
\set ON_ERROR_STOP true
do language plpgsql $$ declare
exc_message text;
exc_context text;
exc_detail text;
begin
set search_path = history, public;
create extension if not exists temporal_tables;
drop schema if exists history cascade;
create schema if not exists history;
{% for table in tables %}
alter table {{table}} add column asof tstzrange;
update {{table}} set asof = tstzrange(now() - interval '1 hour', 'infinity', '[)');
alter table {{table}} alter column asof set not null;
raise notice 'Creating table "{{table}}"';
drop table if exists "{{table}}" cascade;
create table if not exists "{{table}}" (like {{table}});
create trigger versioning_trigger
before insert or update or delete on {{table}}
for each row execute procedure versioning('asof', 'history."{{table}}"', true);
create index on "{{table}}" using gist (asof);
-- alter table "{{table}}" add constraint exclude using gist (???, asof with &&);
alter table {{table}} inherit "{{table}}";
{% endfor %}
exception when others then
get stacked diagnostics exc_message = message_text;
get stacked diagnostics exc_context = pg_exception_context;
get stacked diagnostics exc_detail = pg_exception_detail;
raise exception E'\n------\n%\n%\n------\n\nCONTEXT:\n%\n', exc_message, exc_detail, exc_context;
end $$;