feat(server): 工程骨架后端——上传 → 解析 → 清单最小链路
- core:配置(prod 拒绝默认密钥 / dev 短信 / 假模型)、租户事务 + 行级安全策略、 problem+json 错误码、信封加密与签名下载、对象存储、事务内入队、Valkey stream 事件流、权限点 - identity:手机号 HMAC 存储、注册、登录(挤下线需确认,API3)、服务端会话 - billing:额度流水只追加,预扣 / 结算 / 退回,一笔预扣只有一个结局 - document:分片上传、按文件头识别格式、Gotenberg 转换、文字层与稳定句子编号 - checklist:标注规则识别、模型抽取、三道校验、标注类条款规则兜底、重跑对齐 - ai:提示词注册表(extract.substantive v1 来自 S4 v2)、假模型、OpenAI 兼容供应商、ai_call 流水 - 迁移:data-model 基线、套餐、Procrastinate、会话状态函数 - 测试 30 个:租户隔离、额度、失败退回、对象加密、契约一致性 - Compose(含 migrate)、镜像、CI Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
23
server/migrations/versions/0001_baseline.py
Normal file
23
server/migrations/versions/0001_baseline.py
Normal file
@@ -0,0 +1,23 @@
|
||||
"""基线:data-model v0.1 的全部 40 张表、行级安全策略、角色授权。
|
||||
|
||||
Revision ID: 0001
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
from sqlfiles import run_dir
|
||||
|
||||
revision = "0001"
|
||||
down_revision = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
run_dir("0001_baseline")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
raise NotImplementedError("基线不支持回退")
|
||||
38
server/migrations/versions/0002_seed_plans.py
Normal file
38
server/migrations/versions/0002_seed_plans.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""套餐 v1:额度数字是 PRD 的建议值,D2 成本实测后改数字就是加一个版本(data-model §4.3)。
|
||||
|
||||
Revision ID: 0002
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from alembic import op
|
||||
from sqlalchemy import text
|
||||
|
||||
revision = "0002"
|
||||
down_revision = "0001"
|
||||
|
||||
MB = 1024 * 1024
|
||||
PLANS = [
|
||||
("free", 1, "免费版", {"parse": 3}, {"max_file_bytes": 50 * MB, "max_pages": 300, "max_projects": 1}),
|
||||
(
|
||||
"pro",
|
||||
1,
|
||||
"专业版",
|
||||
{"parse": 500, "generate": 300},
|
||||
{"max_file_bytes": 200 * MB, "max_pages": 1000, "max_projects": None},
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
for code, version, name, quotas, limits in PLANS:
|
||||
op.get_bind().execute(
|
||||
text("INSERT INTO plan (code, version, name, quotas, limits) VALUES (:c, :v, :n, :q, :l)"),
|
||||
{"c": code, "v": version, "n": name, "q": json.dumps(quotas), "l": json.dumps(limits)},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.get_bind().execute(text("DELETE FROM plan WHERE version = 1"))
|
||||
31
server/migrations/versions/0003_procrastinate.py
Normal file
31
server/migrations/versions/0003_procrastinate.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""任务队列 Procrastinate 的表(AR4)。放在 public schema:它的 SQL 函数里表名不带 schema。
|
||||
|
||||
任务状态的权威是业务表(parse_job 等);队列只负责派发(架构 §3.4)。这些表没有 tenant_id,不受行级安全策略约束。
|
||||
升级 Procrastinate 时,按它的版本说明另加迁移(server/pyproject.toml 里锁定了大版本)。
|
||||
|
||||
Revision ID: 0003
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
from procrastinate.schema import SchemaManager
|
||||
|
||||
revision = "0003"
|
||||
down_revision = "0002"
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
bind.exec_driver_sql("SET LOCAL search_path = public")
|
||||
bind.exec_driver_sql(SchemaManager.get_schema().replace("%", "%%"))
|
||||
bind.exec_driver_sql(
|
||||
"GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO bd_app;"
|
||||
"GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO bd_app;"
|
||||
"GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO bd_app;"
|
||||
"SET LOCAL search_path = app, public"
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
raise NotImplementedError
|
||||
30
server/migrations/versions/0004_session_state.py
Normal file
30
server/migrations/versions/0004_session_state.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""登录前查令牌为什么失效:被吊销(含挤下线)还是过期(API3,api-design §2)。
|
||||
|
||||
和 auth_resolve_session 一样是 SECURITY DEFINER,所有者 bd_auth,只返回状态,不返回会话内容(data-model §2.3)。
|
||||
|
||||
Revision ID: 0004
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "0004"
|
||||
down_revision = "0003"
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.get_bind().exec_driver_sql("""
|
||||
CREATE FUNCTION auth_session_state(p_token_hash bytea)
|
||||
RETURNS TABLE (revoke_reason text, expired boolean)
|
||||
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = app AS $$
|
||||
SELECT s.revoke_reason, s.expires_at <= now()
|
||||
FROM user_session s WHERE s.token_hash = p_token_hash
|
||||
$$;
|
||||
ALTER FUNCTION auth_session_state(bytea) OWNER TO bd_auth;
|
||||
GRANT EXECUTE ON FUNCTION auth_session_state(bytea) TO bd_app;
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.get_bind().exec_driver_sql("DROP FUNCTION auth_session_state(bytea)")
|
||||
Reference in New Issue
Block a user