Hi guys

wanting to programmaticaly move a custom node type (music) from my drupal 6 to drupal 7 i have the following script, what it does it connects the drupal 6 and 7 databases to a single drupal 7 install. it then pulls the data from drupal6 and stores it in drupal 7.

The script below succesfuly brings over the music content types and saves the title and node body, but some extra fields i cant figure out how to bring over, any advice would be appreciated.

my drupal music node type has:

Title
Body
CCK Image field
File field mp3 field
Taxonomy (artist name)

As mentioned the below script brings title and body ok, how can i bring in the rest? in my code i have tried saving the taxonomy but im not sure why the code doesnt bring it over.

I first edit settings.php on D7 so it connects to both databases:

<?php
$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'drupal7',
      'username' => 'root',
      'password' => '<your-password>',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),'custom' => 
  array (
    'default' => 
    array (
      'database' => 'custom',
      'username' => 'root',
      'password' => '<your-password>',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    )
  )
);
?>

Then i create a node.php in my root D7 install and run this:

define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
menu_execute_active_handler();

db_set_active('custom');
            $result = db_query("SELECT uid, nid, title, created, changed, promote, sticky, vid FROM {node} WHERE type = 'music' ORDER BY nid ASC ");
            db_set_active('default');
foreach (

$result as $record) {
                $nid = db_insert('node') // Table name no longer needed {}
                                ->fields(array(
                                    'nid' => $record->nid,
                                    'vid' => $record->vid,
                                    'type' => 'music',    // 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('custom');
            $result = db_query("SELECT nid, vid, uid, title, body, teaser FROM {node_revisions} ORDER BY nid ASC");
            $bundle = 'music'; // 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 needed {}
                                ->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 needed {}
                                ->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();
            }

          
function migrate_post_node() {
    db_set_active('custom');
    $result = db_query("SELECT uid, nid, title, created, changed, promote, sticky, vid FROM {node} WHERE type = 'music' ORDER BY nid ASC ");
    db_set_active('default');
    foreach(

$result as $record){
        /* PICKING TERMS , you can query into your old taxonomy and get tid */
        $term = 8;

        if(empty(

$record)){
            return false;
        }else{
            db_set_active('custom');
            $revisions = db_select('node_revisions')->fields('node_revisions')->condition('nid',$record->nid)->range(0,1)->execute()->fetchObject();
            db_set_active('default');
           

$node = new stdClass();
            $node->title = $record->title;
            $node->type = 'music';
            $node->created = $record->created;
            $node->changed= $record->changed;
            $node->comment = 1;
            $node->status = 1;
            $node->promote = 1;
            $node->sticky = 0;
            $node->uid = $record->uid;
            $node->language = 'und';
            $node->timestamp = $revisions->timestamp;
            $node->revision = 0;
            $node->body['und'][0]['summary'] = $revisions->teaser;
            $node->field_artist_tax['und'][0] = array('tid' => $term);
            $node->body['und'][0]['value'] = $revisions->body;
            $node->body['und'][0]['format'] = 'full_html';
           
$node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
node_submit($node);
            node_save($node);       
        }
    }
    echo

'1';
}
echo

"SUCCES IMPORTED DATA ! ";

Comments

prezaeis’s picture

forgot to mention, prior to running the script you need to truncate some tables in the drupal 7 install.
run the following script:

truncate apachesolr_search_node; truncate cache_field; truncate cache_menu; truncate field_data_body; truncate field_data_field_tags; truncate field_revision_body; truncate field_revision_field_tags; truncate node; truncate node_comment_statistics; truncate node_revision; truncate taxonomy_index; truncate url_alias; truncate field_revision_body; truncate field_data_body; truncate node_revision; truncate node;
prezaeis’s picture

sorry to bump

fbrooks’s picture

CCK has a built-in content migration component.

http://drupal.org/node/1144136