This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expenses_db.sql
263 lines (235 loc) · 8.71 KB
/
expenses_db.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
-- object: public | type: SCHEMA --
DROP SCHEMA IF EXISTS public CASCADE;
CREATE SCHEMA public;
-- ddl-end --
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- ddl-end --
-- object: public."user" | type: TABLE --
DROP TABLE IF EXISTS public."user" CASCADE;
CREATE TABLE public."user"
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
username character varying(20) NOT NULL,
firstname character varying(25) NOT NULL,
lastname character varying(25) NOT NULL,
location character varying(50),
email character varying NOT NULL,
password character varying NOT NULL,
activated boolean NOT NULL,
profile_pic character varying,
birthday date,
created_at timestamp with time zone,
CONSTRAINT user_pk PRIMARY KEY (id),
CONSTRAINT username_un UNIQUE (username),
CONSTRAINT email_un UNIQUE (email)
);
-- ddl-end --
-- object: public.trip | type: TABLE --
DROP TABLE IF EXISTS public.trip CASCADE;
CREATE TABLE public.trip
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
name character varying,
description character varying,
location character varying,
start_date date NOT NULL,
end_date date NOT NULL,
CONSTRAINT travel_pk PRIMARY KEY (id)
);
-- ddl-end --
-- object: public.token | type: TABLE --
DROP TABLE IF EXISTS public.token CASCADE;
CREATE TABLE public.token
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
id_user uuid NOT NULL,
token character varying,
type character varying,
created_at timestamp with time zone,
confirmed_at timestamp with time zone,
expires_at timestamp with time zone,
CONSTRAINT token_pk PRIMARY KEY (id),
CONSTRAINT token_un UNIQUE (token)
);
-- ddl-end --
-- object: public.user_trip_association | type: TABLE --
DROP TABLE IF EXISTS public.user_trip_association CASCADE;
CREATE TABLE public.user_trip_association
(
presence_start_date date NOT NULL,
presence_end_date date NOT NULL,
is_accepted boolean NOT NULL,
id_user uuid NOT NULL,
id_trip uuid NOT NULL,
CONSTRAINT many_user_has_many_travel_pk PRIMARY KEY (id_user, id_trip)
);
-- ddl-end --
-- object: public.cost_category | type: TABLE --
DROP TABLE IF EXISTS public.cost_category CASCADE;
CREATE TABLE public.cost_category
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
name character varying,
description character varying,
icon character varying,
color character varying,
id_trip uuid NOT NULL,
CONSTRAINT cost_category_pk PRIMARY KEY (id),
CONSTRAINT cost_category_un UNIQUE (name, id_trip)
);
-- ddl-end --
-- object: public.cost | type: TABLE --
DROP TABLE IF EXISTS public.cost CASCADE;
CREATE TABLE public.cost
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
amount numeric,
description character varying,
created_at timestamp with time zone,
deducted_at date,
end_date date,
id_cost_category uuid NOT NULL,
CONSTRAINT cost_pk PRIMARY KEY (id)
);
-- ddl-end --
-- object: public.user_cost_association | type: TABLE --
DROP TABLE IF EXISTS public.user_cost_association CASCADE;
CREATE TABLE public.user_cost_association
(
id_user uuid NOT NULL,
id_cost uuid NOT NULL,
is_creditor boolean NOT NULL,
amount numeric NOT NULL,
CONSTRAINT many_user_has_many_cost_pk PRIMARY KEY (id_user, id_cost)
);
-- ddl-end --
-- object: public.transaction | type: TABLE --
DROP TABLE IF EXISTS public.transaction CASCADE;
CREATE TABLE public.transaction
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
id_creditor uuid,
id_debtor uuid,
id_trip uuid,
amount numeric,
created_at timestamp with time zone,
currency_code character varying,
is_confirmed boolean NOT NULL,
CONSTRAINT transaction_pk PRIMARY KEY (id)
);
-- ddl-end --
-- object: public.debt | type: TABLE --
DROP TABLE IF EXISTS public.debt CASCADE;
CREATE TABLE public.debt
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
id_creditor uuid,
id_debtor uuid,
id_trip uuid,
amount numeric,
created_at timestamp with time zone,
updated_at timestamp with time zone,
currency_code character varying,
CONSTRAINT debt_pk PRIMARY KEY (id),
CONSTRAINT debt_un UNIQUE (id_creditor, id_debtor, id_trip)
);
-- ddl-end --
-- object: user_fk | type: CONSTRAINT --
-- ALTER TABLE public.token DROP CONSTRAINT IF EXISTS user_fk CASCADE;
ALTER TABLE public.token
ADD CONSTRAINT user_fk FOREIGN KEY (id_user)
REFERENCES public."user" (id) MATCH FULL
ON DELETE CASCADE ON UPDATE NO ACTION;
-- ddl-end --
-- object: user_fk | type: CONSTRAINT --
-- ALTER TABLE public.user_trip_association DROP CONSTRAINT IF EXISTS user_fk CASCADE;
ALTER TABLE public.user_trip_association
ADD CONSTRAINT user_fk FOREIGN KEY (id_user)
REFERENCES public."user" (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: trip_fk | type: CONSTRAINT --
-- ALTER TABLE public.user_trip_association DROP CONSTRAINT IF EXISTS trip_fk CASCADE;
ALTER TABLE public.user_trip_association
ADD CONSTRAINT trip_fk FOREIGN KEY (id_trip)
REFERENCES public.trip (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: trip_fk | type: CONSTRAINT --
-- ALTER TABLE public.cost_category DROP CONSTRAINT IF EXISTS trip_fk CASCADE;
ALTER TABLE public.cost_category
ADD CONSTRAINT trip_fk FOREIGN KEY (id_trip)
REFERENCES public.trip (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: cost_category_fk | type: CONSTRAINT --
-- ALTER TABLE public.cost DROP CONSTRAINT IF EXISTS cost_category_fk CASCADE;
ALTER TABLE public.cost
ADD CONSTRAINT cost_category_fk FOREIGN KEY (id_cost_category)
REFERENCES public.cost_category (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: user_fk | type: CONSTRAINT --
-- ALTER TABLE public.user_cost_association DROP CONSTRAINT IF EXISTS user_fk CASCADE;
ALTER TABLE public.user_cost_association
ADD CONSTRAINT user_fk FOREIGN KEY (id_user)
REFERENCES public."user" (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: cost_fk | type: CONSTRAINT --
-- ALTER TABLE public.user_cost_association DROP CONSTRAINT IF EXISTS cost_fk CASCADE;
ALTER TABLE public.user_cost_association
ADD CONSTRAINT cost_fk FOREIGN KEY (id_cost)
REFERENCES public.cost (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: debitor_fk | type: CONSTRAINT --
-- ALTER TABLE public.transaction DROP CONSTRAINT IF EXISTS debitor_fk CASCADE;
ALTER TABLE public.transaction
ADD CONSTRAINT debtor_fk FOREIGN KEY (id_creditor)
REFERENCES public."user" (id) MATCH SIMPLE
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: creditor_fk | type: CONSTRAINT --
-- ALTER TABLE public.transaction DROP CONSTRAINT IF EXISTS creditor_fk CASCADE;
ALTER TABLE public.transaction
ADD CONSTRAINT creditor_fk FOREIGN KEY (id_debtor)
REFERENCES public."user" (id) MATCH SIMPLE
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: trip_fk | type: CONSTRAINT --
-- ALTER TABLE public.transaction DROP CONSTRAINT IF EXISTS trip_fk CASCADE;
ALTER TABLE public.transaction
ADD CONSTRAINT trip_fk FOREIGN KEY (id_trip)
REFERENCES public.trip (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- object: trip_fk | type: CONSTRAINT --
-- ALTER TABLE public.debt DROP CONSTRAINT IF EXISTS trip_fk CASCADE;
ALTER TABLE public.debt
ADD CONSTRAINT trip_fk FOREIGN KEY (id_trip)
REFERENCES public.trip (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: creditor_fk | type: CONSTRAINT --
-- ALTER TABLE public.debt DROP CONSTRAINT IF EXISTS creditor_fk CASCADE;
ALTER TABLE public.debt
ADD CONSTRAINT creditor_fk FOREIGN KEY (id_creditor)
REFERENCES public."user" (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- ddl-end --
-- object: debitor_fk | type: CONSTRAINT --
-- ALTER TABLE public.debt DROP CONSTRAINT IF EXISTS debitor_fk CASCADE;
ALTER TABLE public.debt
ADD CONSTRAINT debitor_fk FOREIGN KEY (id_debtor)
REFERENCES public."user" (id) MATCH FULL
ON DELETE CASCADE ON UPDATE CASCADE;
-- object: "grant_CU_26541e8cda" | type: PERMISSION --
GRANT CREATE, USAGE
ON SCHEMA public
TO pg_database_owner;
-- ddl-end --
-- object: "grant_U_cd8e46e7b6" | type: PERMISSION --
GRANT USAGE
ON SCHEMA public
TO PUBLIC;
-- ddl-end --