-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path1_initialize_schema.up.sql
62 lines (53 loc) · 2.57 KB
/
1_initialize_schema.up.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
-- create tables
CREATE TYPE consent_status AS ENUM ('approved', 'denied', 'pending');
CREATE TABLE IF NOT EXISTS trust_domains
(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL UNIQUE,
description TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS relationships
(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
trust_domain_a_id UUID NOT NULL,
trust_domain_b_id UUID NOT NULL,
trust_domain_a_consent consent_status NOT NULL DEFAULT 'pending',
trust_domain_b_consent consent_status NOT NULL DEFAULT 'pending',
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
UNIQUE (trust_domain_a_id, trust_domain_b_id)
);
CREATE TABLE IF NOT EXISTS bundles
(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
trust_domain_id UUID NOT NULL UNIQUE,
data BYTEA NOT NULL,
digest BYTEA NOT NULL,
signature BYTEA,
signing_certificate BYTEA,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS join_tokens
(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
trust_domain_id UUID NOT NULL,
token TEXT NOT NULL UNIQUE,
used BOOL NOT NULL DEFAULT FALSE,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
-- define foreign keys
ALTER TABLE "relationships"
ADD FOREIGN KEY ("trust_domain_a_id") REFERENCES "trust_domains" ("id");
ALTER TABLE "relationships"
ADD FOREIGN KEY ("trust_domain_b_id") REFERENCES "trust_domains" ("id");
ALTER TABLE "bundles"
ADD FOREIGN KEY ("trust_domain_id") REFERENCES "trust_domains" ("id");
ALTER TABLE "join_tokens"
ADD FOREIGN KEY ("trust_domain_id") REFERENCES "trust_domains" ("id");
-- create indexes
-- PostgresSQL automatically creates a unique index when a unique constraint or primary key is defined for a table