forked from UTSCC09/project-SUPERBIDDO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbBackup.sql
292 lines (235 loc) · 8.42 KB
/
dbBackup.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
CREATE EXTENSION IF NOT EXISTS "citext";
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
CREATE OR REPLACE FUNCTION public.check_auction_ongoing()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
DECLARE
_start_time timestamptz;
_end_time timestamptz;
BEGIN
SELECT start_time, end_time INTO _start_time, _end_time FROM auction WHERE auction_id = NEW.auction_id;
IF
_start_time IS NULL OR _start_time > now() THEN
RAISE EXCEPTION 'auction_not_started'
USING CONSTRAINT = 'auction_not_started';
END IF;
IF
_end_time < now() THEN
RAISE EXCEPTION 'auction_ended'
USING CONSTRAINT = 'auction_ended';
END IF;
RETURN NEW;
END;
$function$
;
CREATE OR REPLACE FUNCTION public.check_bid_valid()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
DECLARE
max_current_bid NUMERIC(12,2);
current_leading_bidder_id UUID;
auction_spread NUMERIC(12,2);
min_valid_bid NUMERIC(12,2);
BEGIN
SELECT amount, bidder_id INTO max_current_bid, current_leading_bidder_id FROM bid WHERE auction_id = NEW.auction_id ORDER BY amount DESC LIMIT 1;
IF current_leading_bidder_id = NEW.bidder_id THEN
RAISE EXCEPTION 'already_leading'
USING CONSTRAINT = 'already_leading';
END IF;
IF max_current_bid IS NULL THEN
max_current_bid := (SELECT start_price FROM auction WHERE auction_id = NEW.auction_id);
END IF;
SELECT spread INTO auction_spread FROM auction WHERE auction_id = NEW.auction_id;
min_valid_bid := max_current_bid + auction_spread;
IF
NEW.amount < min_valid_bid THEN
RAISE EXCEPTION 'bid_amount_insufficient'
USING CONSTRAINT = 'bid_amount_insufficient',
HINT = FORMAT('Must bid at least %s (current bid amount + spread).', min_valid_bid);
END IF;
RETURN NEW;
END;
$function$
;
CREATE OR REPLACE FUNCTION public.check_bidder_not_auctioneer()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
DECLARE _auctioneer_id uuid;
BEGIN
SELECT auctioneer_id INTO _auctioneer_id FROM auction WHERE auction_id = NEW.auction_id;
IF
NEW.bidder_id = _auctioneer_id THEN
RAISE EXCEPTION 'bidder_same_as_auctioneer'
USING CONSTRAINT = 'bidder_same_as_auctioneer';
END IF;
RETURN NEW;
END;
$function$
;
CREATE OR REPLACE FUNCTION public.check_start_timestamp_in_future()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
IF NEW.start_time < now() - INTERVAL '1 second' THEN -- 1 SECOND buffer FOR TRIGGER TO run
RAISE EXCEPTION 'start_time_in_past';
END IF;
RETURN NEW;
END;
$function$
;
CREATE OR REPLACE FUNCTION public.set_column_to_now()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
-- Set target_column to the value of source_column
NEW := json_populate_record(NEW, json_build_object(TG_ARGV[0], now()));
RETURN NEW;
END;
$function$
;
-- public.account definition
-- Drop table
-- DROP TABLE public.account;
CREATE TABLE public.account (
account_id uuid NOT NULL DEFAULT gen_random_uuid(),
username varchar(40) NOT NULL,
email public."citext" NOT NULL,
passhash bpchar(60) NULL,
address_formatted varchar(300) NULL,
longitude float8 NULL,
latitude float8 NULL,
CONSTRAINT account_email_un UNIQUE (email),
CONSTRAINT account_pk PRIMARY KEY (account_id)
);
-- public."session" definition
-- Drop table
-- DROP TABLE public."session";
CREATE TABLE public."session" (
sid varchar NOT NULL,
sess json NOT NULL,
expire timestamp(6) NOT NULL,
CONSTRAINT session_pkey PRIMARY KEY (sid)
);
CREATE INDEX "IDX_session_expire" ON public.session USING btree (expire);
-- public.auction definition
-- Drop table
-- DROP TABLE public.auction;
CREATE TABLE public.auction (
auction_id uuid NOT NULL DEFAULT gen_random_uuid(),
auctioneer_id uuid NOT NULL,
"name" varchar(100) NOT NULL,
description varchar(500) NULL,
start_price numeric(12, 2) NOT NULL,
spread numeric(12, 2) NOT NULL,
start_time timestamptz NULL DEFAULT now(),
end_time timestamptz NULL,
CONSTRAINT auction_check_min_duration CHECK (((end_time - '00:05:00'::interval) >= start_time)),
CONSTRAINT auction_check_spread_positive CHECK ((spread >= (0)::numeric)),
CONSTRAINT auction_check_start_price_positive CHECK ((start_price >= (0)::numeric)),
CONSTRAINT auction_pk PRIMARY KEY (auction_id),
CONSTRAINT auction_fk_auctioneer FOREIGN KEY (auctioneer_id) REFERENCES public.account(account_id)
);
CREATE INDEX auction_auctioneer_id_idx ON public.auction USING btree (auctioneer_id);
CREATE INDEX trgm_idx ON public.auction USING gist (name gist_trgm_ops);
-- Table Triggers
CREATE TRIGGER check_start_time_in_future_trigger BEFORE
INSERT
ON
public.auction FOR EACH ROW EXECUTE FUNCTION check_start_timestamp_in_future();
-- public.bid definition
-- Drop table
-- DROP TABLE public.bid;
CREATE TABLE public.bid (
bid_id uuid NOT NULL DEFAULT gen_random_uuid(),
auction_id uuid NOT NULL,
bidder_id uuid NOT NULL,
amount numeric(12, 2) NOT NULL,
"timestamp" timestamptz NOT NULL DEFAULT now(),
CONSTRAINT bid_pk PRIMARY KEY (bid_id),
CONSTRAINT bid_fk_auction FOREIGN KEY (auction_id) REFERENCES public.auction(auction_id) ON DELETE CASCADE,
CONSTRAINT bid_fk_bidder FOREIGN KEY (bidder_id) REFERENCES public.account(account_id)
);
CREATE INDEX bid_auction_id_idx ON public.bid USING btree (auction_id);
CREATE INDEX bid_bidder_id_idx ON public.bid USING btree (bidder_id);
-- Table Triggers
CREATE TRIGGER check_auction_ongoing_trigger BEFORE
INSERT
ON
public.bid FOR EACH ROW EXECUTE FUNCTION check_auction_ongoing();
CREATE TRIGGER check_bid_valid_trigger BEFORE
INSERT
ON
public.bid FOR EACH ROW EXECUTE FUNCTION check_bid_valid();
CREATE TRIGGER check_bidder_not_auctioneer_trigger BEFORE
INSERT
ON
public.bid FOR EACH ROW EXECUTE FUNCTION check_bidder_not_auctioneer();
CREATE TRIGGER set_bid_timestamp_now_trigger BEFORE
INSERT
ON
public.bid FOR EACH ROW EXECUTE FUNCTION set_column_to_now('timestamp');
-- public.bundle definition
-- Drop table
-- DROP TABLE public.bundle;
CREATE TABLE public.bundle (
bundle_id uuid NOT NULL DEFAULT gen_random_uuid(),
auction_id uuid NOT NULL,
game varchar(100) NOT NULL,
"name" varchar(100) NOT NULL,
description varchar(500) NULL,
manufacturer varchar(100) NOT NULL,
"set" varchar(100) NOT NULL,
image_url varchar(140) NOT NULL,
CONSTRAINT bundle_pk PRIMARY KEY (bundle_id),
CONSTRAINT bundle_fk_auction FOREIGN KEY (auction_id) REFERENCES public.auction(auction_id) ON DELETE CASCADE
);
CREATE INDEX bundle_auction_id_idx ON public.bundle USING btree (auction_id);
-- public.card definition
-- Drop table
-- DROP TABLE public.card;
CREATE TABLE public.card (
card_id uuid NOT NULL DEFAULT gen_random_uuid(),
auction_id uuid NOT NULL,
game varchar(100) NOT NULL,
"name" varchar(100) NOT NULL,
description varchar(500) NULL,
manufacturer varchar(100) NOT NULL,
quality_ungraded varchar(100) NULL,
rarity varchar(100) NOT NULL,
"set" varchar(100) NULL,
is_foil bool NOT NULL,
quality_psa int4 NULL,
image_url varchar(140) NOT NULL,
CONSTRAINT card_check_exact_one_quality CHECK (((quality_ungraded IS NULL) <> (quality_psa IS NULL))),
CONSTRAINT card_check_psa_range CHECK (((quality_psa IS NULL) OR ((1 <= quality_psa) AND (quality_psa <= 10)))),
CONSTRAINT card_pk PRIMARY KEY (card_id),
CONSTRAINT card_fk_auction FOREIGN KEY (auction_id) REFERENCES public.auction(auction_id) ON DELETE CASCADE
);
CREATE INDEX card_auction_id_idx ON public.card USING btree (auction_id);
-- public.recommendation definition
-- Drop table
-- DROP TABLE public.recommendation;
CREATE TABLE public.recommendation (
account_id uuid NOT NULL,
game varchar(30) NOT NULL,
price numeric(12, 2) NOT NULL,
"action" varchar(30) NOT NULL,
"timestamp" timestamptz NOT NULL DEFAULT now(),
CONSTRAINT recommendation_pk PRIMARY KEY (account_id, "timestamp"),
CONSTRAINT recommendation_fk_account FOREIGN KEY (account_id) REFERENCES public.account(account_id)
);
-- public.watch definition
-- Drop table
-- DROP TABLE public.watch;
CREATE TABLE public.watch (
account_id uuid NOT NULL,
auction_id uuid NOT NULL,
CONSTRAINT watch_pk PRIMARY KEY (account_id, auction_id),
CONSTRAINT watch_fk_account FOREIGN KEY (account_id) REFERENCES public.account(account_id),
CONSTRAINT watch_fk_auction FOREIGN KEY (auction_id) REFERENCES public.auction(auction_id) ON DELETE CASCADE
);