Files
syops/backend/migrations/versions/c27e3d4c5a61_shipping_excel_templates.py
T

82 lines
4.9 KiB
Python

"""新增版本化发货 Excel 模板、库存源绑定与不可变导出产物。"""
from alembic import op
import sqlalchemy as sa
revision = 'c27e3d4c5a61'
down_revision = 'b17e3d4c5a60'
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
'shipping_excel_templates',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('template_code', sa.String(64), nullable=False),
sa.Column('display_name', sa.String(128), nullable=False),
sa.Column('version', sa.Integer(), server_default='1', nullable=False),
sa.Column('status', sa.String(16), server_default='draft', nullable=False),
sa.Column('storage_provider', sa.String(32), server_default='minio', nullable=False),
sa.Column('storage_bucket', sa.String(128), nullable=False),
sa.Column('storage_key', sa.String(512), nullable=False),
sa.Column('original_filename', sa.String(255), nullable=False),
sa.Column('file_checksum', sa.String(64), nullable=False),
sa.Column('definition_json', sa.JSON(), nullable=False),
sa.Column('definition_checksum', sa.String(64), nullable=True),
sa.Column('created_by', sa.Integer(), sa.ForeignKey('users.id'), nullable=True),
sa.Column('published_by', sa.Integer(), sa.ForeignKey('users.id'), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('published_at', sa.DateTime(), nullable=True),
sa.UniqueConstraint('template_code', 'version', name='uq_shipping_excel_templates_code_version'),
)
op.create_index('ix_shipping_excel_templates_status', 'shipping_excel_templates', ['status'])
with op.batch_alter_table('shipping_inventory_sources') as batch:
batch.add_column(sa.Column('excel_template_id', sa.Integer(), nullable=True))
batch.create_index('ix_shipping_inventory_sources_excel_template_id', ['excel_template_id'])
batch.create_foreign_key('fk_inventory_sources_excel_template', 'shipping_excel_templates', ['excel_template_id'], ['id'])
op.execute(
sa.text(
'UPDATE shipping_inventory_sources SET enabled = false '
'WHERE parsing_profile_id IS NULL OR excel_template_id IS NULL'
)
)
with op.batch_alter_table('shipping_inventory_sources') as batch:
batch.create_check_constraint(
'ck_shipping_inventory_sources_enabled_bindings',
'enabled = false OR (parsing_profile_id IS NOT NULL AND excel_template_id IS NOT NULL)',
)
op.create_table(
'shipping_export_artifacts',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('artifact_no', sa.String(64), nullable=False),
sa.Column('task_id', sa.Integer(), sa.ForeignKey('shipping_tasks.id'), nullable=False),
sa.Column('batch_id', sa.Integer(), sa.ForeignKey('shipping_batches.id'), nullable=True),
sa.Column('template_id', sa.Integer(), sa.ForeignKey('shipping_excel_templates.id'), nullable=False),
sa.Column('order_schema_version_id', sa.Integer(), sa.ForeignKey('shipping_order_schema_versions.id'), nullable=False),
sa.Column('order_extraction_id', sa.Integer(), sa.ForeignKey('shipping_order_extractions.id'), nullable=False),
sa.Column('inventory_parsing_profile_id', sa.Integer(), sa.ForeignKey('shipping_inventory_parsing_profiles.id'), nullable=False),
sa.Column('inventory_snapshot_id', sa.Integer(), sa.ForeignKey('shipping_inventory_files.id'), nullable=True),
sa.Column('input_checksum', sa.String(64), nullable=False),
sa.Column('file_path', sa.String(500), nullable=False),
sa.Column('file_name', sa.String(255), nullable=False),
sa.Column('file_checksum', sa.String(64), nullable=False),
sa.Column('file_size', sa.Integer(), nullable=False),
sa.Column('status', sa.String(16), server_default='ready', nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.UniqueConstraint('artifact_no', name='uq_shipping_export_artifacts_no'),
)
op.create_index('ix_shipping_export_artifacts_task_id', 'shipping_export_artifacts', ['task_id'])
op.create_index('ix_shipping_export_artifacts_task_created', 'shipping_export_artifacts', ['task_id', 'created_at'])
op.create_index('ix_shipping_export_artifacts_batch_created', 'shipping_export_artifacts', ['batch_id', 'created_at'])
def downgrade():
op.drop_table('shipping_export_artifacts')
with op.batch_alter_table('shipping_inventory_sources') as batch:
batch.drop_constraint('ck_shipping_inventory_sources_enabled_bindings', type_='check')
batch.drop_constraint('fk_inventory_sources_excel_template', type_='foreignkey')
batch.drop_index('ix_shipping_inventory_sources_excel_template_id')
batch.drop_column('excel_template_id')
op.drop_index('ix_shipping_excel_templates_status', table_name='shipping_excel_templates')
op.drop_table('shipping_excel_templates')