-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sql
74 lines (64 loc) · 1.14 KB
/
init.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
CREATE TABLE public.cities
(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE public.langs
(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);
CREATE FUNCTION public.trigger_check_email()
RETURNS TRIGGER
AS
$$
BEGIN
IF (strpos(NEW.email, '@') = 0) THEN
RAISE EXCEPTION 'email is not valid';
END IF;
RETURN NEW;
END
$$
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER;
CREATE FUNCTION public.trigger_lower_username()
RETURNS TRIGGER
AS
$$
BEGIN
NEW.username = lower(NEW.username);
RETURN NEW;
END
$$
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER;
CREATE FUNCTION public.trigger_check_username()
RETURNS TRIGGER
AS
$$
BEGIN
IF (NEW.username ~* '^[a-z0-9]+$') THEN
RAISE EXCEPTION 'username is not valid';
END IF;
RETURN NEW;
END
$$
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER;
CREATE FUNCTION public.trigger_auto_bio()
RETURNS TRIGGER
AS
$$
BEGIN
IF (NEW.bio IS NULL OR length(NEW.bio) = 0) THEN
NEW.bio = '----';
END IF;
RETURN NEW;
END
$$
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER;