You are here

function system_update_173 in Drupal 5

Same name and namespace in other branches
  1. 4 database/updates.inc \system_update_173()

File

modules/system/system.install, line 2647

Code

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

  // State tracker to determine whether we keep a backup of the files table or not.
  $safe = TRUE;

  // PostgreSQL needs CREATE TABLE foobar _AS_ SELECT ...
  $AS = $GLOBALS['db_type'] == 'pgsql' ? 'AS' : '';

  // Backup the files table.
  $ret[] = update_sql("CREATE TABLE {files_backup} {$AS} SELECT * FROM {files}");

  // Do some files table sanity checking and cleanup.
  $ret[] = update_sql('DELETE FROM {files} WHERE fid = 0');
  $ret[] = update_sql('UPDATE {files} SET vid = nid WHERE vid = 0');

  // Create a temporary table to build the new file_revisions and files tables from.
  $ret[] = update_sql("CREATE TABLE {files_tmp} {$AS} SELECT * FROM {files}");
  $ret[] = update_sql('DROP TABLE {files}');
  switch ($GLOBALS['db_type']) {
    case 'pgsql':

      // create file_revisions table
      $ret[] = update_sql("CREATE TABLE {file_revisions} (\n        fid integer NOT NULL default 0,\n        vid integer NOT NULL default 0,\n        description varchar(255) NOT NULL default '',\n        list smallint NOT NULL default 0,\n        PRIMARY KEY (fid, vid))");
      $result = update_sql("INSERT INTO {file_revisions} SELECT DISTINCT ON (fid,vid) fid, vid, description, list FROM {files_tmp}");
      $ret[] = $result;
      if ($result['success'] === FALSE) {
        $safe = FALSE;
      }

      // Create normalized files table
      $ret[] = update_sql("CREATE TABLE {files} (\n        fid SERIAL,\n        nid integer NOT NULL default 0,\n        filename varchar(255) NOT NULL default '',\n        filepath varchar(255) NOT NULL default '',\n        filemime varchar(255) NOT NULL default '',\n        filesize integer NOT NULL default 0,\n        PRIMARY KEY (fid))");
      $result = update_sql("INSERT INTO {files} SELECT DISTINCT ON (fid) fid, nid, filename, filepath, filemime, filesize FROM {files_tmp}");
      $ret[] = $result;
      if ($result['success'] === FALSE) {
        $safe = FALSE;
      }
      $ret[] = update_sql("SELECT setval('{files}_fid_seq', max(fid)) FROM {files}");
      break;
    case 'mysqli':
    case 'mysql':

      // create file_revisions table
      $ret[] = update_sql("CREATE TABLE {file_revisions} (\n        fid int unsigned NOT NULL default 0,\n        vid int unsigned NOT NULL default 0,\n        description varchar(255) NOT NULL default '',\n        list tinyint unsigned NOT NULL default 0,\n        PRIMARY KEY (fid, vid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */");

      // Try as you might mysql only does distinct row if you are selecting more than 1 column.
      $result = update_sql('INSERT INTO {file_revisions} SELECT DISTINCT fid , vid, description, list FROM {files_tmp}');
      $ret[] = $result;
      if ($result['success'] === FALSE) {
        $safe = FALSE;
      }
      $ret[] = update_sql("CREATE TABLE {files} (\n        fid int unsigned NOT NULL default 0,\n        nid int unsigned NOT NULL default 0,\n        filename varchar(255) NOT NULL default '',\n        filepath varchar(255) NOT NULL default '',\n        filemime varchar(255) NOT NULL default '',\n        filesize int unsigned NOT NULL default 0,\n        PRIMARY KEY (fid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      $result = update_sql("INSERT INTO {files} SELECT DISTINCT fid, nid, filename, filepath, filemime, filesize FROM {files_tmp}");
      $ret[] = $result;
      if ($result['success'] === FALSE) {
        $safe = FALSE;
      }
      break;
  }
  $ret[] = update_sql("DROP TABLE {files_tmp}");

  // Remove original files table if all went well. Otherwise preserve it and notify user.
  if ($safe) {
    $ret[] = update_sql("DROP TABLE {files_backup}");
  }
  else {
    drupal_set_message('Normalizing files table failed. A backup of the original table called {files_backup} remains in your database.');
  }
  return $ret;
}