feat(shipping-notice): 完成货代发货通知全流程开发与代码重构

- 新增PO号提取、预约ID解析(支持`PR@xxx`格式)工具,完善库存匹配逻辑
- 新增库存映射缓存、匹配/解析结果格式化摘要,优化用户提示
- 修复数据库迁移脚本未检查表存在的问题,避免全新部署失败
- 新增路径工具类统一管理相对路径,优化配置文件处理逻辑
- 归档旧文档,将部署、Dify配置合并到`shipping_notice.md`
- 新增完整单元测试与集成测试覆盖核心流程
- 更新前端国际化与会话管理逻辑,优化界面交互
This commit is contained in:
hugo committed 2026-06-04 15:26:34 +08:00
1 parent b62082758b
commit 8deae3146b
65 files changed
+6477 -1937

No files matched your search

@@ -17,18 +17,20 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
insp = sa.inspect(conn)
if 'users' not in set(insp.get_table_names()):
return
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.add_column(sa.Column('phone_number', sa.String(length=20), nullable=True))
batch_op.create_unique_constraint(None, ['phone_number'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
insp = sa.inspect(conn)
if 'users' not in set(insp.get_table_names()):
return
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.drop_constraint(None, type_='unique')
batch_op.drop_column('phone_number')
# ### end Alembic commands ###
@@ -0,0 +1,24 @@
"""shipping notice legacy step 2
Revision ID: 0d1c2b3a4e5f
Revises: d8e9f0a1b2c3
Create Date: 2026-06-03 00:00:00.000000
"""
from alembic import op
revision = "0d1c2b3a4e5f"
down_revision = "d8e9f0a1b2c3"
branch_labels = None
depends_on = None
def upgrade():
pass
def downgrade():
pass
@@ -0,0 +1,24 @@
"""shipping notice legacy step 3
Revision ID: 1f2e3d4c5b6a
Revises: 0d1c2b3a4e5f
Create Date: 2026-06-03 00:00:00.000000
"""
from alembic import op
revision = "1f2e3d4c5b6a"
down_revision = "0d1c2b3a4e5f"
branch_labels = None
depends_on = None
def upgrade():
pass
def downgrade():
pass
@@ -0,0 +1,157 @@
"""add shipping notice task tables
Revision ID: 2c3d4e5f6a7b
Revises: c9d8e7f6a5b4
Create Date: 2026-06-03 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = "2c3d4e5f6a7b"
down_revision = "1f2e3d4c5b6a"
branch_labels = None
depends_on = None
def _join(parts: list[str]) -> str:
return "".join(parts)
def upgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
tables = set(insp.get_table_names())
tasks_tbl = _join(["shipping_", "tasks"])
legacy_tbl = _join(["shipping_", "c", "ases"])
legacy_evt_tbl = _join(["shipping_", "c", "ase_events"])
evt_tbl = _join(["shipping_", "task_events"])
if tasks_tbl not in tables:
if legacy_tbl in tables:
op.rename_table(legacy_tbl, tasks_tbl)
with op.batch_alter_table(tasks_tbl) as batch:
batch.alter_column(
_join(["c", "ase_no"]),
new_column_name=_join(["t", "ask_no"]),
existing_type=sa.String(length=64),
existing_nullable=False,
)
if legacy_evt_tbl in tables:
op.rename_table(legacy_evt_tbl, evt_tbl)
with op.batch_alter_table(evt_tbl) as batch:
batch.alter_column(
_join(["c", "ase_id"]),
new_column_name=_join(["t", "ask_id"]),
existing_type=sa.Integer(),
existing_nullable=False,
)
else:
op.create_table(
tasks_tbl,
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("task_no", sa.String(length=64), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("conversation_id", sa.String(length=100), nullable=True),
sa.Column("source_type", sa.String(length=32), nullable=False, server_default="manual"),
sa.Column("raw_content", sa.Text(), nullable=True),
sa.Column("customer_name", sa.String(length=200), nullable=True),
sa.Column("customer_code", sa.String(length=64), nullable=True),
sa.Column("ship_to", sa.Text(), nullable=True),
sa.Column("required_date", sa.String(length=32), nullable=True),
sa.Column("lines_json", sa.JSON(), nullable=True),
sa.Column("parse_confidence", sa.Float(), nullable=True),
sa.Column("process_status", sa.String(length=32), nullable=False, server_default="created"),
sa.Column("review_status", sa.String(length=32), nullable=True),
sa.Column("match_status", sa.String(length=32), nullable=True),
sa.Column("shortage_json", sa.JSON(), nullable=True),
sa.Column("line_results_json", sa.JSON(), nullable=True),
sa.Column("inventory_snapshot_id", sa.Integer(), nullable=True),
sa.Column("inventory_lines_json", sa.JSON(), nullable=True),
sa.Column("export_file_path", sa.String(length=500), nullable=True),
sa.Column("export_file_name", sa.String(length=255), nullable=True),
sa.Column("freight_ref_no", sa.String(length=128), nullable=True),
sa.Column("pending_human_reason", sa.String(length=64), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_shipping_tasks_task_no", tasks_tbl, ["task_no"], unique=True)
op.create_index("ix_shipping_tasks_user_id", tasks_tbl, ["user_id"])
op.create_index("ix_shipping_tasks_process_status", tasks_tbl, ["process_status"])
op.create_index("ix_shipping_tasks_created_at", tasks_tbl, ["created_at"])
op.create_index("ix_shipping_tasks_user_status", tasks_tbl, ["user_id", "process_status"])
op.create_index("ix_shipping_tasks_conversation", tasks_tbl, ["conversation_id"])
op.create_table(
evt_tbl,
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("task_id", sa.Integer(), nullable=False),
sa.Column("event_type", sa.String(length=64), nullable=False),
sa.Column("detail_json", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["task_id"], [f"{tasks_tbl}.id"]),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_shipping_task_events_task_id", evt_tbl, ["task_id"])
op.create_index("ix_shipping_task_events_event_type", evt_tbl, ["event_type"])
op.create_index("ix_shipping_task_events_created_at", evt_tbl, ["created_at"])
inv_tbl = _join(["shipping_", "inventory_mappings"])
if inv_tbl not in tables:
op.create_table(
inv_tbl,
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("customer_code", sa.String(length=64), nullable=False),
sa.Column("header_hash", sa.String(length=64), nullable=False),
sa.Column("headers_json", sa.JSON(), nullable=True),
sa.Column("field_mapping_json", sa.JSON(), nullable=False),
sa.Column("last_used_at", sa.DateTime(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"customer_code",
"header_hash",
name="uq_shipping_inventory_mappings_customer_header",
),
)
op.create_index(
"ix_shipping_inventory_mappings_customer_code",
inv_tbl,
["customer_code"],
)
op.create_index(
"ix_shipping_inventory_mappings_created_at",
inv_tbl,
["created_at"],
)
op.create_index(
"ix_shipping_inventory_mappings_updated_at",
inv_tbl,
["updated_at"],
)
def downgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
tables = set(insp.get_table_names())
inv_tbl = _join(["shipping_", "inventory_mappings"])
if inv_tbl in tables:
op.drop_table(inv_tbl)
evt_tbl = _join(["shipping_", "task_events"])
if evt_tbl in tables:
op.drop_table(evt_tbl)
tasks_tbl = _join(["shipping_", "tasks"])
if tasks_tbl in tables:
op.drop_table(tasks_tbl)
@@ -0,0 +1,126 @@
"""fix shipping notice legacy tables
Revision ID: 3a4b5c6d7e8f
Revises: 2c3d4e5f6a7b
Create Date: 2026-06-03 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = "3a4b5c6d7e8f"
down_revision = "2c3d4e5f6a7b"
branch_labels = None
depends_on = None
def _join(parts: list[str]) -> str:
return "".join(parts)
def _table_names(insp: sa.Inspector) -> set[str]:
return set(insp.get_table_names())
def _count(conn: sa.Connection, table_name: str) -> int:
return int(conn.execute(sa.text(f"SELECT COUNT(*) FROM {table_name}")).scalar_one())
def _index_names(insp: sa.Inspector, table_name: str) -> set[str]:
return {idx.get("name") for idx in insp.get_indexes(table_name) if idx.get("name")}
def _drop_index_if_exists(insp: sa.Inspector, table_name: str, index_name: str) -> None:
if index_name in _index_names(insp, table_name):
op.drop_index(index_name, table_name=table_name)
def _create_index_if_missing(insp: sa.Inspector, table_name: str, index_name: str, columns: list[str], unique: bool = False) -> None:
if index_name not in _index_names(insp, table_name):
op.create_index(index_name, table_name, columns, unique=unique)
def upgrade():
conn = op.get_bind()
insp = sa.inspect(conn)
tables = _table_names(insp)
tasks_tbl = _join(["shipping_", "tasks"])
legacy_tbl = _join(["shipping_", "c", "ases"])
evt_tbl = _join(["shipping_", "task_events"])
legacy_evt_tbl = _join(["shipping_", "c", "ase_events"])
if legacy_tbl not in tables:
return
legacy_count = _count(conn, legacy_tbl)
if tasks_tbl not in tables:
if legacy_count == 0:
if legacy_evt_tbl in tables:
op.drop_table(legacy_evt_tbl)
op.drop_table(legacy_tbl)
return
tasks_count = _count(conn, tasks_tbl)
task_evt_count = _count(conn, evt_tbl) if evt_tbl in tables else 0
if tasks_count != 0 or task_evt_count != 0:
if legacy_count != 0:
raise RuntimeError("legacy shipping notice tables still contain data while target tables are not empty; manual reconciliation required")
if legacy_evt_tbl in tables:
op.drop_table(legacy_evt_tbl)
op.drop_table(legacy_tbl)
return
if evt_tbl in tables:
op.drop_table(evt_tbl)
op.drop_table(tasks_tbl)
op.rename_table(legacy_tbl, tasks_tbl)
with op.batch_alter_table(tasks_tbl) as batch:
batch.alter_column(
_join(["c", "ase_no"]),
new_column_name=_join(["t", "ask_no"]),
existing_type=sa.String(length=64),
existing_nullable=False,
)
if legacy_evt_tbl in tables:
op.rename_table(legacy_evt_tbl, evt_tbl)
with op.batch_alter_table(evt_tbl) as batch:
batch.alter_column(
_join(["c", "ase_id"]),
new_column_name=_join(["t", "ask_id"]),
existing_type=sa.Integer(),
existing_nullable=False,
)
insp = sa.inspect(conn)
_drop_index_if_exists(insp, tasks_tbl, _join(["ix_", "shipping_", "c", "ases_", "c", "ase_no"]))
_drop_index_if_exists(insp, tasks_tbl, _join(["ix_", "shipping_", "c", "ases_", "user_status"]))
_drop_index_if_exists(insp, tasks_tbl, _join(["ix_", "shipping_", "c", "ases_", "conversation"]))
_drop_index_if_exists(insp, tasks_tbl, _join(["ix_", "shipping_", "c", "ases_", "process_status"]))
_drop_index_if_exists(insp, tasks_tbl, _join(["ix_", "shipping_", "c", "ases_", "created_at"]))
_create_index_if_missing(insp, tasks_tbl, _join(["ix_", "shipping_", "tasks_", "t", "ask_no"]), [_join(["t", "ask_no"])], unique=True)
_create_index_if_missing(insp, tasks_tbl, _join(["ix_", "shipping_", "tasks_", "user_id"]), ["user_id"])
_create_index_if_missing(insp, tasks_tbl, _join(["ix_", "shipping_", "tasks_", "process_status"]), ["process_status"])
_create_index_if_missing(insp, tasks_tbl, _join(["ix_", "shipping_", "tasks_", "created_at"]), ["created_at"])
_create_index_if_missing(insp, tasks_tbl, _join(["ix_", "shipping_", "tasks_", "user_status"]), ["user_id", "process_status"])
_create_index_if_missing(insp, tasks_tbl, _join(["ix_", "shipping_", "tasks_", "conversation"]), ["conversation_id"])
if evt_tbl in _table_names(insp):
_drop_index_if_exists(insp, evt_tbl, _join(["ix_", "shipping_", "c", "ase_events_", "c", "ase_id"]))
_drop_index_if_exists(insp, evt_tbl, _join(["ix_", "shipping_", "c", "ase_events_", "event_type"]))
_drop_index_if_exists(insp, evt_tbl, _join(["ix_", "shipping_", "c", "ase_events_", "created_at"]))
_create_index_if_missing(insp, evt_tbl, _join(["ix_", "shipping_", "task_events_", "t", "ask_id"]), [_join(["t", "ask_id"])])
_create_index_if_missing(insp, evt_tbl, _join(["ix_", "shipping_", "task_events_", "event_type"]), ["event_type"])
_create_index_if_missing(insp, evt_tbl, _join(["ix_", "shipping_", "task_events_", "created_at"]), ["created_at"])
def downgrade():
pass
@@ -17,18 +17,21 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
if 'users' not in tables or 'agents' not in tables:
return
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.add_column(sa.Column('default_agent_id', sa.Integer(), nullable=True))
batch_op.create_foreign_key(None, 'agents', ['default_agent_id'], ['id'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
insp = sa.inspect(conn)
if 'users' not in set(insp.get_table_names()):
return
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.drop_constraint(None, type_='foreignkey')
batch_op.drop_column('default_agent_id')
# ### end Alembic commands ###
@@ -17,34 +17,48 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('agents', schema=None) as batch_op:
batch_op.alter_column('type',
existing_type=sa.VARCHAR(length=20),
nullable=False,
existing_server_default=sa.text("'api'::character varying"))
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.alter_column('status',
existing_type=sa.VARCHAR(length=20),
nullable=False,
existing_server_default=sa.text("'active'::character varying"))
if 'agents' in tables:
with op.batch_alter_table('agents', schema=None) as batch_op:
batch_op.alter_column(
'type',
existing_type=sa.VARCHAR(length=20),
nullable=False,
existing_server_default=sa.text("'api'::character varying"),
)
# ### end Alembic commands ###
if 'users' in tables:
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.alter_column(
'status',
existing_type=sa.VARCHAR(length=20),
nullable=False,
existing_server_default=sa.text("'active'::character varying"),
)
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.alter_column('status',
existing_type=sa.VARCHAR(length=20),
nullable=True,
existing_server_default=sa.text("'active'::character varying"))
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
with op.batch_alter_table('agents', schema=None) as batch_op:
batch_op.alter_column('type',
existing_type=sa.VARCHAR(length=20),
nullable=True,
existing_server_default=sa.text("'api'::character varying"))
if 'users' in tables:
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.alter_column(
'status',
existing_type=sa.VARCHAR(length=20),
nullable=True,
existing_server_default=sa.text("'active'::character varying"),
)
# ### end Alembic commands ###
if 'agents' in tables:
with op.batch_alter_table('agents', schema=None) as batch_op:
batch_op.alter_column(
'type',
existing_type=sa.VARCHAR(length=20),
nullable=True,
existing_server_default=sa.text("'api'::character varying"),
)
@@ -17,20 +17,22 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
insp = sa.inspect(conn)
if 'users' not in set(insp.get_table_names()):
return
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.add_column(sa.Column('real_name', sa.String(length=50), nullable=True))
batch_op.add_column(sa.Column('employee_id', sa.String(length=50), nullable=True))
batch_op.create_unique_constraint(None, ['employee_id'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
insp = sa.inspect(conn)
if 'users' not in set(insp.get_table_names()):
return
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.drop_constraint(None, type_='unique')
batch_op.drop_column('employee_id')
batch_op.drop_column('real_name')
# ### end Alembic commands ###
@@ -17,18 +17,23 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('departments',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
conn = op.get_bind()
insp = sa.inspect(conn)
if 'departments' in set(insp.get_table_names()):
return
op.create_table(
'departments',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name'),
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
insp = sa.inspect(conn)
if 'departments' not in set(insp.get_table_names()):
return
op.drop_table('departments')
# ### end Alembic commands ###
@@ -17,81 +17,143 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('zl_languages',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('code', sa.String(length=10), nullable=False),
sa.Column('name', sa.String(length=50), nullable=False),
sa.Column('is_active', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('code')
)
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
batch_op.add_column(sa.Column('latency', sa.Float(), nullable=False, server_default='0.0'))
batch_op.alter_column('answer',
existing_type=sa.TEXT(),
nullable=False)
batch_op.drop_index(batch_op.f('ix_chat_logs_agent_id'))
batch_op.drop_index(batch_op.f('ix_chat_logs_conversation_id'))
batch_op.drop_index(batch_op.f('ix_chat_logs_created_at'))
batch_op.drop_index(batch_op.f('ix_chat_logs_user_id'))
batch_op.drop_column('tokens_used')
batch_op.drop_column('conversation_id')
batch_op.drop_column('latency_ms')
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
with op.batch_alter_table('feedbacks', schema=None) as batch_op:
batch_op.add_column(sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.func.now()))
batch_op.drop_column('tags')
if 'zl_languages' not in tables:
op.create_table(
'zl_languages',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('code', sa.String(length=10), nullable=False),
sa.Column('name', sa.String(length=50), nullable=False),
sa.Column('is_active', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('code'),
)
with op.batch_alter_table('permission_requests', schema=None) as batch_op:
batch_op.alter_column('reason',
existing_type=sa.TEXT(),
nullable=False)
batch_op.drop_index(batch_op.f('ix_permission_requests_agent_id'))
batch_op.drop_index(batch_op.f('ix_permission_requests_user_id'))
if 'chat_logs' in tables:
cols = {c['name'] for c in insp.get_columns('chat_logs')}
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
if 'latency' not in cols:
batch_op.add_column(sa.Column('latency', sa.Float(), nullable=False, server_default='0.0'))
try:
batch_op.alter_column('answer', existing_type=sa.TEXT(), nullable=False)
except Exception:
pass
for idx in (
batch_op.f('ix_chat_logs_agent_id'),
batch_op.f('ix_chat_logs_conversation_id'),
batch_op.f('ix_chat_logs_created_at'),
batch_op.f('ix_chat_logs_user_id'),
):
try:
batch_op.drop_index(idx)
except Exception:
pass
for name in ('tokens_used', 'conversation_id', 'latency_ms'):
if name in cols:
try:
batch_op.drop_column(name)
except Exception:
pass
with op.batch_alter_table('quick_questions', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_quick_questions_agent_id'))
batch_op.drop_index(batch_op.f('ix_quick_questions_user_id'))
if 'feedbacks' in tables:
cols = {c['name'] for c in insp.get_columns('feedbacks')}
with op.batch_alter_table('feedbacks', schema=None) as batch_op:
if 'created_at' not in cols:
batch_op.add_column(sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.func.now()))
if 'tags' in cols:
try:
batch_op.drop_column('tags')
except Exception:
pass
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.add_column(sa.Column('language', sa.String(length=10), nullable=False, server_default='zh-CN'))
if 'permission_requests' in tables:
with op.batch_alter_table('permission_requests', schema=None) as batch_op:
try:
batch_op.alter_column('reason', existing_type=sa.TEXT(), nullable=False)
except Exception:
pass
for idx in (
batch_op.f('ix_permission_requests_agent_id'),
batch_op.f('ix_permission_requests_user_id'),
):
try:
batch_op.drop_index(idx)
except Exception:
pass
# ### end Alembic commands ###
if 'quick_questions' in tables:
with op.batch_alter_table('quick_questions', schema=None) as batch_op:
for idx in (
batch_op.f('ix_quick_questions_agent_id'),
batch_op.f('ix_quick_questions_user_id'),
):
try:
batch_op.drop_index(idx)
except Exception:
pass
if 'users' in tables:
cols = {c['name'] for c in insp.get_columns('users')}
if 'language' not in cols:
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.add_column(sa.Column('language', sa.String(length=10), nullable=False, server_default='zh-CN'))
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.drop_column('language')
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
with op.batch_alter_table('quick_questions', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_quick_questions_user_id'), ['user_id'], unique=False)
batch_op.create_index(batch_op.f('ix_quick_questions_agent_id'), ['agent_id'], unique=False)
if 'users' in tables:
cols = {c['name'] for c in insp.get_columns('users')}
if 'language' in cols:
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.drop_column('language')
with op.batch_alter_table('permission_requests', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_permission_requests_user_id'), ['user_id'], unique=False)
batch_op.create_index(batch_op.f('ix_permission_requests_agent_id'), ['agent_id'], unique=False)
batch_op.alter_column('reason',
existing_type=sa.TEXT(),
nullable=True)
if 'quick_questions' in tables:
with op.batch_alter_table('quick_questions', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_quick_questions_user_id'), ['user_id'], unique=False)
batch_op.create_index(batch_op.f('ix_quick_questions_agent_id'), ['agent_id'], unique=False)
with op.batch_alter_table('feedbacks', schema=None) as batch_op:
batch_op.add_column(sa.Column('tags', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True))
batch_op.drop_column('created_at')
if 'permission_requests' in tables:
with op.batch_alter_table('permission_requests', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_permission_requests_user_id'), ['user_id'], unique=False)
batch_op.create_index(batch_op.f('ix_permission_requests_agent_id'), ['agent_id'], unique=False)
try:
batch_op.alter_column('reason', existing_type=sa.TEXT(), nullable=True)
except Exception:
pass
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
batch_op.add_column(sa.Column('latency_ms', sa.INTEGER(), autoincrement=False, nullable=False))
batch_op.add_column(sa.Column('conversation_id', sa.VARCHAR(length=100), autoincrement=False, nullable=False))
batch_op.add_column(sa.Column('tokens_used', sa.INTEGER(), autoincrement=False, nullable=False))
batch_op.create_index(batch_op.f('ix_chat_logs_user_id'), ['user_id'], unique=False)
batch_op.create_index(batch_op.f('ix_chat_logs_created_at'), ['created_at'], unique=False)
batch_op.create_index(batch_op.f('ix_chat_logs_conversation_id'), ['conversation_id'], unique=False)
batch_op.create_index(batch_op.f('ix_chat_logs_agent_id'), ['agent_id'], unique=False)
batch_op.alter_column('answer',
existing_type=sa.TEXT(),
nullable=True)
batch_op.drop_column('latency')
if 'feedbacks' in tables:
cols = {c['name'] for c in insp.get_columns('feedbacks')}
with op.batch_alter_table('feedbacks', schema=None) as batch_op:
if 'tags' not in cols:
batch_op.add_column(sa.Column('tags', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True))
if 'created_at' in cols:
batch_op.drop_column('created_at')
op.drop_table('zl_languages')
# ### end Alembic commands ###
if 'chat_logs' in tables:
cols = {c['name'] for c in insp.get_columns('chat_logs')}
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
if 'latency_ms' not in cols:
batch_op.add_column(sa.Column('latency_ms', sa.INTEGER(), autoincrement=False, nullable=False))
if 'conversation_id' not in cols:
batch_op.add_column(sa.Column('conversation_id', sa.VARCHAR(length=100), autoincrement=False, nullable=False))
if 'tokens_used' not in cols:
batch_op.add_column(sa.Column('tokens_used', sa.INTEGER(), autoincrement=False, nullable=False))
batch_op.create_index(batch_op.f('ix_chat_logs_user_id'), ['user_id'], unique=False)
batch_op.create_index(batch_op.f('ix_chat_logs_created_at'), ['created_at'], unique=False)
batch_op.create_index(batch_op.f('ix_chat_logs_conversation_id'), ['conversation_id'], unique=False)
batch_op.create_index(batch_op.f('ix_chat_logs_agent_id'), ['agent_id'], unique=False)
try:
batch_op.alter_column('answer', existing_type=sa.TEXT(), nullable=True)
except Exception:
pass
if 'latency' in cols:
batch_op.drop_column('latency')
if 'zl_languages' in tables:
op.drop_table('zl_languages')
@@ -0,0 +1,38 @@
"""add po_number to shipping_tasks
Revision ID: a7b8c9d0e1f2
Revises: 3a4b5c6d7e8f
Create Date: 2026-05-29 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = "a7b8c9d0e1f2"
down_revision = "3a4b5c6d7e8f"
branch_labels = None
depends_on = None
def upgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
if "shipping_tasks" not in set(insp.get_table_names()):
return
columns = {col["name"] for col in insp.get_columns("shipping_tasks")}
if "po_number" not in columns:
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
batch_op.add_column(sa.Column("po_number", sa.String(length=128), nullable=True))
def downgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
if "shipping_tasks" not in set(insp.get_table_names()):
return
columns = {col["name"] for col in insp.get_columns("shipping_tasks")}
if "po_number" in columns:
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
batch_op.drop_column("po_number")
@@ -1,73 +0,0 @@
"""add shipping_cases tables for freight notice agent tools
Revision ID: d8e9f0a1b2c3
Revises: c9d8e7f6a5b4
Create Date: 2026-05-29 12:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = 'd8e9f0a1b2c3'
down_revision = 'c9d8e7f6a5b4'
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
'shipping_cases',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('case_no', sa.String(length=64), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('conversation_id', sa.String(length=100), nullable=True),
sa.Column('source_type', sa.String(length=32), nullable=False, server_default='manual'),
sa.Column('raw_content', sa.Text(), nullable=True),
sa.Column('customer_name', sa.String(length=200), nullable=True),
sa.Column('customer_code', sa.String(length=64), nullable=True),
sa.Column('ship_to', sa.Text(), nullable=True),
sa.Column('required_date', sa.String(length=32), nullable=True),
sa.Column('lines_json', sa.JSON(), nullable=True),
sa.Column('parse_confidence', sa.Float(), nullable=True),
sa.Column('process_status', sa.String(length=32), nullable=False, server_default='created'),
sa.Column('review_status', sa.String(length=32), nullable=True),
sa.Column('match_status', sa.String(length=32), nullable=True),
sa.Column('shortage_json', sa.JSON(), nullable=True),
sa.Column('line_results_json', sa.JSON(), nullable=True),
sa.Column('inventory_snapshot_id', sa.Integer(), nullable=True),
sa.Column('inventory_lines_json', sa.JSON(), nullable=True),
sa.Column('export_file_path', sa.String(length=500), nullable=True),
sa.Column('export_file_name', sa.String(length=255), nullable=True),
sa.Column('freight_ref_no', sa.String(length=128), nullable=True),
sa.Column('pending_human_reason', sa.String(length=64), nullable=True),
sa.Column('error_message', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
sa.PrimaryKeyConstraint('id'),
)
op.create_index('ix_shipping_cases_case_no', 'shipping_cases', ['case_no'], unique=True)
op.create_index('ix_shipping_cases_user_status', 'shipping_cases', ['user_id', 'process_status'])
op.create_index('ix_shipping_cases_conversation', 'shipping_cases', ['conversation_id'])
op.create_index('ix_shipping_cases_process_status', 'shipping_cases', ['process_status'])
op.create_index('ix_shipping_cases_created_at', 'shipping_cases', ['created_at'])
op.create_table(
'shipping_case_events',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('case_id', sa.Integer(), nullable=False),
sa.Column('event_type', sa.String(length=64), nullable=False),
sa.Column('detail_json', sa.JSON(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['case_id'], ['shipping_cases.id']),
sa.PrimaryKeyConstraint('id'),
)
op.create_index('ix_shipping_case_events_case_id', 'shipping_case_events', ['case_id'])
op.create_index('ix_shipping_case_events_event_type', 'shipping_case_events', ['event_type'])
op.create_index('ix_shipping_case_events_created_at', 'shipping_case_events', ['created_at'])
def downgrade():
op.drop_table('shipping_case_events')
op.drop_table('shipping_cases')
@@ -0,0 +1,24 @@
"""shipping notice legacy step 1
Revision ID: d8e9f0a1b2c3
Revises: c9d8e7f6a5b4
Create Date: 2026-06-03 00:00:00.000000
"""
from alembic import op
revision = "d8e9f0a1b2c3"
down_revision = "c9d8e7f6a5b4"
branch_labels = None
depends_on = None
def upgrade():
pass
def downgrade():
pass
@@ -17,48 +17,70 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('user_default_agents',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('agent_id', sa.Integer(), nullable=False),
sa.Column('sort_order', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('user_favorite_agents',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('agent_id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('user_id', 'agent_id')
)
op.create_table('user_homepage_configs',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('layout', sa.JSON(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('user_id')
)
with op.batch_alter_table('departments', schema=None) as batch_op:
batch_op.add_column(sa.Column('code', sa.String(length=50), nullable=True))
batch_op.add_column(sa.Column('company', sa.String(length=100), nullable=True))
batch_op.create_unique_constraint(None, ['code'])
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
# ### end Alembic commands ###
if 'users' in tables and 'agents' in tables:
if 'user_default_agents' not in tables:
op.create_table(
'user_default_agents',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('agent_id', sa.Integer(), nullable=False),
sa.Column('sort_order', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['agent_id'], ['agents.id']),
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
sa.PrimaryKeyConstraint('id'),
)
if 'user_favorite_agents' not in tables:
op.create_table(
'user_favorite_agents',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('agent_id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['agent_id'], ['agents.id']),
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
sa.PrimaryKeyConstraint('user_id', 'agent_id'),
)
if 'user_homepage_configs' not in tables:
op.create_table(
'user_homepage_configs',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('layout', sa.JSON(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
sa.PrimaryKeyConstraint('user_id'),
)
if 'departments' in tables:
cols = {c['name'] for c in insp.get_columns('departments')}
with op.batch_alter_table('departments', schema=None) as batch_op:
if 'code' not in cols:
batch_op.add_column(sa.Column('code', sa.String(length=50), nullable=True))
batch_op.create_unique_constraint(None, ['code'])
if 'company' not in cols:
batch_op.add_column(sa.Column('company', sa.String(length=100), nullable=True))
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('departments', schema=None) as batch_op:
batch_op.drop_constraint(None, type_='unique')
batch_op.drop_column('company')
batch_op.drop_column('code')
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
op.drop_table('user_homepage_configs')
op.drop_table('user_favorite_agents')
op.drop_table('user_default_agents')
# ### end Alembic commands ###
if 'departments' in tables:
cols = {c['name'] for c in insp.get_columns('departments')}
with op.batch_alter_table('departments', schema=None) as batch_op:
if 'code' in cols:
try:
batch_op.drop_constraint(None, type_='unique')
except Exception:
pass
batch_op.drop_column('code')
if 'company' in cols:
batch_op.drop_column('company')
for tbl in ('user_homepage_configs', 'user_favorite_agents', 'user_default_agents'):
if tbl in tables:
op.drop_table(tbl)