feat(shipping-notice): 新增发货批次和任务管理功能,支持批次创建与任务处理
This commit is contained in:
1 parent
457f0f4e5e
commit
634dee4b81
81 files changed
+9088
-3625
No files matched your search
+166
@@ -0,0 +1,166 @@
|
||||
"""add shipping batches and task upload fields
|
||||
|
||||
Revision ID: b1c2d3e4f5a6
|
||||
Revises: f6f91cb46944
|
||||
Create Date: 2026-06-17 12:30:00.000000
|
||||
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = "b1c2d3e4f5a6"
|
||||
down_revision = "f6f91cb46944"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _table_columns(insp, table_name: str) -> set[str]:
|
||||
if table_name not in set(insp.get_table_names()):
|
||||
return set()
|
||||
return {col["name"] for col in insp.get_columns(table_name)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
insp = sa.inspect(bind)
|
||||
|
||||
if "shipping_batches" not in set(insp.get_table_names()):
|
||||
op.create_table(
|
||||
"shipping_batches",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("batch_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("current_focus_task_no", sa.String(length=64), nullable=True),
|
||||
sa.Column("file_count", sa.Integer(), nullable=False, server_default="1"),
|
||||
sa.Column("process_summary_json", sa.JSON(), 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"),
|
||||
)
|
||||
with op.batch_alter_table("shipping_batches", schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f("ix_shipping_batches_batch_no"), ["batch_no"], unique=True)
|
||||
batch_op.create_index(batch_op.f("ix_shipping_batches_user_id"), ["user_id"], unique=False)
|
||||
batch_op.create_index("ix_shipping_batches_user_created", ["user_id", "created_at"], unique=False)
|
||||
batch_op.create_index("ix_shipping_batches_conversation", ["conversation_id"], unique=False)
|
||||
else:
|
||||
batch_columns = _table_columns(insp, "shipping_batches")
|
||||
batch_indexes = {idx["name"] for idx in insp.get_indexes("shipping_batches")}
|
||||
if "creator_user_id" in batch_columns and "user_id" not in batch_columns:
|
||||
with op.batch_alter_table("shipping_batches", schema=None) as batch_op:
|
||||
if "ix_shipping_batches_creator_user_id" in batch_indexes:
|
||||
batch_op.drop_index("ix_shipping_batches_creator_user_id")
|
||||
batch_op.alter_column("creator_user_id", new_column_name="user_id")
|
||||
batch_indexes = {idx["name"] for idx in sa.inspect(bind).get_indexes("shipping_batches")}
|
||||
with op.batch_alter_table("shipping_batches", schema=None) as batch_op:
|
||||
if "ix_shipping_batches_user_id" not in batch_indexes:
|
||||
batch_op.create_index("ix_shipping_batches_user_id", ["user_id"], unique=False)
|
||||
if "ix_shipping_batches_user_created" not in batch_indexes:
|
||||
batch_op.create_index("ix_shipping_batches_user_created", ["user_id", "created_at"], unique=False)
|
||||
|
||||
columns = _table_columns(insp, "shipping_tasks")
|
||||
if not columns:
|
||||
return
|
||||
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
if "batch_id" not in columns:
|
||||
batch_op.add_column(sa.Column("batch_id", sa.Integer(), nullable=True))
|
||||
batch_op.create_foreign_key("fk_shipping_tasks_batch_id_shipping_batches", "shipping_batches", ["batch_id"], ["id"])
|
||||
if "source_file_name" not in columns:
|
||||
batch_op.add_column(sa.Column("source_file_name", sa.String(length=255), nullable=True))
|
||||
if "source_file_type" not in columns:
|
||||
batch_op.add_column(sa.Column("source_file_type", sa.String(length=64), nullable=True))
|
||||
if "batch_index" not in columns:
|
||||
batch_op.add_column(sa.Column("batch_index", sa.Integer(), nullable=True))
|
||||
if "batch_total" not in columns:
|
||||
batch_op.add_column(sa.Column("batch_total", sa.Integer(), nullable=True))
|
||||
if "current_step" not in columns:
|
||||
batch_op.add_column(sa.Column("current_step", sa.String(length=64), nullable=True))
|
||||
if "export_version" not in columns:
|
||||
batch_op.add_column(sa.Column("export_version", sa.Integer(), nullable=False, server_default="1"))
|
||||
if "upload_status" not in columns:
|
||||
batch_op.add_column(sa.Column("upload_status", sa.String(length=32), nullable=True))
|
||||
if "upload_mode" not in columns:
|
||||
batch_op.add_column(sa.Column("upload_mode", sa.String(length=32), nullable=True))
|
||||
if "upload_attempt" not in columns:
|
||||
batch_op.add_column(sa.Column("upload_attempt", sa.Integer(), nullable=False, server_default="0"))
|
||||
if "idempotency_key" not in columns:
|
||||
batch_op.add_column(sa.Column("idempotency_key", sa.String(length=128), nullable=True))
|
||||
if "upload_started_at" not in columns:
|
||||
batch_op.add_column(sa.Column("upload_started_at", sa.DateTime(), nullable=True))
|
||||
if "upload_finished_at" not in columns:
|
||||
batch_op.add_column(sa.Column("upload_finished_at", sa.DateTime(), nullable=True))
|
||||
if "upload_error_code" not in columns:
|
||||
batch_op.add_column(sa.Column("upload_error_code", sa.String(length=64), nullable=True))
|
||||
if "upload_error_message" not in columns:
|
||||
batch_op.add_column(sa.Column("upload_error_message", sa.Text(), nullable=True))
|
||||
if "upload_receipt_json" not in columns:
|
||||
batch_op.add_column(sa.Column("upload_receipt_json", sa.JSON(), nullable=True))
|
||||
if "step_payload_json" not in columns:
|
||||
batch_op.add_column(sa.Column("step_payload_json", sa.JSON(), nullable=True))
|
||||
|
||||
indexes = {idx["name"] for idx in insp.get_indexes("shipping_tasks")}
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
if "ix_shipping_tasks_batch_id" not in indexes and "batch_id" in _table_columns(sa.inspect(bind), "shipping_tasks"):
|
||||
batch_op.create_index("ix_shipping_tasks_batch_id", ["batch_id"], unique=False)
|
||||
if "ix_shipping_tasks_batch_step" not in indexes and "batch_id" in _table_columns(sa.inspect(bind), "shipping_tasks"):
|
||||
batch_op.create_index("ix_shipping_tasks_batch_step", ["batch_id", "current_step"], unique=False)
|
||||
if "ix_shipping_tasks_upload_status" not in indexes:
|
||||
batch_op.create_index("ix_shipping_tasks_upload_status", ["upload_status"], unique=False)
|
||||
|
||||
|
||||
def downgrade():
|
||||
bind = op.get_bind()
|
||||
insp = sa.inspect(bind)
|
||||
|
||||
columns = _table_columns(insp, "shipping_tasks")
|
||||
if columns:
|
||||
indexes = {idx["name"] for idx in insp.get_indexes("shipping_tasks")}
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
if "ix_shipping_tasks_upload_status" in indexes:
|
||||
batch_op.drop_index("ix_shipping_tasks_upload_status")
|
||||
if "ix_shipping_tasks_batch_step" in indexes:
|
||||
batch_op.drop_index("ix_shipping_tasks_batch_step")
|
||||
if "ix_shipping_tasks_batch_id" in indexes:
|
||||
batch_op.drop_index("ix_shipping_tasks_batch_id")
|
||||
if "step_payload_json" in columns:
|
||||
batch_op.drop_column("step_payload_json")
|
||||
if "upload_receipt_json" in columns:
|
||||
batch_op.drop_column("upload_receipt_json")
|
||||
if "upload_error_message" in columns:
|
||||
batch_op.drop_column("upload_error_message")
|
||||
if "upload_error_code" in columns:
|
||||
batch_op.drop_column("upload_error_code")
|
||||
if "upload_finished_at" in columns:
|
||||
batch_op.drop_column("upload_finished_at")
|
||||
if "upload_started_at" in columns:
|
||||
batch_op.drop_column("upload_started_at")
|
||||
if "idempotency_key" in columns:
|
||||
batch_op.drop_column("idempotency_key")
|
||||
if "upload_attempt" in columns:
|
||||
batch_op.drop_column("upload_attempt")
|
||||
if "upload_mode" in columns:
|
||||
batch_op.drop_column("upload_mode")
|
||||
if "upload_status" in columns:
|
||||
batch_op.drop_column("upload_status")
|
||||
if "export_version" in columns:
|
||||
batch_op.drop_column("export_version")
|
||||
if "current_step" in columns:
|
||||
batch_op.drop_column("current_step")
|
||||
if "batch_total" in columns:
|
||||
batch_op.drop_column("batch_total")
|
||||
if "batch_index" in columns:
|
||||
batch_op.drop_column("batch_index")
|
||||
if "source_file_type" in columns:
|
||||
batch_op.drop_column("source_file_type")
|
||||
if "source_file_name" in columns:
|
||||
batch_op.drop_column("source_file_name")
|
||||
if "batch_id" in columns:
|
||||
batch_op.drop_constraint("fk_shipping_tasks_batch_id_shipping_batches", type_="foreignkey")
|
||||
batch_op.drop_column("batch_id")
|
||||
|
||||
if "shipping_batches" in set(insp.get_table_names()):
|
||||
op.drop_table("shipping_batches")
|
||||
@@ -0,0 +1,70 @@
|
||||
"""rename shipping_batches.creator_user_id to user_id
|
||||
|
||||
Revision ID: c3d4e5f6a7b8
|
||||
Revises: b1c2d3e4f5a6
|
||||
Create Date: 2026-06-17 17:35:00.000000
|
||||
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = "c3d4e5f6a7b8"
|
||||
down_revision = "b1c2d3e4f5a6"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _table_columns(insp, table_name: str) -> set[str]:
|
||||
if table_name not in set(insp.get_table_names()):
|
||||
return set()
|
||||
return {col["name"] for col in insp.get_columns(table_name)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
insp = sa.inspect(bind)
|
||||
if "shipping_batches" not in set(insp.get_table_names()):
|
||||
return
|
||||
|
||||
columns = _table_columns(insp, "shipping_batches")
|
||||
if "creator_user_id" not in columns or "user_id" in columns:
|
||||
return
|
||||
|
||||
indexes = {idx["name"] for idx in insp.get_indexes("shipping_batches")}
|
||||
with op.batch_alter_table("shipping_batches", schema=None) as batch_op:
|
||||
if "ix_shipping_batches_creator_user_id" in indexes:
|
||||
batch_op.drop_index("ix_shipping_batches_creator_user_id")
|
||||
batch_op.alter_column("creator_user_id", new_column_name="user_id")
|
||||
|
||||
indexes = {idx["name"] for idx in sa.inspect(bind).get_indexes("shipping_batches")}
|
||||
with op.batch_alter_table("shipping_batches", schema=None) as batch_op:
|
||||
if "ix_shipping_batches_user_id" not in indexes:
|
||||
batch_op.create_index("ix_shipping_batches_user_id", ["user_id"], unique=False)
|
||||
if "ix_shipping_batches_user_created" not in indexes:
|
||||
batch_op.create_index("ix_shipping_batches_user_created", ["user_id", "created_at"], unique=False)
|
||||
|
||||
|
||||
def downgrade():
|
||||
bind = op.get_bind()
|
||||
insp = sa.inspect(bind)
|
||||
if "shipping_batches" not in set(insp.get_table_names()):
|
||||
return
|
||||
|
||||
columns = _table_columns(insp, "shipping_batches")
|
||||
if "user_id" not in columns or "creator_user_id" in columns:
|
||||
return
|
||||
|
||||
indexes = {idx["name"] for idx in insp.get_indexes("shipping_batches")}
|
||||
with op.batch_alter_table("shipping_batches", schema=None) as batch_op:
|
||||
if "ix_shipping_batches_user_created" in indexes:
|
||||
batch_op.drop_index("ix_shipping_batches_user_created")
|
||||
if "ix_shipping_batches_user_id" in indexes:
|
||||
batch_op.drop_index("ix_shipping_batches_user_id")
|
||||
batch_op.alter_column("user_id", new_column_name="creator_user_id")
|
||||
|
||||
indexes = {idx["name"] for idx in sa.inspect(bind).get_indexes("shipping_batches")}
|
||||
with op.batch_alter_table("shipping_batches", schema=None) as batch_op:
|
||||
if "ix_shipping_batches_creator_user_id" not in indexes:
|
||||
batch_op.create_index("ix_shipping_batches_creator_user_id", ["creator_user_id"], unique=False)
|
||||
Reference in new issue
Block a user