-- 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);