-- Migration 007: Realtime publication for shopping lists & items.
--
-- Supabase Realtime broadcasts Postgres changes through a logical-replication
-- publication named `supabase_realtime`. Tables not in the publication emit
-- no events, so nothing reaches subscribed clients.
--
-- REPLICA IDENTITY FULL makes UPDATE and DELETE events carry the entire row
-- (not just the primary key). We need this for two reasons:
--   1) Clients filter events by `list_id` on shopping_items — a PK-only payload
--      on UPDATE/DELETE doesn't include list_id and the filter silently drops
--      valid events.
--   2) Supabase Realtime evaluates RLS against the row being broadcast; for
--      DELETE events that means evaluating it against the OLD row, which is
--      only available with REPLICA IDENTITY FULL.
--
-- Cost: logical-replication WAL entries grow by ~1 row width per mutation.
-- For the shopping-lists workload (low write rate, small rows) the impact is
-- negligible.

ALTER PUBLICATION supabase_realtime ADD TABLE public.shopping_items;
ALTER PUBLICATION supabase_realtime ADD TABLE public.shopping_lists;

ALTER TABLE public.shopping_items REPLICA IDENTITY FULL;
ALTER TABLE public.shopping_lists REPLICA IDENTITY FULL;
