-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.sql
90 lines (78 loc) · 3.24 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $updatedat_stamp$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$updatedat_stamp$ LANGUAGE plpgsql;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TYPE actions AS ENUM ('create', 'update', 'delete', 'flag_on', 'flag_off', 'circuit_open', 'circuit_close');
CREATE TYPE circuit_states AS ENUM ('open', 'close', 'recovery');
CREATE TYPE recovery_profile AS ENUM ('linear', 'exponential');
CREATE TABLE IF NOT EXISTS apps (
id serial PRIMARY KEY,
title varchar(255) NOT NULL UNIQUE,
created_at timestamp NOT NULL DEFAULT NOW(),
updated_at timestamp NOT NULL DEFAULT NOW()
CHECK(length(trim(title))>0)
);
CREATE TABLE IF NOT EXISTS flags (
id serial PRIMARY KEY,
app_id integer REFERENCES apps(id) ON DELETE CASCADE,
title varchar(255) NOT NULL UNIQUE,
description varchar NOT NULL DEFAULT '',
is_active boolean NOT NULL DEFAULT false,
rollout_percentage integer NOT NULL DEFAULT 0,
white_listed_users varchar NOT NULL DEFAULT '',
error_threshold_percentage integer NOT NULL DEFAULT 0,
circuit_status circuit_states NOT NULL DEFAULT 'open',
is_recoverable boolean NOT NULL DEFAULT false,
circuit_recovery_percentage integer NOT NULL DEFAULT 0,
circuit_recovery_delay integer NOT NULL DEFAULT 100,
circuit_initial_recovery_percentage integer NOT NULL DEFAULT 100,
circuit_recovery_rate integer NOT NULL DEFAULT 100,
circuit_recovery_increment_percentage integer NOT NULL DEFAULT 1,
circuit_recovery_profile recovery_profile NOT NULL DEFAULT 'linear',
webhooks varchar NOT NULL DEFAULT '',
created_at timestamp NOT NULL DEFAULT NOW(),
updated_at timestamp NOT NULL DEFAULT NOW(),
CHECK (rollout_percentage >= 0 AND rollout_percentage <= 1000),
CHECK (error_threshold_percentage >= 0 AND error_threshold_percentage <= 1000),
CHECK (circuit_initial_recovery_percentage >= 0 AND circuit_initial_recovery_percentage <= 1000),
CHECK (circuit_recovery_percentage >= 0 AND circuit_recovery_percentage <= 1000),
CHECK (circuit_recovery_increment_percentage >= 1 AND circuit_recovery_increment_percentage <= 1000),
CHECK (circuit_recovery_delay >= 100),
CHECK (circuit_recovery_rate >= 100)
);
CREATE TABLE IF NOT EXISTS logs (
id serial PRIMARY KEY,
flag_id integer REFERENCES flags(id) ON DELETE SET NULL,
app_id integer REFERENCES apps(id) ON DELETE CASCADE,
description varchar NOT NULL DEFAULT '',
action_type actions NOT NULL,
flag_title varchar NOT NULL DEFAULT '',
flag_description varchar NOT NULL DEFAULT '',
created_at timestamp NOT NULL DEFAULT NOW(),
updated_at timestamp NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS keys (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at timestamp NOT NULL DEFAULT NOW(),
updated_at timestamp NOT NULL DEFAULT NOW()
);
CREATE OR REPLACE TRIGGER set_timestamp
BEFORE UPDATE ON apps
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATE OR REPLACE TRIGGER set_timestamp
BEFORE UPDATE ON flags
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATE OR REPLACE TRIGGER set_timestamp
BEFORE UPDATE ON logs
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATE OR REPLACE TRIGGER set_timestamp
BEFORE UPDATE ON keys
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();