Skip to content
intermediate Phase 39 · Database Operations

Database Migration

Magento database migration using Data Migration Tool, migration scripts, and version upgrades

45m
0 problems
Topic Progress 0%

Data Migration Tool Overview

What is the Data Migration Tool?

Magento's official tool for migrating data from Magento 1 to Magento 2:

# Install via Composer
composer require magento/data-migration-tool

# Or if already installed
bin/magento migration:data --help

Migration Process

1. Settings Migration   → System configuration, websites, stores
2. Data Migration       → Products, customers, orders, CMS
3. Delta Migration      → Incremental changes during migration
4. Go Live              → Final delta sync and switch

Configuration Files

vendor/magento/data-migration-tool/etc/
├── config.xml.dist          # Main configuration
├── map.xml.dist             # Table mapping
├── class-map.xml.dist       # PHP class mapping
└── settings.xml.dist        # Settings mapping

For each version:
├── open-source/
│   ├── 1.9.3.x/
│   │   ├── map.xml.dist
│   │   └── class-map.xml.dist
│   └── ...
└── commerce/
    └── ...

Migration Configuration

config.xml

<!-- app/code/Vendor/Module/etc/config.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:data-migration-tool:config/etc/config.xsd">
    <steps mode="settings">
        <step title="Settings Step">
            <migration\Settings\Step\SettingsStep/>
        </step>
    </steps>
    <steps mode="data">
        <step title="Data Step">
            <migration\Data\Step\DataStep/>
        </step>
    </steps>
    <groups clickzitt="settings">
        <step title="Settings Step">
            <migration\Settings\Step\SettingsStep/>
        </step>
    </groups>
</config>

Source and Destination Configuration

<!-- etc/config.xml -->
<config>
    <source>
        <database>
            <host>localhost</host>
            <username>magento1_user</username>
            <password>password</password>
            <dbname>magento1_db</dbname>
            <prefix>m1_</prefix>
        </database>
    </source>
    <destination>
        <database>
            <host>localhost</host>
            <username>magento2_user</username>
            <password>password</password>
            <dbname>magento2_db</dbname>
            <prefix>m2_</prefix>
        </database>
    </destination>
</config>

Table Mapping

<!-- map.xml.dist -->
<map>
    <source>
        <table name="m1_catalog_product_entity">
            <rule>
                <field name="*">skip</field>
            </rule>
        </table>
    </source>
    <destination>
        <table name="m2_catalog_product_entity">
            <rule>
                <field name="*">map</field>
            </rule>
        </table>
    </destination>
</map>

Running Migrations

Migration Commands

# Step 1: Settings migration
bin/magento migration:settings --mode=settings etc/config.xml

# Step 2: Data migration
bin/magento migration:data etc/config.xml

# Step 3: Delta migration (for live sites)
bin/magento migration:data --delta etc/config.xml

# Step 4: Incremental updates
bin/magento migration:delta etc/config.xml

Migration with Custom Rules

// Custom data transformer
class CustomProductTransformer implements \Migration\Handler\HandlerInterface
{
    public function handle($value)
    {
        // Transform data during migration
        return ucfirst(strtolower($value));
    }
}

// In map.xml
<map>
    <source>
        <table name="m1_catalog_product_entity">
            <field name="name" handler="CustomProductTransformer"/>
        </table>
    </source>
</map>

Version Upgrade Migration

# For upgrades within Magento 2
bin/magento setup:upgrade

# This runs all pending:
# - Schema patches (db_schema.xml changes)
# - Data patches (new data installations)
# - Setup scripts (InstallSchema, InstallData)

# Check upgrade status
bin/magento setup:db:status

Migration Progress

# Check migration status
bin/magento migration:data --status etc/config.xml

# Output:
+----------+---------+--------+
| Step     | Status  | Mode   |
+----------+---------+--------+
| Settings | Complete| ---    |
| Data     | 75%     | ---    |
| Delta    | Pending | ---    |
+----------+---------+--------+

Migration Best Practices

Pre-Migration Checklist

1. Backup both databases
2. Verify Magento 2 meets system requirements
3. Install and configure Data Migration Tool
4. Test migration on development environment
5. Document custom modules and data
6. Plan downtime window
7. Notify stakeholders

Common Migration Issues

Issue Solution
Table prefix mismatch Configure prefixes in config.xml
Data type conflicts Use custom handlers for transformation
Missing foreign keys Review map.xml rules
Large data volumes Run in batches, monitor memory
Custom module data Create custom migration scripts

Post-Migration Tasks

# Reindex all
bin/magento indexer:reindex

# Flush cache
bin/magento cache:flush

# Generate URL rewrites
bin/magento urlrewrite:reindex

# Verify data
bin/magento catalog.product:count
bin/magento customer:count
bin/magento sales:order:count

# Test critical paths
# - Product listing
# - Cart/Checkout
# - Customer login
# - Admin panel

Custom Module Migration

// Create custom migration script
class MigrateCustomModuleData implements DataPatchInterface
{
    public function install(): void
    {
        $this->setup->startSetup();
        $connection = $this->setup->getConnection();

        // Migrate from M1 custom table to M2
        $sourceTable = 'm1_custom_module_data';
        $destTable = $this->setup->getTable('vendor_module_data');

        if ($connection->isTableExists($sourceTable)) {
            $data = $connection->fetchAll("SELECT * FROM $sourceTable");
            foreach ($data as $row) {
                $connection->insert($destTable, [
                    'name' => $row['name'],
                    'status' => $row['status'],
                ]);
            }
        }

        $this->setup->endSetup();
    }
}

Quiz

1. What are the three main migration steps?

Question 1 options

2. What command runs incremental data migration?

Question 2 options

3. What should you do before running migration?

Question 3 options

Flashcards

Question

What is the Data Migration Tool?

Answer

Magento's official tool for migrating data from Magento 1 to Magento 2

Question

What are the migration steps?

Answer

Settings → Data → Delta

Question

What is delta migration?

Answer

Incremental data sync for live sites during migration

Question

What command checks migration status?

Answer

bin/magento migration:data --status etc/config.xml

Question

What runs after migration?

Answer

Reindex, flush cache, regenerate URL rewrites, verify data

Revision Notes

Key Takeaways

  • 1. Data Migration Tool migrates from Magento 1 to Magento 2
  • 2. Three steps: Settings, Data, Delta migration
  • 3. Configuration in config.xml with source/destination databases
  • 4. Custom handlers transform data during migration
  • 5. Always backup, test, and verify after migration

Interview Tips

  • Explain the three migration steps and their purposes
  • Discuss handling custom module data during migration
  • Describe post-migration verification tasks

Cheat Sheet

Migration Commands:
  bin/magento migration:settings --mode=settings etc/config.xml
  bin/magento migration:data etc/config.xml
  bin/magento migration:data --delta etc/config.xml
  bin/magento migration:delta etc/config.xml

Post-Migration:
  bin/magento indexer:reindex
  bin/magento cache:flush
  bin/magento urlrewrite:reindex

Config Files:
  config.xml     → Source/destination databases
  map.xml        → Table mapping
  class-map.xml  → PHP class mapping