You are here

function webform_update_20 in Webform 6.2

Same name and namespace in other branches
  1. 5.2 webform.install \webform_update_20()

Set new primary keys for the submissions table.

File

./webform.install, line 766
Webform module install/schema hooks.

Code

function webform_update_20() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':

      // Update the webform_submissions table with sid primary key instead of nid, sid.
      $ret[] = update_sql('ALTER TABLE {webform_submissions} DROP INDEX sid');
      $ret[] = update_sql('ALTER TABLE {webform_submissions} DROP PRIMARY KEY');
      $ret[] = update_sql('ALTER TABLE {webform_submissions} ADD UNIQUE INDEX sid_nid (sid, nid)');
      $ret[] = update_sql('ALTER TABLE {webform_submissions} ADD PRIMARY KEY (sid)');

      // Update webform_submitted_data table removing nid from the primary key.
      $ret[] = update_sql('ALTER TABLE {webform_submitted_data} DROP PRIMARY KEY');
      $ret[] = update_sql('ALTER TABLE {webform_submitted_data} DROP INDEX sid');

      // While we've got these keys removed, decrease the size of the 'no' column.
      $ret[] = update_sql('ALTER TABLE {webform_submitted_data} CHANGE no no tinyint NOT NULL DEFAULT 0');
      $ret[] = update_sql('ALTER TABLE {webform_submitted_data} ADD PRIMARY KEY (sid, cid, no)');
      $ret[] = update_sql('ALTER TABLE {webform_submitted_data} ADD INDEX nid (nid)');
      $ret[] = update_sql('ALTER TABLE {webform_submitted_data} ADD INDEX sid_nid (sid, nid)');
      break;
    case 'pgsql':
      $ret[] = update_sql('ALTER TABLE {webform_submissions} DROP CONSTRAINT {webform_submissions}_pkey');
      $ret[] = update_sql('DROP INDEX {webform_submissions}_sid_idx');
      $ret[] = update_sql('ALTER TABLE {webform_submissions} ADD PRIMARY KEY (sid)');
      $ret[] = update_sql('ALTER TABLE {webform_submissions} ADD CONSTRAINT {webform_submissions}_sid_nid_key UNIQUE (sid, nid)');
      $ret[] = update_sql('DROP INDEX {webform_submitted_data}_sid_idx');
      db_change_column($ret, 'webform_submitted_data', 'no', 'no', 'smallint', array(
        'not null' => TRUE,
        'default' => '0',
      ));
      $ret[] = update_sql('ALTER TABLE {webform_submitted_data} ADD PRIMARY KEY (sid, cid, no)');
      $ret[] = update_sql('CREATE INDEX {webform_submitted_data}_nid_idx ON {webform_submitted_data} (nid)');
      $ret[] = update_sql('CREATE INDEX {webform_submitted_data}_sid_nid_idx ON {webform_submitted_data} (sid, nid)');
      $ret[] = update_sql('ALTER TABLE {webform_component} ADD PRIMARY KEY (nid, cid)');
      break;
  }
  return $ret;
}