-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonal base.sql
More file actions
3901 lines (2833 loc) · 180 KB
/
Copy pathPersonal base.sql
File metadata and controls
3901 lines (2833 loc) · 180 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
-- Dumped from database version 16.3
-- Dumped by pg_dump version 16.3
-- Started on 2025-06-29 15:04:55
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 6 (class 2615 OID 28704)
-- Name: course; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA course;
ALTER SCHEMA course OWNER TO postgres;
--
-- TOC entry 8 (class 2615 OID 28708)
-- Name: finance; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA finance;
ALTER SCHEMA finance OWNER TO postgres;
--
-- TOC entry 10 (class 2615 OID 28706)
-- Name: habits; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA habits;
ALTER SCHEMA habits OWNER TO postgres;
--
-- TOC entry 9 (class 2615 OID 28709)
-- Name: todo; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA todo;
ALTER SCHEMA todo OWNER TO postgres;
--
-- TOC entry 11 (class 2615 OID 28705)
-- Name: trips; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA trips;
ALTER SCHEMA trips OWNER TO postgres;
--
-- TOC entry 7 (class 2615 OID 28707)
-- Name: user; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA "user";
ALTER SCHEMA "user" OWNER TO postgres;
--
-- TOC entry 274 (class 1255 OID 27192)
-- Name: update_course_status(); Type: FUNCTION; Schema: course; Owner: postgres
--
CREATE FUNCTION course.update_course_status() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
new_status integer;
all_completed boolean;
any_started boolean;
BEGIN
SELECT COUNT(*) = SUM(CASE WHEN completed_date IS NOT NULL THEN 1 ELSE 0 END)
INTO all_completed
FROM course.course_topics
WHERE course_id = NEW.course_id;
SELECT COUNT(*) > 0
INTO any_started
FROM course.course_topics
WHERE course_id = NEW.course_id AND completed_date IS NOT NULL;
IF all_completed THEN
new_status := 3;
ELSIF any_started THEN
new_status := 2;
ELSE
new_status := 1;
END IF;
IF (SELECT status_id FROM course.courses WHERE course_id = NEW.course_id) != new_status THEN
UPDATE course.courses
SET status_id = new_status,
updated_at = CURRENT_TIMESTAMP
WHERE course_id = NEW.course_id;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION course.update_course_status() OWNER TO postgres;
--
-- TOC entry 5170 (class 0 OID 0)
-- Dependencies: 274
-- Name: FUNCTION update_course_status(); Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON FUNCTION course.update_course_status() IS 'Updates status_id in the courses table based on the completion of topics in course_topics: ''Planned'' (no topics), ''Completed'' (all topics completed), ''In Progress'' (otherwise). Triggered by course_topics_status_trigger after inserting or updating completed_date.';
--
-- TOC entry 262 (class 1255 OID 27770)
-- Name: check_finance_amount(); Type: FUNCTION; Schema: finance; Owner: postgres
--
CREATE FUNCTION finance.check_finance_amount() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
is_income boolean;
BEGIN
SELECT ft.is_income INTO is_income
FROM finance.finance_categories fc -- Added finance schema
JOIN finance.finance_types ft ON fc.type_id = ft.type_id
WHERE fc.category_id = NEW.category_id;
IF is_income AND NEW.amount <= 0 THEN
RAISE EXCEPTION 'Amount must be positive for income category';
ELSIF NOT is_income AND NEW.amount >= 0 THEN
RAISE EXCEPTION 'Amount must be negative for expense category';
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION finance.check_finance_amount() OWNER TO postgres;
--
-- TOC entry 5171 (class 0 OID 0)
-- Dependencies: 262
-- Name: FUNCTION check_finance_amount(); Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON FUNCTION finance.check_finance_amount() IS 'Checks the correctness of the amount in the finances table: positive for income (type.name = ''Income''), negative for expenses (type.name = ''Expense''). Triggered by finances_amount_trigger before inserting or updating a record.';
--
-- TOC entry 261 (class 1255 OID 26924)
-- Name: update_updated_at(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.update_updated_at() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.update_updated_at() OWNER TO postgres;
--
-- TOC entry 5172 (class 0 OID 0)
-- Dependencies: 261
-- Name: FUNCTION update_updated_at(); Type: COMMENT; Schema: public; Owner: postgres
--
COMMENT ON FUNCTION public.update_updated_at() IS 'Updates the updated_at field to CURRENT_TIMESTAMP when modifying a record in the users, finances, todos, courses, and other tables. Triggered by updated_at_trigger before updating records.';
--
-- TOC entry 260 (class 1255 OID 26708)
-- Name: set_completed_date(); Type: FUNCTION; Schema: todo; Owner: postgres
--
CREATE FUNCTION todo.set_completed_date() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NEW.is_completed = TRUE AND OLD.is_completed = FALSE THEN
NEW.completed_date = CURRENT_DATE;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION todo.set_completed_date() OWNER TO postgres;
--
-- TOC entry 5173 (class 0 OID 0)
-- Dependencies: 260
-- Name: FUNCTION set_completed_date(); Type: COMMENT; Schema: todo; Owner: postgres
--
COMMENT ON FUNCTION todo.set_completed_date() IS 'Sets the completed_date field in the todos table to the current date (CURRENT_DATE) if is_completed changes to TRUE. Triggered by todos_completed_date_trigger before updating a record.';
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- TOC entry 252 (class 1259 OID 27694)
-- Name: course_statuses; Type: TABLE; Schema: course; Owner: postgres
--
CREATE TABLE course.course_statuses (
status_id integer NOT NULL,
name character varying(50) NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE course.course_statuses OWNER TO postgres;
--
-- TOC entry 5174 (class 0 OID 0)
-- Dependencies: 252
-- Name: TABLE course_statuses; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON TABLE course.course_statuses IS 'Reference table for course statuses (Planned, In Progress, Completed).';
--
-- TOC entry 5175 (class 0 OID 0)
-- Dependencies: 252
-- Name: COLUMN course_statuses.status_id; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_statuses.status_id IS 'Unique status identifier';
--
-- TOC entry 5176 (class 0 OID 0)
-- Dependencies: 252
-- Name: COLUMN course_statuses.name; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_statuses.name IS 'Status name (e.g., Planned, Completed)';
--
-- TOC entry 5177 (class 0 OID 0)
-- Dependencies: 252
-- Name: COLUMN course_statuses.created_at; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_statuses.created_at IS 'Date and time the record was created';
--
-- TOC entry 240 (class 1259 OID 26808)
-- Name: course_topics; Type: TABLE; Schema: course; Owner: postgres
--
CREATE TABLE course.course_topics (
topic_id integer NOT NULL,
course_id integer NOT NULL,
user_id integer NOT NULL,
title character varying(100) NOT NULL,
material text,
grade numeric(5,2),
completed_date date,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT course_topics_grade_check CHECK (((grade >= (0)::numeric) AND (grade <= (5)::numeric)))
);
ALTER TABLE course.course_topics OWNER TO postgres;
--
-- TOC entry 5178 (class 0 OID 0)
-- Dependencies: 240
-- Name: TABLE course_topics; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON TABLE course.course_topics IS 'Table for storing user course topics';
--
-- TOC entry 5179 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.topic_id; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.topic_id IS 'Unique topic identifier (primary key)';
--
-- TOC entry 5180 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.course_id; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.course_id IS 'Identifier of the course to which the topic belongs (foreign key)';
--
-- TOC entry 5181 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.user_id; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.user_id IS 'Identifier of the user who owns the topic (foreign key)';
--
-- TOC entry 5182 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.title; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.title IS 'Topic title';
--
-- TOC entry 5183 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.material; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.material IS 'Topic materials (e.g., lecture text or links)';
--
-- TOC entry 5184 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.grade; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.grade IS 'Grade for the topic (from 0 to 5)';
--
-- TOC entry 5185 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.completed_date; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.completed_date IS 'Date the topic was completed';
--
-- TOC entry 5186 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.created_at; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.created_at IS 'Date and time the topic was created';
--
-- TOC entry 5187 (class 0 OID 0)
-- Dependencies: 240
-- Name: COLUMN course_topics.updated_at; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.course_topics.updated_at IS 'Date and time the topic was last updated';
--
-- TOC entry 238 (class 1259 OID 26793)
-- Name: courses; Type: TABLE; Schema: course; Owner: postgres
--
CREATE TABLE course.courses (
course_id integer NOT NULL,
user_id integer NOT NULL,
title character varying(100) NOT NULL,
description text,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
status_id integer NOT NULL
);
ALTER TABLE course.courses OWNER TO postgres;
--
-- TOC entry 5188 (class 0 OID 0)
-- Dependencies: 238
-- Name: TABLE courses; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON TABLE course.courses IS 'Table for storing user courses';
--
-- TOC entry 5189 (class 0 OID 0)
-- Dependencies: 238
-- Name: COLUMN courses.course_id; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.courses.course_id IS 'Unique course identifier (primary key)';
--
-- TOC entry 5190 (class 0 OID 0)
-- Dependencies: 238
-- Name: COLUMN courses.user_id; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.courses.user_id IS 'Identifier of the user who owns the course (foreign key)';
--
-- TOC entry 5191 (class 0 OID 0)
-- Dependencies: 238
-- Name: COLUMN courses.title; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.courses.title IS 'Course title (unique within the user)';
--
-- TOC entry 5192 (class 0 OID 0)
-- Dependencies: 238
-- Name: COLUMN courses.description; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.courses.description IS 'Course description';
--
-- TOC entry 5193 (class 0 OID 0)
-- Dependencies: 238
-- Name: COLUMN courses.created_at; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.courses.created_at IS 'Date and time the course was created';
--
-- TOC entry 5194 (class 0 OID 0)
-- Dependencies: 238
-- Name: COLUMN courses.updated_at; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.courses.updated_at IS 'Date and time the course was last updated';
--
-- TOC entry 5195 (class 0 OID 0)
-- Dependencies: 238
-- Name: COLUMN courses.status_id; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON COLUMN course.courses.status_id IS 'Identifier of the course status (reference to course_statuses)';
--
-- TOC entry 257 (class 1259 OID 27744)
-- Name: course_grades; Type: VIEW; Schema: course; Owner: postgres
--
CREATE VIEW course.course_grades AS
SELECT c.course_id,
c.user_id,
c.title,
c.description,
cs.name AS status,
avg(ct.grade) AS final_grade
FROM ((course.courses c
LEFT JOIN course.course_topics ct ON ((c.course_id = ct.course_id)))
JOIN course.course_statuses cs ON ((c.status_id = cs.status_id)))
GROUP BY c.course_id, c.user_id, c.title, c.description, cs.name;
ALTER VIEW course.course_grades OWNER TO postgres;
--
-- TOC entry 5196 (class 0 OID 0)
-- Dependencies: 257
-- Name: VIEW course_grades; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON VIEW course.course_grades IS 'View calculating the average grade (final_grade) for topics in each course from the courses table. Includes course_id, user_id, title, description, and status (from course_statuses). Used for performance analysis.';
--
-- TOC entry 248 (class 1259 OID 26899)
-- Name: course_progress; Type: VIEW; Schema: course; Owner: postgres
--
CREATE VIEW course.course_progress AS
SELECT c.course_id,
c.user_id,
c.title,
count(ct.topic_id) AS total_topics,
count(ct.completed_date) AS completed_topics,
(((count(ct.completed_date))::double precision / (NULLIF(count(ct.topic_id), 0))::double precision) * (100)::double precision) AS completion_percentage
FROM (course.courses c
LEFT JOIN course.course_topics ct ON ((c.course_id = ct.course_id)))
GROUP BY c.course_id, c.user_id, c.title;
ALTER VIEW course.course_progress OWNER TO postgres;
--
-- TOC entry 5197 (class 0 OID 0)
-- Dependencies: 248
-- Name: VIEW course_progress; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON VIEW course.course_progress IS 'View showing course completion progress: total topics (total_topics), completed topics (completed_topics), and completion percentage (completion_percentage). Used to track user progress.';
--
-- TOC entry 239 (class 1259 OID 26807)
-- Name: course_topics_topic_id_seq; Type: SEQUENCE; Schema: course; Owner: postgres
--
CREATE SEQUENCE course.course_topics_topic_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE course.course_topics_topic_id_seq OWNER TO postgres;
--
-- TOC entry 5198 (class 0 OID 0)
-- Dependencies: 239
-- Name: course_topics_topic_id_seq; Type: SEQUENCE OWNED BY; Schema: course; Owner: postgres
--
ALTER SEQUENCE course.course_topics_topic_id_seq OWNED BY course.course_topics.topic_id;
--
-- TOC entry 5199 (class 0 OID 0)
-- Dependencies: 239
-- Name: SEQUENCE course_topics_topic_id_seq; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON SEQUENCE course.course_topics_topic_id_seq IS 'Sequence for generating unique identifiers (topic_id) in the course_topics table, which stores course topics.';
--
-- TOC entry 237 (class 1259 OID 26792)
-- Name: courses_course_id_seq; Type: SEQUENCE; Schema: course; Owner: postgres
--
CREATE SEQUENCE course.courses_course_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE course.courses_course_id_seq OWNER TO postgres;
--
-- TOC entry 5200 (class 0 OID 0)
-- Dependencies: 237
-- Name: courses_course_id_seq; Type: SEQUENCE OWNED BY; Schema: course; Owner: postgres
--
ALTER SEQUENCE course.courses_course_id_seq OWNED BY course.courses.course_id;
--
-- TOC entry 5201 (class 0 OID 0)
-- Dependencies: 237
-- Name: SEQUENCE courses_course_id_seq; Type: COMMENT; Schema: course; Owner: postgres
--
COMMENT ON SEQUENCE course.courses_course_id_seq IS 'Sequence for generating unique identifiers (course_id) in the courses table, storing user courses.';
--
-- TOC entry 224 (class 1259 OID 26513)
-- Name: finance_categories; Type: TABLE; Schema: finance; Owner: postgres
--
CREATE TABLE finance.finance_categories (
category_id integer NOT NULL,
user_id integer NOT NULL,
name character varying(100) NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
type_id integer NOT NULL
);
ALTER TABLE finance.finance_categories OWNER TO postgres;
--
-- TOC entry 5202 (class 0 OID 0)
-- Dependencies: 224
-- Name: TABLE finance_categories; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON TABLE finance.finance_categories IS 'Table for storing user financial operation categories';
--
-- TOC entry 5203 (class 0 OID 0)
-- Dependencies: 224
-- Name: COLUMN finance_categories.category_id; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_categories.category_id IS 'Unique category identifier (primary key)';
--
-- TOC entry 5204 (class 0 OID 0)
-- Dependencies: 224
-- Name: COLUMN finance_categories.user_id; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_categories.user_id IS 'Identifier of the user who owns the category (foreign key)';
--
-- TOC entry 5205 (class 0 OID 0)
-- Dependencies: 224
-- Name: COLUMN finance_categories.name; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_categories.name IS 'Category name (unique within the user)';
--
-- TOC entry 5206 (class 0 OID 0)
-- Dependencies: 224
-- Name: COLUMN finance_categories.created_at; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_categories.created_at IS 'Date and time the category was created';
--
-- TOC entry 5207 (class 0 OID 0)
-- Dependencies: 224
-- Name: COLUMN finance_categories.updated_at; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_categories.updated_at IS 'Date and time the category was last updated';
--
-- TOC entry 5208 (class 0 OID 0)
-- Dependencies: 224
-- Name: COLUMN finance_categories.type_id; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_categories.type_id IS 'Identifier of the category type (reference to finance_types)';
--
-- TOC entry 223 (class 1259 OID 26512)
-- Name: finance_categories_category_id_seq; Type: SEQUENCE; Schema: finance; Owner: postgres
--
CREATE SEQUENCE finance.finance_categories_category_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE finance.finance_categories_category_id_seq OWNER TO postgres;
--
-- TOC entry 5209 (class 0 OID 0)
-- Dependencies: 223
-- Name: finance_categories_category_id_seq; Type: SEQUENCE OWNED BY; Schema: finance; Owner: postgres
--
ALTER SEQUENCE finance.finance_categories_category_id_seq OWNED BY finance.finance_categories.category_id;
--
-- TOC entry 5210 (class 0 OID 0)
-- Dependencies: 223
-- Name: SEQUENCE finance_categories_category_id_seq; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON SEQUENCE finance.finance_categories_category_id_seq IS 'Sequence for generating unique identifiers (category_id) in the finance_categories table, which stores user financial operation categories.';
--
-- TOC entry 249 (class 1259 OID 27221)
-- Name: finance_types; Type: TABLE; Schema: finance; Owner: postgres
--
CREATE TABLE finance.finance_types (
type_id integer NOT NULL,
name character varying(50) NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
is_income boolean DEFAULT false NOT NULL
);
ALTER TABLE finance.finance_types OWNER TO postgres;
--
-- TOC entry 5211 (class 0 OID 0)
-- Dependencies: 249
-- Name: TABLE finance_types; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON TABLE finance.finance_types IS 'Reference table for financial operation types (Income, Expense). ';
--
-- TOC entry 5212 (class 0 OID 0)
-- Dependencies: 249
-- Name: COLUMN finance_types.type_id; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_types.type_id IS 'Unique type identifier';
--
-- TOC entry 5213 (class 0 OID 0)
-- Dependencies: 249
-- Name: COLUMN finance_types.name; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_types.name IS 'Type name (e.g., Income, Expense)';
--
-- TOC entry 5214 (class 0 OID 0)
-- Dependencies: 249
-- Name: COLUMN finance_types.created_at; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finance_types.created_at IS 'Date and time the record was created';
--
-- TOC entry 226 (class 1259 OID 26527)
-- Name: finances; Type: TABLE; Schema: finance; Owner: postgres
--
CREATE TABLE finance.finances (
finance_id integer NOT NULL,
user_id integer NOT NULL,
category_id integer NOT NULL,
amount numeric(10,2) NOT NULL,
transaction_date date NOT NULL,
note text,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT finances_check_amount CHECK ((amount <> (0)::numeric))
);
ALTER TABLE finance.finances OWNER TO postgres;
--
-- TOC entry 5215 (class 0 OID 0)
-- Dependencies: 226
-- Name: TABLE finances; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON TABLE finance.finances IS 'Table for tracking user financial operations';
--
-- TOC entry 5216 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN finances.finance_id; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finances.finance_id IS 'Unique financial operation identifier (primary key)';
--
-- TOC entry 5217 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN finances.user_id; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finances.user_id IS 'Identifier of the user who performed the operation (foreign key)';
--
-- TOC entry 5218 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN finances.category_id; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finances.category_id IS 'Identifier of the operation category (foreign key)';
--
-- TOC entry 5219 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN finances.amount; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finances.amount IS 'Operation amount, positive for income, negative for expenses';
--
-- TOC entry 5220 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN finances.transaction_date; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finances.transaction_date IS 'Date of the operation';
--
-- TOC entry 5221 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN finances.note; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finances.note IS 'Note or comment on the operation';
--
-- TOC entry 5222 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN finances.created_at; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finances.created_at IS 'Date and time the operation record was created';
--
-- TOC entry 5223 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN finances.updated_at; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON COLUMN finance.finances.updated_at IS 'Date and time the record was last updated';
--
-- TOC entry 225 (class 1259 OID 26526)
-- Name: finances_finance_id_seq; Type: SEQUENCE; Schema: finance; Owner: postgres
--
CREATE SEQUENCE finance.finances_finance_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE finance.finances_finance_id_seq OWNER TO postgres;
--
-- TOC entry 5224 (class 0 OID 0)
-- Dependencies: 225
-- Name: finances_finance_id_seq; Type: SEQUENCE OWNED BY; Schema: finance; Owner: postgres
--
ALTER SEQUENCE finance.finances_finance_id_seq OWNED BY finance.finances.finance_id;
--
-- TOC entry 5225 (class 0 OID 0)
-- Dependencies: 225
-- Name: SEQUENCE finances_finance_id_seq; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON SEQUENCE finance.finances_finance_id_seq IS 'Sequence for generating unique identifiers (finance_id) in the finances table, which stores user financial operations.';
--
-- TOC entry 259 (class 1259 OID 28710)
-- Name: financial_summary; Type: VIEW; Schema: finance; Owner: postgres
--
CREATE VIEW finance.financial_summary AS
SELECT user_id,
EXTRACT(year FROM transaction_date) AS year,
EXTRACT(month FROM transaction_date) AS month,
sum(
CASE
WHEN (amount > (0)::numeric) THEN amount
ELSE (0)::numeric
END) AS total_income,
sum(
CASE
WHEN (amount < (0)::numeric) THEN (- amount)
ELSE (0)::numeric
END) AS total_expense,
sum(amount) AS balance
FROM finance.finances f
GROUP BY user_id, (EXTRACT(year FROM transaction_date)), (EXTRACT(month FROM transaction_date));
ALTER VIEW finance.financial_summary OWNER TO postgres;
--
-- TOC entry 5226 (class 0 OID 0)
-- Dependencies: 259
-- Name: VIEW financial_summary; Type: COMMENT; Schema: finance; Owner: postgres
--
COMMENT ON VIEW finance.financial_summary IS 'View aggregating income (total_income), expenses (total_expense), and balance (balance) per user by month and year based on the finances table.';
--
-- TOC entry 232 (class 1259 OID 26711)
-- Name: habit_categories; Type: TABLE; Schema: habits; Owner: postgres
--
CREATE TABLE habits.habit_categories (
category_id integer NOT NULL,
user_id integer NOT NULL,
name character varying(50) NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE habits.habit_categories OWNER TO postgres;
--
-- TOC entry 5227 (class 0 OID 0)
-- Dependencies: 232
-- Name: TABLE habit_categories; Type: COMMENT; Schema: habits; Owner: postgres
--
COMMENT ON TABLE habits.habit_categories IS 'Table for storing user habit categories';
--
-- TOC entry 5228 (class 0 OID 0)
-- Dependencies: 232
-- Name: COLUMN habit_categories.category_id; Type: COMMENT; Schema: habits; Owner: postgres
--
COMMENT ON COLUMN habits.habit_categories.category_id IS 'Unique category identifier (primary key)';
--
-- TOC entry 5229 (class 0 OID 0)
-- Dependencies: 232
-- Name: COLUMN habit_categories.user_id; Type: COMMENT; Schema: habits; Owner: postgres
--
COMMENT ON COLUMN habits.habit_categories.user_id IS 'Identifier of the user who owns the category (foreign key)';
--
-- TOC entry 5230 (class 0 OID 0)
-- Dependencies: 232
-- Name: COLUMN habit_categories.name; Type: COMMENT; Schema: habits; Owner: postgres
--
COMMENT ON COLUMN habits.habit_categories.name IS 'Category name (unique within the user)';
--
-- TOC entry 5231 (class 0 OID 0)
-- Dependencies: 232
-- Name: COLUMN habit_categories.created_at; Type: COMMENT; Schema: habits; Owner: postgres
--
COMMENT ON COLUMN habits.habit_categories.created_at IS 'Date and time the category was created';
--
-- TOC entry 5232 (class 0 OID 0)
-- Dependencies: 232
-- Name: COLUMN habit_categories.updated_at; Type: COMMENT; Schema: habits; Owner: postgres
--
COMMENT ON COLUMN habits.habit_categories.updated_at IS 'Date and time the category was last updated';
--
-- TOC entry 231 (class 1259 OID 26710)
-- Name: habit_categories_category_id_seq; Type: SEQUENCE; Schema: habits; Owner: postgres
--
CREATE SEQUENCE habits.habit_categories_category_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE habits.habit_categories_category_id_seq OWNER TO postgres;
--
-- TOC entry 5233 (class 0 OID 0)
-- Dependencies: 231
-- Name: habit_categories_category_id_seq; Type: SEQUENCE OWNED BY; Schema: habits; Owner: postgres
--
ALTER SEQUENCE habits.habit_categories_category_id_seq OWNED BY habits.habit_categories.category_id;
--
-- TOC entry 5234 (class 0 OID 0)
-- Dependencies: 231
-- Name: SEQUENCE habit_categories_category_id_seq; Type: COMMENT; Schema: habits; Owner: postgres
--
COMMENT ON SEQUENCE habits.habit_categories_category_id_seq IS 'Sequence for generating unique identifiers (category_id) in the habit_categories table, which stores user habit categories.';
--
-- TOC entry 255 (class 1259 OID 27718)
-- Name: habit_frequencies; Type: TABLE; Schema: habits; Owner: postgres
--