forked from CodeWithCJ/SparkyFitness
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_schema_backup.sql
More file actions
5040 lines (3574 loc) · 158 KB
/
db_schema_backup.sql
File metadata and controls
5040 lines (3574 loc) · 158 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
--
\restrict Hb9PePndvFR0vQvRT7LibJs74ogZtZW4BjX9ezkShB8cbvZsnfAY1d8Oj2PNNme
-- Dumped from database version 15.14
-- Dumped by pg_dump version 18.0
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_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;
--
-- Name: auth; Type: SCHEMA; Schema: -; Owner: -
--
CREATE SCHEMA auth;
--
-- Name: system; Type: SCHEMA; Schema: -; Owner: -
--
CREATE SCHEMA system;
--
-- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA public;
--
-- Name: EXTENSION pg_stat_statements; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION pg_stat_statements IS 'track planning and execution statistics of all SQL statements executed';
--
-- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
--
-- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
--
-- Name: uuid-ossp; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
--
-- Name: EXTENSION "uuid-ossp"; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
--
-- Name: can_access_user_data(uuid, text, uuid); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.can_access_user_data(target_user_id uuid, permission_type text, authenticated_user_id uuid) RETURNS boolean
LANGUAGE plpgsql STABLE
AS $$
BEGIN
-- If accessing own data, always allow
IF target_user_id = authenticated_user_id THEN
RETURN true;
END IF;
-- Check if current user has family access with the required permission
RETURN EXISTS (
SELECT 1
FROM public.family_access fa
WHERE fa.family_user_id = authenticated_user_id
AND fa.owner_user_id = target_user_id
AND fa.is_active = true
AND (fa.access_end_date IS NULL OR fa.access_end_date > now())
AND (
-- Direct permission check
(fa.access_permissions->permission_type)::boolean = true
OR
-- Inheritance: reports permission grants read access to calorie and checkin
(permission_type IN ('calorie', 'checkin') AND (fa.access_permissions->>'reports')::boolean = true)
OR
-- Inheritance: food_list permission grants read access to calorie data (foods table)
(permission_type = 'calorie' AND (fa.access_permissions->>'food_list')::boolean = true)
)
);
END;
$$;
--
-- Name: check_family_access(uuid, uuid, text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.check_family_access(p_family_user_id uuid, p_owner_user_id uuid, p_permission text) RETURNS boolean
LANGUAGE plpgsql
AS $$
BEGIN
RETURN EXISTS (
SELECT 1
FROM public.family_access
WHERE family_user_id = p_family_user_id
AND owner_user_id = p_owner_user_id
AND is_active = true
AND (access_end_date IS NULL OR access_end_date > now())
AND (access_permissions->p_permission)::boolean = true
);
END;
$$;
--
-- Name: clear_old_chat_history(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.clear_old_chat_history() RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
-- Delete chat history entries older than 7 days for users who have set auto_clear_history to '7days'
DELETE FROM public.sparky_chat_history
WHERE user_id IN (
SELECT user_id
FROM public.user_preferences
WHERE auto_clear_history = '7days'
)
AND created_at < now() - interval '7 days';
END;
$$;
--
-- Name: create_default_external_data_providers(uuid); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.create_default_external_data_providers(p_user_id uuid) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
-- Insert default 'free-exercise-db' provider
INSERT INTO public.external_data_providers (
user_id, provider_name, provider_type, is_active, shared_with_public, created_at, updated_at
) VALUES (
p_user_id, 'Free Exercise DB', 'free-exercise-db', TRUE, FALSE, now(), now()
) ON CONFLICT (user_id, provider_name) DO NOTHING;
-- Insert default 'wger' provider
INSERT INTO public.external_data_providers (
user_id, provider_name, provider_type, is_active, shared_with_public, created_at, updated_at
) VALUES (
p_user_id, 'Wger', 'wger', TRUE, FALSE, now(), now()
) ON CONFLICT (user_id, provider_name) DO NOTHING;
-- Insert default 'openfoodfacts' provider
INSERT INTO public.external_data_providers (
user_id, provider_name, provider_type, is_active, shared_with_public, created_at, updated_at
) VALUES (
p_user_id, 'Open Food Facts', 'openfoodfacts', TRUE, FALSE, now(), now()
) ON CONFLICT (user_id, provider_name) DO NOTHING;
END;
$$;
--
-- Name: create_diary_policy(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.create_diary_policy(table_name text) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
EXECUTE format('
CREATE POLICY select_policy ON public.%I FOR SELECT TO PUBLIC
USING (has_diary_access(user_id));
CREATE POLICY modify_policy ON public.%I FOR ALL TO PUBLIC
USING (has_diary_access(user_id))
WITH CHECK (has_diary_access(user_id));
', table_name, table_name);
END;
$$;
--
-- Name: create_library_policy(text, text, text[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.create_library_policy(table_name text, shared_column text, permissions text[]) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
quoted_permissions text;
shared_expression text;
BEGIN
-- Quote each permission name to ensure valid ARRAY syntax
SELECT array_to_string(ARRAY(
SELECT quote_literal(p) FROM unnest(permissions) p
), ',') INTO quoted_permissions;
-- Use boolean false if shared_column is 'false', otherwise treat as column name
IF shared_column = 'false' THEN
shared_expression := 'false';
ELSE
shared_expression := quote_ident(shared_column);
END IF;
EXECUTE format('
CREATE POLICY select_policy ON public.%I FOR SELECT TO PUBLIC
USING (has_library_access_with_public(user_id, %s, ARRAY[%s]));
CREATE POLICY modify_policy ON public.%I FOR ALL TO PUBLIC
USING (current_user_id() = user_id)
WITH CHECK (current_user_id() = user_id);
', table_name, shared_expression, quoted_permissions, table_name);
END;
$$;
--
-- Name: create_owner_centric_all_policy(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.create_owner_centric_all_policy(table_name text) RETURNS void
LANGUAGE plpgsql
AS $_$
BEGIN
EXECUTE format('
DROP POLICY IF EXISTS %1$s_all_policy ON public.%1$s;
CREATE POLICY %1$s_all_policy ON public.%1$s
FOR ALL
TO sparky_app
USING (user_id = current_setting(''app.user_id'')::uuid)
WITH CHECK (user_id = current_setting(''app.user_id'')::uuid);
', table_name);
END;
$_$;
--
-- Name: create_owner_centric_id_policy(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.create_owner_centric_id_policy(table_name text) RETURNS void
LANGUAGE plpgsql
AS $_$
BEGIN
EXECUTE format('
DROP POLICY IF EXISTS %1$s_all_policy ON public.%1$s;
CREATE POLICY %1$s_all_policy ON public.%1$s
FOR ALL
TO sparky_app
USING (id = current_setting(''app.user_id'')::uuid)
WITH CHECK (id = current_setting(''app.user_id'')::uuid);
', table_name);
END;
$_$;
--
-- Name: create_owner_policy(text, text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.create_owner_policy(table_name text, id_column text DEFAULT 'user_id'::text) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
EXECUTE format('
CREATE POLICY owner_policy ON public.%I FOR ALL TO PUBLIC
USING (%I = current_user_id())
WITH CHECK (%I = current_user_id());
', table_name, id_column, id_column);
END;
$$;
--
-- Name: create_user_centric_policy(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.create_user_centric_policy(table_name text) RETURNS void
LANGUAGE plpgsql
AS $_$
BEGIN
EXECUTE format('
DROP POLICY IF EXISTS %1$s_user_policy ON public.%1$s;
CREATE POLICY %1$s_user_policy ON public.%1$s
FOR ALL
USING (user_id = current_setting(''app.user_id'')::uuid)
WITH CHECK (user_id = current_setting(''app.user_id'')::uuid);
', table_name);
END;
$_$;
--
-- Name: create_user_preferences(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.create_user_preferences() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO public.user_preferences (user_id)
VALUES (NEW.id)
ON CONFLICT (user_id) DO NOTHING;
RETURN NEW;
END;
$$;
--
-- Name: current_user_id(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.current_user_id() RETURNS uuid
LANGUAGE sql STABLE
AS $$
SELECT (current_setting('app.user_id'::text))::uuid;
$$;
--
-- Name: find_user_by_email(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.find_user_by_email(p_email text) RETURNS uuid
LANGUAGE plpgsql
AS $$
DECLARE
user_id uuid;
BEGIN
-- This function runs with elevated privileges to find users by email
SELECT id INTO user_id
FROM auth.users
WHERE email = p_email
LIMIT 1;
RETURN user_id;
END;
$$;
--
-- Name: generate_user_api_key(uuid, text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.generate_user_api_key(p_user_id uuid, p_description text DEFAULT NULL::text) RETURNS text
LANGUAGE plpgsql
AS $$
declare
new_api_key text;
begin
-- Generate a random UUID and use it as the API key
new_api_key := gen_random_uuid();
-- Insert the new API key into the user_api_keys table with default permissions for health data write
insert into public.user_api_keys (user_id, api_key, description, permissions)
values (p_user_id, new_api_key, p_description, '{"health_data_write": true}'::jsonb);
return new_api_key;
end;
$$;
--
-- Name: get_accessible_users(uuid); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.get_accessible_users(p_user_id uuid) RETURNS TABLE(user_id uuid, full_name text, email text, permissions jsonb, access_end_date timestamp with time zone)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
fa.owner_user_id,
p.full_name,
au.email::text, -- Get email from auth.users and explicitly cast to text
fa.access_permissions,
fa.access_end_date
FROM public.family_access fa
JOIN public.profiles p ON p.id = fa.owner_user_id
JOIN auth.users au ON au.id = fa.owner_user_id -- Join with auth.users
WHERE fa.family_user_id = p_user_id
AND fa.is_active = true
AND (fa.access_end_date IS NULL OR fa.access_end_date > now());
END;
$$;
--
-- Name: get_goals_for_date(uuid, date); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.get_goals_for_date(p_user_id uuid, p_date date) RETURNS TABLE(calories numeric, protein numeric, carbs numeric, fat numeric, water_goal integer, saturated_fat numeric, polyunsaturated_fat numeric, monounsaturated_fat numeric, trans_fat numeric, cholesterol numeric, sodium numeric, potassium numeric, dietary_fiber numeric, sugars numeric, vitamin_a numeric, vitamin_c numeric, calcium numeric, iron numeric)
LANGUAGE plpgsql
AS $$
BEGIN
-- First try to get goal for the exact date
RETURN QUERY
SELECT g.calories, g.protein, g.carbs, g.fat, g.water_goal,
g.saturated_fat, g.polyunsaturated_fat, g.monounsaturated_fat, g.trans_fat,
g.cholesterol, g.sodium, g.potassium, g.dietary_fiber, g.sugars,
g.vitamin_a, g.vitamin_c, g.calcium, g.iron
FROM public.user_goals g
WHERE g.user_id = p_user_id AND g.goal_date = p_date
LIMIT 1;
-- If no exact date goal found, get the most recent goal before this date
IF NOT FOUND THEN
RETURN QUERY
SELECT g.calories, g.protein, g.carbs, g.fat, g.water_goal,
g.saturated_fat, g.polyunsaturated_fat, g.monounsaturated_fat, g.trans_fat,
g.cholesterol, g.sodium, g.potassium, g.dietary_fiber, g.sugars,
g.vitamin_a, g.vitamin_c, g.calcium, g.iron
FROM public.user_goals g
WHERE g.user_id = p_user_id
AND (g.goal_date < p_date OR g.goal_date IS NULL)
ORDER BY g.goal_date DESC NULLS LAST
LIMIT 1;
END IF;
-- If still no goal found, return default values
IF NOT FOUND THEN
RETURN QUERY
SELECT 2000::NUMERIC, 150::NUMERIC, 250::NUMERIC, 67::NUMERIC, 8::INTEGER,
20::NUMERIC, 10::NUMERIC, 25::NUMERIC, 0::NUMERIC,
300::NUMERIC, 2300::NUMERIC, 3500::NUMERIC, 25::NUMERIC, 50::NUMERIC,
900::NUMERIC, 90::NUMERIC, 1000::NUMERIC, 18::NUMERIC;
END IF;
END;
$$;
--
-- Name: handle_new_user(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.handle_new_user() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
INSERT INTO public.onboarding_status (user_id)
VALUES (new.id);
-- Call the new function to create default external data providers
PERFORM public.create_default_external_data_providers(new.id);
RETURN new;
END;
$$;
--
-- Name: has_diary_access(uuid); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.has_diary_access(owner_uuid uuid) RETURNS boolean
LANGUAGE sql STABLE
AS $$
SELECT current_user_id() = owner_uuid OR has_family_access(owner_uuid, 'can_manage_diary');
$$;
--
-- Name: has_family_access(uuid, text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.has_family_access(owner_uuid uuid, perm text) RETURNS boolean
LANGUAGE sql STABLE
AS $$
SELECT EXISTS (
SELECT 1 FROM public.family_access fa
WHERE fa.owner_user_id = owner_uuid
AND fa.family_user_id = current_user_id()
AND fa.is_active = true
AND (fa.access_end_date IS NULL OR fa.access_end_date > now())
AND (fa.access_permissions ->> perm)::boolean = true
);
$$;
--
-- Name: has_family_access_or(uuid, text[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.has_family_access_or(owner_uuid uuid, perms text[]) RETURNS boolean
LANGUAGE sql STABLE
AS $$
SELECT EXISTS (
SELECT 1 FROM public.family_access fa
WHERE fa.owner_user_id = owner_uuid
AND fa.family_user_id = current_user_id()
AND fa.is_active = true
AND (fa.access_end_date IS NULL OR fa.access_end_date > now())
AND EXISTS (
SELECT 1 FROM unnest(perms) p
WHERE (fa.access_permissions ->> p)::boolean = true
)
);
$$;
--
-- Name: has_library_access_with_public(uuid, boolean, text[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.has_library_access_with_public(owner_uuid uuid, is_shared boolean, perms text[]) RETURNS boolean
LANGUAGE sql STABLE
AS $$
SELECT current_user_id() = owner_uuid OR is_shared OR has_family_access_or(owner_uuid, perms);
$$;
--
-- Name: manage_goal_timeline(uuid, date, numeric, numeric, numeric, numeric, integer, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.manage_goal_timeline(p_user_id uuid, p_start_date date, p_calories numeric, p_protein numeric, p_carbs numeric, p_fat numeric, p_water_goal integer, p_saturated_fat numeric DEFAULT 20, p_polyunsaturated_fat numeric DEFAULT 10, p_monounsaturated_fat numeric DEFAULT 25, p_trans_fat numeric DEFAULT 0, p_cholesterol numeric DEFAULT 300, p_sodium numeric DEFAULT 2300, p_potassium numeric DEFAULT 3500, p_dietary_fiber numeric DEFAULT 25, p_sugars numeric DEFAULT 50, p_vitamin_a numeric DEFAULT 900, p_vitamin_c numeric DEFAULT 90, p_calcium numeric DEFAULT 1000, p_iron numeric DEFAULT 18) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_end_date DATE;
v_current_date DATE;
BEGIN
-- If editing a past date (before today), only update that specific date
IF p_start_date < CURRENT_DATE THEN
INSERT INTO public.user_goals (
user_id, goal_date, calories, protein, carbs, fat, water_goal,
saturated_fat, polyunsaturated_fat, monounsaturated_fat, trans_fat,
cholesterol, sodium, potassium, dietary_fiber, sugars,
vitamin_a, vitamin_c, calcium, iron
)
VALUES (
p_user_id, p_start_date, p_calories, p_protein, p_carbs, p_fat, p_water_goal,
p_saturated_fat, p_polyunsaturated_fat, p_monounsaturated_fat, p_trans_fat,
p_cholesterol, p_sodium, p_potassium, p_dietary_fiber, p_sugars,
p_vitamin_a, p_vitamin_c, p_calcium, p_iron
)
ON CONFLICT (user_id, COALESCE(goal_date, '1900-01-01'::date))
DO UPDATE SET
calories = EXCLUDED.calories,
protein = EXCLUDED.protein,
carbs = EXCLUDED.carbs,
fat = EXCLUDED.fat,
water_goal = EXCLUDED.water_goal,
saturated_fat = EXCLUDED.saturated_fat,
polyunsaturated_fat = EXCLUDED.polyunsaturated_fat,
monounsaturated_fat = EXCLUDED.monounsaturated_fat,
trans_fat = EXCLUDED.trans_fat,
cholesterol = EXCLUDED.cholesterol,
sodium = EXCLUDED.sodium,
potassium = EXCLUDED.potassium,
dietary_fiber = EXCLUDED.dietary_fiber,
sugars = EXCLUDED.sugars,
vitamin_a = EXCLUDED.vitamin_a,
vitamin_c = EXCLUDED.vitamin_c,
calcium = EXCLUDED.calcium,
iron = EXCLUDED.iron,
updated_at = now();
RETURN;
END IF;
-- For today or future dates: delete 6 months and insert new goals
v_end_date := p_start_date + INTERVAL '6 months';
-- Delete all existing goals from start date for 6 months
DELETE FROM public.user_goals
WHERE user_id = p_user_id
AND goal_date >= p_start_date
AND goal_date < v_end_date
AND goal_date IS NOT NULL;
-- Insert new goals for each day in the 6-month range
v_current_date := v_end_date; -- Start from end date and go backwards to avoid conflicts
WHILE v_current_date >= p_start_date LOOP
INSERT INTO public.user_goals (
user_id, goal_date, calories, protein, carbs, fat, water_goal,
saturated_fat, polyunsaturated_fat, monounsaturated_fat, trans_fat,
cholesterol, sodium, potassium, dietary_fiber, sugars,
vitamin_a, vitamin_c, calcium, iron
)
VALUES (
p_user_id, v_current_date, p_calories, p_protein, p_carbs, p_fat, p_water_goal,
p_saturated_fat, p_polyunsaturated_fat, p_monounsaturated_fat, p_trans_fat,
p_cholesterol, p_sodium, p_potassium, p_dietary_fiber, p_sugars,
p_vitamin_a, p_vitamin_c, p_calcium, p_iron
);
v_current_date := v_current_date - 1;
END LOOP;
-- Remove the default goal (NULL goal_date) to avoid conflicts
DELETE FROM public.user_goals
WHERE user_id = p_user_id AND goal_date IS NULL;
END;
$$;
--
-- Name: revoke_all_user_api_keys(uuid); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.revoke_all_user_api_keys(p_user_id uuid) RETURNS void
LANGUAGE plpgsql
AS $$
begin
update public.user_api_keys
set is_active = false, updated_at = now()
where user_id = p_user_id;
end;
$$;
--
-- Name: revoke_user_api_key(uuid, text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.revoke_user_api_key(p_user_id uuid, p_api_key text) RETURNS void
LANGUAGE plpgsql
AS $$
begin
update public.user_api_keys
set is_active = false, updated_at = now()
where user_id = p_user_id and api_key = p_api_key;
end;
$$;
--
-- Name: set_updated_at_timestamp(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.set_updated_at_timestamp() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
--
-- Name: set_user_id(uuid); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.set_user_id(user_id uuid) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
PERFORM set_config('app.user_id', user_id::text, false);
END;
$$;
--
-- Name: trigger_set_timestamp(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.trigger_set_timestamp() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
--
-- Name: update_external_data_providers_updated_at(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.update_external_data_providers_updated_at() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
--
-- Name: update_timestamp(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.update_timestamp() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
--
-- Name: update_updated_at_column(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.update_updated_at_column() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: users; Type: TABLE; Schema: auth; Owner: -
--
CREATE TABLE auth.users (
id uuid NOT NULL,
email text,
password_hash text NOT NULL,
raw_user_meta_data jsonb,
created_at timestamp with time zone,
updated_at timestamp with time zone,
role character varying(50) DEFAULT 'user'::character varying NOT NULL,
password_reset_token character varying(255),
password_reset_expires bigint,
is_active boolean DEFAULT true,
last_login_at timestamp with time zone,
mfa_secret text,
mfa_totp_enabled boolean DEFAULT false,
mfa_email_enabled boolean DEFAULT false,
mfa_recovery_codes jsonb,
mfa_enforced boolean DEFAULT false,
magic_link_token text,
magic_link_expires timestamp with time zone,
email_mfa_code text,
email_mfa_expires_at timestamp with time zone
);
--
-- Name: admin_activity_logs; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.admin_activity_logs (
id uuid DEFAULT public.uuid_generate_v4() NOT NULL,
admin_user_id uuid NOT NULL,
target_user_id uuid,
action_type character varying(255) NOT NULL,
details jsonb,
created_at timestamp with time zone DEFAULT now()
);
--
-- Name: ai_service_settings; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.ai_service_settings (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
service_type text NOT NULL,
service_name text NOT NULL,
custom_url text,
is_active boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
system_prompt text DEFAULT ''::text,
model_name text,
encrypted_api_key text,
api_key_iv text,
api_key_tag text
);
--
-- Name: backup_settings; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.backup_settings (
id integer NOT NULL,
backup_enabled boolean DEFAULT false NOT NULL,
backup_days text[] DEFAULT '{}'::text[] NOT NULL,
backup_time text DEFAULT '02:00'::text NOT NULL,
retention_days integer DEFAULT 7 NOT NULL,
last_backup_status text,
last_backup_timestamp timestamp with time zone,
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP
);
--
-- Name: backup_settings_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.backup_settings_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: backup_settings_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.backup_settings_id_seq OWNED BY public.backup_settings.id;
--
-- Name: check_in_measurements; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.check_in_measurements (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
entry_date date DEFAULT CURRENT_DATE NOT NULL,
weight numeric,
neck numeric,
waist numeric,
hips numeric,
steps integer,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
height numeric,
body_fat_percentage numeric,
created_by_user_id uuid,
updated_by_user_id uuid
);
--
-- Name: custom_categories; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.custom_categories (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
name character varying(50) NOT NULL,
measurement_type character varying(50) NOT NULL,
frequency text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
data_type text DEFAULT 'numeric'::text,
created_by_user_id uuid,
updated_by_user_id uuid,
display_name character varying(100),
CONSTRAINT custom_categories_frequency_check CHECK ((frequency = ANY (ARRAY['All'::text, 'Daily'::text, 'Hourly'::text])))
);
--
-- Name: COLUMN custom_categories.display_name; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.custom_categories.display_name IS 'User-editable display name for the category. If NULL, the name field is used for display. The name field serves as the stable identifier for syncing and lookups.';
--
-- Name: custom_measurements; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.custom_measurements (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
category_id uuid NOT NULL,
value text NOT NULL,
entry_date date NOT NULL,
entry_hour integer,
entry_timestamp timestamp with time zone DEFAULT now() NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
notes text,
created_by_user_id uuid,
updated_by_user_id uuid,
source character varying(50) DEFAULT 'manual'::character varying NOT NULL
);
--
-- Name: exercise_entries; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.exercise_entries (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
exercise_id uuid NOT NULL,
duration_minutes numeric NOT NULL,
calories_burned numeric NOT NULL,
entry_date date DEFAULT CURRENT_DATE,
notes text,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now() NOT NULL,
workout_plan_assignment_id integer,
image_url text,
created_by_user_id uuid,
exercise_name text,
calories_per_hour numeric,
updated_by_user_id uuid,
category text,
source character varying(50),
source_id character varying(255),
force character varying(50),
level character varying(50),
mechanic character varying(50),
equipment text,
primary_muscles text,
secondary_muscles text,
instructions text,
images text,
distance numeric,
avg_heart_rate integer,
exercise_preset_entry_id uuid
);
--
-- Name: exercise_entry_activity_details; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.exercise_entry_activity_details (
id uuid DEFAULT public.uuid_generate_v4() NOT NULL,
exercise_entry_id uuid,
provider_name text NOT NULL,
detail_type text NOT NULL,
detail_data jsonb NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
created_by_user_id uuid,
updated_by_user_id uuid,
exercise_preset_entry_id uuid,
CONSTRAINT chk_exercise_entry_id_or_preset_id CHECK ((((exercise_entry_id IS NOT NULL) AND (exercise_preset_entry_id IS NULL)) OR ((exercise_entry_id IS NULL) AND (exercise_preset_entry_id IS NOT NULL))))
);
--
-- Name: exercise_entry_sets; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.exercise_entry_sets (
id integer NOT NULL,
exercise_entry_id uuid NOT NULL,
set_number integer NOT NULL,
set_type text DEFAULT 'Working Set'::text,
reps integer,
weight numeric(10,2),
duration integer,
rest_time integer,
notes text,
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP
);
--
-- Name: exercise_entry_sets_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.exercise_entry_sets_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: exercise_entry_sets_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.exercise_entry_sets_id_seq OWNED BY public.exercise_entry_sets.id;