Files
petfeed/prisma/backfill-counters.sql
adminandClaude Sonnet 5 f14be58c06 feat: engagement-counter denormalization, pagination fixes, dead-code cleanup
Bundles the 2026-07-12 code-audit session (Clusters A/B/C/D partial/E/F):
denormalized Post/Advertisement reaction/comment/repost counters synced
transactionally instead of live _count queries; real cursor-based pagination
for followers/following/blocks lists; assertPetOwnership + formatRelativeTime
centralized; dead r2.ts + AWS SDK deps removed; missing DB indexes added;
account-deletion flow, mention notifications, pull-to-refresh feed, and
mobile UI/i18n fixes from the surrounding sessions.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-16 19:22:23 +02:00

15 lines
940 B
SQL

-- One-off backfill for the new denormalized engagement counters (Cluster A).
-- Run once, immediately after `prisma migrate dev` adds the columns (which
-- default to 0), so existing posts/ads don't briefly show 0 engagement.
-- Safe to re-run — it always recomputes from the source-of-truth tables.
UPDATE "Post" p
SET "reactionCount" = COALESCE((SELECT COUNT(*) FROM "Reaction" r WHERE r."postId" = p.id), 0),
"commentCount" = COALESCE((SELECT COUNT(*) FROM "Comment" c WHERE c."postId" = p.id), 0),
"repostCount" = COALESCE((SELECT COUNT(*) FROM "Repost" rp WHERE rp."originalPostId" = p.id), 0);
UPDATE "Advertisement" a
SET "adReactionCount" = COALESCE((SELECT COUNT(*) FROM "AdReaction" ar WHERE ar."adId" = a.id), 0),
"adCommentCount" = COALESCE((SELECT COUNT(*) FROM "AdComment" ac WHERE ac."adId" = a.id), 0),
"adRepostCount" = COALESCE((SELECT COUNT(*) FROM "AdRepost" arp WHERE arp."adId" = a.id), 0);