Files
xiaoxia-saas/migrations
xiaoxia c3e1ab62ed
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 1s
CI/CD Pipeline / Integration Tests (pull_request) Failing after 1m57s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m26s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 2m51s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 3m17s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m18s
CI/CD Pipeline / Validate - Style (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Security (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
AI Code Review / AI Code Review (pull_request) Has been cancelled
PR Automation / Auto Approve on CI Green (pull_request) Has been cancelled
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Failing after 30h47m23s
CI/CD Pipeline / PR Build Web Image (pull_request) Failing after 30h47m29s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 30h47m31s
CI/CD Pipeline / Check push changed paths (pull_request) Failing after 30h46m59s
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 30h46m57s
CI/CD Pipeline / ACR Image Cleanup (pull_request) Failing after 30h46m49s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Failing after 30h46m54s
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Failing after 30h46m54s
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Failing after 30h46m54s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Failing after 30h46m55s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 30h46m55s
CI/CD Pipeline / Staging API Integration Tests (pull_request) Failing after 30h46m49s
CI/CD Pipeline / Frontend Lint (pull_request) Failing after 30h46m56s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 30h46m57s
feat: add membership & points system backend (#1895)
- Add migration 008_membership_points.sql: users table new fields + 4 new tables
- Add domain objects: PointsAccount, PointsTransaction, PointsOrder, DailyUsageRecord
- Add repository ports & SQLAlchemy adapter implementations
- Add PointsService with balance/charge/refund/recharge/daily-usage logic
- Add points_rules configuration (9 scenes, packages, membership pricing)
- Add points_gate middleware decorator for AI feature entry points
- Add API routes: points balance/transactions/rules/packages/check/deduct/refund/recharge
- Add API routes: membership status, daily usage
- Add unit tests for points_rules (20 tests) and points_service (20 tests)
2026-09-15 08:35:55 +08:00
..

数据库迁移指南

环境准备

1. 安装 PostgreSQL

# Ubuntu/Debian
sudo apt-get install postgresql postgresql-contrib

# macOS
brew install postgresql

2. 创建数据库

# 切换到 postgres 用户
sudo -u postgres psql

# 在 psql 中执行
CREATE DATABASE xiaoxia_saas;
CREATE USER xiaoxia_user WITH PASSWORD 'your_password_here';
GRANT ALL PRIVILEGES ON DATABASE xiaoxia_saas TO xiaoxia_user;
\q

3. 配置连接字符串

在 .env 文件中配置:

DATABASE_URL=postgresql://xiaoxia_user:your_password_here@localhost:5432/xiaoxia_saas

执行迁移

方法 1: 使用 psql

# 执行初始化脚本
psql postgresql://xiaoxia_user:your_password_here@localhost:5432/xiaoxia_saas \
  -f migrations/001_initial_schema.sql

方法 2: 使用 Python 脚本

python scripts/run_migrations.py

迁移文件说明

  • 001_initial_schema.sql - 初始表结构
    • users - 用户表
    • workspaces - 工作空间表
    • workspace_members - 成员表
    • workspace_invitations - 邀请表
    • projects - 项目表(占位)

验证迁移

-- 查看所有表
\dt

-- 查看 users 表结构
\d users

-- 查看索引
\di

-- 查看外键约束
SELECT conname, conrelid::regclass AS table_name 
FROM pg_constraint 
WHERE contype = 'f';

回滚

如果需要重置数据库:

# ⚠️ 警告:这会删除所有数据
psql postgresql://xiaoxia_user:password@localhost:5432/xiaoxia_saas \
  -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"

# 然后重新执行迁移
psql ... -f migrations/001_initial_schema.sql

生产环境注意事项

  1. 使用强密码
  2. 配置 SSL 连接
  3. 定期备份数据
  4. 监控数据库性能
  5. 设置连接池(推荐使用 pgBouncer)

连接池配置(可选)

在 Python 中使用 psycopg2 连接池:

from psycopg2 import pool

connection_pool = pool.SimpleConnectionPool(
    minconn=1,
    maxconn=20,
    dsn="postgresql://..."
)

后续迁移

后续的 schema 变更应该创建新的迁移文件:

  • 002_add_xxx.sql
  • 003_alter_xxx.sql
  • ...

每个迁移文件应该是幂等的(可以多次执行而不出错)。