feat(shipping-notice): 增强订单抽取功能,添加进度事件和诊断运行管理,优化数据模型和访问控制
This commit is contained in:
1 parent
ce26d6d0c4
commit
c4095b08da
175 files changed
+13064
-1814
No files matched your search
@@ -0,0 +1,57 @@
|
||||
"""
|
||||
用途:清理内部执行器误进入用户授权、收藏、默认应用和待审批申请的历史关系。
|
||||
依赖:f51a2b3c4d5e 订单上下文字段迁移;agents.exposure_mode 用户/内部角色划分。
|
||||
逻辑:仅移除或失效 exposure_mode=internal 的用户侧关系,不删除 Agent、内容或业务记录。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'a62b3c4d5e6f'
|
||||
down_revision = 'f51a2b3c4d5e'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""移除内部执行器的用户侧授权状态,并拒绝尚未处理的历史权限申请。"""
|
||||
op.execute(
|
||||
"""
|
||||
DELETE FROM user_agent_permissions
|
||||
WHERE agent_id IN (SELECT id FROM agents WHERE exposure_mode = 'internal')
|
||||
"""
|
||||
)
|
||||
op.execute(
|
||||
"""
|
||||
DELETE FROM user_favorite_agents
|
||||
WHERE agent_id IN (SELECT id FROM agents WHERE exposure_mode = 'internal')
|
||||
"""
|
||||
)
|
||||
op.execute(
|
||||
"""
|
||||
DELETE FROM user_default_agents
|
||||
WHERE agent_id IN (SELECT id FROM agents WHERE exposure_mode = 'internal')
|
||||
"""
|
||||
)
|
||||
op.execute(
|
||||
"""
|
||||
UPDATE users
|
||||
SET default_agent_id = NULL
|
||||
WHERE default_agent_id IN (SELECT id FROM agents WHERE exposure_mode = 'internal')
|
||||
"""
|
||||
)
|
||||
op.execute(
|
||||
"""
|
||||
UPDATE permission_requests
|
||||
SET status = 'rejected', reviewed_at = CURRENT_TIMESTAMP
|
||||
WHERE status = 'pending'
|
||||
AND agent_id IN (SELECT id FROM agents WHERE exposure_mode = 'internal')
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""清理结果不可逆;回滚不得伪造已删除的用户授权和收藏关系。"""
|
||||
pass
|
||||
@@ -0,0 +1,115 @@
|
||||
"""Add order extraction progress, attention, and diagnostic audit foundation.
|
||||
|
||||
Revision ID: a63b4c5d6e7f
|
||||
Revises: a62b3c4d5e6f
|
||||
Create Date: 2026-07-21 13:30:00.000000
|
||||
|
||||
用途:为订单抽取 Run 增加当前进度投影、90天脱敏事件明细、管理员诊断审计与同会话单活动约束。
|
||||
依赖:shipping_order_extraction_runs、jobs、users。
|
||||
逻辑:建立进度数据基础,供现有 Dify 事件观察器写入脱敏技术事件并向用户投影通用阶段。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'a63b4c5d6e7f'
|
||||
down_revision = 'a62b3c4d5e6f'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""新增进度投影列、进度/诊断审计表及会话活动 Run 唯一约束。"""
|
||||
with op.batch_alter_table('shipping_order_extraction_runs') as batch_op:
|
||||
batch_op.add_column(sa.Column('progress_version', sa.Integer(), server_default='1', nullable=False))
|
||||
batch_op.add_column(sa.Column('current_stage_code', sa.String(length=64), nullable=True))
|
||||
batch_op.add_column(sa.Column('active_stage_codes_json', sa.JSON(), nullable=True))
|
||||
batch_op.add_column(sa.Column('completed_stage_codes_json', sa.JSON(), nullable=True))
|
||||
batch_op.add_column(sa.Column('progress_message_code', sa.String(length=128), nullable=True))
|
||||
batch_op.add_column(sa.Column('progress_health', sa.String(length=32), server_default='normal', nullable=False))
|
||||
batch_op.add_column(sa.Column('last_activity_at', sa.DateTime(), nullable=True))
|
||||
batch_op.add_column(sa.Column('progress_summary_json', sa.JSON(), nullable=True))
|
||||
batch_op.add_column(sa.Column('progress_mapping_version', sa.String(length=32), nullable=True))
|
||||
batch_op.add_column(sa.Column('attention_generated_at', sa.DateTime(), nullable=True))
|
||||
batch_op.add_column(sa.Column('seen_at', sa.DateTime(), nullable=True))
|
||||
|
||||
op.create_index(
|
||||
'uq_shipping_order_extraction_runs_active_conversation',
|
||||
'shipping_order_extraction_runs',
|
||||
['user_id', 'owner_agent_id', 'conversation_id'],
|
||||
unique=True,
|
||||
postgresql_where=sa.text("status IN ('queued', 'running', 'result_persisted')"),
|
||||
sqlite_where=sa.text("status IN ('queued', 'running', 'result_persisted')"),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'shipping_order_extraction_progress_events',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('run_id', sa.Integer(), nullable=False),
|
||||
sa.Column('job_id', sa.Integer(), nullable=True),
|
||||
sa.Column('attempt_count', sa.Integer(), nullable=True),
|
||||
sa.Column('workflow_run_id', sa.String(length=128), nullable=True),
|
||||
sa.Column('node_execution_id', sa.String(length=128), nullable=True),
|
||||
sa.Column('event_type', sa.String(length=64), nullable=False),
|
||||
sa.Column('node_type', sa.String(length=64), nullable=True),
|
||||
sa.Column('node_title', sa.String(length=255), nullable=True),
|
||||
sa.Column('stage_code', sa.String(length=64), nullable=True),
|
||||
sa.Column('event_status', sa.String(length=32), nullable=True),
|
||||
sa.Column('provider_occurred_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('received_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('details_json', sa.JSON(), nullable=True),
|
||||
sa.Column('idempotency_key', sa.String(length=128), nullable=False),
|
||||
sa.Column('mapping_version', sa.String(length=32), nullable=True),
|
||||
sa.Column('expires_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['job_id'], ['jobs.id']),
|
||||
sa.ForeignKeyConstraint(['run_id'], ['shipping_order_extraction_runs.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('idempotency_key', name='uq_shipping_order_extraction_progress_event_key'),
|
||||
)
|
||||
op.create_index('ix_shipping_order_extraction_progress_events_run_created', 'shipping_order_extraction_progress_events', ['run_id', 'created_at'])
|
||||
op.create_index('ix_shipping_order_extraction_progress_events_expires_id', 'shipping_order_extraction_progress_events', ['expires_at', 'id'])
|
||||
op.create_index('ix_shipping_order_extraction_progress_events_workflow_node', 'shipping_order_extraction_progress_events', ['workflow_run_id', 'node_execution_id'])
|
||||
|
||||
op.create_table(
|
||||
'shipping_order_extraction_diagnostic_audits',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('admin_user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('run_id', sa.Integer(), nullable=True),
|
||||
sa.Column('action', sa.String(length=64), nullable=False),
|
||||
sa.Column('request_id', sa.String(length=64), nullable=True),
|
||||
sa.Column('query_summary_json', sa.JSON(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['admin_user_id'], ['users.id']),
|
||||
sa.ForeignKeyConstraint(['run_id'], ['shipping_order_extraction_runs.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
)
|
||||
op.create_index('ix_shipping_order_extraction_diagnostic_audits_run_created', 'shipping_order_extraction_diagnostic_audits', ['run_id', 'created_at'])
|
||||
op.create_index('ix_shipping_order_extraction_diagnostic_audits_admin_created', 'shipping_order_extraction_diagnostic_audits', ['admin_user_id', 'created_at'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""移除尚未承载历史兼容职责的进度基础结构。"""
|
||||
op.drop_index('ix_shipping_order_extraction_diagnostic_audits_admin_created', table_name='shipping_order_extraction_diagnostic_audits')
|
||||
op.drop_index('ix_shipping_order_extraction_diagnostic_audits_run_created', table_name='shipping_order_extraction_diagnostic_audits')
|
||||
op.drop_table('shipping_order_extraction_diagnostic_audits')
|
||||
op.drop_index('ix_shipping_order_extraction_progress_events_workflow_node', table_name='shipping_order_extraction_progress_events')
|
||||
op.drop_index('ix_shipping_order_extraction_progress_events_expires_id', table_name='shipping_order_extraction_progress_events')
|
||||
op.drop_index('ix_shipping_order_extraction_progress_events_run_created', table_name='shipping_order_extraction_progress_events')
|
||||
op.drop_table('shipping_order_extraction_progress_events')
|
||||
op.drop_index('uq_shipping_order_extraction_runs_active_conversation', table_name='shipping_order_extraction_runs')
|
||||
with op.batch_alter_table('shipping_order_extraction_runs') as batch_op:
|
||||
batch_op.drop_column('seen_at')
|
||||
batch_op.drop_column('attention_generated_at')
|
||||
batch_op.drop_column('progress_mapping_version')
|
||||
batch_op.drop_column('progress_summary_json')
|
||||
batch_op.drop_column('last_activity_at')
|
||||
batch_op.drop_column('progress_health')
|
||||
batch_op.drop_column('progress_message_code')
|
||||
batch_op.drop_column('completed_stage_codes_json')
|
||||
batch_op.drop_column('active_stage_codes_json')
|
||||
batch_op.drop_column('current_stage_code')
|
||||
batch_op.drop_column('progress_version')
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
"""Remove sensitive order extraction progress node title.
|
||||
|
||||
Revision ID: a64c5d6e7f80
|
||||
Revises: a63b4c5d6e7f
|
||||
Create Date: 2026-07-21 15:50:00.000000
|
||||
|
||||
用途:停止持久化可能包含客户或业务意图的 Dify 自定义节点标题。
|
||||
依赖:a63b4c5d6e7f 创建的 shipping_order_extraction_progress_events。
|
||||
逻辑:仅移除诊断列;阶段映射继续使用受控事件类型和技术节点类型。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'a64c5d6e7f80'
|
||||
down_revision = 'a63b4c5d6e7f'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""删除不再采集的自定义节点标题列。"""
|
||||
with op.batch_alter_table('shipping_order_extraction_progress_events') as batch_op:
|
||||
batch_op.drop_column('node_title')
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""仅为迁移回滚恢复可空列;运行时仍不会写入标题。"""
|
||||
with op.batch_alter_table('shipping_order_extraction_progress_events') as batch_op:
|
||||
batch_op.add_column(sa.Column('node_title', sa.String(length=255), nullable=True))
|
||||
@@ -0,0 +1,127 @@
|
||||
"""Add persisted order extraction plans and document units.
|
||||
|
||||
Revision ID: a65d6e7f8091
|
||||
Revises: a64c5d6e7f80
|
||||
Create Date: 2026-07-21 18:00:00.000000
|
||||
|
||||
用途:为冻结 Run 建立可恢复的文档级抽取计划和局部状态单元。
|
||||
依赖:shipping_order_extraction_runs、shipping_notice_intakes、shipping_notice_documents。
|
||||
逻辑:每个 Run 唯一计划,每个冻结 Document 在计划内唯一;不复制正文或 Provider 凭据。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'a65d6e7f8091'
|
||||
down_revision = 'a64c5d6e7f80'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""创建抽取计划与文档单元表。"""
|
||||
op.create_table(
|
||||
'shipping_order_extraction_plans',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('run_id', sa.Integer(), nullable=False),
|
||||
sa.Column('plan_version', sa.String(length=32), nullable=False),
|
||||
sa.Column('input_snapshot_hash', sa.String(length=64), nullable=False),
|
||||
sa.Column('status', sa.String(length=32), server_default='planned', nullable=False),
|
||||
sa.Column('unit_count', sa.Integer(), server_default='0', nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.CheckConstraint(
|
||||
"status IN ('planned', 'running', 'completed', 'partial_failed', 'failed')",
|
||||
name='ck_shipping_order_extraction_plans_status',
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
['run_id'], ['shipping_order_extraction_runs.id'], ondelete='CASCADE',
|
||||
),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('run_id', name='uq_shipping_order_extraction_plans_run'),
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_order_extraction_plans_status_created',
|
||||
'shipping_order_extraction_plans',
|
||||
['status', 'created_at'],
|
||||
unique=False,
|
||||
)
|
||||
op.create_table(
|
||||
'shipping_order_extraction_units',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('plan_id', sa.Integer(), nullable=False),
|
||||
sa.Column('intake_id', sa.Integer(), nullable=False),
|
||||
sa.Column('document_id', sa.Integer(), nullable=False),
|
||||
sa.Column('position', sa.Integer(), nullable=False),
|
||||
sa.Column('unit_type', sa.String(length=32), nullable=False),
|
||||
sa.Column('document_type', sa.String(length=32), nullable=False),
|
||||
sa.Column('content_hash', sa.String(length=64), nullable=True),
|
||||
sa.Column('status', sa.String(length=32), server_default='pending', nullable=False),
|
||||
sa.Column('attempt_count', sa.Integer(), server_default='0', nullable=False),
|
||||
sa.Column('result_json', sa.JSON(), nullable=True),
|
||||
sa.Column('error_code', sa.String(length=64), nullable=True),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
sa.Column('started_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('finished_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.CheckConstraint(
|
||||
"status IN ('pending', 'running', 'succeeded', 'failed', 'skipped')",
|
||||
name='ck_shipping_order_extraction_units_status',
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"unit_type IN ('document_extraction', 'email_relation')",
|
||||
name='ck_shipping_order_extraction_units_type',
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
['intake_id'], ['shipping_notice_intakes.id'],
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
['document_id', 'intake_id'],
|
||||
['shipping_notice_documents.id', 'shipping_notice_documents.intake_id'],
|
||||
name='fk_shipping_order_extraction_units_document_intake',
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
['plan_id'], ['shipping_order_extraction_plans.id'], ondelete='CASCADE',
|
||||
),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint(
|
||||
'plan_id', 'document_id', name='uq_shipping_order_extraction_units_document',
|
||||
),
|
||||
sa.UniqueConstraint(
|
||||
'plan_id', 'position', name='uq_shipping_order_extraction_units_position',
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_order_extraction_units_document',
|
||||
'shipping_order_extraction_units',
|
||||
['document_id'],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_order_extraction_units_plan_status',
|
||||
'shipping_order_extraction_units',
|
||||
['plan_id', 'status'],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""按依赖顺序删除单元和计划表。"""
|
||||
op.drop_index(
|
||||
'ix_shipping_order_extraction_units_plan_status',
|
||||
table_name='shipping_order_extraction_units',
|
||||
)
|
||||
op.drop_index(
|
||||
'ix_shipping_order_extraction_units_document',
|
||||
table_name='shipping_order_extraction_units',
|
||||
)
|
||||
op.drop_table('shipping_order_extraction_units')
|
||||
op.drop_index(
|
||||
'ix_shipping_order_extraction_plans_status_created',
|
||||
table_name='shipping_order_extraction_plans',
|
||||
)
|
||||
op.drop_table('shipping_order_extraction_plans')
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Expand order extraction plan version capacity.
|
||||
|
||||
Revision ID: a66e7f8091a2
|
||||
Revises: a65d6e7f8091
|
||||
Create Date: 2026-07-21 19:15:00.000000
|
||||
|
||||
用途:修正订单抽取计划版本标识超过初始32字符列上限时无法创建 Run 的问题。
|
||||
依赖:a65d6e7f8091 创建的 shipping_order_extraction_plans。
|
||||
逻辑:仅扩展 plan_version 容量,不修改既有版本值或业务计划事实。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'a66e7f8091a2'
|
||||
down_revision = 'a65d6e7f8091'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""将计划版本列扩至64字符,保留所有已存计划记录。"""
|
||||
with op.batch_alter_table('shipping_order_extraction_plans') as batch_op:
|
||||
batch_op.alter_column(
|
||||
'plan_version',
|
||||
existing_type=sa.String(length=32),
|
||||
type_=sa.String(length=64),
|
||||
existing_nullable=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""回滚到初始容量;只应在确认没有超过32字符的版本值时执行。"""
|
||||
with op.batch_alter_table('shipping_order_extraction_plans') as batch_op:
|
||||
batch_op.alter_column(
|
||||
'plan_version',
|
||||
existing_type=sa.String(length=64),
|
||||
type_=sa.String(length=32),
|
||||
existing_nullable=False,
|
||||
)
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Add global display sequence to order fields.
|
||||
|
||||
Revision ID: a67f8091a2b3
|
||||
Revises: a66e7f8091a2
|
||||
Create Date: 2026-07-21 20:40:00.000000
|
||||
|
||||
用途:保存订单字段目录的当前全局顺序,使页面刷新、草稿和发布版本之外仍有稳定排序。
|
||||
依赖:shipping_order_fields 稳定字段目录。
|
||||
逻辑:新增非空 display_sequence,并按当前字段编号稳定回填;版本快照 sequence 保持不变。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'a67f8091a2b3'
|
||||
down_revision = 'a66e7f8091a2'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""新增并回填字段目录全局显示顺序。"""
|
||||
op.add_column(
|
||||
'shipping_order_fields',
|
||||
sa.Column('display_sequence', sa.Integer(), nullable=False, server_default='0'),
|
||||
)
|
||||
connection = op.get_bind()
|
||||
rows = connection.execute(sa.text(
|
||||
'SELECT id FROM shipping_order_fields ORDER BY schema_id, field_code, id'
|
||||
)).fetchall()
|
||||
for sequence, row in enumerate(rows, start=1):
|
||||
connection.execute(
|
||||
sa.text('UPDATE shipping_order_fields SET display_sequence = :sequence WHERE id = :field_id'),
|
||||
{'sequence': sequence, 'field_id': row[0]},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""移除字段目录全局显示顺序。"""
|
||||
op.drop_column('shipping_order_fields', 'display_sequence')
|
||||
Reference in new issue
Block a user