Files
backend_v2/scripts/sql/008_create_histories.sql
toom1996 1fd9c48f58 update
2026-09-02 21:51:35 +08:00

22 lines
1.3 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.

-- 008: histories —— 用户浏览历史(服务端持久化,按用户隔离,跨设备一致)
-- 与 favorites 同理:表结构由 SQL 托管,不依赖 GORM AutoMigrate。
--
-- 2026-09-02 收敛为「文章级」浏览历史:
-- 1) 触发点:点开某篇走秀/街拍详情(/item/xxx)时记一条;进入列表页不再记。
-- 2) 仅存 (user_id, target_uid, viewed_at):target_uid 为带类型前缀的对外编码串
-- (runway r=xxx / street s=xxx,见后端 hashid.EncodeTyped),前缀本身携带类型,
-- 无需 kind / target_type 列。
-- 3) title/cover/brand 快照已移除:账户页展示时按 target_uid 回查公开详情接口,
-- 保持表最简、展示数据新鲜。
-- 唯一键 (user_id, target_uid):同一篇文章反复打开只 upsert 刷新 viewed_at,绝不新增行。
-- 每用户上限 HISTORY_CAP(应用层维护,默认 2000),超出 FIFO 删最旧。
CREATE TABLE IF NOT EXISTS `histories` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`viewed_at` int unsigned NOT NULL DEFAULT 0,
`user_id` int unsigned NOT NULL,
`target_uid` varchar(64) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_user_target` (`user_id`, `target_uid`),
KEY `idx_user_viewed` (`user_id`, `viewed_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;