-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: audit trail on custom list value insertion
- Loading branch information
1 parent
59f8f37
commit 48bbc08
Showing
6 changed files
with
104 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
repositories/migrations/20240402153800_audit_trail_trigger.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TYPE audit_operation AS ENUM('INSERT', 'UPDATE', 'DELETE'); | ||
|
||
-- CreateTable | ||
CREATE TABLE | ||
audit ( | ||
"id" UUID NOT NULL DEFAULT gen_random_uuid () PRIMARY KEY, | ||
"operation" audit_operation NOT NULL, | ||
"user_id" TEXT, | ||
"table" VARCHAR NOT NULL, | ||
"entity_id" UUID NOT NULL, | ||
"data" JSONB NOT NULL DEFAULT '{}', | ||
"created_at" TIMESTAMPTZ (6) NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
-- Order audit trigger function | ||
CREATE | ||
OR REPLACE FUNCTION global_audit () RETURNS TRIGGER AS $$ | ||
BEGIN | ||
IF (TG_OP = 'DELETE') THEN | ||
INSERT INTO audit ("operation", "user_id", "table", "entity_id", "data", "created_at") | ||
VALUES ('DELETE', current_setting('custom.current_user_id', TRUE), TG_TABLE_NAME, OLD.id, to_jsonb(OLD), now()); | ||
|
||
ELSIF (TG_OP = 'UPDATE') THEN | ||
INSERT INTO audit ("operation", "user_id", "table", "entity_id", "data", "created_at") | ||
VALUES ('UPDATE', current_setting('custom.current_user_id', TRUE), TG_TABLE_NAME, NEW.id, to_jsonb(NEW), now()); | ||
|
||
ELSIF (TG_OP = 'INSERT') THEN | ||
INSERT INTO audit ("operation", "user_id", "table", "entity_id", "data", "created_at") | ||
VALUES ('INSERT', current_setting('custom.current_user_id', TRUE), TG_TABLE_NAME, NEW.id, to_jsonb(NEW), now()); | ||
END IF; | ||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER audit | ||
AFTER INSERT | ||
OR | ||
UPDATE | ||
OR DELETE ON custom_list_values FOR EACH ROW | ||
EXECUTE FUNCTION global_audit (); | ||
|
||
-- +goose StatementEnd | ||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TRIGGER audit ON custom_list_values; | ||
|
||
DROP FUNCTION global_audit; | ||
|
||
DROP TABLE audit; | ||
|
||
DROP TYPE audit_operation; | ||
|
||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters