BOD-5 #comment Data level changes to support audit trails. Only enabled for jobs. Potentially big impact changes from this commit forward. Details logged in reference file.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
[]
|
||||
25
hasura/migrations/1583795594722_run_sql_migration/up.yaml
Normal file
25
hasura/migrations/1583795594722_run_sql_migration/up.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
- args:
|
||||
cascade: true
|
||||
read_only: false
|
||||
sql: "CREATE OR REPLACE FUNCTION audit_trigger() RETURNS trigger AS $$\r\n \r\n
|
||||
DECLARE\r\n shopid uuid ;\r\n email text;\r\n\r\n BEGIN\r\n \r\n select
|
||||
b.id, u.email INTO shopid, email from users u join associations a on u.email
|
||||
= a.useremail join bodyshops b on b.id = a.shopid where u.authid = current_setting('hasura.user',
|
||||
't')::jsonb->>'x-hasura-user-id' and a.active = true;\r\n\r\n IF
|
||||
\ TG_OP = 'INSERT'\r\n \r\n THEN\r\n \r\n INSERT
|
||||
INTO public.audit_trail (tabname, schemaname, operation, new_val, recordid,
|
||||
bodyshopid, useremail)\r\n \r\n VALUES (TG_RELNAME,
|
||||
TG_TABLE_SCHEMA, TG_OP, row_to_json(NEW), NEW.id, shopid, email);\r\n \r\n RETURN
|
||||
NEW;\r\n \r\n ELSIF TG_OP = 'UPDATE'\r\n \r\n THEN\r\n
|
||||
\r\n INSERT INTO public.audit_trail (tabname, schemaname,
|
||||
operation, old_val, new_val, recordid, bodyshopid, useremail)\r\n \r\n VALUES
|
||||
(TG_RELNAME, TG_TABLE_SCHEMA, TG_OP,\r\n \r\n json_diff(to_jsonb(OLD),
|
||||
to_jsonb(NEW)) , json_diff(to_jsonb(NEW), to_jsonb(OLD)), OLD.id, shopid,
|
||||
email);\r\n \r\n RETURN NEW;\r\n \r\n ELSIF
|
||||
\ TG_OP = 'DELETE'\r\n \r\n THEN\r\n \r\n INSERT
|
||||
INTO public.audit_trail (tabname, schemaname, operation, old_val, recordid,
|
||||
bodyshopid, useremail)\r\n \r\n VALUES (TG_RELNAME,
|
||||
TG_TABLE_SCHEMA, TG_OP, row_to_json(OLD), OLD.ID, shopid, email);\r\n \r\n
|
||||
\ RETURN OLD;\r\n \r\n END IF;\r\n \r\n
|
||||
\ END;\r\n \r\n$$ LANGUAGE 'plpgsql' SECURITY DEFINER;"
|
||||
type: run_sql
|
||||
Reference in New Issue
Block a user