You are here

uuid.install in Universally Unique IDentifier 6

Same filename and directory in other branches
  1. 5 uuid.install
  2. 7 uuid.install

Install, update and uninstall functions for the uuid module.

File

uuid.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the uuid module.
 */

/**
 * Implementation of hook_enable().
 */
function uuid_enable() {
  module_load_include('inc', 'uuid', 'uuid.admin');
  uuid_sync();
}

/**
 * Implementation of hook_install().
 */
function uuid_install() {

  // Create tables.
  drupal_install_schema('uuid');
}

/**
 * Implementation of hook_schema().
 */
function uuid_schema() {
  $schema = array(
    'uuid_node' => uuid_table_schema('node', 'nid'),
    'uuid_node_revisions' => uuid_table_schema('node_revisions', 'vid'),
    'uuid_users' => uuid_table_schema('users', 'uid'),
    'uuid_vocabulary' => uuid_table_schema('vocabulary', 'vid'),
    'uuid_term_data' => uuid_table_schema('term_data', 'tid'),
    'uuid_comments' => uuid_table_schema('comments', 'cid'),
  );

  // The uuid_node_revisions table requires an additional column (node id) to
  // support revision deletion upon node delete. No index is needed on this
  // column as it will only be used during a node delete.
  $schema['uuid_node_revisions']['fields']['nid'] = array(
    'type' => 'int',
    'length' => 10,
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
    'description' => 'The revision\'s node id. Needed for delete node operations.',
  );
  return $schema;
}

/**
 * Return schema for a uuid table.
 *
 * @param $key
 *   Name of key field, e.g. nid for nodes.
 *
 * @return
 *   Array with table structure definition (schema).
 */
function uuid_table_schema($table, $key = 'key') {
  return array(
    'fields' => array(
      $key => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The primary key of the record which this UUID was generated for.',
      ),
      'uuid' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The Universally Unique Identifier.',
      ),
    ),
    'primary key' => array(
      $key,
    ),
    'unique keys' => array(
      'uuid_' . $table . '_uuid_key' => array(
        'uuid',
      ),
    ),
  );
}

/**
 * Implementation of hook_uninstall().
 */
function uuid_uninstall() {
  variable_del('uuid_automatic_for_nodes');
  variable_del('uuid_automatic_for_users');
  variable_del('uuid_automatic_for_taxonomy');
  variable_del('uuid_automatic_for_comments');

  // Remove tables.
  drupal_uninstall_schema('uuid');
}

/**
 * Create uuid_vocabulary and uuid_term_data tables.
 */
function uuid_update_6001() {
  $ret = array();
  db_create_table($ret, 'uuid_vocabulary', uuid_table_schema('vocabulary', 'vid'));
  db_create_table($ret, 'uuid_term_data', uuid_table_schema('term_data', 'tid'));
  return $ret;
}

/**
 * For each of out tables, drop the indexe on the UUID column and add a unique
 * key on that column.
 */
function uuid_update_6002() {
  $ret = array();
  foreach (uuid_schema() as $table => $schema) {
    db_drop_index($ret, $table, $table . '_uuid_idx');
    db_add_unique_key($ret, $table, $table . '_uuid_key', array(
      'uuid',
    ));
  }
  return $ret;
}

/**
 * Create uuid_comment table.
 */
function uuid_update_6003() {
  $ret = array();
  db_create_table($ret, 'uuid_comments', uuid_table_schema('comments', 'cid'));
  return $ret;
}

/**
 * Fix the column definitions for uuid columns in all tables
 * to use the more efficient char spec.
 */
function uuid_update_6004() {
  $ret = array();

  // Use what's in uuid_table_schema in order to be consistent.
  $tables = uuid_schema();
  $spec = $tables['uuid_node']['fields']['uuid'];
  foreach ($tables as $tablename => $schema) {
    if (db_table_exists($tablename)) {
      db_change_field($ret, $tablename, 'uuid', 'uuid', $spec);
    }
  }
  return $ret;
}

/**
 * Modify existing uuid_node_revisions table to support revision deletion, and
 * add in as much legacy data as possible.
 */
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;
}

Functions

Namesort descending Description
uuid_enable Implementation of hook_enable().
uuid_install Implementation of hook_install().
uuid_schema Implementation of hook_schema().
uuid_table_schema Return schema for a uuid table.
uuid_uninstall Implementation of hook_uninstall().
uuid_update_6001 Create uuid_vocabulary and uuid_term_data tables.
uuid_update_6002 For each of out tables, drop the indexe on the UUID column and add a unique key on that column.
uuid_update_6003 Create uuid_comment table.
uuid_update_6004 Fix the column definitions for uuid columns in all tables to use the more efficient char spec.
uuid_update_6005 Modify existing uuid_node_revisions table to support revision deletion, and add in as much legacy data as possible.