aboutsummaryrefslogtreecommitdiff
path: root/dbinit/triggers.sql
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-03-24 15:47:11 +0100
committerlonkaars <loek@pipeframe.xyz>2023-03-24 15:47:11 +0100
commit835fa6f6461f9dece61dc6cf0ebd0887b7f367be (patch)
tree89cf1091ce58fb2ca261b700ee3dfddb7015fe20 /dbinit/triggers.sql
parent45c8c0fdb135c8eaff3d9bbb7d23426fcd57eebc (diff)
implement audit log + triggers
Diffstat (limited to 'dbinit/triggers.sql')
-rw-r--r--dbinit/triggers.sql79
1 files changed, 79 insertions, 0 deletions
diff --git a/dbinit/triggers.sql b/dbinit/triggers.sql
new file mode 100644
index 0000000..4acc242
--- /dev/null
+++ b/dbinit/triggers.sql
@@ -0,0 +1,79 @@
+drop trigger if exists endposition_ai;
+drop trigger if exists endposition_ad;
+drop trigger if exists endposition_au;
+drop trigger if exists fastestlap_ai;
+drop trigger if exists fastestlap_ad;
+drop trigger if exists fastestlap_au;
+
+delimiter $$
+create trigger endposition_ai after insert on endposition
+for each row begin
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'insert', 'endposition.memberID', cast(new.`memberID` as char)
+ from `endposition` as d where d.`ID` = new.`ID`;
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'insert', 'endposition.position', cast(new.`position` as char)
+ from `endposition` as d where d.`ID` = new.`ID`;
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'insert', 'endposition.specialPositionID', cast(new.`specialPositionID` as char)
+ from `endposition` as d where d.`ID` = new.`ID`;
+end; $$
+delimiter ;
+
+delimiter $$
+create trigger endposition_ad before delete on endposition
+for each row begin
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'delete', 'endposition.memberID', NULL
+ from `endposition` as d where d.`ID` = old.`ID`;
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'delete', 'endposition.position', NULL
+ from `endposition` as d where d.`ID` = old.`ID`;
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'delete', 'endposition.specialPositionID', NULL
+ from `endposition` as d where d.`ID` = old.`ID`;
+end; $$
+delimiter ;
+
+delimiter $$
+create trigger endposition_au after update on endposition
+for each row begin
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'update', 'endposition.memberID', cast(new.`memberID` as char)
+ from `endposition` as d where d.`ID` = new.`ID` and old.`memberID` != new.`memberID`;
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'update', 'endposition.position', cast(new.`position` as char)
+ from `endposition` as d where d.`ID` = new.`ID` and old.`position` != new.`position`;
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'update', 'endposition.specialPositionID', cast(new.`specialPositionID` as char)
+ from `endposition` as d where d.`ID` = new.`ID` and old.`specialPositionID` != new.`specialPositionID`;
+end; $$
+delimiter ;
+
+
+delimiter $$
+create trigger fastestlap_ai after insert on fastestlap
+for each row begin
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'insert', 'fastestlap.memberID', cast(new.`memberID` as char)
+ from `fastestlap` as d where d.`ID` = new.`ID`;
+end; $$
+delimiter ;
+
+delimiter $$
+create trigger fastestlap_ad before delete on fastestlap
+for each row begin
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'delete', 'fastestlap.memberID', NULL
+ from `fastestlap` as d where d.`ID` = old.`ID`;
+end; $$
+delimiter ;
+
+delimiter $$
+create trigger fastestlap_au after update on fastestlap
+for each row begin
+ insert into `formula1`.`auditlog` (`action`, `fieldName`, `newData`)
+ select 'update', 'fastestlap.memberID', cast(new.`memberID` as char)
+ from `fastestlap` as d where d.`ID` = new.`ID` and old.`memberID` != new.`memberID`;
+end; $$
+delimiter ;