feat: add get_artist_fans RPC for paginated fan lookup

Closed
AR
7mo ago
+69-0

Summary

  • Adds public.get_artist_fans(artist_account_id uuid, limit_count int, offset_count int) which joins artist_segments -> fan_segments -> socials, DISTINCT-on socials.id, orders by updated_at DESC NULLS LAST, id, and paginates in-database.
  • Returns the 9-field socials projection plus total_count via COUNT(*) OVER(), repeated on each row (callers read from the first row).
  • Replaces the api-side composer that fetched every fan_social_id into memory and sliced — that approach silently truncated above Supabase's implicit 10,000-row ceiling for popular artists.

Why

The api route GET /api/artists/{id}/fans currently composes three round-trips and a memory-side dedupe+slice. Pushing DISTINCT/ORDER/LIMIT/OFFSET into Postgres removes the row ceiling, cuts wire volume, and lets existing indexes (idx_artist_segments_artist_account_id, idx_fan_segments_segment_id, idx_fan_segments_fan_social_id, socials PK) do the work.

Notes

  • STABLE LANGUAGE sql. No RLS on artist_segments, fan_segments, or socials today, so the function inherits current access semantics.
  • Envelope/shape matches the api response byte-for-byte; no api-surface change.
  • Response envelope produced by the api caller stays identical: { status, fans, pagination: { total_count, page, limit, total_pages } }.

Test plan

  • Apply migration to the dev Supabase instance.
  • Smoke: select * from get_artist_fans('<artist uuid>', 20, 0); returns rows with total_count column matching a manual COUNT(DISTINCT fan_social_id).
  • Verify api PR recoupable/api#453 against preview once migration is applied.

Deploy ordering

Must land and be applied to the Supabase instance before recoupable/api#453.


Summary by cubic

Adds a Postgres RPC to fetch an artist’s fans with in-database dedupe, ordering, and pagination, replacing the API-side composer. This removes the 10k-row ceiling for popular artists and cuts round-trips.

  • New Features

    • New public.get_artist_fans(artist_account_id uuid, limit int, offset int) joining artist_segments → fan_segments → socials.
    • Uses DISTINCT on socials.id, orders by updated_at DESC NULLS LAST, id, and paginates in DB.
    • Returns the socials projection plus total_count via COUNT(*) OVER(); API response shape remains unchanged.
  • Migration

    • Apply the Supabase migration before recoupable/api#453.
    • Smoke test: select * from public.get_artist_fans('<artist-uuid>', 20, 0); and verify total_count.