owpengram-server/deploy/migrations/0002_channel_topic_read.up.sql

23 lines
1.4 KiB
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.

-- per-(user, channel, topic) 独立已读水位。
-- 背景forum 话题已读此前被频道级单一水位 channel_members.read_inbox_max_id 承载,
-- 读一个 topic 会污染其它 topic 的未读现算reply_to_top_id 过滤 + id > 频道级水位)。
-- channel_forum_topics 那几个 read_*_max_id 列无 user 维度PK=channel_id,topic_id
-- 无法承载 per-viewer 已读故新建本表topic_id=1 表示 General。
CREATE TABLE public.channel_topic_read (
channel_id bigint NOT NULL,
user_id bigint NOT NULL,
topic_id integer NOT NULL,
read_inbox_max_id integer DEFAULT 0 NOT NULL,
read_outbox_max_id integer DEFAULT 0 NOT NULL,
read_inbox_date integer DEFAULT 0 NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT channel_topic_read_pkey PRIMARY KEY (channel_id, user_id, topic_id),
CONSTRAINT channel_topic_read_topic_check CHECK (topic_id > 0),
CONSTRAINT channel_topic_read_channel_fkey FOREIGN KEY (channel_id)
REFERENCES public.channels (id) ON DELETE CASCADE
);
-- outbox 回执反查:某 topic 内某 viewer 自己消息被读到哪 = 该 topic 其他成员 read_inbox_max_id 的最大值。
-- (channel_id, topic_id, read_inbox_max_id DESC) 让 MAX 走 index-only避免每帧全表聚合。
CREATE INDEX channel_topic_read_outbox_idx
ON public.channel_topic_read (channel_id, topic_id, read_inbox_max_id DESC);