Post Carbon Institute

Skip to content

Reduce Consumption : Produce Locally


Jason Arnold's blog

Migrating Legacy Data to Drupal CCK Nodes Example

Submitted by Jason Arnold on June 3, 2008 - 11:42pm.

At tonight's Sonoma County Drupal User Group meeting, someone raised a question about migrating legacy content into Drupal CCK nodes. I figured that the question is best answered with an example, so I've cobbled together a contrived scenario with working sample code.

Imagine a table containing custom content, looking like so:


mysql> select * from custom_profile;
+-----+----------+------------+
| id | first | last |
+-----+----------+------------+
| 1 | Bilbo | Baggins |
| 2 | Frodo | Baggins |
| 34 | Peregrin | Took |
| 300 | Meriadoc | Brandybuck |
+-----+----------+------------+
4 rows in set (0.00 sec)

We are going to map the above table onto the CCK content type 'profile' shown here:

CCK Fields Screenshot

First, we import the custom table into the Drupal database, side by side with the Drupal tables. The main reason for this is to make it easy to access that table using the Drupal API. One way to do this import is from the command line:


mysql -u [user] -p [drupal_database] < custom_profile.sql

To actually run the code, we write a Drupal callback that executes the migration - this might be more cleanly done as a standalone PHP script that bootstraps a Drupal instance, but for the purposes of this example, it works fine.

We load up each row from the table (though the same ideas can work in a more complex scenario), build out a Drupal node, and save it to the database. And that's about it!

To actually run the migration, you invoke it through the browser by navigating to http://website.url/drupalroot/legacy_migration - this outputs some status information and also writes progress to the watchdog logs.

Working sample code is attached. I'd love to hear suggestions for improvements to this method.


Energy Farms Network  ·  Global Public Media  ·  Oil Depletion Protocol  ·  Post Carbon Cities  ·  Relocalization Network  ·  Solar Car Share
© 2004-2008 Post Carbon Institute. Post Carbon Institute is a 501(c)3 non-profit organization incorporated in the United States. Login