Set up Drupal7 to Drupal8 migration in simple steps (using Drush)


Step1:
Download Drupal8 and its dependencies using composer

Note: Drush8 is needed for migrate_upgrade module to execute. migrate_upgrade is not working with Drush9.

>sudo composer create-project drupal-composer/drupal-project:~8.4 migrate843 --stability dev --no-interaction

This will install drush and drupal-console

Reference:
https://www.drupal.org/docs/develop/using-composer/using-composer-to-manage-drupal-site-dependencies

Step2:
Install Drupal using Drush

> sudo ../vendor/bin/drush site-install --db-url=mysql://username:password@host/dbname

Step3:
Enable migration related modules
drush en migrate
drush en migrate_drupal
drush en migrate_tools
drush en migrate_plus
drush en migrate_upgrade

Step4:
Now run the migration.
--configure-only option, which instead of running the migration will generate migration configuration entities, which may then be edited to customize the migration path and run using the migrate_tools module

> drush migrate-upgrade --legacy-db-url=mysql://username:password@host/dbname --legacy-root=http://sourcesite_url --configure-only
(or)
> drush migrate-upgrade --legacy-db-key=migrate --configure-only

Note:The above command will create configuration entities for the migration in drupal8 database.
You can export the database configuration entities to yml entities by exporting using drush.
drush cex --destination=/tmp/export. Read README file of migrate_upgrade for detailed instructions.

Example output of > drush migration-upgrade:
Exporting contact_category as upgrade_contact_category
Exporting d7_dblog_settings as upgrade_d7_dblog_settings
Exporting d7_file_private as upgrade_d7_file_private
Exporting d7_filter_format as upgrade_d7_filter_format
Exporting d7_filter_settings as upgrade_d7_filter_settings
Exporting d7_global_theme_settings as upgrade_d7_global_theme_settings
Exporting d7_image_settings as upgrade_d7_image_settings
Exporting d7_image_styles as upgrade_d7_image_styles
Exporting d7_node_settings as upgrade_d7_node_settings
Exporting d7_search_settings as upgrade_d7_search_settings
Exporting d7_system_authorize as upgrade_d7_system_authorize
Exporting d7_system_cron as upgrade_d7_system_cron
Exporting d7_system_date as upgrade_d7_system_date
Exporting d7_system_file as upgrade_d7_system_file
Exporting d7_system_mail as upgrade_d7_system_mail

See the migration status
>  drush migrate-status
drush migrate-status --tag="Drupal 7"

Example output:
 Group: Import from Drupal 7 (migrate_drupal_7)            Status     Total  Imported  Unprocessed  Last imported     
 upgrade_block_content_type                                Idle       1      0         1                             
 upgrade_contact_category                                  Idle       1      0         1                             
 upgrade_d7_dblog_settings                                 Idle       0      0         0                             
 upgrade_d7_file_private                                   Idle       29546  0         29546 

Step5: 
Do the migration one by one

Example: 

Do in the below order.

user roles (upgrade_d7_user_role)
users (upgrade_d7_user)
vocabularies (upgrade_d7_taxonomy_vocabulary)

