00006_movies_realtime_replica_identity.sql 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. -- Enable Supabase Realtime UPDATE payloads for movies.
  2. --
  3. -- Two requirements for cross-window real-time UPDATE propagation:
  4. --
  5. -- 1. The table must be a member of the `supabase_realtime` publication.
  6. -- Without this, no postgres_changes events are emitted at all.
  7. --
  8. -- 2. REPLICA IDENTITY FULL is required for UPDATE payloads to include the
  9. -- full new row. With the default (REPLICA IDENTITY DEFAULT, which logs
  10. -- only the primary key), `payload.new` arrives with only `id` populated
  11. -- and our TanStack Query cache merge replaces a fully-populated row with
  12. -- a sparse one, losing the `watched` flag and other fields.
  13. --
  14. -- INSERT and DELETE work without FULL because INSERT carries the full new
  15. -- row regardless and DELETE only needs the PK to identify the row to remove.
  16. -- That matches the user-reported symptom (only UPDATE was broken).
  17. ALTER TABLE public.movies REPLICA IDENTITY FULL;
  18. -- Idempotent publication membership: ADD TABLE errors if already present,
  19. -- so guard with a DO block that checks pg_publication_tables first.
  20. DO $$
  21. BEGIN
  22. IF NOT EXISTS (
  23. SELECT 1 FROM pg_publication_tables
  24. WHERE pubname = 'supabase_realtime'
  25. AND schemaname = 'public'
  26. AND tablename = 'movies'
  27. ) THEN
  28. EXECUTE 'ALTER PUBLICATION supabase_realtime ADD TABLE public.movies';
  29. END IF;
  30. END
  31. $$;