-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase-schema
460 lines (355 loc) · 12.9 KB
/
database-schema
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
--
-- PostgreSQL database dump
--
-- Dumped from database version 12.8 (Ubuntu 12.8-0ubuntu0.20.04.1)
-- Dumped by pg_dump version 12.8 (Ubuntu 12.8-0ubuntu0.20.04.1)
-- Started on 2021-08-30 18:14:10 PDT
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- TOC entry 212 (class 1255 OID 33045)
-- Name: delete_deposit(bigint); Type: PROCEDURE; Schema: public; Owner: -
--
CREATE PROCEDURE public.delete_deposit(in_deposit_id bigint)
LANGUAGE plpgsql
AS $$
DECLARE
this_deposit deposit_table%ROWTYPE;
BEGIN
SELECT * INTO this_deposit FROM deposit_table WHERE deposit_id=in_deposit_id;
UPDATE company_table
SET deposit_count = deposit_count - 1
WHERE company_id=this_deposit.company_id;
UPDATE company_table
SET deposit_total = deposit_total - this_deposit.value
WHERE company_id=this_deposit.company_id;
DELETE FROM deposit_table WHERE deposit_id=in_deposit_id;
END;
$$;
--
-- TOC entry 225 (class 1255 OID 33046)
-- Name: delete_expense(bigint); Type: PROCEDURE; Schema: public; Owner: -
--
CREATE PROCEDURE public.delete_expense(in_expense_id bigint)
LANGUAGE plpgsql
AS $$
DECLARE
this_expense expense_table%ROWTYPE;
BEGIN
SELECT * INTO this_expense FROM expense_table WHERE expense_id=in_expense_id;
UPDATE company_table
SET expense_count = expense_count - 1
WHERE company_id=this_expense.company_id;
UPDATE company_table
SET expense_total = expense_total - (this_expense.quantity*this_expense.price_per_unit)
WHERE company_id=this_expense.company_id;
DELETE FROM expense_table WHERE expense_id=in_expense_id;
END;
$$;
--
-- TOC entry 226 (class 1255 OID 33047)
-- Name: delete_transaction(bigint); Type: PROCEDURE; Schema: public; Owner: -
--
CREATE PROCEDURE public.delete_transaction(in_transaction_id bigint)
LANGUAGE plpgsql
AS $$
DECLARE
this_transaction transaction_table%ROWTYPE;
BEGIN
SELECT * INTO this_transaction FROM transaction_table WHERE transaction_id=in_transaction_id;
UPDATE company_table
SET transaction_count = transaction_count - 1
WHERE company_id=this_transaction.company_id;
UPDATE company_table
SET transaction_total = transaction_total - (this_transaction.quantity*this_transaction.price_per_unit)
WHERE company_id=this_transaction.company_id;
UPDATE company_table
SET product_count = product_count - this_transaction.quantity
WHERE company_id=this_transaction.company_id;
IF this_transaction.payment_method = 'square' THEN
UPDATE business
SET square_total = square_total - this_transaction.quantity
WHERE company_id=this_transaction.company_id;
END IF;
DELETE FROM transaction_table WHERE transaction_id=in_transaction_id;
END;
$$;
--
-- TOC entry 228 (class 1255 OID 33048)
-- Name: insert_deposit(text, text, money, text); Type: PROCEDURE; Schema: public; Owner: -
--
CREATE PROCEDURE public.insert_deposit(in_company_id text, in_user_id text, in_value money, in_description text)
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO deposit_table (user_id, company_id, value, description)
VALUES (in_user_id, in_company_id, in_value, in_description);
UPDATE company_table
SET deposit_count = deposit_count + 1
WHERE company_id=in_company_id;
UPDATE company_table
SET deposit_total = deposit_total + in_value
WHERE company_id=in_company_id;
END;
$$;
--
-- TOC entry 227 (class 1255 OID 33049)
-- Name: insert_expense(text, text, text, text, numeric, date, text, money, text); Type: PROCEDURE; Schema: public; Owner: -
--
CREATE PROCEDURE public.insert_expense(in_user_id text, in_company_id text, in_product text, in_company text, in_quantity numeric, in_date date, in_payment text, in_price money, in_description text)
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO expense_table (user_id, company_id, product, company, quantity, date, payment_method, price_per_unit, description)
VALUES (in_user_id, in_company_id, in_product, in_company, in_quantity, in_date, in_payment, in_price, in_description);
UPDATE company_table
SET expense_count = expense_count + 1
WHERE company_id=in_company_id;
UPDATE company_table
SET expense_total = expense_total + (in_quantity*in_price)
WHERE company_id=in_company_id;
END;
$$;
--
-- TOC entry 229 (class 1255 OID 33053)
-- Name: insert_transaction(text, text, text, text, date, text, numeric, money); Type: PROCEDURE; Schema: public; Owner: -
--
CREATE PROCEDURE public.insert_transaction(in_user_id text, in_company_id text, in_customer text, in_product text, in_date date, in_payment text, in_quantity numeric, in_price money)
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO transaction_table (user_id, company_id, customer, date, product, payment_method, quantity, price_per_unit)
VALUES (in_user_id, in_company_id, in_customer, in_date, in_product, in_payment, in_quantity, in_price);
UPDATE company_table
SET transaction_count = transaction_count + 1
WHERE company_id=in_company_id;
UPDATE company_table
SET transaction_total = transaction_total + (in_quantity*in_price)
WHERE company_id=in_company_id;
UPDATE company_table
SET product_count = product_count + in_quantity
WHERE company_id=in_company_id;
IF in_payment::text = 'square' then
UPDATE company_table
SET square_total = square_total + (in_quantity*in_price)
WHERE company_id=in_company_id;
END IF;
END;
$$;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- TOC entry 203 (class 1259 OID 32954)
-- Name: company_table; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.company_table (
company_id text NOT NULL,
section text,
instructor text,
profit_goal money DEFAULT 500 NOT NULL,
stretch_profit_goal money DEFAULT 750 NOT NULL,
profit money GENERATED ALWAYS AS ((transaction_total - expense_total)) STORED NOT NULL,
expense_total money DEFAULT 0 NOT NULL,
transaction_total money DEFAULT 0 NOT NULL,
deposit_total money DEFAULT 0 NOT NULL,
square_total money DEFAULT 0 NOT NULL,
expense_count integer DEFAULT 0 NOT NULL,
transaction_count integer DEFAULT 0 NOT NULL,
deposit_count integer DEFAULT 0 NOT NULL,
product_count integer DEFAULT 0 NOT NULL
);
--
-- TOC entry 211 (class 1259 OID 33041)
-- Name: company_view; Type: VIEW; Schema: public; Owner: -
--
CREATE VIEW public.company_view AS
SELECT company_table.company_id,
company_table.section,
company_table.instructor,
(company_table.square_total)::numeric AS square_total,
(company_table.deposit_total)::numeric AS deposit_total,
(company_table.expense_total)::numeric AS expense_total,
(company_table.transaction_total)::numeric AS transaction_total,
company_table.transaction_count,
company_table.expense_count,
company_table.deposit_count,
company_table.product_count,
(company_table.profit)::numeric AS profit,
(company_table.profit_goal)::numeric AS profit_goal,
(company_table.stretch_profit_goal)::numeric AS stretch_profit_goal
FROM public.company_table;
--
-- TOC entry 205 (class 1259 OID 32975)
-- Name: deposit_table; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.deposit_table (
deposit_id bigint NOT NULL,
company_id text NOT NULL,
user_id text DEFAULT 'none'::text NOT NULL,
value money NOT NULL,
description text,
date date DEFAULT CURRENT_DATE NOT NULL
);
--
-- TOC entry 204 (class 1259 OID 32973)
-- Name: deposit_table_deposit_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.deposit_table ALTER COLUMN deposit_id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME public.deposit_table_deposit_id_seq
START WITH 0
INCREMENT BY 1
MINVALUE 0
MAXVALUE 20000
CACHE 1
CYCLE
);
--
-- TOC entry 207 (class 1259 OID 32993)
-- Name: expense_table; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.expense_table (
expense_id bigint NOT NULL,
company_id text NOT NULL,
user_id text NOT NULL,
product text NOT NULL,
company text NOT NULL,
payment_method text NOT NULL,
description text NOT NULL,
quantity numeric NOT NULL,
price_per_unit money NOT NULL,
date date DEFAULT CURRENT_DATE NOT NULL,
total money GENERATED ALWAYS AS ((price_per_unit * (quantity)::double precision)) STORED NOT NULL
);
--
-- TOC entry 206 (class 1259 OID 32991)
-- Name: expense_table_expense_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.expense_table ALTER COLUMN expense_id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME public.expense_table_expense_id_seq
START WITH 0
INCREMENT BY 1
MINVALUE 0
MAXVALUE 20000
CACHE 5
CYCLE
);
--
-- TOC entry 209 (class 1259 OID 33010)
-- Name: transaction_table; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.transaction_table (
transaction_id bigint NOT NULL,
company_id text NOT NULL,
user_id text NOT NULL,
product text NOT NULL,
payment_method text NOT NULL,
customer text NOT NULL,
quantity numeric NOT NULL,
price_per_unit money NOT NULL,
date date DEFAULT CURRENT_DATE NOT NULL,
total money GENERATED ALWAYS AS ((price_per_unit * (quantity)::double precision)) STORED NOT NULL
);
--
-- TOC entry 208 (class 1259 OID 33008)
-- Name: transaction_table_transaction_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.transaction_table ALTER COLUMN transaction_id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME public.transaction_table_transaction_id_seq
START WITH 0
INCREMENT BY 1
MINVALUE 0
MAXVALUE 20000
CACHE 1
CYCLE
);
--
-- TOC entry 210 (class 1259 OID 33025)
-- Name: user_has_company; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.user_has_company (
user_id text NOT NULL,
company_id text NOT NULL
);
--
-- TOC entry 202 (class 1259 OID 32946)
-- Name: user_table; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.user_table (
user_id text NOT NULL,
first_name text NOT NULL,
last_name text NOT NULL,
section text,
role integer NOT NULL
);
--
-- TOC entry 2856 (class 2606 OID 32972)
-- Name: company_table company_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.company_table
ADD CONSTRAINT company_pkey PRIMARY KEY (company_id);
--
-- TOC entry 2858 (class 2606 OID 32984)
-- Name: deposit_table deposit_table_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.deposit_table
ADD CONSTRAINT deposit_table_pkey PRIMARY KEY (deposit_id);
--
-- TOC entry 2860 (class 2606 OID 33002)
-- Name: expense_table expense_table_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.expense_table
ADD CONSTRAINT expense_table_pkey PRIMARY KEY (expense_id);
--
-- TOC entry 2862 (class 2606 OID 33019)
-- Name: transaction_table transaction_table_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.transaction_table
ADD CONSTRAINT transaction_table_pkey PRIMARY KEY (transaction_id);
--
-- TOC entry 2854 (class 2606 OID 32953)
-- Name: user_table user_table_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_table
ADD CONSTRAINT user_table_pkey PRIMARY KEY (user_id);
--
-- TOC entry 2866 (class 2606 OID 33031)
-- Name: user_has_company company_id; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_has_company
ADD CONSTRAINT company_id FOREIGN KEY (company_id) REFERENCES public.company_table(company_id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- TOC entry 2863 (class 2606 OID 32985)
-- Name: deposit_table deposit_table_cid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.deposit_table
ADD CONSTRAINT deposit_table_cid_fkey FOREIGN KEY (company_id) REFERENCES public.company_table(company_id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- TOC entry 2864 (class 2606 OID 33003)
-- Name: expense_table expense_cid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.expense_table
ADD CONSTRAINT expense_cid_fkey FOREIGN KEY (company_id) REFERENCES public.company_table(company_id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- TOC entry 2865 (class 2606 OID 33054)
-- Name: transaction_table transaction_table_cid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.transaction_table
ADD CONSTRAINT transaction_table_cid_fkey FOREIGN KEY (company_id) REFERENCES public.company_table(company_id) ON UPDATE CASCADE ON DELETE CASCADE NOT VALID;
--
-- TOC entry 2867 (class 2606 OID 33036)
-- Name: user_has_company user_id; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_has_company
ADD CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES public.user_table(user_id) ON UPDATE CASCADE ON DELETE CASCADE;
-- Completed on 2021-08-30 18:14:18 PDT
--
-- PostgreSQL database dump complete
--