yggdrasil/migrations/016_assets_content_hash.sql
xfy 5743c7a581
Some checks failed
CI / check (push) Has been cancelled
CI / build (push) Has been cancelled
feat(upload): 上传图片按内容 SHA-256 去重,重复上传复用已登记素材
assets 新增 content_hash 列(唯一索引,迁移 016;存量行保持 NULL
不参与去重)。上传在安全性校验后、转码前对原始字节算 SHA-256:
命中即刷新 created_at/updated_at(重启 7 天清理保护窗)并返回
已有 URL,跳过 GIF/WebP 验证、转码与落盘。并发同内容上传由唯一
索引 + INSERT ... ON CONFLICT DO NOTHING 兜底,落败者删落盘文件
复用胜出者路径。命中响应带 reused: true。仅精确去重,视觉相似
检测(pHash)有意不做。
2026-07-27 13:25:26 +08:00

11 lines
674 B
SQL
Raw 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.

-- 上传内容去重assets.content_hash 存原始上传字节的 SHA-256hex
-- 同一内容重复上传时复用已登记素材(同一行、同一文件),不再重复落盘。
-- 唯一索引是并发去重的正确性基础:两个并发上传同内容时,后到的
-- INSERT ... ON CONFLICT (content_hash) DO NOTHING 落空,转而复用先到的行。
-- 存量行不回填(需读全量文件,代价高):保持 NULL不参与去重
-- PG 唯一索引容许多个 NULL互不冲突。
ALTER TABLE assets ADD COLUMN IF NOT EXISTS content_hash TEXT;
CREATE UNIQUE INDEX IF NOT EXISTS idx_assets_content_hash ON assets (content_hash);