SlideShare a Scribd company logo
Migrating data into Drupal
using the migrate module


       Oxford DrupalCamp
       Friday 22 June, 2012

         Johan Gant
What's involved?
 Migrate me an army of
         data!




      Uh, oh!!
What's involved?
                Planning/analysis




Tech/dev work                       Acceptance




                                      Done!
Successful migrations
Move data from A to B in a planned, methodical manner.

Repeatable
Measurable
Work with realistic data

Safe
             Use the right tools for the task

      The complexity of your solution should be driven by
               the task at hand, not your tech
When stuff goes wrong...
Un...
          Safe
         Reliable
         Planned
          Done


Upset...
        Processes
         Clients
        Developers

                     RAGEBALLS FOR EVERYONE
Drupal migrate module
Provides a great framework for moving content into Drupal
Encourages good habits
Drush support
migrate_ui adds a nice front end
The Drupal way – so other Drupal devs can
pick up your work without too much bother.

Not a one stop shop for migrations


Use the right tools, or combination of
tools, to get the job done.


                                               Let's look at some code
<?php
class DemoMigration extends Migration {
  public function __construct() {
    parent::construct();
    $this->description = t('A sample migration module');

        // Define the map between your source and destination
        $this->map = new MigrateSQLMap(
          $this->machineName, // defaults to your migration class name
          array(
             'id' => array(
               'type' => 'int',
               ....
             ),                                                        http://drupal.org/node/1528934
          ),
          MigrateDestinationNode::getKeySchema();
        );

        // Define the fields from your source
        $source_fields = array(
          'id' => t('description'),
          'field1' => t('Node title'),
          .....
        );

        $query = db_select('source_table_name', 'sql-alias')
                   ->fields('alias', array_keys($source_fields))
                   ->orderBy('id', 'ASC');

        $this->source = new MigrateSourceSQL($query);

        // Define what sort of destination you have
        // - options include Nodes, Terms, Users and Comments
        $this->destination = new MigrateDestinationNode('node_machine_name');

        // Define your field mappings - nice options include default values (can include PHP),
        // groupings, callbacks – see beer.inc example class for details
        $this->addFieldMapping('source_body', 'field_body')
          ->defaultValue('Wibble'),
        .....
    }
}
/**
  * Useful function to help 'massage' your awkward source data into shape
  */
public function prepareRow($row) {
   // Convert ISO date to UNIX timestamp
   if ($row->created_at) {
     $row->created_at = strtotime($row->created_at);
   }
}


Handling taxonomy terms
/**
 * Term syntax/format a bit awkward,depends on whether you're using tid or name.
 * Migrate module will match on tid or name and handle the rest for you :)
 */
$this->addFieldMapping('source_col_name', 'terms')
  ->arguments(array('source_type' => 'name')), // use tid if you've got term ids
  ->separator('$$'); // Used to split long string of term names in source


Handling node reference fields

/**
  * Not documented when I used migrate, but expects a nid
  */
if ($row->some-identifier) {
   $row->reference_nid = my_own_function_to_lookup_a_nid($row->some_identifier);
}
Migration!


Rails/PostgreSQL > Drupal 6

Export data into CSV, import into Drupal db via table
wizard module
Pre-migration script to create image nodes
Run migration

Client demo and deployment
5 days from start to finish
Problems!


               Steep(ish) learning curve

    Documentation a bit sparse in places

         Awkward handling of taxonomy,
        node reference, and domain data

                   Pre-migration fudges

                           Performance
Should I use Drupal migrate?
Yes / absolutely / do it now                Probably not

●   Want to move a reasonable           ●   Low volume or low complexity
    volume of data INTO Drupal from
    MySQL/XML/JSON/CSV
                                        ●   Not familiar with coding

●   Have complex data mappings that
                                        ●   Already got something that works
    are best expressed                      well
    programmatically                    ●   Trying to move data OUT of Drupal
●   Make best use of Drupal tools and       to somewhere else
    existing code
●   Want to recycle code between
    projects
Drupal migrate - resources
●   Migrate module page -
    http://drupal.org/project/migrate
●   Economist migration (great summary) -
    http://drupal.org/node/915102
●   Torchbox Team Drupal blog -
    http://drupalblog.torchbox.com/

More Related Content

Similar to Migrating data into Drupal using the migrate module

Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011
Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011
Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011
camp_drupal_ua
 
Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.
Nelson Gomes
 

Similar to Migrating data into Drupal using the migrate module (20)

Automating Drupal Migrations
Automating Drupal MigrationsAutomating Drupal Migrations
Automating Drupal Migrations
 
Data migration to Drupal using Migrate Module
Data migration to Drupal using Migrate ModuleData migration to Drupal using Migrate Module
Data migration to Drupal using Migrate Module
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
 
Migrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Migrating to Drupal 8: How to Migrate Your Content and Minimize the RisksMigrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Migrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
 
Migrating data to drupal 8
Migrating data to drupal 8Migrating data to drupal 8
Migrating data to drupal 8
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
 
Migrate
MigrateMigrate
Migrate
 
Migrations
MigrationsMigrations
Migrations
 
Drupal content-migration
Drupal content-migrationDrupal content-migration
Drupal content-migration
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Spark Structured APIs
Spark Structured APIsSpark Structured APIs
Spark Structured APIs
 
Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011
Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011
Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
 
Drupal Module Development
Drupal Module DevelopmentDrupal Module Development
Drupal Module Development
 
Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
Getting Started with DrupalGap
Getting Started with DrupalGapGetting Started with DrupalGap
Getting Started with DrupalGap
 

Recently uploaded

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 

Migrating data into Drupal using the migrate module

  • 1. Migrating data into Drupal using the migrate module Oxford DrupalCamp Friday 22 June, 2012 Johan Gant
  • 2. What's involved? Migrate me an army of data! Uh, oh!!
  • 3. What's involved? Planning/analysis Tech/dev work Acceptance Done!
  • 4. Successful migrations Move data from A to B in a planned, methodical manner. Repeatable Measurable Work with realistic data Safe Use the right tools for the task The complexity of your solution should be driven by the task at hand, not your tech
  • 5. When stuff goes wrong... Un... Safe Reliable Planned Done Upset... Processes Clients Developers RAGEBALLS FOR EVERYONE
  • 6. Drupal migrate module Provides a great framework for moving content into Drupal Encourages good habits Drush support migrate_ui adds a nice front end The Drupal way – so other Drupal devs can pick up your work without too much bother. Not a one stop shop for migrations Use the right tools, or combination of tools, to get the job done. Let's look at some code
  • 7. <?php class DemoMigration extends Migration { public function __construct() { parent::construct(); $this->description = t('A sample migration module'); // Define the map between your source and destination $this->map = new MigrateSQLMap( $this->machineName, // defaults to your migration class name array( 'id' => array( 'type' => 'int', .... ), http://drupal.org/node/1528934 ), MigrateDestinationNode::getKeySchema(); ); // Define the fields from your source $source_fields = array( 'id' => t('description'), 'field1' => t('Node title'), ..... ); $query = db_select('source_table_name', 'sql-alias') ->fields('alias', array_keys($source_fields)) ->orderBy('id', 'ASC'); $this->source = new MigrateSourceSQL($query); // Define what sort of destination you have // - options include Nodes, Terms, Users and Comments $this->destination = new MigrateDestinationNode('node_machine_name'); // Define your field mappings - nice options include default values (can include PHP), // groupings, callbacks – see beer.inc example class for details $this->addFieldMapping('source_body', 'field_body') ->defaultValue('Wibble'), ..... } }
  • 8. /** * Useful function to help 'massage' your awkward source data into shape */ public function prepareRow($row) { // Convert ISO date to UNIX timestamp if ($row->created_at) { $row->created_at = strtotime($row->created_at); } } Handling taxonomy terms /** * Term syntax/format a bit awkward,depends on whether you're using tid or name. * Migrate module will match on tid or name and handle the rest for you :) */ $this->addFieldMapping('source_col_name', 'terms') ->arguments(array('source_type' => 'name')), // use tid if you've got term ids ->separator('$$'); // Used to split long string of term names in source Handling node reference fields /** * Not documented when I used migrate, but expects a nid */ if ($row->some-identifier) { $row->reference_nid = my_own_function_to_lookup_a_nid($row->some_identifier); }
  • 9. Migration! Rails/PostgreSQL > Drupal 6 Export data into CSV, import into Drupal db via table wizard module Pre-migration script to create image nodes Run migration Client demo and deployment 5 days from start to finish
  • 10. Problems! Steep(ish) learning curve Documentation a bit sparse in places Awkward handling of taxonomy, node reference, and domain data Pre-migration fudges Performance
  • 11.
  • 12. Should I use Drupal migrate? Yes / absolutely / do it now Probably not ● Want to move a reasonable ● Low volume or low complexity volume of data INTO Drupal from MySQL/XML/JSON/CSV ● Not familiar with coding ● Have complex data mappings that ● Already got something that works are best expressed well programmatically ● Trying to move data OUT of Drupal ● Make best use of Drupal tools and to somewhere else existing code ● Want to recycle code between projects
  • 13. Drupal migrate - resources ● Migrate module page - http://drupal.org/project/migrate ● Economist migration (great summary) - http://drupal.org/node/915102 ● Torchbox Team Drupal blog - http://drupalblog.torchbox.com/