below are entity types
node type (upgrade_d7_node_type)
fieldable panel pane type (upgrade_d7_fieldable_panels_pane_type) (make sure all the panel pane types are available in d7 source database. Sometimes they come from features.

INSERT INTO `fieldable_panels_pane_type` (`name`, `title`, `description`) VALUES ('textblock','textblock','Create textblock Element for panel pages.');

field collection type (upgrade_d7_field_collection_type)
paragraph type (upgrade_d7_field_collection_type)
other custom entity types (if any)

below are for fields and view modes
field configuration (upgrade_d7_field)
field instance config (upgrade_d7_field_instance)
field instances widget settings (upgrade_d7_field_instance_widget_settings)
view modes (upgrade_d7_view_modes)
field formatter configuration (upgrade_d7_field_formatter_settings)

below ar for nodes
node (upgrade_d7_node_product)
node revision (upgrade_d7_node_revision_product) => this will execute all the nodes


for news content type
drush mim upgrade_d7_user --execute-dependencies
drush mim upgrade_d7_field --execute-dependencies
drush mim upgrade_d7_node_type --execute-dependencies
drush mim upgrade_d7_field_instance --execute-dependencies
drush mim upgrade_d7_field_formatter_settings --execute-dependencies
drush mim upgrade_d7_field_instance_widget_settings --execute-dependencies
drush mim d7_filter_settings
drush mim d7_filter_format
drush mim upgrade_d7_file --execute-dependencies


for taxonomy
> drush migrate-import upgrade_d7_taxonomy_term_media_folders

for files
> drush migrate-import upgrade_d7_file_private

Step6:
Check the destination for results

=============
Media Migration:
=============
The below article is specific to media migration from Drupal7 to Drupal8
Step1:
First set up your media environment in Drupal8 site as given in example below.
https://www.webwash.net/managing-media-assets-using-core-media-in-drupal-8/

some other references:
https://www.drupal.org/project/drupal/issues/2835825
https://gist.github.com/jibran/8e7cd2319e873858dd49a272227a4fd2
migrate_plus has a example module to do custom migrations using drush


===================
Overriding the migration
===================
Use case:
Migrate only news node type.

Step1:
First we need to override the prepareRow hook of migrate module. For that we need a custom module.

The below module is a good starting point.
https://github.com/Lullabot/d8_custom_migration
Install this module.

Step2:
In custom_migration.module comment everything inside custom_migration_migrate_prepare_row
Add the following in  custom_migration_migrate_prepare_row

if ($row->getSourceProperty('type') != 'news') {
    drush_print_r($row->getSourceProperty('type'));
    return FALSE;
  }

You can learn lot other things from this module code.

Step3:
> drush mim upgrade_d7_node_type

Now you can see only news content type has been migrated.

Other references:
https://hackernoon.com/migrate-api-custom-drupal-to-drupal-migration-5c3b86816ece
https://github.com/npinos/d7migrate
https://www.drupal.org/docs/8/api/migrate-api/migrating-drupal-7-page-article-to-drupal-8-page-article
https://www.drupal.org/files/migrate_ya_example.tar_.gz

> drupal generate:module
> drupal generate:plugin:migrate:source  --module="blogger"  --class="Blogger"  --plugin-id="d7_blogger"  --table="bloggers"  --alias="bloggers"

You might need to add this

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids['id']['type'] = 'integer';
    return $ids;
  }

https://www.mediacurrent.com/blog/registering-migrations-drupal-8

I believe that groups are a feature of the migrate_plus module. In order for them to be recognised, the file needs to be named migrate_plus.migration_group.<group_name>.yml. The actual migrations also need to be formatted like this migrate_plus.migration.<migration_name>.yml.

https://drupal.stackexchange.com/questions/223536/how-do-i-write-migrate-group-files

Other notes:
to debug upgrade
drush mmsg upgrade_d7_file
devel/config
drush migrate-upgrade --legacy-db-key=migrate --configure-only
drush -l mysite.com mim --migrate-debug --limit=1 --update custom_product_image_step1
drush migrate-status --tag="Drupal 7"
https://www.drupal.org/project/migrate_pack
https://www.drupal.org/project/config_devel => to reload configuration

Examples:
  migrate-import --all                              Perform all...                                  
  migrate-import --tag=user,main_content            Import all...
  migrate-import classification,article             Import new terms...
  migrate-import beer_user --limit=2                Import no more...
  migrate-import beer_user --idlist=5               Import the user...
  migrate-import beer_user --limit=50 --feedback=20 Import 50 users...

put ksm($value) in respective migration plugin transform() function to see whats going on with migration. Run it in browser. Then you will see the ksm() results in browser.

