Skip to content

Commit 9ed0ba9

Browse files
committed
create triggers for audit table
1 parent 751e217 commit 9ed0ba9

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

src/db_audit.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
"""

src/db_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from sqlalchemy.orm import DeclarativeBase
99
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
1010
from sqlalchemy.sql import text
11+
from db_audit import AUDIT_DDL
1112

1213
SQLALCHEMY_DATABASE_URL = os.environ["SQLALCHEMY_DATABASE_URL"]
1314

@@ -29,6 +30,11 @@ async def create_db_and_tables():
2930
async with engine.begin() as conn:
3031
await conn.run_sync(Base.metadata.create_all)
3132

33+
async def create_audit_ddl():
34+
35+
async with engine.begin() as conn:
36+
await conn.execute(text(AUDIT_DDL))
37+
3238

3339
async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
3440
"""Retorna a sessão do banco de dados.

0 commit comments

Comments
 (0)