This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.sql
191 lines (134 loc) · 5.35 KB
/
setup.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
--
-- Create tables for a Jinaga database
--
-- Before executing, be sure you have created the database and the dev role.
--
-- CREATE DATABASE myapplication;
-- \connect myapplication
--
-- CREATE USER dev WITH
-- LOGIN
-- ENCRYPTED PASSWORD 'devpassword'
-- NOSUPERUSER
-- INHERIT
-- NOCREATEDB
-- NOCREATEROLE
-- NOREPLICATION
-- VALID UNTIL 'infinity';
--
DO
$do$
BEGIN
--
-- Edge
--
IF (SELECT to_regclass('public.edge') IS NULL) THEN
CREATE TABLE public.edge (
successor_type character varying(50),
successor_hash character varying(100),
predecessor_type character varying(50),
predecessor_hash character varying(100),
role character varying(20)
);
ALTER TABLE public.edge OWNER TO postgres;
-- Most unique first, for fastest uniqueness check on insert.
CREATE UNIQUE INDEX ux_edge ON public.edge USING btree (successor_hash, predecessor_hash, role, successor_type, predecessor_type);
-- Covering index based on successor, favoring most likely members of WHERE clause.
CREATE INDEX ix_successor ON public.edge USING btree (successor_hash, role, successor_type, predecessor_hash, predecessor_type);
-- Covering index based on predecessor, favoring most likely members of WHERE clause.
CREATE INDEX ix_predecessor ON public.edge USING btree (predecessor_hash, role, predecessor_type, successor_hash, successor_type);
GRANT SELECT,INSERT ON TABLE public.edge TO dev;
END IF;
--
-- Fact
--
IF (SELECT to_regclass('public.fact') IS NULL) THEN
CREATE TABLE public.fact (
type character varying(50),
hash character varying(100),
fields jsonb,
predecessors jsonb,
date_learned timestamp NOT NULL
DEFAULT (now() at time zone 'utc')
);
ALTER TABLE public.fact OWNER TO postgres;
CREATE UNIQUE INDEX ux_fact ON public.fact USING btree (hash, type);
GRANT SELECT,INSERT ON TABLE public.fact TO dev;
ELSE
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_schema='public' AND table_name='fact' AND column_name='date_learned') THEN
ALTER TABLE public.fact
ADD date_learned timestamp NOT NULL
DEFAULT (now() at time zone 'utc');
END IF;
END IF;
--
-- User
--
IF (SELECT to_regclass('public.user') IS NULL) THEN
CREATE TABLE public."user" (
provider character varying(100),
user_id character varying(50),
private_key character varying(1800),
public_key character varying(500)
);
ALTER TABLE public."user" OWNER TO postgres;
CREATE UNIQUE INDEX ux_user ON public."user" USING btree (user_id, provider);
CREATE UNIQUE INDEX ux_user_public_key ON public."user" (public_key);
GRANT SELECT,INSERT ON TABLE public."user" TO dev;
ELSE
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_schema='public' AND table_name='user' AND column_name='public_key'
AND character_maximum_length >= 500) THEN
ALTER TABLE public.user
ALTER COLUMN public_key TYPE character varying(500);
END IF;
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_schema='public' AND table_name='user' AND column_name='private_key'
AND character_maximum_length >= 1800) THEN
ALTER TABLE public.user
ALTER COLUMN private_key TYPE character varying(1800);
END IF;
IF (SELECT to_regclass('public.duplicate_users') IS NULL) THEN
CREATE TABLE public.duplicate_users AS
SELECT u.provider, u.user_id, u.public_key, u.private_key
FROM public."user" AS u
JOIN (SELECT public_key FROM public."user" GROUP BY public_key HAVING count(*) > 1) AS dup
ON dup.public_key = u.public_key;
DELETE FROM public."user" AS u
WHERE EXISTS (SELECT 1 FROM public.duplicate_users AS d
WHERE d.provider = u.provider AND d.user_id = u.user_id);
CREATE UNIQUE INDEX IF NOT EXISTS ux_user_public_key ON public."user" (public_key);
END IF;
END IF;
--
-- Signature
--
IF (SELECT to_regclass('public.signature') IS NULL) THEN
CREATE TABLE public."signature" (
type character varying(50),
hash character varying(100),
public_key character varying(500),
signature character varying(400),
date_learned timestamp NOT NULL
DEFAULT (now() at time zone 'utc')
);
ALTER TABLE public."signature" OWNER TO postgres;
CREATE UNIQUE INDEX ux_signature ON public."signature" USING btree (hash, public_key, type);
GRANT SELECT,INSERT ON TABLE public."signature" TO dev;
ELSE
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_schema='public' AND table_name='signature' AND column_name='public_key'
AND character_maximum_length >= 500) THEN
ALTER TABLE public.signature
ALTER COLUMN public_key TYPE character varying(500);
END IF;
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_schema='public' AND table_name='signature' AND column_name='signature'
AND character_maximum_length >= 400) THEN
ALTER TABLE public.signature
ALTER COLUMN signature TYPE character varying(400);
END IF;
END IF;
END
$do$