CREATE TABLE IF NOT EXISTS user_events ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, ad_id TEXT, comment_id TEXT, contract_id TEXT, data JSONB NOT NULL, name TEXT NOT NULL, ts TIMESTAMPTZ DEFAULT now() NOT NULL, user_id TEXT ); -- Row Level Security ALTER TABLE user_events ENABLE ROW LEVEL SECURITY; -- Policies DROP POLICY IF EXISTS "self and admin read" ON user_events; CREATE POLICY "self and admin read" ON user_events FOR SELECT USING ((user_id = firebase_uid()) OR is_admin(firebase_uid())); DROP POLICY IF EXISTS "user can insert" ON user_events; CREATE POLICY "user can insert" ON user_events FOR INSERT WITH CHECK (true); DROP POLICY IF EXISTS "anyone can insert" ON user_events; create policy "anyone can insert" on user_events for insert to anon with check (true); -- Indexes -- Primary key automatically creates a unique index on (id) CREATE INDEX IF NOT EXISTS user_events_ts ON public.user_events (ts DESC);