Hi there,

I reacently wrote a piece of code to migrate from upload to filefield. Then I generalized it, although still needs some work. It is not a drush command yet but can easily be done. Perhaps it has room in drush_extras.

function upload2filefield($type, $field) {
  print "upload2filefield for $type - $field\n";
  $field = content_fields($field, $type);
  $dbinfo = content_database_info($field);

  $rsc = db_query("SELECT * FROM {upload} u LEFT JOIN {node} n ON n.nid=u.nid WHERE n.type='%s' ORDER BY n.vid, u.weight, u.fid", $type);
  $vid = FALSE;
  while ($file = db_fetch_array($rsc)) {
    $attachment = new stdClass();
    $attachment->vid = $file['vid'];
    $attachment->nid = $file['nid'];
    $attachment->{$dbinfo['columns']['fid']['column']} = $file['fid'];
    $attachment->{$dbinfo['columns']['list']['column']} = $file['list'];
    $attachment->{$dbinfo['columns']['data']['column']} = array('description' => $file['description']);

    // Single-valued fields are stored in the content-type's table so we
    // will update.
    if ($field['multiple'] == 0) {
      $update = 'vid';
    }
    // Multivalued fields use a separate table.
    else {
      $update = NULL;
      if ($vid != $file['vid']) {
        $delta = 0;
        $vid = $file['vid'];
      }
      else {
        $delta++;
      }
      $attachment->delta = $delta;
    }

    if (!drupal_write_record($dbinfo['table'], $attachment, $update)) {
      drush_print(dt("Convert failed for file !f in node !n", array('!f' => $file['fid'], '!n' => $file['nid'])));
    }
    else {
      // Cleanup.
      db_query('DELETE FROM upload WHERE fid = %d', $file['fid']);
    }
  }
}

usage: upload2filefield('my_content_type', 'field_my_filefield');

Comments

greg.1.anderson’s picture

This is pretty cool; even better would be a facility to import a new file that was not yet uploaded to the Drupal site. The upload migration command could call through to the file upload command.