You are here

function advpoll_update_8 in Advanced Poll 5

Same name and namespace in other branches
  1. 6 advpoll.install \advpoll_update_8()
  2. 6.2 advpoll.install \advpoll_update_8()

Refactor the advpoll_choices database table.

Rename vote_offset to weight, change it to be NOT NULL & remove its extra key, and add an auto_increment id to each choice.

File

./advpoll.install, line 289

Code

function advpoll_update_8() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {advpoll_choices} CHANGE vote_offset weight int NOT NULL');
      $ret[] = update_sql('ALTER TABLE {advpoll_choices} DROP INDEX vote_offset');
      $ret[] = update_sql('ALTER TABLE {advpoll_choices} DROP PRIMARY KEY');
      $ret[] = update_sql('ALTER TABLE {advpoll_choices} ADD cid int unsigned NOT NULL auto_increment PRIMARY KEY FIRST');
      break;
    case 'pgsql':
      db_change_column($ret, 'advpoll_choices', 'vote_offset', 'weight', 'smallint', array(
        'not null' => TRUE,
      ));
      db_add_column($ret, 'advpoll_choices', 'cid', 'serial', array(
        'not null' => TRUE,
      ));
      $ret[] = update_sql('ALTER TABLE {advpoll_choices} ADD PRIMARY KEY (cid)');
      break;
  }

  // Votes should reference the cid rather than the weight of each choice.
  $result = db_query('SELECT nid, cid, weight FROM {advpoll_choices}');
  while ($choice = db_fetch_object($result)) {
    db_query('UPDATE {votingapi_vote} SET tag = %d WHERE content_id = %d AND tag = %d', $choice->cid, $choice->nid, $choice->weight);
    db_query('UPDATE {votingapi_cache} SET tag = %d WHERE content_id = %d AND tag = %d', $choice->cid, $choice->nid, $choice->weight);
  }
  return $ret;
}