You are here

function comment_upload_update_6000 in Comment Upload 6

Move all data in {comment_upload_files} to {files} and {comment_upload}.

Implementation of hook_update_N().

File

./comment_upload.install, line 168
The install file that defines the tables in use by comment_upload.

Code

function comment_upload_update_6000() {
  global $db_url;
  $schema = array();
  $schema['comment_upload'] = array(
    'description' => t('Stores uploaded file information and table associations.'),
    'fields' => array(
      'fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('Primary Key: The {files}.fid.'),
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {node}.nid of the comment the uploaded files is associated with.'),
      ),
      'cid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {comment}.cid associated with the uploaded file.'),
      ),
      'description' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => t('Description of the uploaded file.'),
      ),
      'list' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => t('Whether the file should be visibly listed on the comment: yes(1) or no(0).'),
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => t('Weight of this upload in relation to other uploads - determines the order.'),
      ),
      'legacy_fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The file ID from the Drupal 5 version, if applicable.'),
      ),
    ),
    'primary key' => array(
      'fid',
    ),
    'indexes' => array(
      'cid_fid' => array(
        'cid',
        'fid',
      ),
      'nid' => array(
        'nid',
      ),
    ),
  );
  $ret = array();
  db_create_table($ret, 'comment_upload', $schema['comment_upload']);
  if (substr($db_url, 0, 5) == 'mysql') {
    $max_file_id = db_result(db_query("SELECT MAX(fid) FROM {files}"));
    $offset = $max_file_id + 1;
    db_query("INSERT INTO {files} (fid, uid, filename, filepath, filemime, filesize, status, timestamp) SELECT cuf.fid + %d, c.uid, cuf.filename, cuf.filepath, cuf.filemime, cuf.filesize, 1, c.timestamp FROM {comment_upload_files} cuf INNER JOIN {comments} c ON cuf.cid = c.cid", $offset);
    $max_file_id = db_result(db_query("SELECT MAX(fid) FROM {files}"));
    db_query("ALTER TABLE {files} AUTO_INCREMENT = %d", $max_file_id + 1);
    db_query("INSERT INTO {comment_upload} (fid, nid, cid, description, list, legacy_fid) SELECT cuf.fid + %d, cuf.nid, cuf.cid, cuf.description, cuf.list, cuf.fid FROM {comment_upload_files} cuf", $offset);
  }
  else {
    $result = db_query("SELECT cuf.*, c.uid FROM {comment_upload_files} cuf INNER JOIN {comments} c ON cuf.cid = c.cid");
    while ($file = db_fetch_object($result)) {
      db_query("INSERT INTO {files} (filename, filepath, filemime, filesize, uid, status) VALUES ('%s', '%s', '%s', %d, %d, %d)", $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->uid, 1);
      $fid = db_last_insert_id('files', 'fid');
      db_query("INSERT INTO {comment_upload} (fid, nid, cid, description, list) VALUES (%d, %d, %d, '%s', %d)", $fid, $file->nid, $file->cid, $file->description, $file->list);
    }
  }
  db_drop_table($ret, 'comment_upload_files');
  return $ret;
}