You are here

function features_update_7202 in Features 7.2

Create a new table 'features_signature' to store signatures.

File

./features.install, line 205
Install, update and uninstall functions for the features module.

Code

function features_update_7202() {
  if (!db_table_exists('features_signature')) {

    // Create the new table for signatures.
    $schema = array(
      'description' => 'Stores hashes that reflect the last known state of a features component.',
      'fields' => array(
        'module' => array(
          'description' => 'Name of the feature module.',
          'type' => 'varchar',
          'length' => 64,
          'not null' => TRUE,
        ),
        'component' => array(
          'description' => 'Name of the features component.',
          'type' => 'varchar',
          'length' => 128,
          'not null' => TRUE,
        ),
        'signature' => array(
          'description' => 'Hash reflecting the last approved state of the component in code.',
          'type' => 'varchar',
          'length' => 128,
          'not null' => TRUE,
        ),
        'updated' => array(
          'description' => 'Timestamp when the signature was last updated.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
        ),
        'message' => array(
          'description' => 'Message to document why the component was updated.',
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,
        ),
      ),
      'primary key' => array(
        'module',
        'component',
      ),
      'indexes' => array(
        'module' => array(
          'module',
        ),
        'component' => array(
          'component',
        ),
      ),
    );
    db_create_table('features_signature', $schema);
  }

  // Load existing signatures.
  if (db_table_exists('cache_featurestate')) {

    // The old version of features_update_7201() has run in the past, and a
    // cache table was created to replace the 'features_codecache' variable.
    $cache = cache_get('features_codecache', 'cache_featurestate');
    $signaturess = !empty($cache->data) ? $cache->data : array();
    $message = __FUNCTION__ . '() - from cache storage';
  }
  else {

    // The current (inactive) version of features_update_7201() has run, and the
    // 'features_codecache' variable still exists.
    $signaturess = variable_get('features_codecache', array());
    $message = __FUNCTION__ . '() - from variable storage';
  }

  // Prevent existing records from being inserted again.
  // This way we don't need a REPLACE query.
  // This only applies if the table was already created e.g. in a previous
  // failed attempt to run this update.
  $q = db_select('features_signature', 'fs')
    ->fields('fs');
  if ($qr = $q
    ->execute()) {
    foreach ($qr as $obj) {
      unset($signaturess[$obj->module][$obj->component]);
    }
  }

  // Get a timestamp to be stored in each record.
  $timestamp = time();

  // Build the insert query.
  $insert = db_insert('features_signature')
    ->fields(array(
    'module',
    'component',
    'signature',
    'updated',
    'message',
  ));
  foreach ($signaturess as $module => $signatures) {
    foreach ($signatures as $component => $signature) {
      $record = array(
        'module' => $module,
        'component' => $component,
        'signature' => $signature,
        'updated' => $timestamp,
        'message' => $message,
      );
      $insert
        ->values($record);
    }
  }

  // Execute the insert query.
  // On failure, allow the exception to trickle up.
  $insert
    ->execute();

  // Set a temporary marker variable for subsequent updates.
  // This variable was not set in an older version of this update.
  variable_set('features_update_7202_fixed_tmp', TRUE);

  // Delete the old table and variable if the data migration was successful.
  variable_del('features_codecache');
  if (db_table_exists('cache_featurestate')) {
    db_drop_table('cache_featurestate');
  }

  // Reset the static cache that determines the storage type.
  drupal_static_reset('_features_get_signature_storage_type');
}