Dissecting Migration Via the migrate_example_baseball

  • John Boranprasit's picture
    January 29, 2016

Going through my first migration was a challenge. In my most recent project I had to move files, sql databases and copy html  from URLS. 3 Different types of migrations using various libraries and of course the Migrate module contributed to Drupal.org. In this post I would really like to share the challenges that encountered and considerations you must account for when doing a Migration. There are no example as you can find that anywhere, but this is more conceptual to compliment the example code that you will read. As a reference point you can use the example in the migrate module inside the folder migrate_example_baseball


How does Migrate work

Migration is built to iterate through elements safely one record at a time. When a migration is defined, the Migration will show that it is registered under admin > content > migrate. When you are done defining your migration you would go to this section of your site and run your migration. From this panel, you can do all sorts of really helpful functions like:

  • Records per migration - Shows (even before) the number of records in a migration
  • Rollback - Functionality to rollback any changes from a previous release
  • Update - Instead of creating nodes you can update nodes if that is all you want to do. You can also select how many records you would like to delete

When a migration is selected and imported, Drupal then run goes through each record of the source ( IE - files in a folder, records in a table/database, HTML source pulled from a site), follows the mapping  to copy the source field to the destination field, and then does any post processing either when a row is updated or when the an entity/node is created. All migrations will have the following: 

Migrate definition (See migrate_example_baseball_node_info in migrate_example_baseball.features.inc)

Source (See GameBaseball in migrate_example_baseball.migrate.inc)

Map (See GameBaseball in migrate_example_baseball.migrate.inc)

Process a record (See GameBaseball in migrate_example_baseball.migrate.inc)