feat: 合同付款预警迁移到agent页面
This commit is contained in:
1 parent
ef6bc42c20
commit
4ce2defef0
49 files changed
+1052
-423
No files matched your search
@@ -0,0 +1,163 @@
|
||||
"""Seed contract_payment Chat Agent and migrate feature-permission grants.
|
||||
|
||||
Revision ID: cp8a9b0c1d2e
|
||||
Revises: fb26bc033d27
|
||||
Create Date: 2026-09-15 17:50:00.000000
|
||||
|
||||
用途:为合同付款预警注册 builtin Chat Agent,并把旧功能权限 `contract_payment.access`
|
||||
迁移为 user_agent_permissions;随后从 users.feature_permissions 去掉该键。
|
||||
依赖:agents、users、user_agent_permissions 表已存在。
|
||||
逻辑:先按 agent_code 复用已有实例(含停用);没有则插入。插入前把 agents.id
|
||||
序列对齐 MAX(id),避免本地库序列落后时撞主键。显式勾选过访问权的普通用户
|
||||
获得该 Agent;admin 仍按运行时角色看到全部 Agent,不必写关联表。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from alembic import op
|
||||
from sqlalchemy import text
|
||||
|
||||
|
||||
revision = "cp8a9b0c1d2e"
|
||||
down_revision = "fb26bc033d27"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
AGENT_CODE = "contract_payment"
|
||||
|
||||
|
||||
def _resync_agents_id_sequence() -> None:
|
||||
"""把 agents 主键序列对齐现有最大 id,避免 nextval 撞上已占用主键。"""
|
||||
bind = op.get_bind()
|
||||
if bind.dialect.name != "postgresql":
|
||||
return
|
||||
op.execute(
|
||||
"""
|
||||
SELECT setval(
|
||||
pg_get_serial_sequence('public.agents', 'id'),
|
||||
COALESCE((SELECT MAX(id) FROM public.agents), 1),
|
||||
true
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def _write_feature_permissions(conn, user_id: int, permissions: dict) -> None:
|
||||
"""把功能权限 JSON 写回 users;psycopg2 不能直接绑定 Python dict。"""
|
||||
conn.execute(
|
||||
text(
|
||||
"UPDATE users SET feature_permissions = CAST(:perms AS JSON) "
|
||||
"WHERE id = :user_id"
|
||||
),
|
||||
{"perms": json.dumps(permissions, ensure_ascii=False), "user_id": user_id},
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""插入或复用合同付款预警 Agent,并把旧功能权限授予改写为 Agent 授权。"""
|
||||
conn = op.get_bind()
|
||||
agent_id = conn.execute(
|
||||
text(
|
||||
"SELECT id FROM agents WHERE agent_code = :code "
|
||||
"ORDER BY id ASC LIMIT 1"
|
||||
),
|
||||
{"code": AGENT_CODE},
|
||||
).scalar()
|
||||
if agent_id is not None:
|
||||
conn.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE agents
|
||||
SET is_active = true,
|
||||
runtime_provider = 'builtin',
|
||||
conversation_mode = 'task_chat',
|
||||
dify_app_type = 'chatflow',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :agent_id
|
||||
"""
|
||||
),
|
||||
{"agent_id": agent_id},
|
||||
)
|
||||
else:
|
||||
_resync_agents_id_sequence()
|
||||
conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO agents (
|
||||
name, description, type, is_active, is_aggregate,
|
||||
agent_code, runtime_provider, conversation_mode, dify_app_type,
|
||||
suggested_questions_style, sort_order, created_at, updated_at
|
||||
) VALUES (
|
||||
:name, :description, 'api', true, false,
|
||||
:code, 'builtin', 'task_chat', 'chatflow',
|
||||
'plain', 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
|
||||
)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"name": "合同付款预警",
|
||||
"description": "查看采购合同台账、待确认项和周五付款预警汇总。",
|
||||
"code": AGENT_CODE,
|
||||
},
|
||||
)
|
||||
agent_id = conn.execute(
|
||||
text(
|
||||
"SELECT id FROM agents WHERE agent_code = :code AND is_active = true "
|
||||
"ORDER BY id ASC LIMIT 1"
|
||||
),
|
||||
{"code": AGENT_CODE},
|
||||
).scalar()
|
||||
|
||||
users = conn.execute(text("SELECT id, feature_permissions FROM users")).fetchall()
|
||||
for user_id, raw_permissions in users:
|
||||
permissions = raw_permissions if isinstance(raw_permissions, dict) else {}
|
||||
granted = bool(permissions.get("contract_payment.access"))
|
||||
if granted and agent_id is not None:
|
||||
conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO user_agent_permissions (user_id, agent_id)
|
||||
SELECT :user_id, :agent_id
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM user_agent_permissions
|
||||
WHERE user_id = :user_id AND agent_id = :agent_id
|
||||
)
|
||||
"""
|
||||
),
|
||||
{"user_id": user_id, "agent_id": agent_id},
|
||||
)
|
||||
if "contract_payment.access" in permissions:
|
||||
cleaned = {
|
||||
key: value
|
||||
for key, value in permissions.items()
|
||||
if key != "contract_payment.access"
|
||||
}
|
||||
_write_feature_permissions(conn, user_id, cleaned)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""恢复功能权限键;不删除已创建的 Agent 实例,以免误伤运营配置。"""
|
||||
conn = op.get_bind()
|
||||
agent_id = conn.execute(
|
||||
text(
|
||||
"SELECT id FROM agents WHERE agent_code = :code "
|
||||
"ORDER BY id ASC LIMIT 1"
|
||||
),
|
||||
{"code": AGENT_CODE},
|
||||
).scalar()
|
||||
if agent_id is None:
|
||||
return
|
||||
granted_user_ids = conn.execute(
|
||||
text("SELECT user_id FROM user_agent_permissions WHERE agent_id = :agent_id"),
|
||||
{"agent_id": agent_id},
|
||||
).fetchall()
|
||||
for (user_id,) in granted_user_ids:
|
||||
row = conn.execute(
|
||||
text("SELECT feature_permissions FROM users WHERE id = :user_id"),
|
||||
{"user_id": user_id},
|
||||
).fetchone()
|
||||
permissions = dict(row[0] if row and isinstance(row[0], dict) else {})
|
||||
permissions["contract_payment.access"] = True
|
||||
_write_feature_permissions(conn, user_id, permissions)
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"""合并合同付款 Chat Agent 与立账凭证 PO 打印两条 Alembic 头。
|
||||
|
||||
用途:invoice-check 合入 dev 后,``vc4e5f6a7b8c`` 与 ``cp8a9b0c1d2e`` 并列成双 head,
|
||||
导致 ``flask db upgrade`` 无法解析默认 ``head``。
|
||||
依赖:``cp8a9b0c1d2e``(合同付款预警 Chat Agent seed)、
|
||||
``vc4e5f6a7b8c``(立账凭证 PO 订单生成周批 seed)。
|
||||
逻辑:不修改业务表,只声明两条已完成迁移线在此收敛。
|
||||
"""
|
||||
|
||||
from alembic import op # noqa: F401 - Alembic migration module contract
|
||||
|
||||
|
||||
revision = "cp9b0c1d2e3f"
|
||||
down_revision = ("cp8a9b0c1d2e", "vc4e5f6a7b8c")
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""合并合同付款 Agent seed 与 PO 打印排程 seed,不执行数据库结构变更。"""
|
||||
pass
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""撤销合并节点;不会回滚两个父分支的业务变更。"""
|
||||
pass
|
||||
Reference in new issue
Block a user