-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres-sql.sql
220 lines (176 loc) · 5.61 KB
/
postgres-sql.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/************
"likes" table *************/
-- Table: public.likes
-- DROP TABLE public.likes;
CREATE TABLE public.likes
(
likeid uuid NOT NULL DEFAULT gen_random_uuid(),
userliking text COLLATE pg_catalog."default" NOT NULL,
likedpin text COLLATE pg_catalog."default" NOT NULL,
active boolean,
likeddate timestamp with time zone,
CONSTRAINT like_pkey PRIMARY KEY (likeid),
CONSTRAINT user_pin_link UNIQUE (userliking, likedpin),
CONSTRAINT users_pincid FOREIGN KEY (userliking)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT volumeid FOREIGN KEY (likedpin)
REFERENCES public.pins (volume) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.likes
OWNER to /*[YOUR OWNER USERNAME]*/;
COMMENT ON CONSTRAINT user_pin_link ON public.likes
IS 'one user per pin id';
COMMENT ON CONSTRAINT users_pincid ON public.likes
IS 'pincid not "id"';
COMMENT ON CONSTRAINT volumeid ON public.likes
IS 'pin''s volume id';
/************
"ownership" table *************/
-- Table: public.ownership
-- DROP TABLE public.ownership;
CREATE TABLE public.ownership
(
owner text COLLATE pg_catalog."default" NOT NULL,
pinid text COLLATE pg_catalog."default" NOT NULL,
date_added text COLLATE pg_catalog."default",
date_removed text COLLATE pg_catalog."default",
active boolean,
id uuid NOT NULL DEFAULT gen_random_uuid(),
tradeable boolean NOT NULL DEFAULT true,
CONSTRAINT ownership_pkey PRIMARY KEY (id),
CONSTRAINT "one owner per pinid" UNIQUE (owner, pinid),
CONSTRAINT "none" FOREIGN KEY (pinid)
REFERENCES public.pins (volume) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT owner FOREIGN KEY (owner)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.ownership
OWNER to /*[YOUR OWNER USERNAME]*/;
COMMENT ON COLUMN public.ownership.pinid
IS '"volume" from pins table';
COMMENT ON CONSTRAINT "none" ON public.ownership
IS 'volume is the pin''s uuid';
COMMENT ON CONSTRAINT owner ON public.ownership
IS 'user''s id';
-- Trigger: initlike_trigger
-- DROP TRIGGER initlike_trigger ON public.ownership;
CREATE TRIGGER initlike_trigger
AFTER INSERT
ON public.ownership
FOR EACH ROW
EXECUTE PROCEDURE public.initlike();
/************
"pins" table *************/
-- Table: public.pins
-- DROP TABLE public.pins;
CREATE TABLE public.pins
(
title text COLLATE pg_catalog."default",
href text COLLATE pg_catalog."default" NOT NULL,
image_url text COLLATE pg_catalog."default",
url text COLLATE pg_catalog."default",
active boolean DEFAULT true,
tsv tsvector,
origindate text COLLATE pg_catalog."default" DEFAULT 'unknown'::text,
volume text COLLATE pg_catalog."default" NOT NULL DEFAULT (gen_random_uuid())::text,
tags text[] COLLATE pg_catalog."default" NOT NULL DEFAULT '{None}'::text[],
lastchecked timestamp with time zone,
size text COLLATE pg_catalog."default",
fformat text COLLATE pg_catalog."default",
CONSTRAINT vol_pkey PRIMARY KEY (volume),
CONSTRAINT pins_href_key UNIQUE (href)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.pins
OWNER to /*[YOUR OWNER USERNAME]*/;
COMMENT ON COLUMN public.pins.origindate
IS 'date first added to DB';
COMMENT ON COLUMN public.pins.tags
IS 'may be redundant with ownership';
COMMENT ON COLUMN public.pins.size
IS 'bytes when added';
COMMENT ON COLUMN public.pins.fformat
IS 'type of image format';
-- Trigger: tsvectorupdate
-- DROP TRIGGER "tsvectorupdate " ON public.pins;
CREATE TRIGGER "tsvectorupdate "
BEFORE INSERT OR UPDATE
ON public.pins
FOR EACH ROW
EXECUTE PROCEDURE public.tsv_trigger();
/************
"session" table *************/
-- Table: public.session
-- DROP TABLE public.session;
CREATE TABLE public.session
(
sid character varying COLLATE pg_catalog."default" NOT NULL,
sess json NOT NULL,
expire timestamp(6) without time zone NOT NULL,
CONSTRAINT session_pkey PRIMARY KEY (sid)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.session
OWNER to /*[YOUR OWNER USERNAME]*/ ;
/************
"users" table *************/
-- Table: public.users
-- DROP TABLE public.users;
CREATE TABLE public.users
(
id text COLLATE pg_catalog."default" NOT NULL,
"displayName" text COLLATE pg_catalog."default",
gender text COLLATE pg_catalog."default",
locations text[] COLLATE pg_catalog."default",
ownership text[] COLLATE pg_catalog."default",
city text COLLATE pg_catalog."default",
state text COLLATE pg_catalog."default",
pinc_id uuid NOT NULL DEFAULT gen_random_uuid(),
image_url text COLLATE pg_catalog."default",
CONSTRAINT users_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.users
OWNER to /*[YOUR OWNER USERNAME]*/ ;
COMMENT ON COLUMN public.users.image_url
IS 'profile image';
/*Trigger Functions*/
--initlike
BEGIN
INSERT INTO public.likes("userliking","likedpin","likeddate","active")
VALUES(NEW.owner, NEW.pinid, NEW.date_added::timestamptz, true);
RETURN NEW;
END;
--tsv_trigger
BEGIN
new.tsv :=
to_tsvector('pg_catalog.english', coalesce(new.title,'')) ||
to_tsvector('pg_catalog.english', coalesce(new.volume,'')) ||
to_tsvector('pg_catalog.english', coalesce(array_to_string(new.tags,' '),''));
return new;
END