Go + PostgreSQL + Valkey backend (cmd/internal/migrations), uni-app mobile frontend, PC web (web/), admin console (admin/), Android WebView container (android_project/). Build artifacts, node_modules, local env and debug screenshots excluded.
80 lines
3.5 KiB
SQL
80 lines
3.5 KiB
SQL
-- +goose Up
|
|
|
|
-- 1. 用户表扩展字段
|
|
ALTER TABLE users ADD COLUMN IF NOT EXISTS gender TEXT DEFAULT 'unknown';
|
|
ALTER TABLE users ADD COLUMN IF NOT EXISTS region TEXT;
|
|
|
|
-- 2. 模板表
|
|
CREATE TABLE IF NOT EXISTS templates (
|
|
id UUID PRIMARY KEY,
|
|
title TEXT NOT NULL,
|
|
description TEXT,
|
|
category TEXT NOT NULL DEFAULT 'general',
|
|
cover_url TEXT,
|
|
target_platform TEXT NOT NULL DEFAULT 'all',
|
|
sections_json JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_templates_category ON templates (category, updated_at DESC);
|
|
|
|
-- 3. 话题表
|
|
CREATE TABLE IF NOT EXISTS topics (
|
|
id UUID PRIMARY KEY,
|
|
name TEXT NOT NULL UNIQUE,
|
|
description TEXT,
|
|
cover_url TEXT,
|
|
category_id UUID REFERENCES categories(id) ON DELETE SET NULL,
|
|
follow_count BIGINT NOT NULL DEFAULT 0 CHECK (follow_count >= 0),
|
|
article_count BIGINT NOT NULL DEFAULT 0 CHECK (article_count >= 0),
|
|
status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'hidden', 'disabled')),
|
|
creator_id UUID REFERENCES users(id) ON DELETE SET NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_topics_status ON topics (status, updated_at DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_topics_category ON topics (category_id);
|
|
CREATE INDEX IF NOT EXISTS idx_topics_heat ON topics ((article_count + follow_count) DESC);
|
|
|
|
-- 文章表关联话题
|
|
ALTER TABLE articles ADD COLUMN IF NOT EXISTS topic_id UUID REFERENCES topics(id) ON DELETE SET NULL;
|
|
CREATE INDEX IF NOT EXISTS idx_articles_topic ON articles (topic_id) WHERE deleted_at IS NULL;
|
|
|
|
-- 4. 用户关注话题
|
|
CREATE TABLE IF NOT EXISTS user_topic_follows (
|
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
topic_id UUID NOT NULL REFERENCES topics(id) ON DELETE CASCADE,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
PRIMARY KEY (user_id, topic_id)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_utf_topic ON user_topic_follows (topic_id, created_at DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_utf_user ON user_topic_follows (user_id, created_at DESC);
|
|
|
|
-- 5. 文章浏览历史
|
|
CREATE TABLE IF NOT EXISTS article_views (
|
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
article_id UUID NOT NULL REFERENCES articles(id) ON DELETE CASCADE,
|
|
viewed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
PRIMARY KEY (user_id, article_id)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_article_views_user ON article_views (user_id, viewed_at DESC);
|
|
|
|
CREATE TABLE IF NOT EXISTS article_view_history (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
article_id UUID NOT NULL REFERENCES articles(id) ON DELETE CASCADE,
|
|
viewed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
CONSTRAINT uq_user_article_history UNIQUE (user_id, article_id)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_article_view_history_user ON article_view_history (user_id, viewed_at DESC);
|
|
|
|
-- +goose Down
|
|
DROP TABLE IF EXISTS article_view_history;
|
|
DROP TABLE IF EXISTS article_views;
|
|
DROP TABLE IF EXISTS user_topic_follows;
|
|
ALTER TABLE articles DROP COLUMN IF EXISTS topic_id;
|
|
DROP TABLE IF EXISTS topics;
|
|
DROP TABLE IF EXISTS templates;
|
|
ALTER TABLE users DROP COLUMN IF EXISTS region;
|
|
ALTER TABLE users DROP COLUMN IF EXISTS gender;
|