Skip to content

Commit

Permalink
Merge c09b013 into c7562d1
Browse files Browse the repository at this point in the history
  • Loading branch information
mfshao committed Nov 19, 2019
2 parents c7562d1 + c09b013 commit e487294
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions fence/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,45 @@ def migrate(driver):
FOR EACH ROW EXECUTE PROCEDURE process_user_audit();"""
)

session.execute(
"""\
CREATE OR REPLACE FUNCTION process_cert_audit() RETURNS TRIGGER AS $cert_audit$
BEGIN
IF (TG_OP = 'DELETE') THEN
INSERT INTO cert_audit_logs (timestamp, operation, user_id, username, old_values)
SELECT now(), 'DELETE', "User".id, "User".username, row_to_json(OLD)
FROM certificate INNER JOIN application ON certificate.application_id = application.id
INNER JOIN "User" ON application.user_id = "User".id;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO cert_audit_logs (timestamp, operation, user_id, username, old_values, new_values)
SELECT now(), 'UPDATE', "User".id, "User".username, row_to_json(OLD), row_to_json(NEW)
FROM certificate INNER JOIN application ON certificate.application_id = application.id
INNER JOIN "User" ON application.user_id = "User".id;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO cert_audit_logs (timestamp, operation, user_id, username, new_values)
SELECT now(), 'INSERT', "User".id, "User".username, row_to_json(NEW)
FROM certificate INNER JOIN application ON certificate.application_id = application.id
INNER JOIN "User" ON application.user_id = "User".id;
RETURN NEW;
END IF;
RETURN NULL;
END;
$cert_audit$ LANGUAGE plpgsql;"""
)

exist = session.scalar(
"SELECT exists (SELECT * FROM pg_trigger WHERE tgname = 'cert_audit')"
)
session.execute(
("DROP TRIGGER cert_audit ON certificate; " if exist else "")
+ """\
CREATE TRIGGER cert_audit
AFTER INSERT OR UPDATE OR DELETE ON certificate
FOR EACH ROW EXECUTE PROCEDURE process_cert_audit();"""
)


def add_foreign_key_column_if_not_exist(
table_name,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ setuptools>=40.3.0
six==1.11.0
SQLAlchemy==1.3.3
temps==0.3.0
userdatamodel==2.2.0
git+https://github.com/uc-cdis/userdatamodel.git@256b26763f8d725131ab0e09a3b9681bbcd50ba9
Werkzeug==0.16.0
pyyaml==5.1
retry==0.9.2
Expand Down

0 comments on commit e487294

Please sign in to comment.