-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathschema.sql
More file actions
3657 lines (3272 loc) · 151 KB
/
Copy pathschema.sql
File metadata and controls
3657 lines (3272 loc) · 151 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
-- =============================================================================
-- Roost Community Platform - Consolidated Database Schema
-- =============================================================================
-- This is the COMPLETE schema for fresh installations.
-- It represents the final state after all migrations (001-059) and fixes.
-- Run this in Supabase SQL Editor for a new deployment.
--
-- Generated: 2026-03-23
-- Tables: 40+
-- Functions: 30+
-- Triggers: 20+
-- RLS Policies: 80+
-- =============================================================================
-- #############################################################################
-- SECTION 1: EXTENSIONS
-- #############################################################################
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- #############################################################################
-- SECTION 2: ENUM TYPES
-- #############################################################################
-- Group member roles
CREATE TYPE group_role AS ENUM ('admin', 'moderator', 'member');
-- Platform-level user roles
CREATE TYPE user_role AS ENUM ('user', 'moderator', 'admin', 'superadmin');
-- Reaction types (includes haha from migration 042)
CREATE TYPE reaction_type AS ENUM ('like', 'love', 'fire', 'clap', 'think', 'haha');
-- What entity a reaction is attached to (includes showcase, feature_request)
CREATE TYPE reactable_type AS ENUM ('post', 'comment', 'showcase', 'feature_request');
-- Asset file types
CREATE TYPE asset_type AS ENUM ('image', 'video', 'document', 'other');
-- Notification types (includes comment_reply from migration 025)
CREATE TYPE notification_type AS ENUM (
'new_comment',
'new_reaction',
'new_message',
'new_follower',
'mention',
'group_invite',
'group_join',
'event_reminder',
'comment_reply'
);
-- RSVP status for events
CREATE TYPE rsvp_status AS ENUM ('going', 'maybe', 'not_going');
-- Point activity types (includes showcase_approved from migration 023)
CREATE TYPE point_action_type AS ENUM (
'post_created',
'comment_created',
'reaction_given',
'reaction_received',
'event_attended',
'daily_login',
'profile_completed',
'manual_adjustment',
'showcase_approved'
);
-- Announcement types
CREATE TYPE announcement_type AS ENUM ('info', 'warning', 'success', 'error');
CREATE TYPE announcement_scope AS ENUM ('global', 'group');
-- Showcase types
CREATE TYPE showcase_status AS ENUM ('pending', 'approved', 'rejected', 'featured');
CREATE TYPE showcase_category AS ENUM (
'web_app', 'mobile_app', 'saas', 'tool', 'api',
'website', 'game', 'extension', 'other'
);
-- Activation request status
CREATE TYPE activation_request_status AS ENUM (
'pending', 'in_progress', 'completed', 'rejected'
);
-- Feature request types
CREATE TYPE feature_request_status AS ENUM (
'under_review', 'planned', 'in_progress', 'released', 'declined', 'duplicate'
);
CREATE TYPE feature_request_type AS ENUM (
'feature_request', 'bug_report', 'improvement'
);
-- #############################################################################
-- SECTION 3: CORE TABLES
-- #############################################################################
-- =============================================================================
-- 3.1 PROFILES (User Profiles)
-- =============================================================================
CREATE TABLE IF NOT EXISTS profiles (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
username TEXT UNIQUE NOT NULL,
display_name TEXT NOT NULL,
avatar_url TEXT,
cover_url TEXT, -- Profile cover/banner photo (migration 057)
bio TEXT,
location TEXT,
website TEXT,
is_online BOOLEAN DEFAULT false,
last_seen_at TIMESTAMPTZ DEFAULT NOW(),
-- Platform role (migration 003_platform_roles)
role user_role DEFAULT 'user',
-- Membership tier (migration 029)
membership_type TEXT DEFAULT 'free' CHECK (membership_type IN ('free', 'premium')),
-- Ban system (migration 017)
is_banned BOOLEAN DEFAULT false,
ban_reason TEXT,
ban_expires_at TIMESTAMPTZ,
banned_by UUID REFERENCES profiles(id),
banned_at TIMESTAMPTZ,
-- Two-factor auth (migration 024)
two_factor_enabled BOOLEAN DEFAULT FALSE,
two_factor_secret TEXT,
two_factor_verified_at TIMESTAMPTZ,
-- Timestamps
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
-- Constraints
CONSTRAINT username_length CHECK (char_length(username) >= 3 AND char_length(username) <= 30),
CONSTRAINT username_format CHECK (username ~ '^[a-zA-Z0-9_-]+$')
);
CREATE INDEX idx_profiles_username ON profiles(username);
CREATE INDEX idx_profiles_online ON profiles(is_online) WHERE is_online = true;
CREATE INDEX idx_profiles_role ON profiles(role);
CREATE INDEX idx_profiles_membership_type ON profiles(membership_type);
CREATE INDEX IF NOT EXISTS idx_profiles_is_banned ON profiles(is_banned) WHERE is_banned = true;
CREATE INDEX IF NOT EXISTS idx_profiles_ban_expires ON profiles(ban_expires_at) WHERE ban_expires_at IS NOT NULL;
-- =============================================================================
-- 3.2 GROUPS (Communities/Groups)
-- =============================================================================
CREATE TABLE IF NOT EXISTS groups (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
slug TEXT UNIQUE NOT NULL,
description TEXT,
avatar_url TEXT,
cover_url TEXT,
is_private BOOLEAN DEFAULT false,
is_premium BOOLEAN DEFAULT false, -- Premium-only groups (migration 029)
layout_mode TEXT NOT NULL DEFAULT 'sidebar' -- default, sidebar, learn (migration 006, 058)
CHECK (layout_mode IN ('default', 'sidebar', 'learn')),
created_by UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT group_name_length CHECK (char_length(name) >= 2 AND char_length(name) <= 100),
CONSTRAINT group_slug_format CHECK (slug ~ '^[a-z0-9-]+$')
);
CREATE INDEX idx_groups_slug ON groups(slug);
CREATE INDEX idx_groups_created_by ON groups(created_by);
CREATE INDEX IF NOT EXISTS idx_groups_is_premium ON groups(is_premium);
-- =============================================================================
-- 3.3 GROUP_MEMBERS (Group Membership)
-- =============================================================================
CREATE TABLE IF NOT EXISTS group_members (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
group_id UUID NOT NULL REFERENCES groups(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
role group_role DEFAULT 'member',
joined_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(group_id, user_id)
);
CREATE INDEX idx_group_members_group ON group_members(group_id);
CREATE INDEX idx_group_members_user ON group_members(user_id);
CREATE INDEX idx_group_members_role ON group_members(role);
-- =============================================================================
-- 3.4 CATEGORIES (Post Categories)
-- =============================================================================
CREATE TABLE IF NOT EXISTS categories (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
slug TEXT UNIQUE NOT NULL,
color TEXT DEFAULT '#6366f1',
icon TEXT,
group_id UUID REFERENCES groups(id) ON DELETE CASCADE,
display_order INTEGER DEFAULT 0, -- migration 017
is_active BOOLEAN DEFAULT true, -- migration 017
created_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT category_name_length CHECK (char_length(name) >= 2 AND char_length(name) <= 50)
);
CREATE INDEX idx_categories_slug ON categories(slug);
CREATE INDEX idx_categories_group ON categories(group_id);
CREATE INDEX IF NOT EXISTS idx_categories_order ON categories(display_order);
-- =============================================================================
-- 3.5 POSTS (Community Posts) - group_id is NULLABLE (migration 004)
-- =============================================================================
CREATE TABLE IF NOT EXISTS posts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT,
content TEXT NOT NULL,
author_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
group_id UUID REFERENCES groups(id) ON DELETE CASCADE, -- NULLABLE for general feed
category_id UUID REFERENCES categories(id) ON DELETE SET NULL,
is_pinned BOOLEAN DEFAULT false,
is_edited BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT post_content_length CHECK (char_length(content) >= 1 AND char_length(content) <= 10000)
);
CREATE INDEX idx_posts_author ON posts(author_id);
CREATE INDEX idx_posts_group ON posts(group_id) WHERE group_id IS NOT NULL;
CREATE INDEX idx_posts_no_group ON posts(created_at DESC) WHERE group_id IS NULL;
CREATE INDEX idx_posts_category ON posts(category_id);
CREATE INDEX idx_posts_pinned ON posts(is_pinned) WHERE is_pinned = true;
CREATE INDEX idx_posts_created_at ON posts(created_at DESC);
-- =============================================================================
-- 3.6 COMMENTS (Post & Recording Comments)
-- =============================================================================
CREATE TABLE IF NOT EXISTS comments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
post_id UUID REFERENCES posts(id) ON DELETE CASCADE, -- NULLABLE (migration 058)
recording_id UUID, -- FK added after recordings table
author_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
content TEXT NOT NULL,
parent_comment_id UUID REFERENCES comments(id) ON DELETE CASCADE,
is_edited BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT comment_content_length CHECK (char_length(content) >= 1 AND char_length(content) <= 5000),
CONSTRAINT comments_target_check CHECK (post_id IS NOT NULL OR recording_id IS NOT NULL)
);
CREATE INDEX idx_comments_post ON comments(post_id);
CREATE INDEX idx_comments_author ON comments(author_id);
CREATE INDEX idx_comments_parent ON comments(parent_comment_id);
CREATE INDEX idx_comments_created_at ON comments(created_at ASC);
-- =============================================================================
-- 3.7 REACTIONS (Likes/Reactions to Posts, Comments, Showcases, Feature Requests)
-- =============================================================================
CREATE TABLE IF NOT EXISTS reactions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
reactable_type reactable_type NOT NULL,
reactable_id UUID NOT NULL,
reaction_type reaction_type DEFAULT 'like',
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, reactable_type, reactable_id)
);
CREATE INDEX idx_reactions_user ON reactions(user_id);
CREATE INDEX idx_reactions_reactable ON reactions(reactable_type, reactable_id);
-- =============================================================================
-- 3.8 MESSAGES (Direct Messages)
-- =============================================================================
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
sender_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
recipient_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
content TEXT NOT NULL,
is_read BOOLEAN DEFAULT false,
read_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT message_content_length CHECK (char_length(content) >= 1 AND char_length(content) <= 5000),
CONSTRAINT no_self_message CHECK (sender_id != recipient_id)
);
CREATE INDEX idx_messages_sender ON messages(sender_id);
CREATE INDEX idx_messages_recipient ON messages(recipient_id);
CREATE INDEX idx_messages_unread ON messages(recipient_id, is_read) WHERE is_read = false;
CREATE INDEX idx_messages_created_at ON messages(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_messages_conversation ON messages (
LEAST(sender_id, recipient_id),
GREATEST(sender_id, recipient_id),
created_at DESC
);
-- =============================================================================
-- 3.9 ASSETS (Uploaded Files/Media)
-- =============================================================================
CREATE TABLE IF NOT EXISTS assets (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
filename TEXT NOT NULL,
file_url TEXT NOT NULL,
file_size BIGINT,
mime_type TEXT,
asset_type asset_type NOT NULL,
uploaded_by UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
post_id UUID REFERENCES posts(id) ON DELETE CASCADE,
message_id UUID REFERENCES messages(id) ON DELETE CASCADE,
feature_request_id UUID, -- FK added after feature_requests table (migration 049)
created_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT file_size_check CHECK (file_size <= 104857600) -- 100MB max
);
CREATE INDEX idx_assets_uploader ON assets(uploaded_by);
CREATE INDEX idx_assets_post ON assets(post_id);
CREATE INDEX idx_assets_message ON assets(message_id);
CREATE INDEX idx_assets_type ON assets(asset_type);
-- =============================================================================
-- 3.10 NOTIFICATIONS (User Notifications)
-- =============================================================================
CREATE TABLE IF NOT EXISTS notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
type notification_type NOT NULL,
title TEXT NOT NULL,
message TEXT,
link TEXT,
is_read BOOLEAN DEFAULT false,
email_pending BOOLEAN DEFAULT FALSE, -- migration 025
email_sent_at TIMESTAMPTZ, -- migration 025
created_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT notification_title_length CHECK (char_length(title) >= 1 AND char_length(title) <= 200)
);
CREATE INDEX idx_notifications_user ON notifications(user_id);
CREATE INDEX idx_notifications_unread ON notifications(user_id, is_read) WHERE is_read = false;
CREATE INDEX idx_notifications_created_at ON notifications(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notifications_email_pending ON notifications(email_pending) WHERE email_pending = TRUE;
-- =============================================================================
-- 3.11 LEADERBOARD_ENTRIES (Points and Ranking)
-- =============================================================================
CREATE TABLE IF NOT EXISTS leaderboard_entries (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
group_id UUID REFERENCES groups(id) ON DELETE CASCADE,
total_points INTEGER DEFAULT 0,
rank INTEGER,
period_start DATE NOT NULL,
period_end DATE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_leaderboard_user ON leaderboard_entries(user_id);
CREATE INDEX idx_leaderboard_group ON leaderboard_entries(group_id);
CREATE INDEX idx_leaderboard_points ON leaderboard_entries(total_points DESC);
CREATE INDEX idx_leaderboard_period ON leaderboard_entries(period_start, period_end);
-- Unique index that properly handles NULL group_id
CREATE UNIQUE INDEX idx_leaderboard_unique_entry
ON leaderboard_entries (
user_id,
COALESCE(group_id, '00000000-0000-0000-0000-000000000000'::uuid),
period_start,
period_end
);
-- =============================================================================
-- 3.12 EVENTS (Calendar Events) - group_id is NULLABLE (migration 015)
-- =============================================================================
CREATE TABLE IF NOT EXISTS events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
description TEXT,
start_time TIMESTAMPTZ NOT NULL,
end_time TIMESTAMPTZ NOT NULL,
location TEXT,
is_virtual BOOLEAN DEFAULT false,
meeting_url TEXT,
group_id UUID REFERENCES groups(id) ON DELETE CASCADE, -- NULLABLE for community-wide events
created_by UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT event_title_length CHECK (char_length(title) >= 2 AND char_length(title) <= 200),
CONSTRAINT event_time_check CHECK (end_time > start_time)
);
CREATE INDEX idx_events_group ON events(group_id) WHERE group_id IS NOT NULL;
CREATE INDEX idx_events_no_group ON events(start_time) WHERE group_id IS NULL;
CREATE INDEX idx_events_creator ON events(created_by);
CREATE INDEX idx_events_start_time ON events(start_time ASC);
-- =============================================================================
-- 3.13 EVENT_ATTENDEES (Event RSVPs)
-- =============================================================================
CREATE TABLE IF NOT EXISTS event_attendees (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
event_id UUID NOT NULL REFERENCES events(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
status rsvp_status DEFAULT 'going',
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(event_id, user_id)
);
CREATE INDEX idx_event_attendees_event ON event_attendees(event_id);
CREATE INDEX idx_event_attendees_user ON event_attendees(user_id);
CREATE INDEX idx_event_attendees_status ON event_attendees(status);
-- =============================================================================
-- 3.14 POINT_ACTIVITIES (Points Tracking)
-- =============================================================================
CREATE TABLE IF NOT EXISTS point_activities (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
group_id UUID REFERENCES groups(id) ON DELETE CASCADE,
action_type point_action_type NOT NULL,
points INTEGER NOT NULL,
description TEXT,
reference_id UUID,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_point_activities_user ON point_activities(user_id);
CREATE INDEX idx_point_activities_group ON point_activities(group_id);
CREATE INDEX idx_point_activities_action ON point_activities(action_type);
CREATE INDEX idx_point_activities_created ON point_activities(created_at DESC);
CREATE INDEX idx_point_activities_user_created ON point_activities(user_id, created_at DESC);
-- =============================================================================
-- 3.15 GROUP_ASSETS (Group File Attachments)
-- =============================================================================
CREATE TABLE IF NOT EXISTS group_assets (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
group_id UUID NOT NULL REFERENCES groups(id) ON DELETE CASCADE,
asset_id UUID NOT NULL REFERENCES assets(id) ON DELETE CASCADE,
uploaded_by UUID NOT NULL REFERENCES profiles(id),
module_id UUID, -- FK added after modules table (migration 058)
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(group_id, asset_id)
);
CREATE INDEX idx_group_assets_group ON group_assets(group_id);
CREATE INDEX idx_group_assets_asset ON group_assets(asset_id);
-- =============================================================================
-- 3.16 RECORDINGS (Video Recordings)
-- =============================================================================
CREATE TABLE IF NOT EXISTS recordings (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
group_id UUID NOT NULL REFERENCES groups(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT,
video_url TEXT NOT NULL,
video_platform TEXT NOT NULL CHECK (video_platform IN ('youtube', 'vimeo')),
video_id TEXT NOT NULL,
thumbnail_url TEXT,
module_id UUID, -- FK added after modules table (migration 058)
display_order INTEGER NOT NULL DEFAULT 0, -- migration 058
published_by UUID NOT NULL REFERENCES profiles(id),
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_recordings_group ON recordings(group_id);
CREATE INDEX idx_recordings_created ON recordings(created_at DESC);
-- Now add FK for comments.recording_id
ALTER TABLE comments ADD CONSTRAINT comments_recording_id_fkey
FOREIGN KEY (recording_id) REFERENCES recordings(id) ON DELETE CASCADE;
CREATE INDEX idx_comments_recording ON comments(recording_id);
-- #############################################################################
-- SECTION 4: FEATURE TABLES
-- #############################################################################
-- =============================================================================
-- 4.1 ANNOUNCEMENTS
-- =============================================================================
CREATE TABLE IF NOT EXISTS announcements (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR(200) NOT NULL,
content TEXT NOT NULL,
type announcement_type DEFAULT 'info',
scope announcement_scope DEFAULT 'global',
group_id UUID REFERENCES groups(id) ON DELETE CASCADE,
is_active BOOLEAN DEFAULT true,
is_dismissible BOOLEAN DEFAULT true,
starts_at TIMESTAMPTZ DEFAULT NOW(),
expires_at TIMESTAMPTZ,
created_by UUID REFERENCES profiles(id) NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_announcements_active ON announcements(is_active) WHERE is_active = true;
CREATE INDEX IF NOT EXISTS idx_announcements_scope ON announcements(scope);
CREATE INDEX IF NOT EXISTS idx_announcements_group ON announcements(group_id) WHERE group_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_announcements_dates ON announcements(starts_at, expires_at);
-- =============================================================================
-- 4.2 ANNOUNCEMENT_DISMISSALS
-- =============================================================================
CREATE TABLE IF NOT EXISTS announcement_dismissals (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
announcement_id UUID REFERENCES announcements(id) ON DELETE CASCADE NOT NULL,
user_id UUID REFERENCES profiles(id) ON DELETE CASCADE NOT NULL,
dismissed_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(announcement_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_announcement_dismissals_user ON announcement_dismissals(user_id);
-- =============================================================================
-- 4.3 PASSWORD_RESET_TOKENS
-- =============================================================================
CREATE TABLE IF NOT EXISTS password_reset_tokens (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
token_hash TEXT NOT NULL UNIQUE,
expires_at TIMESTAMPTZ NOT NULL,
used_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_password_reset_tokens_hash ON password_reset_tokens(token_hash);
CREATE INDEX IF NOT EXISTS idx_password_reset_tokens_user ON password_reset_tokens(user_id);
CREATE INDEX IF NOT EXISTS idx_password_reset_tokens_expires ON password_reset_tokens(expires_at);
-- =============================================================================
-- 4.4 TWO_FACTOR_BACKUP_CODES
-- =============================================================================
CREATE TABLE IF NOT EXISTS two_factor_backup_codes (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
code_hash TEXT NOT NULL,
used_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_two_factor_backup_codes_user_id ON two_factor_backup_codes(user_id);
CREATE INDEX IF NOT EXISTS idx_two_factor_backup_codes_code_hash ON two_factor_backup_codes(code_hash);
-- =============================================================================
-- 4.5 NOTIFICATION_PREFERENCES
-- =============================================================================
CREATE TABLE IF NOT EXISTS notification_preferences (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
notify_comments BOOLEAN DEFAULT TRUE,
notify_replies BOOLEAN DEFAULT TRUE,
notify_mentions BOOLEAN DEFAULT TRUE,
notify_messages BOOLEAN DEFAULT TRUE,
notify_reactions BOOLEAN DEFAULT TRUE,
email_comments BOOLEAN DEFAULT TRUE,
email_replies BOOLEAN DEFAULT TRUE,
email_mentions BOOLEAN DEFAULT TRUE,
email_messages BOOLEAN DEFAULT TRUE,
email_announcements BOOLEAN DEFAULT TRUE,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id)
);
CREATE INDEX idx_notification_preferences_user ON notification_preferences(user_id);
-- =============================================================================
-- 4.6 SHOWCASES (ProductHunt-style showcase)
-- =============================================================================
CREATE TABLE IF NOT EXISTS showcases (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR(255) NOT NULL,
tagline VARCHAR(150) NOT NULL,
description TEXT NOT NULL,
url VARCHAR(500) NOT NULL,
thumbnail_url VARCHAR(500),
category showcase_category NOT NULL,
tech_stack TEXT[] DEFAULT '{}',
author_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
status showcase_status DEFAULT 'pending',
moderation_notes TEXT,
moderated_by UUID REFERENCES profiles(id),
moderated_at TIMESTAMPTZ,
launch_date DATE,
is_featured BOOLEAN DEFAULT false,
featured_at TIMESTAMPTZ,
vote_count INTEGER DEFAULT 0,
review_count INTEGER DEFAULT 0,
average_rating NUMERIC(2,1) DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT showcase_title_length CHECK (char_length(title) >= 3 AND char_length(title) <= 255),
CONSTRAINT showcase_tagline_length CHECK (char_length(tagline) >= 10 AND char_length(tagline) <= 150),
CONSTRAINT showcase_description_length CHECK (char_length(description) >= 50 AND char_length(description) <= 10000),
CONSTRAINT showcase_url_format CHECK (url ~ '^https?://')
);
CREATE INDEX idx_showcases_status ON showcases(status);
CREATE INDEX idx_showcases_author ON showcases(author_id);
CREATE INDEX idx_showcases_category ON showcases(category);
CREATE INDEX idx_showcases_launch_date ON showcases(launch_date DESC NULLS LAST);
CREATE INDEX idx_showcases_vote_count ON showcases(vote_count DESC);
CREATE INDEX idx_showcases_featured ON showcases(is_featured) WHERE is_featured = true;
CREATE INDEX idx_showcases_created_at ON showcases(created_at DESC);
-- =============================================================================
-- 4.7 SHOWCASE_IMAGES
-- =============================================================================
CREATE TABLE IF NOT EXISTS showcase_images (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
showcase_id UUID NOT NULL REFERENCES showcases(id) ON DELETE CASCADE,
image_url VARCHAR(500) NOT NULL,
display_order INTEGER DEFAULT 0,
caption VARCHAR(255),
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_showcase_images_showcase ON showcase_images(showcase_id);
CREATE INDEX idx_showcase_images_order ON showcase_images(showcase_id, display_order);
-- =============================================================================
-- 4.8 SHOWCASE_TAGS
-- =============================================================================
CREATE TABLE IF NOT EXISTS showcase_tags (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(50) NOT NULL UNIQUE,
slug VARCHAR(50) NOT NULL UNIQUE,
color VARCHAR(7) DEFAULT '#6B7280',
created_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT showcase_tag_name_length CHECK (char_length(name) >= 2 AND char_length(name) <= 50),
CONSTRAINT showcase_tag_slug_format CHECK (slug ~ '^[a-z0-9-]+$')
);
CREATE INDEX idx_showcase_tags_slug ON showcase_tags(slug);
-- =============================================================================
-- 4.9 SHOWCASE_TAG_RELATIONS
-- =============================================================================
CREATE TABLE IF NOT EXISTS showcase_tag_relations (
showcase_id UUID NOT NULL REFERENCES showcases(id) ON DELETE CASCADE,
tag_id UUID NOT NULL REFERENCES showcase_tags(id) ON DELETE CASCADE,
PRIMARY KEY (showcase_id, tag_id)
);
CREATE INDEX idx_showcase_tag_relations_tag ON showcase_tag_relations(tag_id);
-- =============================================================================
-- 4.10 SHOWCASE_REVIEWS
-- =============================================================================
CREATE TABLE IF NOT EXISTS showcase_reviews (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
showcase_id UUID NOT NULL REFERENCES showcases(id) ON DELETE CASCADE,
author_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
content TEXT NOT NULL,
rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 5),
maker_reply TEXT,
maker_replied_at TIMESTAMPTZ,
is_edited BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(showcase_id, author_id),
CONSTRAINT showcase_review_content_length CHECK (char_length(content) >= 10 AND char_length(content) <= 2000)
);
CREATE INDEX idx_showcase_reviews_showcase ON showcase_reviews(showcase_id);
CREATE INDEX idx_showcase_reviews_author ON showcase_reviews(author_id);
CREATE INDEX idx_showcase_reviews_rating ON showcase_reviews(rating);
CREATE INDEX idx_showcase_reviews_created_at ON showcase_reviews(created_at DESC);
-- =============================================================================
-- 4.11 ACTIVATION_PRODUCTS
-- =============================================================================
CREATE TABLE IF NOT EXISTS activation_products (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
slug TEXT UNIQUE NOT NULL,
description TEXT,
product_type TEXT NOT NULL DEFAULT 'plugin',
monthly_limit INTEGER NOT NULL DEFAULT 1,
is_active BOOLEAN DEFAULT true,
icon_url TEXT,
instructions TEXT,
license_key TEXT DEFAULT NULL, -- migration 044
file_url TEXT DEFAULT NULL, -- migration 045
file_name TEXT DEFAULT NULL, -- migration 045
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT monthly_limit_positive CHECK (monthly_limit > 0)
);
CREATE INDEX IF NOT EXISTS idx_activation_products_slug ON activation_products(slug);
CREATE INDEX IF NOT EXISTS idx_activation_products_type ON activation_products(product_type);
CREATE INDEX IF NOT EXISTS idx_activation_products_active ON activation_products(is_active) WHERE is_active = true;
-- =============================================================================
-- 4.12 ACTIVATION_REQUESTS
-- =============================================================================
CREATE TABLE IF NOT EXISTS activation_requests (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
product_id UUID NOT NULL REFERENCES activation_products(id) ON DELETE CASCADE,
status activation_request_status DEFAULT 'pending',
website_url TEXT NOT NULL,
wp_username TEXT NOT NULL,
wp_password TEXT NOT NULL,
notes TEXT,
admin_notes TEXT,
processed_by UUID REFERENCES profiles(id),
processed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_activation_requests_user ON activation_requests(user_id);
CREATE INDEX IF NOT EXISTS idx_activation_requests_product ON activation_requests(product_id);
CREATE INDEX IF NOT EXISTS idx_activation_requests_status ON activation_requests(status);
CREATE INDEX IF NOT EXISTS idx_activation_requests_pending ON activation_requests(status, created_at)
WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS idx_activation_requests_month ON activation_requests(user_id, product_id, created_at);
-- =============================================================================
-- 4.13 ACTIVATION_USAGE
-- =============================================================================
CREATE TABLE IF NOT EXISTS activation_usage (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
product_id UUID NOT NULL REFERENCES activation_products(id) ON DELETE CASCADE,
month_year DATE NOT NULL,
usage_count INTEGER DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT unique_user_product_month UNIQUE (user_id, product_id, month_year),
CONSTRAINT usage_count_positive CHECK (usage_count >= 0)
);
CREATE INDEX IF NOT EXISTS idx_activation_usage_lookup ON activation_usage(user_id, product_id, month_year);
-- =============================================================================
-- 4.14 FEATURE_REQUESTS
-- =============================================================================
CREATE TABLE IF NOT EXISTS feature_requests (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
type feature_request_type NOT NULL DEFAULT 'feature_request',
status feature_request_status NOT NULL DEFAULT 'under_review',
author_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
admin_response TEXT,
is_pinned BOOLEAN DEFAULT false,
vote_count INTEGER DEFAULT 0,
comment_count INTEGER DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT fr_title_length CHECK (char_length(title) >= 5 AND char_length(title) <= 255),
CONSTRAINT fr_description_length CHECK (char_length(description) >= 20 AND char_length(description) <= 10000)
);
CREATE INDEX idx_feature_requests_status ON feature_requests(status);
CREATE INDEX idx_feature_requests_type ON feature_requests(type);
CREATE INDEX idx_feature_requests_author ON feature_requests(author_id);
CREATE INDEX idx_feature_requests_vote_count ON feature_requests(vote_count DESC);
CREATE INDEX idx_feature_requests_created_at ON feature_requests(created_at DESC);
CREATE INDEX idx_feature_requests_pinned ON feature_requests(is_pinned) WHERE is_pinned = true;
-- Add FK for assets.feature_request_id
ALTER TABLE assets ADD CONSTRAINT assets_feature_request_id_fkey
FOREIGN KEY (feature_request_id) REFERENCES feature_requests(id) ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS idx_assets_feature_request_id ON assets(feature_request_id)
WHERE feature_request_id IS NOT NULL;
-- =============================================================================
-- 4.15 FEATURE_REQUEST_COMMENTS
-- =============================================================================
CREATE TABLE IF NOT EXISTS feature_request_comments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
feature_request_id UUID NOT NULL REFERENCES feature_requests(id) ON DELETE CASCADE,
author_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
content TEXT NOT NULL,
parent_comment_id UUID REFERENCES feature_request_comments(id) ON DELETE CASCADE,
is_edited BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT fr_comment_length CHECK (char_length(content) >= 1 AND char_length(content) <= 5000)
);
CREATE INDEX idx_fr_comments_request ON feature_request_comments(feature_request_id);
CREATE INDEX idx_fr_comments_author ON feature_request_comments(author_id);
CREATE INDEX idx_fr_comments_parent ON feature_request_comments(parent_comment_id);
CREATE INDEX idx_fr_comments_created ON feature_request_comments(created_at);
-- =============================================================================
-- 4.16 FOLLOWS (User Following)
-- =============================================================================
CREATE TABLE IF NOT EXISTS follows (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
follower_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
following_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(follower_id, following_id),
CONSTRAINT no_self_follow CHECK (follower_id != following_id)
);
CREATE INDEX IF NOT EXISTS idx_follows_follower ON follows(follower_id);
CREATE INDEX IF NOT EXISTS idx_follows_following ON follows(following_id);
-- =============================================================================
-- 4.17 LIVE_SESSIONS
-- =============================================================================
CREATE TABLE IF NOT EXISTS live_sessions (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
title TEXT NOT NULL DEFAULT 'Live Session',
description TEXT,
youtube_embed_url TEXT,
scheduled_at TIMESTAMPTZ,
visibility TEXT NOT NULL DEFAULT 'unlisted' CHECK (visibility IN ('unlisted', 'private')),
status TEXT NOT NULL DEFAULT 'idle' CHECK (status IN ('idle', 'live', 'ended')),
started_at TIMESTAMPTZ,
ended_at TIMESTAMPTZ,
created_by UUID REFERENCES profiles(id),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_live_sessions_status ON live_sessions(status) WHERE status = 'live';
CREATE INDEX idx_live_sessions_created_at ON live_sessions(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_live_sessions_scheduled ON live_sessions(scheduled_at) WHERE status = 'idle';
-- =============================================================================
-- 4.18 LIVE_SESSION_RSVPS
-- =============================================================================
CREATE TABLE IF NOT EXISTS live_session_rsvps (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
session_id UUID NOT NULL REFERENCES live_sessions(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
email TEXT NOT NULL,
display_name TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(session_id, user_id)
);
CREATE INDEX idx_live_session_rsvps_session ON live_session_rsvps(session_id);
CREATE INDEX idx_live_session_rsvps_user ON live_session_rsvps(user_id);
-- =============================================================================
-- 4.19 LIVE_SESSION_MESSAGES
-- =============================================================================
CREATE TABLE IF NOT EXISTS live_session_messages (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
session_id UUID NOT NULL REFERENCES live_sessions(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
content TEXT NOT NULL CHECK (char_length(content) BETWEEN 1 AND 500),
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_live_messages_session ON live_session_messages(session_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_live_messages_user ON live_session_messages(user_id);
-- =============================================================================
-- 4.20 COMMENT_VOTES (Upvote/Downvote)
-- =============================================================================
CREATE TABLE IF NOT EXISTS comment_votes (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
comment_id UUID NOT NULL REFERENCES comments(id) ON DELETE CASCADE,
vote_type TEXT NOT NULL CHECK (vote_type IN ('up', 'down')),
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, comment_id)
);
CREATE INDEX idx_comment_votes_comment ON comment_votes(comment_id);
CREATE INDEX idx_comment_votes_user ON comment_votes(user_id);
-- =============================================================================
-- 4.21 MODULES (Learn Mode)
-- =============================================================================
CREATE TABLE IF NOT EXISTS modules (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
group_id UUID NOT NULL REFERENCES groups(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT,
thumbnail_url TEXT,
display_order INTEGER NOT NULL DEFAULT 0,
created_by UUID NOT NULL REFERENCES profiles(id),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_modules_group ON modules(group_id);
CREATE INDEX idx_modules_order ON modules(group_id, display_order);
-- Add FK for recordings.module_id and group_assets.module_id
ALTER TABLE recordings ADD CONSTRAINT recordings_module_id_fkey
FOREIGN KEY (module_id) REFERENCES modules(id) ON DELETE SET NULL;
CREATE INDEX idx_recordings_module ON recordings(module_id);
ALTER TABLE group_assets ADD CONSTRAINT group_assets_module_id_fkey
FOREIGN KEY (module_id) REFERENCES modules(id) ON DELETE SET NULL;
CREATE INDEX idx_group_assets_module ON group_assets(module_id);
-- =============================================================================
-- 4.22 LESSON_COMPLETIONS
-- =============================================================================
CREATE TABLE IF NOT EXISTS lesson_completions (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
recording_id UUID NOT NULL REFERENCES recordings(id) ON DELETE CASCADE,
module_id UUID NOT NULL REFERENCES modules(id) ON DELETE CASCADE,
completed_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, recording_id)
);
CREATE INDEX idx_lesson_completions_user ON lesson_completions(user_id);
CREATE INDEX idx_lesson_completions_module ON lesson_completions(user_id, module_id);
CREATE INDEX idx_lesson_completions_recording ON lesson_completions(recording_id);
-- #############################################################################
-- SECTION 5: PAYMENT TABLES (Stripe)
-- #############################################################################
-- =============================================================================
-- 5.1 STRIPE_CUSTOMERS
-- =============================================================================
CREATE TABLE IF NOT EXISTS stripe_customers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
stripe_customer_id TEXT UNIQUE NOT NULL,
user_id UUID REFERENCES profiles(id) ON DELETE SET NULL,
email TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_stripe_customers_stripe_id ON stripe_customers(stripe_customer_id);
CREATE INDEX idx_stripe_customers_user_id ON stripe_customers(user_id);
CREATE INDEX idx_stripe_customers_email ON stripe_customers(email);
-- =============================================================================
-- 5.2 STRIPE_SUBSCRIPTIONS
-- =============================================================================
CREATE TABLE IF NOT EXISTS stripe_subscriptions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
stripe_subscription_id TEXT UNIQUE NOT NULL,
stripe_customer_id TEXT NOT NULL,
user_id UUID REFERENCES profiles(id) ON DELETE SET NULL,
stripe_price_id TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'active',
current_period_start TIMESTAMPTZ,
current_period_end TIMESTAMPTZ,
cancel_at_period_end BOOLEAN DEFAULT FALSE,
cancelled_at TIMESTAMPTZ,
ended_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT stripe_sub_status_check CHECK (
status IN ('active', 'cancelled', 'expired', 'failed_payment', 'refunded', 'past_due', 'trialing')
)
);
CREATE INDEX idx_stripe_subscriptions_sub_id ON stripe_subscriptions(stripe_subscription_id);
CREATE INDEX idx_stripe_subscriptions_customer_id ON stripe_subscriptions(stripe_customer_id);
CREATE INDEX idx_stripe_subscriptions_user_id ON stripe_subscriptions(user_id);
CREATE INDEX idx_stripe_subscriptions_status ON stripe_subscriptions(status);
-- =============================================================================
-- 5.3 STRIPE_WEBHOOK_LOGS
-- =============================================================================
CREATE TABLE IF NOT EXISTS stripe_webhook_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
event_type TEXT NOT NULL,
stripe_event_id TEXT UNIQUE,
payload JSONB NOT NULL DEFAULT '{}'::jsonb,
processed BOOLEAN DEFAULT FALSE,
error_message TEXT,
ip_address TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
processed_at TIMESTAMPTZ
);
CREATE INDEX idx_stripe_webhook_logs_event_id ON stripe_webhook_logs(stripe_event_id);
CREATE INDEX idx_stripe_webhook_logs_created_at ON stripe_webhook_logs(created_at DESC);
CREATE INDEX idx_stripe_webhook_logs_processed ON stripe_webhook_logs(processed);