This commit is contained in:
zlwu committed 2026-06-21 12:41:39 +08:00
1 parent 299609706c
commit e46c0480d4
87 files changed
+11975 -15543

No files matched your search

+2 -34
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
"""Migrate legacy dibang business data into shanxia2.0 db_* tables.
Reads from the original four tables (vehicles, purchase_plans, inbound_outbound,
Reads from the original three tables (purchase_plans, inbound_outbound,
weighing_slips), preserves legacy int IDs as VARCHAR primary keys (e.g. 12 → "12"),
remaps foreign keys, and writes portal public fields (tenant_no, del_flag, etc.).
@@ -40,14 +40,12 @@ if str(BACKEND_ROOT) not in sys.path:
from app.core.config import settings
SOURCE_TABLES = {
"vehicles": "vehicles",
"purchase_plans": "purchase_plans",
"inbound_outbound": "inbound_outbound",
"weighing_slips": "weighing_slips",
}
TARGET_TABLES = {
"vehicles": "db_vehicle",
"purchase_plans": "db_purchase_plan",
"inbound_outbound": "db_inbound_outbound",
"weighing_slips": "db_weighing_slip",
@@ -158,23 +156,6 @@ def build_id_maps(rows_by_table: Dict[str, List[dict]]) -> Dict[str, Dict[int, s
return id_maps
def transform_vehicle(row: dict, new_id: str, tenant_no: str, stats: MigrationStats) -> dict:
create_time = parse_datetime(row.get("created_at")) or datetime.now()
return {
"id": new_id,
"vehicle_no": str(row["vehicle_no"]).strip(),
"tenant_no": tenant_no,
"order_num": None,
"remark": clip_text(row.get("remarks"), 512, "vehicle.remark", stats),
"create_by": None,
"create_dept_id": "",
"create_time": create_time,
"update_by": None,
"update_time": create_time,
"del_flag": int(row.get("is_deleted") or 0),
}
def transform_plan(
row: dict,
new_id: str,
@@ -311,15 +292,6 @@ def transform_weighing(
INSERT_SQL = {
"vehicles": """
INSERT INTO db_vehicle (
id, vehicle_no, tenant_no, order_num, remark,
create_by, create_dept_id, create_time, update_by, update_time, del_flag
) VALUES (
:id, :vehicle_no, :tenant_no, :order_num, :remark,
:create_by, :create_dept_id, :create_time, :update_by, :update_time, :del_flag
)
""",
"purchase_plans": """
INSERT INTO db_purchase_plan (
id, plan_no, plan_date, start_date, end_date, business_type, product, specification,
@@ -440,10 +412,6 @@ def migrate(
id_maps = build_id_maps(rows_by_table)
transformed: Dict[str, List[dict]] = {
"vehicles": [
transform_vehicle(row, id_maps["vehicles"][int(row["id"])], tenant_no, stats)
for row in rows_by_table["vehicles"]
],
"purchase_plans": [
transform_plan(
row,
@@ -502,7 +470,7 @@ def migrate(
if truncate_target:
truncate_target_tables(conn, tenant_no)
for key in ("vehicles", "purchase_plans", "inbound_outbound", "weighing_slips"):
for key in ("purchase_plans", "inbound_outbound", "weighing_slips"):
for payload in transformed[key]:
conn.execute(text(INSERT_SQL[key]), payload)
stats.inserted[key] = len(transformed[key])