Download backend/supabase_schema.sql from isold23/MeloonTop: direct link, hf CLI and curl.
- Browser
- Download file 15.2 kB
-
https://huggingface.co/spaces/isold23/MeloonTop/resolve/main/backend/supabase_schema.sql
- Command line
-
hf download hf://spaces/isold23/MeloonTop/backend/supabase_schema.sql
-
curl -L -o supabase_schema.sql https://huggingface.co/spaces/isold23/MeloonTop/resolve/main/backend/supabase_schema.sql
15.2 kB
| -- 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); | |