feat(integration-connector): database 传输类型、两步绑定编排与调用日志捕获查询
- 新增 database 传输:直连 MySQL/Oracle/SQLServer/PostgreSQL 执行绑定 SQL(新增 pymysql/pymssql 驱动依赖) - 绑定支持 steps_json 多步编排:每步独立传输调用与审计,无 steps 的既有绑定走单步兼容路径 - 客户端实例校验 connector_type ∈ rest/soap/database,database 强制 db_kind 白名单,非 database 置空 - 三个传输层捕获线级事实(最终 SQL/渲染后 XML/原始响应)随调用记录落库,引擎可选透传 business_ref 业务锚点 - 新增调用日志查询 API(列表/详情/同 operation 步骤时间线,仅管理员)与管理端「调用日志」tab:筛选、双栏对照、外部工具复现命令 - 迁移 tt7a/tta9/tt5b:连接器类型枚举、绑定 steps_json、放开 calls 操作唯一约束 - 说明:wire_json 列迁移挂链尾(依赖域表迁移),随后续批次单独落地
This commit is contained in:
1 parent
8a814e7152
commit
e750fa8602
47 files changed
+7441
-353
No files matched your search
@@ -0,0 +1,51 @@
|
||||
"""
|
||||
backend/migrations/versions/tt5b6c7d8e9f_drop_integration_calls_operation_unique.py
|
||||
|
||||
用途:integration_calls.operation_id 唯一约束 → 普通索引(多步顺序引擎审计 N 行同 operation_id)。
|
||||
依赖:Alembic head tta9b0c1d2e3;IntegrationCall 模型已同步删除
|
||||
UniqueConstraint('operation_id') 并新增 Index('ix_integration_calls_operation_id')。
|
||||
逻辑:多步顺序引擎(Task 4)一次能力调用按步骤落 N 条审计、共享同一 operation_id
|
||||
(计划 D13/全局约束:一票一次执行 = N 条,按 operation_id 串联);原唯一约束
|
||||
uq_integration_calls_operation_id 兼作的索引由普通索引
|
||||
ix_integration_calls_operation_id 补回(无任何代码按 operation_id 查询单行,
|
||||
删约束无消费方风险)。downgrade 精确反向(重建唯一约束);若库中已存在
|
||||
同 operation_id 多行,downgrade 会因唯一约束失败——这是预期:
|
||||
多步审计本身就不兼容旧约束(必要时应先人工清理多步行)。
|
||||
|
||||
Revision ID: tt5b6c7d8e9f
|
||||
Revises: tta9b0c1d2e3
|
||||
Create Date: 2026-09-24 03:00:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = 'tt5b6c7d8e9f'
|
||||
down_revision = 'tta9b0c1d2e3'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
# 与模型 IntegrationCall.__table_args__ 同源的约束/索引命名(ic1a2b3c4d5f 创建时的原名)。
|
||||
_UQ_NAME = 'uq_integration_calls_operation_id'
|
||||
_IX_NAME = 'ix_integration_calls_operation_id'
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""删 operation_id 唯一约束,补回同名普通索引(查询性能不回退)。"""
|
||||
# batch_alter_table:SQLite 不支持 DROP CONSTRAINT,须经批处理模式重建表;
|
||||
# PostgreSQL 等方言按普通 ALTER 执行。
|
||||
with op.batch_alter_table('integration_calls') as batch:
|
||||
batch.drop_constraint(_UQ_NAME, type_='unique')
|
||||
op.create_index(_IX_NAME, 'integration_calls', ['operation_id'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""回滚:删普通索引并重建唯一约束。
|
||||
|
||||
注意:若已存在同 operation_id 多行(多步审计的正常数据形态),重建唯一约束
|
||||
将失败——这是预期,多步审计与旧约束不兼容;必要时应先人工归并/清理多步行。
|
||||
"""
|
||||
op.drop_index(_IX_NAME, table_name='integration_calls')
|
||||
with op.batch_alter_table('integration_calls') as batch:
|
||||
batch.create_unique_constraint(_UQ_NAME, ['operation_id'])
|
||||
@@ -0,0 +1,54 @@
|
||||
"""add database connector type and db_kind column
|
||||
|
||||
Revision ID: tt7a8b9c0d1e
|
||||
Revises: rn1a2b3c4d5e6
|
||||
Create Date: 2026-09-24 00:30:00.000000
|
||||
|
||||
用途:Integration Connector 扩枚举 database;客户端实例新增 db_kind 专列
|
||||
(mysql/oracle/sqlserver/postgresql);重建 client/interface 两表 connector_type CHECK。
|
||||
依赖:当前 alembic head rn1a2b3c4d5e6;integration_client_instances /
|
||||
integration_interface_definitions 已存在。
|
||||
逻辑:先删后建两表 CHECK(含 database);client_instances 加可空 db_kind 列
|
||||
(存量 rest/soap 为 NULL);downgrade 反向(删列 + CHECK 回 rest/soap)。
|
||||
采用与 if1b2c3d4e5i 相同的直连 drop/create(生产 PostgreSQL);不走 SQLite
|
||||
batch 表重建,避免同批改 CHECK+加列时的临时表约束冲突。
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = 'tt7a8b9c0d1e'
|
||||
down_revision = 'rn1a2b3c4d5e6'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
_CLIENT_CK = 'ck_integration_client_instances_connector_type'
|
||||
_IFACE_CK = 'ck_integration_interface_definitions_connector_type'
|
||||
_TYPES_WITH_DATABASE = "connector_type IN ('rest', 'soap', 'database')"
|
||||
_TYPES_LEGACY = "connector_type IN ('rest', 'soap')"
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""扩 connector_type CHECK 含 database,并为客户端实例加 db_kind 列。"""
|
||||
# [STEP 1] 客户端实例:先删后建 CHECK,再加 db_kind 列。
|
||||
op.drop_constraint(_CLIENT_CK, 'integration_client_instances', type_='check')
|
||||
op.create_check_constraint(_CLIENT_CK, 'integration_client_instances', _TYPES_WITH_DATABASE)
|
||||
op.add_column(
|
||||
'integration_client_instances',
|
||||
sa.Column('db_kind', sa.String(length=32), nullable=True),
|
||||
)
|
||||
|
||||
# [STEP 2] 接口定义:同步扩 CHECK(接口类型跟随客户端)。
|
||||
op.drop_constraint(_IFACE_CK, 'integration_interface_definitions', type_='check')
|
||||
op.create_check_constraint(_IFACE_CK, 'integration_interface_definitions', _TYPES_WITH_DATABASE)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""回滚:删 db_kind,两表 CHECK 恢复为 rest/soap(须无 database 存量行)。"""
|
||||
op.drop_constraint(_IFACE_CK, 'integration_interface_definitions', type_='check')
|
||||
op.create_check_constraint(_IFACE_CK, 'integration_interface_definitions', _TYPES_LEGACY)
|
||||
|
||||
op.drop_column('integration_client_instances', 'db_kind')
|
||||
op.drop_constraint(_CLIENT_CK, 'integration_client_instances', type_='check')
|
||||
op.create_check_constraint(_CLIENT_CK, 'integration_client_instances', _TYPES_LEGACY)
|
||||
@@ -0,0 +1,83 @@
|
||||
"""
|
||||
backend/migrations/versions/tta9b0c1d2e3_add_binding_steps_json.py
|
||||
|
||||
用途:IntegrationBinding 增加 steps_json(一对多有序绑定步骤);存量行回填为单元素数组。
|
||||
依赖:Alembic head tt7a8b9c0d1e;integration_bindings 已有 interface_definition_code /
|
||||
param_mapping_json(旧单步列保留至 P2)。
|
||||
逻辑:加列(默认 [])→ 按旧列回填 [{"interface_code", "param_mapping_json"}];
|
||||
downgrade 仅删列。旧列不删、不改 NOT NULL(解析层优先读 steps_json)。
|
||||
|
||||
Revision ID: tta9b0c1d2e3
|
||||
Revises: tt7a8b9c0d1e
|
||||
Create Date: 2026-09-24 01:10:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
revision = 'tta9b0c1d2e3'
|
||||
down_revision = 'tt7a8b9c0d1e'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
# 与 context_selector_json / param_mapping_json 同类型约定:PostgreSQL JSONB,其余 JSON。
|
||||
STEPS_JSON = sa.JSON().with_variant(postgresql.JSONB(), 'postgresql')
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""加 steps_json 并将存量单步绑定回填为一元素数组。"""
|
||||
# [STEP 1] 加列(server_default 便于存量 NOT NULL 加列;回填后业务不再依赖默认空数组)。
|
||||
op.add_column(
|
||||
'integration_bindings',
|
||||
sa.Column('steps_json', STEPS_JSON, nullable=False, server_default=sa.text("'[]'")),
|
||||
)
|
||||
|
||||
# [STEP 2] 存量回填:旧 interface_definition_code + param_mapping_json → 单元素 steps。
|
||||
# PostgreSQL:jsonb_build_*;其他方言(如 SQLite 迁移单测)用 JSON 文本拼接兜底。
|
||||
bind = op.get_bind()
|
||||
if bind.dialect.name == 'postgresql':
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE integration_bindings
|
||||
SET steps_json = jsonb_build_array(
|
||||
jsonb_build_object(
|
||||
'interface_code', interface_definition_code,
|
||||
'param_mapping_json', COALESCE(param_mapping_json, '{}'::jsonb)
|
||||
)
|
||||
)
|
||||
"""
|
||||
)
|
||||
)
|
||||
else:
|
||||
# SQLite / 其他:用 Python 侧逐行回填,避免方言 JSON 函数差异。
|
||||
rows = bind.execute(
|
||||
sa.text(
|
||||
'SELECT id, interface_definition_code, param_mapping_json '
|
||||
'FROM integration_bindings'
|
||||
)
|
||||
).fetchall()
|
||||
import json
|
||||
|
||||
for row_id, iface_code, mapping in rows:
|
||||
# 原生 SELECT 绕过 ORM 类型层:JSON 列读回是 JSON 文本,须先还原为
|
||||
# dict 再回填,否则 steps 里 param_mapping_json 是字符串,
|
||||
# 解析层会按 param_mapping_not_dict fail-closed(存量绑定全灭)。
|
||||
if isinstance(mapping, str):
|
||||
try:
|
||||
mapping = json.loads(mapping)
|
||||
except (TypeError, ValueError):
|
||||
mapping = {}
|
||||
steps = [{'interface_code': iface_code, 'param_mapping_json': mapping or {}}]
|
||||
bind.execute(
|
||||
sa.text('UPDATE integration_bindings SET steps_json = :steps WHERE id = :id'),
|
||||
{'steps': json.dumps(steps), 'id': row_id},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""回滚:删除 steps_json(旧单步列仍在,可继续服务)。"""
|
||||
op.drop_column('integration_bindings', 'steps_json')
|
||||
Reference in new issue
Block a user