描述如何使用 Magento Data Migration Tool 迁移自定义 EAV 属性。

解读

在国内 Magento 2 升级或站群整合项目中,自定义 EAV 属性(如商品“国检编号”、客户“身份证照片”)能否完整迁移,直接决定业务能否上线。面试官通过此题考察三点:

  1. 是否真正用过 Data Migration Tool(DMT) 而非手动 SQL;
  2. 是否理解 EAV 在 M1 与 M2 表结构的差异(属性集、属性组、静态/动态字段);
  3. 能否在 “增量迁移”+“回滚” 场景下保证数据一致性,避免生产库锁表。

知识点

  • EAV 表链:eav_attribute → eav_attribute_set → eav_attribute_group → catalog_eav_attribute → 实体表(catalog_product_entity_*、customer_entity_*)。
  • DMT 四阶段:Integrity Check → Data Stage → Volume Check → Delta Stage;自定义属性必须在 map.xml 与 eav-attribute-groups.xml 同时声明。
  • 属性类型对齐:M1 的 varchar(255) 到 M2 可能需升级为 text 或 static 字段,否则会在 Volume Check 报 MISMATCHED COLUMN TYPE。
  • Delta 迁移锁表:若属性写入频繁,需在 settings.xml 开启 direct_document_copy=1 并调大 bulk_size,避免 MySQL metadata lock 导致下单失败。
  • Code 一致性:M2 端必须提前通过 InstallDataScript 或 db_schema.xml 创建同名属性,否则 DMT 会报 Attribute code does not exist in destination。
  • 国内合规字段:如“身份证”、“统一社会信用代码”需设置 is_used_in_grid=1、is_visible_in_grid=0,防止后台列表页暴露敏感信息。

答案

  1. 预检
    在 M1 端执行

    SELECT attribute_code, backend_type, frontend_input FROM eav_attribute WHERE entity_type_id=4 AND attribute_code LIKE 'my_%';
    

    记录所有自定义属性,确认无 backend_model 硬编码调用 M1 已废弃类。

  2. M2 端提前建属性
    通过 Setup/Patch/Data/CreateCustomAttribute.php 创建同名属性,保证 attribute_id 自增不会冲突;若属性需存 JSON(如国检报告 URL 数组),把 backend_type 设为 text,frontend_input 设为 textarea。

  3. 安装并配置 DMT

    composer require magento/data-migration-tool:2.4.6 --no-update
    composer update
    cp vendor/magento/data-migration-tool/etc/opensource-to-opensource/2.4.6/* .
    

    在 map.xml 追加

    <source>
        <document_rules>
            <ignore>
                <document>eav_attribute</document>
                <field>attribute_code</field>
                <value>my_custom_attr</value>
            </ignore>
        </document_rules>
    </source>
    

    目的是让 DMT 在 Integrity Check 阶段跳过属性代码重复校验,因为我们已在 M2 端手动建属性。

  4. 声明 EAV 属性映射
    新建 eav-attribute-groups.xml

    <groups>
        <group name="custom" entity_type="catalog_product">
            <attribute code="my_custom_attr" map="my_custom_attr"/>
        </group>
    </groups>
    

    并在 config.xml 中打开

    <options>
        <eav_attribute_groups>etc/eav-attribute-groups.xml</eav_attribute_groups>
    </options>
    
  5. 执行全量迁移

    php bin/magento migrate:data --reset vendor/magento/data-migration-tool/etc/opensource-to-opensource/2.4.6/config.xml
    

    观察 Volume Check 输出,确认 my_custom_attr 行数一致。

  6. 增量迁移(Delta)
    在 settings.xml 增加

    <delta>
        <eav_attribute>
            <ignore_fields>
                <field>attribute_id</field>
            </ignore_fields>
        </eav_attribute>
    </delta>
    

    防止自增 ID 差异导致 Delta Stage 报错;随后

    php bin/magento migrate:delta config.xml
    
  7. 验证与回滚
    在 M2 后台新建商品,填写自定义属性后保存;再执行一次 Delta 确认无新增差异。若需回滚,直接 truncate catalog_product_entity_* 相关表并重新执行 Data Stage,不可手动改 eav_attribute 表,否则 attribute_set_id 会乱。

拓展思考

  • 多店铺值迁移:若自定义属性在 M1 端按店铺存储不同值(如“国检编号”在大陆店与香港店不同),需在 map.xml 中关闭 <ignore><document>catalog_product_entity_*</document></ignore>,并在 Delta 阶段开启 track_changes=1,保证店铺级值同步。
  • Elasticsearch 索引重建:迁移后必须 reindex 并在 elasticsearch.yml 增加 index.mapping.total_fields.limit: 5000,防止自定义属性过多导致 field limit 报错。
  • Hyvä 主题兼容性:若新前端采用 Hyvä,其 GraphQL schema 默认只暴露 used_in_product_listing=1 的属性;需在 M2 后台把自定义属性设为 “Used in Product Listing” 并清除 var/page_cache,否则 PLP 会取不到值。