-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres-sql.sql
212 lines (178 loc) · 5.57 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
/*Tables*/
-- Table: public.books
-- DROP TABLE public.books;
CREATE TABLE public.books
(
title text COLLATE pg_catalog."default",
isbn13 text COLLATE pg_catalog."default" NOT NULL,
pages text COLLATE pg_catalog."default",
image_url text COLLATE pg_catalog."default",
language text COLLATE pg_catalog."default",
url text COLLATE pg_catalog."default",
active boolean DEFAULT true,
tsv tsvector,
publisheddate text COLLATE pg_catalog."default" DEFAULT 'unknown'::text,
volume text COLLATE pg_catalog."default" NOT NULL DEFAULT (gen_random_uuid())::text,
authors text[] COLLATE pg_catalog."default" NOT NULL DEFAULT '{None}'::text[],
CONSTRAINT vol_pkey PRIMARY KEY (volume)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.books
OWNER to /*{yourDBUserName}*/;
-- Trigger: tsvectorupdate
-- DROP TRIGGER "tsvectorupdate " ON public.books;
CREATE TRIGGER "tsvectorupdate "
BEFORE INSERT OR UPDATE
ON public.books
FOR EACH ROW
EXECUTE PROCEDURE public.tsv_trigger();
/*************/
-- Table: public.ownership
-- DROP TABLE public.ownership;
CREATE TABLE public.ownership
(
owner text COLLATE pg_catalog."default" NOT NULL,
bookid 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 owned_book FOREIGN KEY (bookid)
REFERENCES public.books (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 /*{yourDBUserName}*/;
COMMENT ON CONSTRAINT owner ON public.ownership
IS 'user''s id';
-- Trigger: cancelAnyTrades
-- DROP TRIGGER "cancelAnyTrades" ON public.ownership;
CREATE TRIGGER "cancelAnyTrades"
AFTER UPDATE OF active
ON public.ownership
FOR EACH ROW
WHEN ((new.active = false))
EXECUTE PROCEDURE public."tradesCancel"();
/*************/
-- 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 /*{yourDBUserName}*/;
/*************/
-- Table: public.trades
-- DROP TABLE public.trades;
CREATE TABLE public.trades
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
proposer text COLLATE pg_catalog."default",
receiver text COLLATE pg_catalog."default",
status text COLLATE pg_catalog."default" DEFAULT 'created'::text,
active boolean NOT NULL DEFAULT true,
date_proposed timestamp with time zone,
date_responded timestamp with time zone,
paired_trade uuid,
pro_ownership uuid,
rec_ownership uuid,
CONSTRAINT trades_pkey PRIMARY KEY (id),
CONSTRAINT linked_trade FOREIGN KEY (paired_trade)
REFERENCES public.trades (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT proposer FOREIGN KEY (proposer)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT proposers_own_record FOREIGN KEY (pro_ownership)
REFERENCES public.ownership (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT receiver FOREIGN KEY (receiver)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT receivers_own_record FOREIGN KEY (rec_ownership)
REFERENCES public.ownership (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.trades
OWNER to /*{yourDBUserName}*/;
COMMENT ON COLUMN public.trades.date_proposed
IS 'date the trade was requested';
COMMENT ON COLUMN public.trades.date_responded
IS 'when the receiver responds to trade';
COMMENT ON COLUMN public.trades.paired_trade
IS 'the trades record to link to this trade';
COMMENT ON CONSTRAINT proposer ON public.trades
IS 'id of trade proposer';
/*************/
-- 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",
CONSTRAINT users_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.users
OWNER to /*{yourDBUserName}*/;
/*Trigger functions*/
/****
"tradesCancel"
To cancel pending trades when a book ownership is removed
******/
BEGIN
UPDATE trades
set status = 'CANCELED'
WHERE pro_ownership = NEW.id OR rec_ownership = NEW.id;
RETURN NEW;
END;
/****
"tsv_trigger"
To handle search functionality.
******/
BEGIN
new.tsv :=
to_tsvector('pg_catalog.english', coalesce(new.title,'')) ||
to_tsvector('pg_catalog.english', coalesce(new.isbn13,'')) ||
to_tsvector('pg_catalog.english', coalesce(array_to_string(new.authors,' '),''));
return new;
END