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:
26
backend/migrations/001_users.up.sql
Normal file
26
backend/migrations/001_users.up.sql
Normal 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);
|
||||
Reference in New Issue
Block a user