20250611112005-a020aac0-375b-4fa5-bb38-1be2a4a63a80.sql 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. -- Create articles table to store RSS feed content
  2. CREATE TABLE public.articles (
  3. id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  4. feed_id UUID REFERENCES public.feeds(id) ON DELETE CASCADE NOT NULL,
  5. title TEXT NOT NULL,
  6. description TEXT,
  7. content TEXT,
  8. url TEXT,
  9. image_url TEXT,
  10. published_at TIMESTAMP WITH TIME ZONE NOT NULL,
  11. guid TEXT, -- RSS GUID for deduplication
  12. read_time INTEGER DEFAULT 5, -- estimated read time in minutes
  13. created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  14. updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  15. -- Ensure we don't duplicate articles
  16. UNIQUE(feed_id, guid)
  17. );
  18. -- Add indexes for better performance
  19. CREATE INDEX idx_articles_feed_id ON public.articles(feed_id);
  20. CREATE INDEX idx_articles_published_at ON public.articles(published_at DESC);
  21. CREATE INDEX idx_articles_guid ON public.articles(guid);
  22. -- Create user_articles table to track read status and pins
  23. CREATE TABLE public.user_articles (
  24. id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  25. user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
  26. article_id UUID REFERENCES public.articles(id) ON DELETE CASCADE NOT NULL,
  27. is_read BOOLEAN NOT NULL DEFAULT false,
  28. is_pinned BOOLEAN NOT NULL DEFAULT false,
  29. read_at TIMESTAMP WITH TIME ZONE,
  30. created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  31. -- Ensure one record per user per article
  32. UNIQUE(user_id, article_id)
  33. );
  34. -- Add indexes for user_articles
  35. CREATE INDEX idx_user_articles_user_id ON public.user_articles(user_id);
  36. CREATE INDEX idx_user_articles_article_id ON public.user_articles(article_id);
  37. -- Enable RLS on articles (public read, but we'll filter by user subscriptions in code)
  38. ALTER TABLE public.articles ENABLE ROW LEVEL SECURITY;
  39. -- Allow authenticated users to read articles
  40. CREATE POLICY "Authenticated users can read articles"
  41. ON public.articles
  42. FOR SELECT
  43. TO authenticated
  44. USING (true);
  45. -- Enable RLS on user_articles
  46. ALTER TABLE public.user_articles ENABLE ROW LEVEL SECURITY;
  47. -- Users can only access their own article interactions
  48. CREATE POLICY "Users can manage their own article interactions"
  49. ON public.user_articles
  50. FOR ALL
  51. TO authenticated
  52. USING (auth.uid() = user_id)
  53. WITH CHECK (auth.uid() = user_id);
  54. -- Update feeds table to track last fetch time
  55. ALTER TABLE public.feeds
  56. ADD COLUMN IF NOT EXISTS last_fetched_at TIMESTAMP WITH TIME ZONE;