Files
spsays-bff/migrations/0016_backfill_reshare_inherited_images.up.sql

30 lines
1.3 KiB
SQL

-- Backfill the "reshares inherit the canonical's images" rule for any
-- reshare row that landed in `posts` without an image set.
--
-- Two paths created such rows:
--
-- 1. Reshares made BEFORE migration 0014 had no per-resharer image
-- table to snapshot into, so when 0015 collapsed the old
-- post_reshares model into real posts rows there was nothing for
-- it to copy — those reshares now show with a blank background
-- where the original author has a cover image.
--
-- 2. Any future code path that creates a reshare row but forgets to
-- copy the images. Defensive — current `Reshare` in
-- internal/repository/post_repo.go does copy, but this migration
-- makes the database self-healing if we ever regress.
--
-- The copy is scoped to reshares with ZERO images of their own so we
-- never overwrite a resharer's explicit customization (including an
-- explicit "no background" — though that path didn't exist at the time
-- the affected rows were created, the guard keeps us safe for any
-- future re-runs of equivalent backfills).
INSERT INTO post_images (post_id, url, position)
SELECT r.id, i.url, i.position
FROM posts r
JOIN post_images i ON i.post_id = r.reshared_from_post_id
WHERE r.reshared_from_post_id IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM post_images WHERE post_id = r.id
);