You are here

function image_update_5200 in Image 7

Same name and namespace in other branches
  1. 5.2 image.install \image_update_5200()
  2. 6 image.install \image_update_5200()

Move image files into their own table.

First update for the 5.2 branch, using the update naming convention layed out in: http://drupal.org/node/114774#update-n

File

./image.install, line 233

Code

function image_update_5200() {
  $ret = array();

  // Versions prior to 5 had an {image} table which is no longer used on 5.1
  // but not removed.
  if (db_table_exists('image')) {
    db_rename_table($ret, 'image', 'image_old');

    // Notify the user that their table has been moved.
    drupal_set_message('Your existing {image} table has been renamed {image_old}.');
  }
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {image} (\n          `nid` INTEGER UNSIGNED NOT NULL,\n          `fid` INTEGER UNSIGNED NOT NULL,\n          `image_size` VARCHAR(32) NOT NULL,\n          PRIMARY KEY (`nid`, `image_size`),\n          INDEX image_fid(`fid`)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {image} (\n          nid int_unsigned NOT NULL,\n          fid int_unsigned NOT NULL,\n          image_size VARCHAR(32) NOT NULL,\n          PRIMARY KEY (nid, image_size)\n        );");
      $ret[] = update_sql("CREATE INDEX {image_fid} on {image}(fid);");
      break;
  }

  // Copy image files records into the new table.
  drupal_load('module', 'image');
  $args = array_map('db_escape_string', array_keys(image_get_sizes()));
  $cond = " IN ('" . implode("', '", $args) . "')";

  // Upgrade from 5.x-1.x to 5.x-2.x.
  // Group by clause prevents duplicate insertions from bad data:
  // @see http://drupal.org/node/207557
  if (db_table_exists('file_revisions')) {
    $ret[] = update_sql("INSERT INTO {image} SELECT DISTINCT f.nid, f.fid, f.filename FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND f.filename" . $cond . " GROUP BY nid,filename");
    $ret[] = update_sql("DELETE FROM {file_revisions} WHERE EXISTS (SELECT * FROM {image} WHERE {image}.fid = {file_revisions}.fid)");
  }
  else {
    $ret[] = update_sql("INSERT INTO {image} SELECT DISTINCT u.nid, f.fid, f.filename FROM {upload} u INNER JOIN {files} f ON u.fid = f.fid INNER JOIN {node} n ON u.nid = n.nid WHERE n.type = 'image' AND f.filename" . $cond . " GROUP BY nid,filename");
    $ret[] = update_sql("DELETE FROM {upload} WHERE EXISTS (SELECT * FROM {image} WHERE {image}.fid = {upload}.fid)");
  }
  return $ret;
}