feat: 编写用户表 schema 迁移脚本

- 新增 migrations/001_users.up.sql:创建 users 表和 refresh_tokens 表
- 新增 migrations/001_users.down.sql:回滚脚本
- users 表包含 id, username, password_hash, created_at, updated_at
- refresh_tokens 表包含 id, user_id, token_hash, expires_at, created_at
- 添加必要的索引优化查询性能
This commit is contained in:
hhs
2026-06-14 16:42:12 +08:00
parent 0edafbbf8a
commit bf1e24453c
2 changed files with 31 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
-- 用户表
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(64) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- 用户名索引(用于登录查询)
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
-- Refresh Token 表
CREATE TABLE IF NOT EXISTS refresh_tokens (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token_hash VARCHAR(64) NOT NULL UNIQUE,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Token hash 索引(用于刷新验证)
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_token_hash ON refresh_tokens(token_hash);
-- 用户 ID 索引(用于登出所有设备)
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_user_id ON refresh_tokens(user_id);