Files
backend_v2/db/migrations/2026-09-24-01-runway-parent-image-id.sql
toom1996 aaeab41d67 update
2026-09-25 19:59:06 +08:00

41 lines
2.1 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 2026-09-24-01 走秀图片改用 parent_image_id 分组(与街拍同构),移除 look_index。
--
-- 背景:走秀图片原用 look_index 把「主图 + 细节图」归到同一 Look 桶(look_index 仅作为分组桶号,
-- 对产品无意义)。现统一为 parent_image_id(细节图直接指向所属主图的行 id),与 street_snap_images
-- 完全同构,后台可复用同一套「合并 / 拆出」逻辑。
--
-- 步骤:
-- 1) 给 brand_runway_images 加 parent_image_id 列(与街拍对齐,默认 0);
-- 2) 回填:每个 look 内 is_detail=0 的主图,其同 look 的 is_detail=1 细节图 parent_image_id 置为该主图 id;
-- 3) 删 look_index 列;
-- 4) 重建 public_brand_runway_images 视图(原 SELECT i.* 在删列后失效,重建即按新列集展开)。
--
-- 幂等:ADD COLUMN IF NOT EXISTS;视图 DROP VIEW IF EXISTS 后重建。
-- 应用后请重新导出结构:dbtool dump -clean -out db_dump.sql
ALTER TABLE brand_runway_images ADD COLUMN IF NOT EXISTS parent_image_id bigint NOT NULL DEFAULT 0;
-- 回填:对每个 runway,把每个 look 的 is_detail=1 细节图 parent_image_id 置为该 look 内 is_detail=0 主图的 id。
-- 孤儿细节图(同 look 无主图)保持 parent_image_id=0(COALESCE 兜底,避免 NOT NULL 报错)。
UPDATE brand_runway_images AS d
SET parent_image_id = COALESCE((
SELECT MIN(m.id)
FROM brand_runway_images AS m
WHERE m.runway_id = d.runway_id
AND m.look_index = d.look_index
AND m.is_detail = 0
AND m.is_deleted = 0
), 0)
WHERE d.is_detail = 1 AND d.is_deleted = 0 AND d.parent_image_id = 0;
-- 注意:必须**先**删视图,再删 look_index 列——视图 SELECT i.* 依赖 look_index,
-- 列还在时 DROP COLUMN 会报 "cannot drop column ... because other objects depend on it"。
DROP VIEW IF EXISTS public_brand_runway_images;
ALTER TABLE brand_runway_images DROP COLUMN IF EXISTS look_index;
CREATE VIEW public_brand_runway_images AS
SELECT i.* FROM brand_runway_images i
JOIN brand_runways r ON r.id = i.runway_id
WHERE i.is_deleted = 0 AND r.status = 'published' AND r.is_deleted = 0;