Custom MySQL Database to Drupal 8 Migration
There are thousands of websites which are using MySQL as database. If
we need to migrate data from Websites, which are using MySQL database,
to Drupal 8 then we can use this blog post as starting point.
We have a sample MySQL Database 'mysql_custom_db' which contains two tables, users and pages. The data in 'users' table looks like below,
As all the fields in 'users' table, from source database, will map to User entity in Drupal 8 site so we don't need make any changes there. But as we are migrating 'pages' table data to 'Basic Page' content type in Drupal 8 site, so we need to create required fields in 'Basic Page' content type.
We have created the fields in 'Basic Page' content type and it looks like below,
In our migration, field mapping between source site (pages) and destination site (Basic Page' content type) is like below,
city + state => Place
site_name + site_url => URL (title + url)
keywords => Tags
Similarly, you may need to complete site building part in your Drupal 8 site before you can proceed to migration.
Firstly we need to access source database from our Drupal 8 site so that we can migration the content. To do this, we have imported the source database on our system and added connection details to settings.php. It looks like below,
Please note the 'migrate' key above. With this, our source plugins would be able to directly query source database as those would be extended from 'SqlBase' source migration class.
Next, we have created a custom module 'migrate_sql_d8_custom' in D8 site for the migration. We have defined Migration Configuration Entity for Users in migrate_plus.migration.users.yml which looks like below,
It contains mapping between source user's data and destination user's data under 'process' key. We have used available core/contrib Process plugin for status / roles / name / created fields above so that data can be formatted / converted to desired format. Destination for user's data would be Users Entity so in built destination plugin can be used. To read source user's data, we have created new custom source plugin, Users.php, which will query users table from source database and define the primary key details. It looks like below,
Next we have created Migration Configuration Entity for Pages in migrate_plus.migration.pages.yml which looks like below,
It's quite similar to Users migration configuration entity (.yml) file. Process key contains mapping between source pages data and destination entity i.e. 'Basic Page' content type. We have also defined migration dependency to 'users' as we have to run 'users' migration first because user id is being referred in Pages migration. If we will try to run 'pages' migration before 'users' migration then it will indicate that we have to run 'users' migration first.
To read source pages table data, we have created new custom source plugin, Pages.php, which will query pages table from source database and define the primary key details. It looks like below,
After defining migration configuration entities for users and pages along with their source plugins, we are ready to start the migration. We have used following drush commands (migrate-statue, migrate-import) to migrate the source MySQL database data to Drupal 8,
User data has been migrated to Users Entity in Drupal 8 and it looks like below,
Pages data has been migrated to Node Entity in Drupal 8 and it looks like below,
The source code is also available on Git repository.
We have a sample MySQL Database 'mysql_custom_db' which contains two tables, users and pages. The data in 'users' table looks like below,
The data in 'pages' table looks like below,
As all the fields in 'users' table, from source database, will map to User entity in Drupal 8 site so we don't need make any changes there. But as we are migrating 'pages' table data to 'Basic Page' content type in Drupal 8 site, so we need to create required fields in 'Basic Page' content type.
We have created the fields in 'Basic Page' content type and it looks like below,
In our migration, field mapping between source site (pages) and destination site (Basic Page' content type) is like below,
city + state => Place
site_name + site_url => URL (title + url)
keywords => Tags
Similarly, you may need to complete site building part in your Drupal 8 site before you can proceed to migration.
Firstly we need to access source database from our Drupal 8 site so that we can migration the content. To do this, we have imported the source database on our system and added connection details to settings.php. It looks like below,
Please note the 'migrate' key above. With this, our source plugins would be able to directly query source database as those would be extended from 'SqlBase' source migration class.
Next, we have created a custom module 'migrate_sql_d8_custom' in D8 site for the migration. We have defined Migration Configuration Entity for Users in migrate_plus.migration.users.yml which looks like below,
It contains mapping between source user's data and destination user's data under 'process' key. We have used available core/contrib Process plugin for status / roles / name / created fields above so that data can be formatted / converted to desired format. Destination for user's data would be Users Entity so in built destination plugin can be used. To read source user's data, we have created new custom source plugin, Users.php, which will query users table from source database and define the primary key details. It looks like below,
Next we have created Migration Configuration Entity for Pages in migrate_plus.migration.pages.yml which looks like below,
It's quite similar to Users migration configuration entity (.yml) file. Process key contains mapping between source pages data and destination entity i.e. 'Basic Page' content type. We have also defined migration dependency to 'users' as we have to run 'users' migration first because user id is being referred in Pages migration. If we will try to run 'pages' migration before 'users' migration then it will indicate that we have to run 'users' migration first.
To read source pages table data, we have created new custom source plugin, Pages.php, which will query pages table from source database and define the primary key details. It looks like below,
After defining migration configuration entities for users and pages along with their source plugins, we are ready to start the migration. We have used following drush commands (migrate-statue, migrate-import) to migrate the source MySQL database data to Drupal 8,
User data has been migrated to Users Entity in Drupal 8 and it looks like below,
Pages data has been migrated to Node Entity in Drupal 8 and it looks like below,
The source code is also available on Git repository.










Comments
exception 'Drupal\Core\Config\UnsupportedDataTypeConfigException' with message 'Invalid data type in config migrate_plus.migration.pages, found in [error]
filemodules/custom/migrate_sql_d8_custom/config/install/migrate_plus.migration.pages.yml : yaml_parse(): scanning error encountered during parsing: block sequence entries are not allowed in this context (line 14,
column 16)' in /var/www/drupalvm/projects/project_name/core/lib/Drupal/Core/Config/FileStorage.php:117
We have tried it again with latest Drupal core 8.4.x and it works fine. Otherwise please share the .yml file and we might be able to help you.