example composer.json to begin with
{
    "name": "drupal/recommended-project",
    "description": "Project template for Drupal 8 projects with a relocated document root",
    "type": "project",
    "license": "GPL-2.0-or-later",
    "homepage": "https://www.drupal.org/project/drupal",
    "support": {
        "docs": "https://www.drupal.org/docs/user_guide/en/index.html",
        "chat": "https://www.drupal.org/node/314178"
    },
    "repositories": [
        {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        }
    ],
    "require": {
        "composer/installers": "^1.2",
        "cweagans/composer-patches": "^1.6",
        "drupal/backup_migrate": "^4.1",
        "drupal/core-composer-scaffold": "^8.8",
        "drupal/core-project-message": "^8.8",
        "drupal/core-recommended": "^8.8",
        "drupal/csv_importer": "^1.6",
        "drupal/devel": "^2.1",
        "drupal/entity_usage": "^2.0",
        "drupal/imce": "^1.7",
        "drupal/inline_entity_form": "^1.0",
        "drupal/migrate_devel": "^1.2",
        "drupal/migrate_manifest": "^1.9",
        "drupal/migrate_pack": "^1.0@alpha",
        "drupal/migrate_plus": "^4.2",
        "drupal/migrate_tools": "^4.5",
        "drupal/migrate_upgrade": "^3.1",
        "drupal/paragraphs": "^1.10",
        "drupal/pathauto": "^1.6",
        "drupal/redirect": "^1.5"
    },
    "require-dev": {
        "drush/drush": "^8.0.0"
    },
    "conflict": {
        "drupal/drupal": "*"
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "config": {
        "sort-packages": true
    },
    "extra": {
        "drupal-scaffold": {
            "locations": {
                "web-root": "web/"
            }
        },
        "installer-paths": {
            "web/core": ["type:drupal-core"],
            "web/libraries/{$name}": ["type:drupal-library"],
            "web/modules/contrib/{$name}": ["type:drupal-module"],
            "web/profiles/contrib/{$name}": ["type:drupal-profile"],
            "web/themes/contrib/{$name}": ["type:drupal-theme"],
            "drush/Commands/contrib/{$name}": ["type:drupal-drush"],
            "web/modules/custom/{$name}": ["type:drupal-custom-module"],
            "web/themes/custom/{$name}": ["type:drupal-custom-theme"]
        },
        "drupal-core-project-message": {
            "include-keys": ["homepage", "support"],
            "post-create-project-cmd-message": [
                "<bg=blue;fg=white>                                                         </>",
                "<bg=blue;fg=white>  Congratulations, you’ve installed the Drupal codebase  </>",
                "<bg=blue;fg=white>  from the drupal/recommended-project template!          </>",
                "<bg=blue;fg=white>                                                         </>",
                "",
                "<bg=yellow;fg=black>Next steps</>:",

                "  * Install the site: https://www.drupal.org/docs/8/install",
                "  * Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html",
                "  * Get support: https://www.drupal.org/support",
                "  * Get involved with the Drupal community:",
                "      https://www.drupal.org/getting-involved",
                "  * Remove the plugin that prints this message:",
                "      composer remove drupal/core-project-message"
            ]
        },
        "enable-patching": true,
        "patches": {
            "drupal/core": {
                "Expose Layout Builder data to REST and JSON:API": "https://www.drupal.org/files/issues/2019-11-21/layout_builder-export-content-2942975-67.patch"
            }
        }
    }
}

https://slideplayer.com/slide/16307056/
boilerplate:
https://github.com/thinktandem/migration_boilerplate

tips:
once doing drush migrate-upgrade --legacy-db-key=migrate --configure-only you can see the migration "Import from Drupal 7" at admin/structure/migrate

Use following command to update the migration
> drush mim --update udm_process_intro

Some commands:
1. To get the list of source migration plugins
> drush ev "print_r(array_keys(\Drupal::service('plugin.manager.migrate.source')->getDefinitions()));" 

Isssues and possible causes:

> drush mim <something>
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'sourceid1' at row 1

Explanation: Migration yml expected int and string was given
data_rows:
-
id: 1,

it should be
data_rows:
-
id: 1

Comments

  1. https://www.drupal.org/project/migrate_upgrade/issues/2996962#comment-12770565

    ReplyDelete
  2. https://www.drupal.org/project/migrate_upgrade/issues/2898028

    ReplyDelete
  3. https://www.drupal.org/project/drupal/issues/2985882

    ReplyDelete

Post a Comment

Popular posts from this blog

Programatically create layout builder section in Drupal

Code quality analysis of Drupal. Can I use Sonar?