-- 用户表
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
email VARCHAR ( 255 ) UNIQUE NOT NULL ,
password_hash VARCHAR ( 255 ) NOT NULL ,
username VARCHAR ( 20 ) UNIQUE NOT NULL ,
avatar_url TEXT ,
bio TEXT DEFAULT '' ,
role VARCHAR ( 10 ) DEFAULT 'user' CHECK ( role IN ( 'user' , 'admin' )),
created_at TIMESTAMPTZ DEFAULT now ()
);
-- 帖子表
CREATE TABLE IF NOT EXISTS posts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
user_id UUID NOT NULL REFERENCES users ( id ) ON DELETE CASCADE ,
title VARCHAR ( 200 ) NOT NULL ,
content TEXT NOT NULL ,
images TEXT [] DEFAULT '{}' ,
created_at TIMESTAMPTZ DEFAULT now (),
updated_at TIMESTAMPTZ DEFAULT now ()
);
-- 回复表
CREATE TABLE IF NOT EXISTS replies (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
post_id UUID NOT NULL REFERENCES posts ( id ) ON DELETE CASCADE ,
user_id UUID NOT NULL REFERENCES users ( id ) ON DELETE CASCADE ,
content TEXT NOT NULL ,
created_at TIMESTAMPTZ DEFAULT now ()
);
-- 友情链接表
CREATE TABLE IF NOT EXISTS friendly_links (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
title VARCHAR ( 100 ) NOT NULL ,
url TEXT NOT NULL ,
sort_order INT DEFAULT 0
);
-- 举报表
CREATE TABLE IF NOT EXISTS reports (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
post_id UUID NOT NULL REFERENCES posts ( id ) ON DELETE CASCADE ,
reporter_id UUID NOT NULL REFERENCES users ( id ) ON DELETE CASCADE ,
reason VARCHAR ( 100 ) NOT NULL ,
status VARCHAR ( 20 ) DEFAULT 'pending' CHECK ( status IN ( 'pending' , 'approved' , 'rejected' )),
created_at TIMESTAMPTZ DEFAULT now (),
handled_at TIMESTAMPTZ ,
handler_id UUID REFERENCES users ( id ) ON DELETE SET NULL ,
handler_note TEXT
);
-- 事件日志表
CREATE TABLE IF NOT EXISTS event_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
user_id UUID REFERENCES users ( id ) ON DELETE SET NULL ,
action VARCHAR ( 50 ) NOT NULL ,
ip VARCHAR ( 45 ),
created_at TIMESTAMPTZ DEFAULT now ()
);
-- ============================================================
-- 论坛设置表(新增)
-- ============================================================
CREATE TABLE IF NOT EXISTS settings (
key VARCHAR ( 50 ) PRIMARY KEY ,
value TEXT NOT NULL ,
updated_at TIMESTAMPTZ DEFAULT now ()
);
-- 插入默认论坛名称
INSERT INTO settings ( key , value ) VALUES ( 'forum_name' , 'Forumlify' )
ON CONFLICT ( key ) DO NOTHING ;
-- ============================================================
-- 自动更新 updated_at 的触发器
-- ============================================================
CREATE OR REPLACE FUNCTION update_updated_at ()
RETURNS TRIGGER AS $$
BEGIN
NEW . updated_at = now ();
RETURN NEW ;
END ;
$$ LANGUAGE plpgsql ;
DROP TRIGGER IF EXISTS update_posts_updated_at ON posts ;
CREATE TRIGGER update_posts_updated_at
BEFORE UPDATE ON posts
FOR EACH ROW EXECUTE FUNCTION update_updated_at ();
-- ============================================================
-- 私信系统(conversations + messages)
-- ============================================================
CREATE TABLE IF NOT EXISTS conversations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
user1_id UUID NOT NULL REFERENCES users ( id ) ON DELETE CASCADE ,
user2_id UUID NOT NULL REFERENCES users ( id ) ON DELETE CASCADE ,
last_message_at TIMESTAMPTZ DEFAULT now (),
created_at TIMESTAMPTZ DEFAULT now ()
);
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
conversation_id UUID NOT NULL REFERENCES conversations ( id ) ON DELETE CASCADE ,
sender_id UUID NOT NULL REFERENCES users ( id ) ON DELETE CASCADE ,
content TEXT NOT NULL ,
is_read BOOLEAN DEFAULT false ,
created_at TIMESTAMPTZ DEFAULT now ()
);
CREATE INDEX IF NOT EXISTS idx_conversations_user1 ON conversations ( user1_id );
CREATE INDEX IF NOT EXISTS idx_conversations_user2 ON conversations ( user2_id );
CREATE INDEX IF NOT EXISTS idx_messages_conversation ON messages ( conversation_id );