mike-backend / backend /migrations /008_notifications.sql
bardd's picture
Deploy Mike backend-only Space
06671aa verified
Raw History Blame Contribute Delete
2.64 kB
-- In-app notifications
create table if not exists public.notifications (
id uuid primary key default gen_random_uuid(),
-- IMPORTANT: auth.users.id is TEXT in this codebase (see migrations/999_mock_auth.sql:
-- "create table auth.users (id text primary key, email text)"), and every user FK
-- elsewhere is text (e.g. document_comment_threads.author_user_id text). Using uuid
-- here would (a) fail the FK (incompatible types uuid vs text) and (b) reject inserts
-- of text ids like 'dev-user-001'. Keep it text.
user_id text not null references auth.users(id) on delete cascade,
type text not null check (type = any (array['project_added'::text, 'comment_mention'::text])),
title text not null,
body text not null,
link_url text not null,
read_at timestamptz,
metadata jsonb not null default '{}'::jsonb,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
comment on table public.notifications is 'In-app notifications for project sharing and comment mentions.';
create index if not exists notifications_user_created_idx
on public.notifications(user_id, created_at desc);
create index if not exists notifications_user_unread_idx
on public.notifications(user_id) where read_at is null;
drop trigger if exists notifications_set_updated_at on public.notifications;
create trigger notifications_set_updated_at
before update on public.notifications
for each row execute function public.set_updated_at();
-- SECURITY MODEL (read carefully):
-- We intentionally do NOT `enable row level security` and do NOT create RLS
-- policies on this table. Two reasons:
-- 1. The backend DB shim (backend/src/lib/supabase.ts) runs raw SQL on a
-- plain pg Pool with NO auth context, so auth.uid() is always NULL.
-- If RLS were enabled, the read/write policies below would block the
-- server itself (every query), breaking the feature. (Only user_profiles
-- has RLS enabled anywhere in this schema; every other table relies on
-- application-level filtering — this table follows that convention.)
-- 2. In the mock/local-auth environment, `auth.uid()` may not even resolve,
-- so `create policy ... using (auth.uid() = user_id)` could fail to apply.
-- Recipient isolation is enforced in application code: every notifications
-- query is scoped with `.eq("user_id", userId)` where userId is the
-- authenticated user (see backend/src/routes/notifications.ts). Do NOT add
-- `alter table public.notifications enable row level security;` without also
-- introducing a service-role/RLS-bypassing client or a real auth context.