Migrating from D6 upload.module to filefield.module

So building on my last post for creating CCK fields, here's some code I whipped up to migrate from the D6's core upload.module to the filefield.module. This isn't general purpose code but might help someone else out. The catch is I'd built a video node with and was using the upload module to attach exactly two files, an image and a video. The new node will have separate thumbnail and video fields. If you'll be moving to a multi-value field this code won't work for you.

The gist is the same as before, setup your field for video and your field for images then export using:

<?php
var_export
(content_fields('field_web_video', 'video'), 1);
?>
and
<?php
var_export
(content_fields('field_video_thumb', 'video'), 1);
?>

Then roll that into an update function that also moves the file data around in the database. Code is after the jump.

<?php
/**
* Add filefields to the video nodes and migrate the files.
*/
function foo_video_update_6000() {
 
// Make sure the filefield* modules are installed correctly.
 
drupal_install_modules(array('filefield', 'filefield_image', 'filefield_imagecache'));
 
drupal_flush_all_caches();

 
module_load_include('inc', 'content', 'includes/content.admin');
 
content_alter_db_cleanup();
 
 
// Need to load the CCK include file where content_field_instance_create() is defined.
 
module_load_include('inc', 'content', 'includes/content.crud');
 
 
$thumb_field = array (
//
// DROPPED THE CCK FIELD DEFINITION FROM HERE
//
 
);
 
content_field_instance_create($thumb_field);

 
$video_field = array (
//
// DROPPED THE CCK FIELD DEFINITION FROM HERE
//
 
);
 
content_field_instance_create($video_field);

 
 
// Migrate the videos
 
$fids = array();
 
$result = db_query("SELECT n.nid, n.vid, f.fid, u.description, u.list FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid INNER JOIN {node} n ON u.vid = n.vid WHERE n.type = 'video' AND filemime LIKE 'video/%'");
  while (
$file = db_fetch_object($result)) {
   
$fids[] = $file->fid;
   
// Check for a record... it adds a bunch more queries but it's simple and we only run this once.
   
if (db_result(db_query("SELECT COUNT(*) FROM {content_type_video} WHERE vid = %d", $file->vid))) {
     
db_query("UPDATE {content_type_video} SET field_web_video_fid = %d, field_web_video_description = '%s', field_web_video_list = %d WHERE vid = %d",
       
$file->fid, $file->description, $file->list, $file->vid);
    }
    else {
     
db_query("INSERT INTO {content_type_video} (nid, vid, field_web_video_fid, field_web_video_description, field_web_video_list) VALUES (%d, %d, %d, '%s', %d)",
       
$file->nid, $file->vid, $file->fid, $file->description, $file->list);
    }
  }
 
db_query("DELETE FROM {upload} WHERE fid IN (". db_placeholders($fids, 'int') .")", $fids);

 
// Migrate the images
 
$fids = array();
 
$result = db_query("SELECT n.nid, n.vid, f.fid, u.description, u.list FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid INNER JOIN {node} n ON u.vid = n.vid WHERE n.type = 'video' AND filemime LIKE 'image/%'");
  while (
$file = db_fetch_object($result)) {
   
$fids[] = $file->fid;
   
// Check for a record... it adds a bunch more queries but it's simple and we only run this once.
   
if (db_result(db_query("SELECT COUNT(*) FROM {content_type_video} WHERE vid = %d", $file->vid))) {
     
db_query("UPDATE {content_type_video} SET field_video_thumb_fid = %d, field_video_thumb_description = '%s', field_video_thumb_list = %d WHERE vid = %d",
       
$file->fid, $file->description, $file->list, $file->vid);
    }
    else {
     
db_query("INSERT INTO {content_type_video} (nid, vid, field_video_thumb_fid, field_video_thumb_description, field_video_thumb_list) VALUES (%d, %d, %d, '%s', %d)",
       
$file->nid, $file->vid, $file->fid, $file->description, $file->list);
    }
  }
 
db_query("DELETE FROM {upload} WHERE fid IN (". db_placeholders($fids, 'int') .")", $fids);

 
// No more uploads on video nodes!
 
variable_set('upload_video', 0);

  return array();
}
?>

Update: I posted some additional info on this topic over on: http://drupal.org/node/292904

the d6 module is very

Thanks for module, i like it

Thanks for module, i like it

Mitul from Technology Blog

You have a nice blog … I liked your blog very much. The contents are worth reading and I’ve found much valuable information from your post. My learning curve is increasing :-). Thanks for sharing such a nice post with us. Keep it up.
Mitul from Technology Blog

D6

Thanks for D6 module
porno | sikis | porno

Review

The update procedure described here is designed to avoid code version conflicts during update or migration. In it, you will leave a copy of your entire site intact on your server, as is (as a backup), and copy only site configuration and data files into a new Acquia Drupal code base. Actually I am a teacher and running an academy where I teach about the 70-294 exam exam and 650-575 exam exam as well, there are many students out there and it is difficult to manage that all because some of my students are also preparing for the 70-291 exam. So don't have much time but whenever I am free, I try to get back here.

What is Reality? Is there

What is Reality? Is there something out there, or are we all figments of your imagination? Science fiction fans will recognize in such questions the theme from movies like the Matrix or books such as Permutation City (Greg Egan), which have addressed this issue in a brilliant, thought- provoking and very entertaining way. Believe it or not, this question has now transcended both fancy and philosophy and is an active area of research with a potentially tremendous social impact.

Regards,
Research Paper Writing

Wijnand Ijsselsteijn is

Wijnand Ijsselsteijn is assistant professor at the Human Technology Interaction Group of the Eindhoven University of Technology.

He is currently working on embodiment, bodily identification, and especially how non biological props and tools can become part of our body.
Custom Term Paper AND Custom Thesis

The academy will be held on

The academy will be held on Wednesday's beginning today. The focus is to provide a greater understanding of the police department's operations.

Regards,
Academic research writing AND Dissertation Writing

They should have taken

They should have taken backup from a third party service , not to loose the market. örgü modelleri