Skip to content

Commit fb8122b

Browse files
committed
chore: added trigger to backup integrations table content on change
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent 50068e3 commit fb8122b

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

backend/src/database/migrations/U1762508771__autobackup-integrations.sql

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
create table "integrationsHistory" as
2+
select *
3+
from "integrations"
4+
where 1 = 2;
5+
6+
alter table "integrationsHistory"
7+
add column "historyCreatedAt" timestamptz not null default now();
8+
9+
create index if not exists ix_integration_history_integration_id on "integrationsHistory" ("id");
10+
11+
create function log_integrations_changes()
12+
returns trigger as
13+
$$
14+
begin
15+
insert into "integrationsHistory" select old.*;
16+
17+
if (tg_op = 'DELETE') then
18+
return old;
19+
else
20+
return new;
21+
end if;
22+
end;
23+
$$ language plpgsql;
24+
25+
create trigger integrations_audit_trigger
26+
after update or delete
27+
on integrations
28+
for each row
29+
execute function log_integrations_changes();

0 commit comments

Comments
 (0)