Files
shanxia2.0/docs/data-migration/phase-1-crm/product-schema-diff-and-scripts.md
T
zlwuandCursor 10c3c5470c chore: sync local PL/CRM/BX and migration work to new Gogs host
Bundle pending backend/web-admin feature updates, data-migration scripts,
and related docs so the V1.5 branch on the Tencent Cloud Gogs matches the
current workspace (excluding local secrets).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-01 09:19:21 +08:00

3.7 KiB
Raw Blame History

CRM 产品迁移:表模型差异与脚本说明

对照:docs/sql/sx.sql(源) vs docs/sql/shanxia2.0.sql(门户最新)
脚本:backend/scripts/migrate_crm_product_category.py、migrate_crm_product.py
ID 策略:原样 str(源 bigint),见 ../05-id-preservation-policy.md


1. 以前有没有脚本?能不能直接用?

脚本 有无 能否直接用
migrate_crm_product_category.py 有(历史 P1) 须保持 status 原样:门户字典 sys_normal_disable 为 0=正常/1=停用(勿按 DDL「1启用」反转)。2026-07-31 曾误反转已回修。
migrate_crm_product.py 有 基本可用(字段映射已覆盖当前门户列)。已补充 --clear-target、文档化字段差异。
品牌/别名 无(sx 无表) 门户有 crm_product_brand / crm_product_alias,不在本次从 sx 迁移范围。

结论:有历史脚本,但分类 status 不能直接用旧逻辑;产品主表脚本可在修正分类后继续用。


2. crm_product 字段对照

sx 门户 处理
id bigint id varchar(64) str(id) 原样
category_id bigint category_id varchar(64) str(category_id)
cost cost_price 改名
min_price — 丢弃
tenant_id tenant_no 固定 shanxia2.0
create_dept bigint create_dept_id varchar 本脚本置 ''(用户映射管 create_by)
create_by / update_by bigint varchar user_mapping(crm_user_id);源 1→admin
version — 丢弃
— is_toxic / is_explosive 默认 0
— order_num 默认 0
status on/off status char(3) 原样规范化
其余同名业务列 同名 直接映射

快照规模参考:sx.sql ≈ 248 INSERT;live 通常 del_flag=0 约 240+。


3. crm_product_category 字段对照

sx 门户 处理
id / parent_id varchar str(...);0→'0'
sort order_num 改名
status '0'正常/'1'停用 同左(字典 sys_normal_disable) 原样保持,勿反转(DDL 注释「1启用」易误导)
ancestors / version / tenant_id / create_dept — / tenant_no / create_dept_id 丢弃或门户惯例
— category_code / level code=NULL;level 按树计算

4. 推荐执行顺序(未自动执行,需你确认后跑)

cd backend
export PATH="venv/bin:$PATH"
SX_URL='mysql+pymysql://USER:PASS@HOST:3306/sx'
UM=../docs/data-migration/mappings/user_mapping_result.csv

# 1) 分类(先清本表再灌,修正 status)
python scripts/migrate_crm_product_category.py --dry-run --env-file .env.production --source-url "$SX_URL" --user-mapping "$UM"
python scripts/migrate_crm_product_category.py --env-file .env.production --source-url "$SX_URL" \
  --clear-target --user-mapping "$UM" \
  --mapping-out ../docs/data-migration/mappings/crm_product_category_id_mapping.csv

# 2) 产品(先清本表再灌,id 与 sx 一致)
python scripts/migrate_crm_product.py --dry-run --env-file .env.production --source-url "$SX_URL" --user-mapping "$UM"
python scripts/migrate_crm_product.py --env-file .env.production --source-url "$SX_URL" \
  --clear-target --user-mapping "$UM" \
  --mapping-out ../docs/data-migration/mappings/crm_product_id_mapping.csv

可选后续:renumber_crm_product_no.py --apply(统一产品编号;与「id 保持一致」无关)。


5. 不在本次范围

  • crm_product_brand / crm_product_alias(门户新能力,sx 无源数据)
  • 地磅 cargos、测试库里混入的 UUID 产品行(--clear-target 会清掉目标租户产品后以 sx 为准)