You are here

function uuid_update_6005 in Universally Unique IDentifier 7

Same name and namespace in other branches
  1. 6 uuid.install \uuid_update_6005()

Support deleting node revision.

Modify existing uuid_node_revisions table to support revision deletion, and add in as much legacy data as possible.

File

./uuid.install, line 179
Install, update and uninstall functions for the uuid module.

Code

function uuid_update_6005() {
  $ret = array();
  if (db_table_exists('uuid_node_revisions')) {

    // Use what's already defined in uuid schema in order to be consistent.
    $schema = uuid_schema();
    $spec = $schema['uuid_node_revisions']['fields']['nid'];
    db_add_field($ret, 'uuid_node_revisions', 'nid', $spec);

    // Add node ids to the new column, for revisions that exist, but now have a
    // default value of 0 in uuid_node_revisions.
    $result = db_query('SELECT nr.nid, nr.vid FROM {node_revisions} AS nr LEFT JOIN {uuid_node_revisions} AS unr ON unr.vid=nr.vid WHERE unr.nid=%d', 0);
    while ($item = db_fetch_object($result)) {
      $ret[] = update_sql('UPDATE {uuid_node_revisions} SET nid=' . (int) $item->nid . ' WHERE vid=' . (int) $item->vid);
    }

    // Add uuid_node_revision rows for rows that don't exist, but should.
    $result = db_query('SELECT nr.nid, nr.vid FROM {node_revisions} AS nr LEFT JOIN {uuid_node_revisions} AS unr ON unr.vid=nr.vid WHERE unr.nid IS NULL');
    while ($item = db_fetch_object($result)) {
      $ret[] = update_sql(sprintf("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES(%d, '%s', %d)", $item->vid, uuid_uuid(), $item->nid));
    }

    // Delete any orphaned revision vid, uuid pairs.
    $ret[] = update_sql('DELETE FROM {uuid_node_revisions} WHERE nid=0');
  }
  return $ret;
}