Migrating Legacy Data to Drupal CCK Nodes Example
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:

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.
| Attachment | Size |
|---|---|
| legacy_migration-5.x-0.1.tar.gz | 1.82 KB |












I can picture this growing into a general purpose module with,
1) an admin interface for selecting, checking, and creating tables, fields, data types, etc., and
2) the ability to export CCK data into XML or other standard format for easy migration of CCK customizations between sites.
Post new comment