Tuesday, November 29, 2016

Migration Tutorial: Files Migration from Drupal 7 to Drupal 8

As per Wikipedia Data Migration is the process of transferring data from one system to another while changing the storage, database or application. In reference to the ETL (Extract-Transform-Load) process, data migration always requires at least Extract and Load steps.
 Like Migrating Data from Drupal 7 database to Drupal 8 database. We can perform Migration on file systems.
Migration can also happen to same (Drupal 7 to Drupal 7) or in between two other systems.

Where Upgrade is the process of replacing your existing software with a newer version of the same product. Like Upgrading your site from Drupal 7 to Drupal 8 version.
 
Here We'll focus on Migration of NODE or Content Type. Before we start reading about the NODE migration process we must be familiar with some basic things.
Please be sure that character sets (encoding) are set as same of Migration Source Data and Migration Destination Data like UTF-8.
Migration basically happens with three major steps i.e analyzing source data, prepare destination data structure and the process stays in between.
Say we are developing a module as Custom Migration module with Module key as "migrate_custom".
And yes we'll require to develop this for Drupal 8 :) by maintaining its standard.
migrate_custom will have some dependencies with other modules like Migrate (migrate), Migrate Plus (migrate_plus), Migrate Tools (migrate_tools), Migrate Drupal ...


Lets start with Files Migration with Code Example:
Our aim is to migrate all records from d7.file_managed to d8.file_managed through a command like
$ drush migrate-import custom_file --limit="500 items"
Ad per Drupal 8 standard we need a yml file to define everything and a php file to process that yml.
Here is there file/ folder structure
migrate_custom\src\Plugin\migrate\source\File.php
migrate_custom\config\install\migrate_plus.migration.custom_file.yml

In .yml file we define four major parts. These are id, source, process and destination. Id is to identify this Migration process. Source is defined with the data source. Process define basically the mapping of fields in between destination and source.

id: custom_file
source:
  plugin: custom_file
process:
  fid: fid
  filename: filename
  uri: uri
  uid:
    -
      plugin: default_value
      default_value: 1
destination:
  plugin: entity:file
status: true
migration_group: custom
dependencies:
  module:
    - file
label: Migrate D7 Files
migration_dependencies: {}

Inside File.php class File extends SqlBase { } with four basic public functions these are
1. query() to fetch data from the source.
2. fields() to defined needed fields.
3. getIds() to get and return fid of File.
4. prepareRow() is basically to set source Property. Here we are settings uri.

A working copy of this Migration is available at Github.

No comments:

Post a Comment