1+ AUDIT_DDL = """
2+ CREATE SCHEMA IF NOT EXISTS auditoria;
3+
4+ CREATE TABLE IF NOT EXISTS auditoria.auditoria_db (
5+ id SERIAL PRIMARY KEY,
6+ operacao TEXT,
7+ tabela TEXT,
8+ registro_antigo JSONB,
9+ registro_novo JSONB,
10+ usuario TEXT,
11+ data_operacao TIMESTAMPTZ DEFAULT now()
12+ );
13+
14+ CREATE OR REPLACE FUNCTION auditoria.fn_auditoria() RETURNS TRIGGER AS $$
15+ BEGIN
16+ IF TG_OP = 'INSERT' THEN
17+ INSERT INTO auditoria.auditoria_db (operacao, tabela, registro_novo, usuario)
18+ VALUES ('INSERT', TG_TABLE_NAME, to_jsonb(NEW), current_user);
19+
20+ ELSIF TG_OP = 'UPDATE' THEN
21+ INSERT INTO auditoria.auditoria_db (operacao, tabela, registro_antigo, registro_novo, usuario)
22+ VALUES ('UPDATE', TG_TABLE_NAME, to_jsonb(OLD), to_jsonb(NEW), current_user);
23+
24+ ELSIF TG_OP = 'DELETE' THEN
25+ INSERT INTO auditoria.auditoria_db (operacao, tabela, registro_antigo, usuario)
26+ VALUES ('DELETE', TG_TABLE_NAME, to_jsonb(OLD), current_user);
27+ END IF;
28+
29+ RETURN NULL;
30+ END;
31+ $$ LANGUAGE plpgsql;
32+
33+ DROP TRIGGER IF EXISTS tr_auditoria_pt ON plano_trabalho;
34+ DROP TRIGGER IF EXISTS tr_auditoria_pe ON plano_entregas;
35+ DROP TRIGGER IF EXISTS tr_auditoria_part ON participante;
36+ DROP TRIGGER IF EXISTS tr_auditoria_us ON users;
37+ DROP TRIGGER IF EXISTS tr_auditoria_co ON contribuicao;
38+ DROP TRIGGER IF EXISTS tr_auditoria_en ON entrega;
39+ DROP TRIGGER IF EXISTS tr_auditoria_are ON avaliacao_registros_execucao;
40+
41+
42+ CREATE TRIGGER tr_auditoria_pt
43+ AFTER INSERT OR UPDATE OR DELETE ON plano_trabalho
44+ FOR EACH ROW EXECUTE FUNCTION auditoria.fn_auditoria();
45+
46+ CREATE TRIGGER tr_auditoria_pe
47+ AFTER INSERT OR UPDATE OR DELETE ON plano_entregas
48+ FOR EACH ROW EXECUTE FUNCTION auditoria.fn_auditoria();
49+
50+ CREATE TRIGGER tr_auditoria_part
51+ AFTER INSERT OR UPDATE OR DELETE ON participante
52+ FOR EACH ROW EXECUTE FUNCTION auditoria.fn_auditoria();
53+
54+ CREATE TRIGGER tr_auditoria_us
55+ AFTER INSERT OR UPDATE OR DELETE ON users
56+ FOR EACH ROW EXECUTE FUNCTION auditoria.fn_auditoria();
57+
58+ CREATE TRIGGER tr_auditoria_co
59+ AFTER INSERT OR UPDATE OR DELETE ON contribuicao
60+ FOR EACH ROW EXECUTE FUNCTION auditoria.fn_auditoria();
61+
62+ CREATE TRIGGER tr_auditoria_en
63+ AFTER INSERT OR UPDATE OR DELETE ON entrega
64+ FOR EACH ROW EXECUTE FUNCTION auditoria.fn_auditoria();
65+
66+ CREATE TRIGGER tr_auditoria_are
67+ AFTER INSERT OR UPDATE OR DELETE ON avaliacao_registros_execucao
68+ FOR EACH ROW EXECUTE FUNCTION auditoria.fn_auditoria();
69+ """
0 commit comments