-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_db.sql
96 lines (69 loc) · 1.88 KB
/
init_db.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
-- FUNCTION: public.DeleteAccount()
-- DROP FUNCTION public."DeleteAccount"() CASCADE;
CREATE FUNCTION public."DeleteAccount"()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
BEGIN
DELETE FROM public."Accounts" WHERE "accountUuid"=OLD.id;
RETURN OLD;
END
$BODY$;
ALTER FUNCTION public."DeleteAccount"()
OWNER TO postgres;
-- FUNCTION: public.DuplicateAccount()
-- DROP FUNCTION public."DuplicateAccount"() CASCADE;
CREATE FUNCTION public."DuplicateAccount"()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
BEGIN
INSERT INTO public."Accounts" VALUES (NEW.id, NEW.traits::json->'email', null, NOW(), NOW());
RETURN NEW;
END
$BODY$;
ALTER FUNCTION public."DuplicateAccount"()
OWNER TO postgres;
-- FUNCTION: public.UpdateAccount()
-- DROP FUNCTION public."UpdateAccount"() CASCADE;
CREATE FUNCTION public."UpdateAccount"()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
BEGIN
UPDATE public."Accounts" SET
email=NEW.traits::json->'email',
"updatedAt"=NOW()
WHERE "accountUuid"=OLD.id;
RETURN OLD;
END
$BODY$;
ALTER FUNCTION public."UpdateAccount"()
OWNER TO postgres;
-- Trigger: DeleteAccount
-- DROP TRIGGER "DeleteAccount" ON public.identities;
CREATE TRIGGER "DeleteAccount"
BEFORE DELETE
ON public.identities
FOR EACH ROW
EXECUTE PROCEDURE public."DeleteAccount"();
-- Trigger: DuplicateAccount
-- DROP TRIGGER "DuplicateAccount" ON public.identities;
CREATE TRIGGER "DuplicateAccount"
AFTER INSERT
ON public.identities
FOR EACH ROW
EXECUTE PROCEDURE public."DuplicateAccount"();
-- Trigger: UpdateAccount
-- DROP TRIGGER "UpdateAccount" ON public.identities;
CREATE TRIGGER "UpdateAccount"
BEFORE UPDATE
ON public.identities
FOR EACH ROW
EXECUTE PROCEDURE public."UpdateAccount"();