File size: 15,183 Bytes
b7efbe8 a95ade8 41ff5f6 a95ade8 41ff5f6 a95ade8 0985350 a95ade8 0985350 a95ade8 0985350 a95ade8 0985350 a95ade8 19e5dce a95ade8 0985350 a95ade8 0985350 a95ade8 0985350 a95ade8 0985350 e7643ca 0985350 e7643ca 41ff5f6 3039242 0985350 3039242 e7643ca 0985350 e7643ca 0985350 e7643ca 0985350 e7643ca 0985350 e7643ca 0985350 e7643ca 0985350 e7643ca 0985350 e7643ca c6622f3 0985350 c6622f3 0985350 c6622f3 41979ae c6622f3 0985350 4b0d2b1 0985350 4b0d2b1 0985350 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | -- Create the shared users table without dropping existing data.
CREATE TABLE IF NOT EXISTS public.users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
username TEXT UNIQUE,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
nickname TEXT,
avatar_url TEXT,
bio TEXT,
role TEXT DEFAULT 'student',
status TEXT DEFAULT 'active',
locale TEXT DEFAULT 'zh-CN',
timezone TEXT DEFAULT 'Asia/Shanghai',
last_login_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
-- Create the top_tools table
CREATE TABLE IF NOT EXISTS public.top_tools (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
url TEXT NOT NULL,
category TEXT NOT NULL,
tags TEXT[] DEFAULT '{}',
likes INTEGER DEFAULT 0,
added_at BIGINT NOT NULL,
image_url TEXT,
i18n JSONB NOT NULL,
user_id UUID REFERENCES public.users(id) ON DELETE CASCADE
);
-- Create the top_games table
CREATE TABLE IF NOT EXISTS public.top_games (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
url TEXT NOT NULL,
category TEXT NOT NULL DEFAULT 'game',
tags TEXT[] DEFAULT '{}',
likes INTEGER DEFAULT 0,
added_at BIGINT NOT NULL,
image_url TEXT,
i18n JSONB NOT NULL,
user_id UUID REFERENCES public.users(id) ON DELETE CASCADE
);
-- Create the top_prompts table
CREATE TABLE IF NOT EXISTS public.top_prompts (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
url TEXT NOT NULL,
category TEXT NOT NULL DEFAULT 'prompt',
tags TEXT[] DEFAULT '{}',
likes INTEGER DEFAULT 0,
added_at BIGINT NOT NULL,
image_url TEXT,
i18n JSONB NOT NULL,
user_id UUID REFERENCES public.users(id) ON DELETE CASCADE
);
-- Dynamics table
CREATE TABLE IF NOT EXISTS public.top_dynamics (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
original_content TEXT,
url TEXT,
source TEXT,
category TEXT,
published_at TIMESTAMPTZ DEFAULT now()
);
-- Performance Indexes for Search
CREATE INDEX IF NOT EXISTS idx_top_tools_name_description ON public.top_tools USING gin(to_tsvector('english', name || ' ' || description));
CREATE INDEX IF NOT EXISTS idx_top_games_name_description ON public.top_games USING gin(to_tsvector('english', name || ' ' || description));
CREATE INDEX IF NOT EXISTS idx_top_prompts_name_description ON public.top_prompts USING gin(to_tsvector('english', name || ' ' || description));
-- Ratings table
CREATE TABLE IF NOT EXISTS public.top_ratings (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
item_id TEXT NOT NULL,
item_type TEXT NOT NULL CHECK (item_type IN ('tool', 'game', 'prompt')),
user_id UUID REFERENCES public.users(id) ON DELETE CASCADE,
rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 5),
created_at TIMESTAMPTZ DEFAULT now(),
UNIQUE(item_id, item_type, user_id)
);
-- Comments table
CREATE TABLE IF NOT EXISTS public.top_comments (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
item_id TEXT NOT NULL,
item_type TEXT NOT NULL CHECK (item_type IN ('tool', 'game', 'prompt')),
user_id UUID REFERENCES public.users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_top_ratings_item ON public.top_ratings(item_id, item_type);
CREATE INDEX IF NOT EXISTS idx_top_comments_item ON public.top_comments(item_id, item_type);
-- ==========================================
-- MeloonExam 试卷共享与研讨数据表模型
-- ==========================================
-- 用户档案中心表 (Profiles)
CREATE TABLE IF NOT EXISTS public.exam_profiles (
id TEXT PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
nickname TEXT,
avatar TEXT,
role TEXT DEFAULT 'student' CHECK (role IN ('teacher', 'student', 'researcher')),
school TEXT,
grade TEXT,
bio TEXT,
points INTEGER DEFAULT 200,
uploaded_count INTEGER DEFAULT 0,
downloaded_count INTEGER DEFAULT 0,
favorites_count INTEGER DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
-- ==============================================================================
-- 统一用户主表与产品用户扩展表
-- users 保存登录和通用资料,产品特有资料分别进入独立 profile 表。
-- ==============================================================================
CREATE TABLE IF NOT EXISTS public.exam_profile (
user_id UUID PRIMARY KEY REFERENCES public.users(id) ON DELETE CASCADE,
school TEXT,
grade TEXT,
stage TEXT DEFAULT 'gaokao',
selected_subjects JSONB NOT NULL DEFAULT '[]'::jsonb,
subject_expectations JSONB NOT NULL DEFAULT '{}'::jsonb,
subject_scores JSONB NOT NULL DEFAULT '{}'::jsonb,
settings JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT exam_profile_selected_subjects_array
CHECK (jsonb_typeof(selected_subjects) = 'array'),
CONSTRAINT exam_profile_subject_expectations_object
CHECK (jsonb_typeof(subject_expectations) = 'object'),
CONSTRAINT exam_profile_subject_scores_object
CHECK (jsonb_typeof(subject_scores) = 'object')
);
CREATE TABLE IF NOT EXISTS public.top_profile (
user_id UUID PRIMARY KEY REFERENCES public.users(id) ON DELETE CASCADE,
theme TEXT NOT NULL DEFAULT 'system',
language TEXT NOT NULL DEFAULT 'zh-CN',
favorite_categories JSONB NOT NULL DEFAULT '[]'::jsonb,
settings JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT top_profile_favorite_categories_array
CHECK (jsonb_typeof(favorite_categories) = 'array')
);
CREATE TABLE IF NOT EXISTS public.chatbi_profile (
user_id UUID PRIMARY KEY REFERENCES public.users(id) ON DELETE CASCADE,
default_workspace_name TEXT,
default_data_source_id BIGINT,
settings 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_exam_profile_school_grade
ON public.exam_profile(school, grade);
-- 历史成绩表:每条记录对应一个用户在一次考试中的一个科目成绩
CREATE TABLE IF NOT EXISTS public.exam_score_history (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES public.exam_profiles(id) ON DELETE CASCADE,
exam_name TEXT NOT NULL,
exam_date DATE,
stage TEXT,
subject TEXT NOT NULL,
score NUMERIC(6, 2) NOT NULL CHECK (score >= 0),
full_score NUMERIC(6, 2) NOT NULL DEFAULT 150 CHECK (full_score > 0),
rank INTEGER CHECK (rank IS NULL OR rank > 0),
percentile NUMERIC(5, 2) CHECK (percentile IS NULL OR percentile BETWEEN 0 AND 100),
notes TEXT,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_exam_score_history_user_date
ON public.exam_score_history(user_id, exam_date DESC);
CREATE INDEX IF NOT EXISTS idx_exam_score_history_user_subject
ON public.exam_score_history(user_id, subject);
-- 试卷资源表 (Exam Papers)
CREATE TABLE IF NOT EXISTS public.exam_paper (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
stage TEXT NOT NULL, -- gaokao / zhongkao / simulated
subject TEXT NOT NULL, -- math, physics, etc.
year INTEGER NOT NULL,
province TEXT NOT NULL,
city TEXT DEFAULT '全国通用',
school TEXT,
category TEXT DEFAULT '真题',
full_score INTEGER DEFAULT 150,
time_minutes INTEGER DEFAULT 120,
difficulty INTEGER DEFAULT 3,
tags TEXT[] DEFAULT '{}',
contributor JSONB NOT NULL DEFAULT '{}'::jsonb,
questions JSONB NOT NULL DEFAULT '[]'::jsonb,
has_answer BOOLEAN DEFAULT true,
has_analysis BOOLEAN DEFAULT true,
file_size_mb NUMERIC(5, 2) DEFAULT 3.0,
download_count INTEGER DEFAULT 0,
view_count INTEGER DEFAULT 0,
like_count INTEGER DEFAULT 0,
is_featured BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT now()
);
-- 试卷考题研讨与引用评论表 (Paper Comments)
CREATE TABLE IF NOT EXISTS public.exam_paper_comments (
id TEXT PRIMARY KEY,
paper_id TEXT NOT NULL REFERENCES public.exam_paper(id) ON DELETE CASCADE,
question_number INTEGER,
user_id TEXT,
user_name TEXT DEFAULT '匿名考生',
user_avatar TEXT,
user_role TEXT DEFAULT 'student',
user_badge TEXT,
content TEXT NOT NULL,
likes INTEGER DEFAULT 0,
reply_to_id TEXT,
reply_to_user TEXT,
quoted_comment JSONB,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
-- 评论点赞记录表 (Comment Likes)
CREATE TABLE IF NOT EXISTS public.exam_comment_likes (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
comment_id TEXT NOT NULL REFERENCES public.exam_paper_comments(id) ON DELETE CASCADE,
user_id TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now(),
UNIQUE(comment_id, user_id)
);
-- 试卷下载历史表 (User Downloads)
CREATE TABLE IF NOT EXISTS public.exam_user_downloads (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
paper_id TEXT NOT NULL REFERENCES public.exam_paper(id) ON DELETE CASCADE,
paper_title TEXT NOT NULL,
stage TEXT NOT NULL,
subject TEXT NOT NULL,
year INTEGER NOT NULL,
file_size_mb NUMERIC(5, 2) DEFAULT 2.5,
format TEXT DEFAULT 'PDF',
downloaded_at TIMESTAMPTZ DEFAULT now()
);
-- 试卷收藏记录表 (User Favorites)
CREATE TABLE IF NOT EXISTS public.exam_user_favorites (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id TEXT NOT NULL,
paper_id TEXT NOT NULL REFERENCES public.exam_paper(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT now(),
UNIQUE(user_id, paper_id)
);
-- 索引构建
CREATE INDEX IF NOT EXISTS idx_exam_paper_stage_subject ON public.exam_paper(stage, subject);
CREATE INDEX IF NOT EXISTS idx_exam_paper_created_at ON public.exam_paper(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_exam_paper_comments_paper_id ON public.exam_paper_comments(paper_id);
CREATE INDEX IF NOT EXISTS idx_exam_comment_likes_comment_user ON public.exam_comment_likes(comment_id, user_id);
CREATE INDEX IF NOT EXISTS idx_exam_user_downloads_user ON public.exam_user_downloads(user_id);
CREATE INDEX IF NOT EXISTS idx_exam_user_favorites_user ON public.exam_user_favorites(user_id);
-- 自增下载计数存储过程
CREATE OR REPLACE FUNCTION increment_user_download_count(p_user_id TEXT)
RETURNS VOID AS $$
BEGIN
UPDATE public.exam_profiles
SET downloaded_count = downloaded_count + 1
WHERE id = p_user_id;
END;
$$ LANGUAGE plpgsql;
-- ==========================================
-- MeloonExam 错题本与考试日历 (来自 meloonexam-fastapi-backend 合并)
-- ==========================================
-- 用户智能错题本表 (User Mistakes)
CREATE TABLE IF NOT EXISTS public.exam_user_mistakes (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
question_id TEXT NOT NULL,
paper_id TEXT,
paper_title TEXT,
stage TEXT DEFAULT 'gaokao',
subject TEXT DEFAULT 'math',
stem TEXT NOT NULL,
type TEXT DEFAULT 'subjective',
options JSONB DEFAULT '[]'::jsonb,
answer TEXT NOT NULL,
analysis TEXT,
user_note TEXT,
is_mastered BOOLEAN DEFAULT false,
wrong_count INTEGER DEFAULT 1,
knowledge_points TEXT[] DEFAULT '{}',
added_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_exam_user_mistakes_user_id ON public.exam_user_mistakes(user_id);
CREATE INDEX IF NOT EXISTS idx_exam_user_mistakes_subject ON public.exam_user_mistakes(user_id, subject);
-- 用户错题本表(历史命名 mistakes -> exam_mistakes)
-- 由 migrations/20260930_rename_product_tables.sql 从旧表 mistakes 重命名而来;
-- app/routers/users.py 的 /api/users/me/mistakes 系列接口读写此表。
CREATE TABLE IF NOT EXISTS public.exam_mistakes (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
question_id TEXT NOT NULL,
paper_id TEXT,
paper_title TEXT,
stage TEXT DEFAULT 'gaokao',
subject TEXT DEFAULT 'math',
stem TEXT NOT NULL,
type TEXT DEFAULT 'subjective',
options JSONB DEFAULT '[]'::jsonb,
answer TEXT NOT NULL,
analysis TEXT,
user_note TEXT,
is_mastered BOOLEAN DEFAULT false,
wrong_count INTEGER DEFAULT 1,
knowledge_points TEXT[] DEFAULT '{}',
added_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_exam_mistakes_user_subject ON public.exam_mistakes(user_id, subject);
CREATE INDEX IF NOT EXISTS idx_exam_mistakes_added_at ON public.exam_mistakes(added_at DESC);
CREATE INDEX IF NOT EXISTS idx_exam_mistakes_mastered ON public.exam_mistakes(user_id, is_mastered);
-- 考试日历日程表 (Exam Events)
CREATE TABLE IF NOT EXISTS public.exam_events (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
date TEXT NOT NULL,
category TEXT DEFAULT '高考',
stage TEXT DEFAULT 'gaokao',
location TEXT,
description TEXT,
subscribers_count INTEGER DEFAULT 0,
is_subscribed BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_exam_events_stage ON public.exam_events(stage);
CREATE INDEX IF NOT EXISTS idx_exam_events_date ON public.exam_events(date);
-- exam_profiles 表新增学科偏好字段 (如不存在则添加)
-- ==========================================
-- MeloonExam 用户建议与反馈表 (来自 meloonexam-fastapi-backend 合并)
-- ==========================================
CREATE TABLE IF NOT EXISTS public.exam_user_feedbacks (
id TEXT PRIMARY KEY DEFAULT ('fb-' || floor(extract(epoch from now()) * 1000)::text),
category VARCHAR(50) DEFAULT 'feature' CHECK (category IN ('feature', 'question_error', 'paper_request', 'ui_ux', 'study_tool', 'other')),
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
contact VARCHAR(255),
rating INTEGER DEFAULT 5 CHECK (rating BETWEEN 1 AND 5),
user_id TEXT,
user_name VARCHAR(100),
user_avatar TEXT,
user_role VARCHAR(50) DEFAULT 'student' CHECK (user_role IN ('student', 'teacher', 'admin', 'parent')),
status VARCHAR(50) DEFAULT 'pending' CHECK (status IN ('pending', 'evaluating', 'accepted', 'implemented', 'closed')),
upvotes INTEGER DEFAULT 0,
tags JSONB DEFAULT '[]'::jsonb,
official_reply JSONB,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_exam_user_feedbacks_category ON public.exam_user_feedbacks(category);
CREATE INDEX IF NOT EXISTS idx_exam_user_feedbacks_status ON public.exam_user_feedbacks(status);
CREATE INDEX IF NOT EXISTS idx_exam_user_feedbacks_user ON public.exam_user_feedbacks(user_id);
CREATE INDEX IF NOT EXISTS idx_exam_user_feedbacks_created_at ON public.exam_user_feedbacks(created_at DESC);
|