-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_triggers.sql
105 lines (92 loc) · 5.22 KB
/
db_triggers.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
-- tables
-- changes_cache
drop table if exists changes_cache;
create table changes_cache(
ky varchar(16) not null,
value varchar(40) not null,
updated_at datetime(6) not null default now(),
status varchar(16) not null,
primary key(ky, value, status)
) engine=InnoDB default charset=utf8mb4 collate=utf8mb4_unicode_520_ci;
-- changes_cache indices
alter table changes_cache add index changes_cache_ky_idx(ky);
alter table changes_cache add index changes_cache_value_idx(value);
alter table changes_cache add index changes_cache_updated_at_idx(updated_at);
alter table changes_cache add index changes_cache_status_idx(status);
-- triggers
delimiter $
-- identities
drop trigger if exists identities_after_insert_trigger;
create trigger identities_after_insert_trigger after insert on identities
for each row begin
insert into changes_cache(ky, value, status) values('profile', new.uuid, 'pending') on duplicate key update updated_at = now();
insert into changes_cache(ky, value, status) values('identity', new.id, 'pending') on duplicate key update updated_at = now();
end$
drop trigger if exists identities_after_update_trigger;
create trigger identities_after_update_trigger after update on identities
for each row begin
if old.source != new.source or not(old.name <=> new.name) or not(old.email <=> new.email) or not(old.username <=> new.username) or not(old.uuid <=> new.uuid) then
insert into changes_cache(ky, value, status) values('profile', new.uuid, 'pending') on duplicate key update updated_at = now();
if not(old.uuid <=> new.uuid) then
insert into changes_cache(ky, value, status) values('profile', old.uuid, 'pending') on duplicate key update updated_at = now();
end if;
insert into changes_cache(ky, value, status) values('identity', new.id, 'pending') on duplicate key update updated_at = now();
end if;
end$
drop trigger if exists identities_after_delete_trigger;
create trigger identities_after_delete_trigger after delete on identities
for each row begin
insert into changes_cache(ky, value, status) values('profile', old.uuid, 'pending') on duplicate key update updated_at = now();
insert into changes_cache(ky, value, status) values('identity', old.id, 'pending') on duplicate key update updated_at = now();
end$
-- profiles
drop trigger if exists profiles_after_insert_trigger;
create trigger profiles_after_insert_trigger after insert on profiles
for each row begin
insert into changes_cache(ky, value, status) values('profile', new.uuid, 'pending') on duplicate key update updated_at = now();
end$
drop trigger if exists profiles_after_update_trigger;
create trigger profiles_after_update_trigger after update on profiles
for each row begin
if not(old.name <=> new.name) or not(old.email <=> new.email) or not(old.gender <=> new.gender) or not(old.gender_acc <=> new.gender_acc) or not(old.is_bot <=> new.is_bot) or not(old.country_code <=> new.country_code) then
insert into changes_cache(ky, value, status) values('profile', new.uuid, 'pending') on duplicate key update updated_at = now();
end if;
end$
drop trigger if exists profiles_after_delete_trigger;
create trigger profiles_after_delete_trigger after delete on profiles
for each row begin
insert into changes_cache(ky, value, status) values('profile', old.uuid, 'pending') on duplicate key update updated_at = now();
end$
-- enrollments
drop trigger if exists enrollments_after_insert_trigger;
create trigger enrollments_after_insert_trigger after insert on enrollments
for each row begin
insert into changes_cache(ky, value, status) values('enrollment', convert(new.id, char), 'pending') on duplicate key update updated_at = now();
end$
drop trigger if exists enrollments_after_update_trigger;
create trigger enrollments_after_update_trigger after update on enrollments
for each row begin
if old.uuid != new.uuid or old.organization_id != new.organization_id or old.start != new.start or old.end != new.end then
insert into changes_cache(ky, value, status) values('enrollment', convert(new.id, char), 'pending') on duplicate key update updated_at = now();
if not(old.uuid <=> new.uuid) then
insert into changes_cache(ky, value, status) values('enrollment', convert(old.id, char), 'pending') on duplicate key update updated_at = now();
end if;
end if;
end$
drop trigger if exists enrollments_after_delete_trigger;
create trigger enrollments_after_delete_trigger after delete on enrollments
for each row begin
insert into changes_cache(ky, value, status) values('enrollment', convert(old.id, char), 'pending') on duplicate key update updated_at = now();
end$
-- organizations
-- if we add new org - we don't have any new roll/profile yet - so no triggers needed
-- if we delete an org - it will cascade delete rolls with that org so "enrollments on delete" trigger will fire, we're OK
-- if we update org name - then all rolls using that org must be invalidated
drop trigger if exists organizations_after_update_trigger;
create trigger organizations_after_update_trigger after update on organizations
for each row begin
if old.name != new.name then
insert into changes_cache(ky, value, status) select 'enrollment', convert(id, char), 'pending' from enrollments where organization_id = new.id on duplicate key update updated_at = now();
end if;
end$
delimiter ;