"""V30 业务接入与核算准备解耦;无默认回填。""" from pathlib import Path from alembic import op from sqlalchemy import text revision = "30c1d2e3f4a5" down_revision = "29b0c1d2e3f4" branch_labels = None depends_on = None def upgrade(): script = Path(__file__).resolve().parents[3] / "db/migration/V30__tplus_business_sync.sql" for statement in script.read_text(encoding="utf-8").split(";\n"): sql = "\n".join(line for line in statement.splitlines() if not line.lstrip().startswith("--")).strip() if sql: op.get_bind().execute(text(sql)) def downgrade(): connection = op.get_bind() for table, condition in (("tplus_business_documents", "1=1"), ("products", "product_category IS NULL"), ("customers", "customer_type IS NULL"), ("business_items", "product_category IS NULL"), ("shipments", "freight_amount IS NULL")): if connection.scalar(text(f"SELECT COUNT(*) FROM {table} WHERE {condition}")): raise RuntimeError("已有接入或待补资料,请关闭接入并回退兼容版本;不删除业务数据以降级结构") op.drop_table("tplus_business_documents") op.drop_table("tplus_business_sync_configs") for index in ("idx_tplus_source_heads_parent", "idx_tplus_source_heads_customer_date", "idx_tplus_source_heads_date"): op.drop_index(index, "tplus_source_heads") for column in ("source_parent_id", "source_customer_id", "source_date"): op.drop_column("tplus_source_heads", column) op.drop_column("businesses", "integration_json") for table, column, sql_type, default in (("products", "product_category", "VARCHAR(32)", ""), ("customers", "customer_type", "VARCHAR(32)", " DEFAULT 'personal'"), ("business_items", "product_category", "VARCHAR(32)", ""), ("shipments", "freight_amount", "DECIMAL(14,2)", " DEFAULT 0")): connection.execute(text(f"ALTER TABLE {table} MODIFY {column} {sql_type} NOT NULL{default}"))