How to moving database node from Drupal 6 into Drupal 7 Part I

I will make several post to moving database from Drupal 6 into Drupal 7. Which it include with nodes, taxonomy, users, comments, url alias and many else. I will make it part by part, so you can follow it easily. In this first part, i will show how to setup multiple database connection and importing nodes from old into new databases. This post is good for you that want to full migrate from drupal 6 into drupal 7 without upgrading or update. We will use importing from database. So, watch this steps :

1. Make database multiple connection
You have drupal-6 and drupal-7 database. The logic, you will load drupal-6 database into your drupal 7 websites.
Then you can make script to migrate content from the old database into new database. Follow this for Make database multiple connection in Drupal 7

2. Scripting for moving node & node_revisions into Drupal 7
I assumed you have old as name connection into Drupal 6 database. In this example, i will show how to import story.
You can change into page or your custom type. You can put this script into your modules or themes. Remember to TRUNCATE node, node_revision, field_data_body and field_revision_body.

<?php
db_set_active
('old');
           
$result = db_query("SELECT uid, nid, title, created, changed, promote, sticky, vid FROM {node} WHERE type = 'story' ORDER BY nid ASC ");
           
db_set_active('default');

foreach (

$result as $record) {
               
$nid = db_insert('node') // Table name no longer needs {}
                               
->fields(array(
                                   
'nid' => $record->nid,
                                   
'vid' => $record->vid,
                                   
'type' => 'article',    // default type in drupal 7
                                   
'language' => 'und'// default language in drupal 7
                                   
'title' => $record->title,
                                   
'uid' => $record->uid
                                   
'status' => '1',
                                   
'created' => $record->created,
                                   
'changed' => $record->changed,
                                   
'comment' => '0',   // 0 for no comment, 2 for allowing commenting
                                   
'promote' => '1',
                                   
'sticky' => '0',
                                   
'tnid' => '0',
                                   
'translate' => '0',
                                ))
                                ->
execute();

               

$nid = db_insert('node_revision') // Table name no longer needs {}
                               
->fields(array(
                                   
'nid' => $record->nid,
                                   
'vid' => $record->nid,
                                   
'log' => '',
                                   
'title' => $record->title,
                                   
'uid' => $record->uid,
                                   
'status' => '1',
                                   
'timestamp' => $record->changed,
                                   
'comment' => '0',
                                   
'promote' => '1',
                                   
'sticky' => '0',
                                ))
                                ->
execute();
            }

           

db_set_active('old');
           
$result = db_query("SELECT nid, vid, uid, title, body, teaser FROM {node_revisions} ORDER BY nid ASC");
           
$bundle = 'article'; // Default type in Drupal 7
           
$entity_type = 'node';  
           
db_set_active('default');

foreach (

$result as $record) {
               
$nid = db_insert('field_data_body') // Table name no longer needs {}
                               
->fields(array(
                                   
'entity_type' => $entity_type,
                                   
'bundle' => $bundle,
                                   
'deleted' => '0',
                                   
'entity_id' => $record->nid,
                                   
'revision_id' => $record->nid,
                                   
'language' => 'und',
                                   
'delta' => '0',
                                   
'body_value' => $record->body,
                                   
'body_summary' => $record->teaser,
                                   
'body_format' => 'full_html'       // filtered_html or full_html
                               
))
                                ->
execute();

$nid = db_insert('field_revision_body') // Table name no longer needs {}
                               
->fields(array(
                                   
'entity_type' => $entity_type,
                                   
'bundle' => $bundle,
                                   
'deleted' => '0',
                                   
'entity_id' => $record->nid,
                                   
'revision_id' => $record->nid,
                                   
'language' => 'und',
                                   
'delta' => '0',
                                   
'body_value' => $record->body,
                                   
'body_summary' =>  $record->teaser,
                                   
'body_format' => 'full_html'
                               
))
                                ->
execute();
            }

echo

"SUCCES IMPORTED DATA ! ";
?>


Next step, i will show you to import taxonomy and tags. Cheers!

Updated!

Because a few people always asking me how to execute this scripts and seem some people have difficult days, I try to easy the pain :)

First, put this code completely ( including with php tags ) into your themes you USE. For instance, you using Bartik themes for your Drupal web, so open page.tpl.php and put this on "bottom" line of files. Remember, we want Drupal execute this script. So, open your website (front page or something) that need "page.tpl.php" loaded. When page.tpl.php loaded, it will execute this script automatically. If you have a lot of data need to be migrate, then you will get slow loading page which indicating a transfer process.

Also, i put "SUCCES IMPORTED DATA ! " on last line, which is will show up on your bottom page (or view page source maybe).

If you think this not works, please check your database settings. Do simple things that make query on multiple databases using db_set_active() and show it results into page.tpl.php for easy debug :

For intance, we will make query into 'old' database. Put this on bottom of page.tpl.php ( again, with php tags ) :

<?php
// query into drupal6 database
db_set_active('old');
$result = db_query("SELECT uid, nid, title, created, changed, promote, sticky, vid FROM {node} WHERE type = 'story' ORDER BY nid ASC ");
var_dump($result)
db_set_active('default');
// Do another test query here...
?>

Still confuse ?



You should follow me here

Comments

I created the db connection and the php file on the modules folder. I ran it (name of file is story.php and user.php to move the users.
but it didnt move the content to drupal 7

Not sure if you doing right. But try to use this on themes. When you called some page, this function will work automatically.

It should be works.

Ok, the nodes are in my drupal database. When I go to the field_revision_body table, I can see the texts. But If I go to the site, I only see the titles and nothing else. Even in edit mode I can't see the text.

You do need to put this script inside a module or a theme. A separate page like this won't work.

Correction: Only the first 103 nodes are uploaded correctly. The next 100 nodes have only a title and the text is not uploaded (while it certainly is in the old database).

yes, it true this script should place into themes or modules.

Please check your server load, mysql timeout, and php timeout. I guess it because because your server get overload and stop when in the middle of migrating process.

Hi
I've put the script onto /themes/bartik/import.php but nothing happen and I can't get launching the script by something like http://localhost/import.php
Should you give more information on the way to launch the script ??
Thanks an very best for the script
D.

As i explained, you can put this script into your modules or themes.

In your case, you put this into your Bartik page.tpl.php and reload your page.
You can modified the script to print "success" results after executing database.

Doesn't work.
Perhaps a step by step by step ?

I did exactly as you suggest, copy the script into the top of page.tpl.php
The whole site fails to display anything.
Removed the script from page.tpl.php and things work again, so
I copied the script without the <?php and ?> and the page displays, but the
script didn't produce any results, anywhere. No "finished" message, no anything.
Entries are not created in the database.

I've now tried everything I can think of, and more, ( execute PHP in a block from Google ) and it simply doesn't work.
Trying to pull users from a D6 database called drupal-test and put them into the default database called D7
Apparently, much, much more detail ( which lines in page.tpl.php ? ) is needed in the explanation of how to make the script execute, and how to tell if it can access the second database ?
Great idea that would make my life much much easier *if* it worked for me.

Add new comment