Migrate and Table Wizard for CSV migration in Drupal

We have been exploring different ways to import CSV data into Drupal and found a nice workflow with the Migrate and Table Wizard module. There are still a few tricky parts to the process — documentation is often unclear or conflicting – and following is a description of our steps to import a CSV file into Drupal as nodes with CCK fields.
Migrating a CSV file into a Drupal content type
- Prune your CSV file so that it plays nice with Table Wizard. There are some tips in the README.txt for the Migrate module and I recommend you read this, especially if you would like to import taxonomy.
- Import the CSV file into a database table using 'Upload delimited files' at Administer > Content management > Table Wizard. You will likely get a message telling you that you need to define a primary key for the table:
data.csv has been imported into new table csv_data. All fields have initially been defined as text, with no indexing - it is important to use your database management software to set appropriate data types for each field, and to define a single (unique, non-null) field as the primary key. After you have done this, reanalyze the table.
- A primary key is a unique way to identify a row in your table, just like how Drupal uses a Node ID. There are a number of ways to assign a primary key for your data:
If you are using phpMyAdmin and your dataset already has a primary key, launch phpMyAdmin and make the following changes:
- go to the Structure view of your uploaded table
- change the data type of the column you wish to use as a key from longtext to a type that is finite (I used int(11))
- select the column you wish to use as a key and click on the little key icon to make it the primary key
If you don't have a primary key column in your data set, you can add one easily in phpMyAdmin. Create a new column (field), give it a name (something like 'rid'), make it of type INT, and make it auto increment (A_I).
Or you can run an SQL query to do everything for you:
ALTER TABLE `csv_data` ADD `rid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST - Now that your data has a primary key go back to Administer > Content management > Table Wizard and click on the 'Analysis' link for your data. On the Analysis page click 'Reanalyze'. If Table Wizard recognized your key, you will have a checkmark under 'PK' for your primary key's column.
- At this point, if you can bring up your data when you click 'View table contents' then Table Wizard understands your data.
- Go to Administer > Content management > Migrate > Content sets and add a content set:
- Give it a description
- choose the view that contains your data. It will probably be something like:
tw: csv_dataWhich was created by the Table Wizard - Set kind to 'node'
- choose the destination content type for your data
- This will bring up a form to map your data columns to the node fields. I set Published to a default value of 1 and then just mapped the title and body fields. Blank date fields will be populated with the current time and blank author fields will be filled in with the current user account.
- Go to Administer > Content management > Migrate > Process and let's create some nodes
- Select the checkbox in the Import column for the content set you created
- Hit Submit
If you are curious what each of the options does:
- Clear - deletes all imported nodes
- Import - imports once
- Scan - checks and imports new rows on a continuing basis via cron
- Go to Administer > Content management > Content and make sure you data was imported correctly. We're done!
The Migrate and Table Wizard modules provide a powerful way to work with data external to Drupal. Although this is a simple use case, I think it provides a good introduction to how you might utilize these modules. I'm excited to see this project mature and I hope this can help clarify the current process for others.





















Comments
node_import module?
Hi Tylor,
Have you tried the node_import module? If so, how was your experience with it and how does it compare with the method you describe above? http://drupal.org/project/node_import
Thanks.
re: node_import module?
Hey Ryan,
I just tried out node_import and it definitely is a simpler way to do this. It was intuitive, well documented, and did exactly what I wanted. Wish I had seen it sooner ;) One of the main advantages I still see with Migrate is that it tracks what it has imported and if you mess up, makes it easy to delete the imported nodes. Another thing I like is that Migrate works off a table in the database, providing a little more manipulation if needed. Finally, Migrate facilitates ongoing imports from a remote database, which wasn't helpful in this case but could be very useful. So, maybe Node_import for one time jobs, Migrate for more complex situations?
Thanks!
Thanks for such a great summary of the process, I've set up this instead of a FeedAPI solution and it's much better. The only thing we need is a way for the module to detect changes to existing imported rows, and import the changes. Do you know of a solution to this?
Hi Michael, Glad this post
Hi Michael,
Glad this post helped out with your project. From what I can tell the changes you've described are going to be somewhat custom. I think that if you can ensure that all your rows will pass through the normal migration process (take a look through migrate_content_process_import()), then you could use hook_migrate_import_$contenttype() to check for changes to your rows (see migrate.api.php for documentation).
I hope this can point you in the right direction. I haven't tried any code, but this is where I would start. Let us know how it goes!
Hi Tylor, Thanks for the
Hi Tylor,
Thanks for the great post. Its was very helpful to me. i have a question. I did as the post says. i can see the content page with the data (Administer -> Content management -> content.)what i need now is. i want the content page to be placed inside another page as a link.
For Example : i have content page called "Home". now i migrate the data.now i want all this content into "Home" content page as link.
Is there any solution for this? please feel free to ask any question if i'm not clear to you.
Thanks in advance :)