-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_updates.sql
More file actions
233 lines (187 loc) · 6.99 KB
/
database_updates.sql
File metadata and controls
233 lines (187 loc) · 6.99 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
-- ============================================
-- DATABASE UPDATES FOR NEW FEATURES
-- Run these in your Supabase SQL Editor
-- ============================================
-- ============================================
-- 1. ADD NEW COLUMNS TO NOTES TABLE
-- ============================================
-- Add year column for past papers
ALTER TABLE notes
ADD COLUMN IF NOT EXISTS year INTEGER;
-- Add chapter/unit column for organizing by syllabus
ALTER TABLE notes
ADD COLUMN IF NOT EXISTS chapter TEXT;
-- Add tags array for better searchability
ALTER TABLE notes
ADD COLUMN IF NOT EXISTS tags TEXT[];
-- Add views counter for popularity tracking
ALTER TABLE notes
ADD COLUMN IF NOT EXISTS views INTEGER DEFAULT 0;
-- ============================================
-- 2. UPDATE EXISTING NOTES TABLE (if needed)
-- ============================================
-- Make sure downloads column exists with default
ALTER TABLE notes
ALTER COLUMN downloads SET DEFAULT 0;
-- ============================================
-- 3. ADD POLICY FOR UPDATES (if not exists)
-- ============================================
-- Policy for Notes: Everyone can update (for download/view counts)
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_policies
WHERE tablename = 'notes'
AND policyname = 'Public Notes Update'
) THEN
CREATE POLICY "Public Notes Update"
ON notes FOR UPDATE
USING (true);
END IF;
END $$;
-- ============================================
-- 4. CREATE INDEX FOR BETTER PERFORMANCE
-- ============================================
-- Index for grade filtering
CREATE INDEX IF NOT EXISTS idx_notes_grade ON notes(grade);
-- Index for medium filtering
CREATE INDEX IF NOT EXISTS idx_notes_medium ON notes(medium);
-- Index for type filtering
CREATE INDEX IF NOT EXISTS idx_notes_type ON notes(type);
-- Index for year filtering
CREATE INDEX IF NOT EXISTS idx_notes_year ON notes(year);
-- Index for subject filtering
CREATE INDEX IF NOT EXISTS idx_notes_subject ON notes(subject);
-- Composite index for common filter combinations
CREATE INDEX IF NOT EXISTS idx_notes_grade_medium ON notes(grade, medium);
CREATE INDEX IF NOT EXISTS idx_notes_grade_type ON notes(grade, type);
-- ============================================
-- 5. OPTIONAL: CREATE BOOKMARKS TABLE
-- (For future bookmarks/favorites feature)
-- ============================================
CREATE TABLE IF NOT EXISTS bookmarks (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
user_id TEXT NOT NULL, -- Can be device ID or user ID if you add auth
note_id BIGINT NOT NULL REFERENCES notes(id) ON DELETE CASCADE,
UNIQUE(user_id, note_id)
);
-- Enable RLS for bookmarks
ALTER TABLE bookmarks ENABLE ROW LEVEL SECURITY;
-- Policy for Bookmarks: Users can manage their own bookmarks
CREATE POLICY "Users can manage bookmarks"
ON bookmarks FOR ALL
USING (true)
WITH CHECK (true);
-- ============================================
-- 6. OPTIONAL: CREATE RATINGS TABLE
-- (For future rating feature)
-- ============================================
CREATE TABLE IF NOT EXISTS ratings (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
user_id TEXT NOT NULL,
note_id BIGINT NOT NULL REFERENCES notes(id) ON DELETE CASCADE,
rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 5),
UNIQUE(user_id, note_id)
);
-- Enable RLS for ratings
ALTER TABLE ratings ENABLE ROW LEVEL SECURITY;
-- Policy for Ratings
CREATE POLICY "Public Ratings Access"
ON ratings FOR ALL
USING (true)
WITH CHECK (true);
-- ============================================
-- 7. OPTIONAL: CREATE COMMENTS TABLE
-- (For future comments feature)
-- ============================================
CREATE TABLE IF NOT EXISTS comments (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
user_name TEXT NOT NULL,
note_id BIGINT NOT NULL REFERENCES notes(id) ON DELETE CASCADE,
content TEXT NOT NULL,
likes INTEGER DEFAULT 0
);
-- Enable RLS for comments
ALTER TABLE comments ENABLE ROW LEVEL SECURITY;
-- Policy for Comments
CREATE POLICY "Public Comments Access"
ON comments FOR SELECT
USING (true);
CREATE POLICY "Public Comments Insert"
ON comments FOR INSERT
WITH CHECK (true);
-- ============================================
-- 8. CREATE THANK YOU NOTES TABLE
-- (For community appreciation feature)
-- ============================================
CREATE TABLE IF NOT EXISTS thank_you_notes (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
student_name TEXT NOT NULL,
message TEXT NOT NULL,
contributor_name TEXT DEFAULT 'All Contributors'
);
-- Enable RLS for thank_you_notes
ALTER TABLE thank_you_notes ENABLE ROW LEVEL SECURITY;
-- Policy for Thank You Notes: Everyone can read
CREATE POLICY "Public Thank You Notes Read"
ON thank_you_notes FOR SELECT
USING (true);
-- Policy for Thank You Notes: Everyone can insert
CREATE POLICY "Public Thank You Notes Insert"
ON thank_you_notes FOR INSERT
WITH CHECK (true);
-- Index for faster queries
CREATE INDEX IF NOT EXISTS idx_thank_you_notes_created ON thank_you_notes(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_thank_you_notes_contributor ON thank_you_notes(contributor_name);
-- ============================================
-- 9. HELPFUL VIEWS (OPTIONAL)
-- ============================================
-- View for popular materials (most downloaded)
CREATE OR REPLACE VIEW popular_notes AS
SELECT * FROM notes
ORDER BY downloads DESC
LIMIT 10;
-- View for recent materials
CREATE OR REPLACE VIEW recent_notes AS
SELECT * FROM notes
ORDER BY created_at DESC
LIMIT 10;
-- View for O/L materials
CREATE OR REPLACE VIEW ol_notes AS
SELECT * FROM notes
WHERE grade = 'Grade 11 (O/L)'
ORDER BY created_at DESC;
-- View for A/L materials
CREATE OR REPLACE VIEW al_notes AS
SELECT * FROM notes
WHERE grade = 'Grade 12-13 (A/L)'
ORDER BY created_at DESC;
-- ============================================
-- 9. FUNCTION TO GET SUBJECTS LIST
-- ============================================
CREATE OR REPLACE FUNCTION get_unique_subjects()
RETURNS TABLE(subject TEXT) AS $$
BEGIN
RETURN QUERY
SELECT DISTINCT n.subject
FROM notes n
WHERE n.subject IS NOT NULL
ORDER BY n.subject;
END;
$$ LANGUAGE plpgsql;
-- ============================================
-- VERIFICATION QUERIES
-- Run these to verify the updates worked
-- ============================================
-- Check notes table structure
-- SELECT column_name, data_type, column_default
-- FROM information_schema.columns
-- WHERE table_name = 'notes';
-- Check existing data
-- SELECT id, title, grade, medium, type, year, downloads FROM notes LIMIT 10;
-- Check indexes
-- SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'notes';