-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADD_VIEW_COUNT.sql
More file actions
30 lines (25 loc) · 954 Bytes
/
Copy pathADD_VIEW_COUNT.sql
File metadata and controls
30 lines (25 loc) · 954 Bytes
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
-- Migration: Add view_count column to entries table
-- Run this in your Supabase SQL Editor (https://supabase.com/dashboard → SQL Editor)
-- 1. Add view_count column
ALTER TABLE entries
ADD COLUMN IF NOT EXISTS view_count INTEGER DEFAULT 0;
-- 2. Create index for optimized queries
CREATE INDEX IF NOT EXISTS idx_entries_view_count ON entries(view_count DESC, created_at DESC);
-- 3. Create RPC function for atomic view count increment
-- This prevents race conditions when multiple requests try to increment simultaneously
CREATE OR REPLACE FUNCTION increment_entry_view_count(entry_id UUID, owner_id UUID)
RETURNS BOOLEAN
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
rows_updated INTEGER;
BEGIN
UPDATE entries
SET view_count = COALESCE(view_count, 0) + 1
WHERE id = entry_id AND user_id = owner_id;
GET DIAGNOSTICS rows_updated = ROW_COUNT;
RETURN rows_updated > 0;
END;
$$;
-- Done! The view_count feature should now work.