You are here

function date_update_7006 in Date 7.3

Same name and namespace in other branches
  1. 7.2 date.install \date_update_7006()

Add date value indexes to existed field tables.

File

./date.install, line 241
Install, update and uninstall functions for the Date module.

Code

function date_update_7006() {

  // Get all fields provided by the Data module.
  $query = db_select('field_config', 'f')
    ->fields('f', array(
    'id',
    'field_name',
    'data',
  ))
    ->condition('f.type', array(
    'datetime',
    'date',
    'datestamp',
  ));

  // Special handling for the Encrypt module.
  if (module_exists('field_encrypt')) {
    $query
      ->condition('f.data', '%' . db_like(' field_encrypt";a:1:{s:7:"encrypt";i:0 ') . '%', 'LIKE');
  }

  // Complete the query.
  $fields = $query
    ->execute()
    ->fetchAllAssoc('id');
  foreach ($fields as $id => $info) {
    $field_info = field_info_field($info->field_name);

    // Add indexes only for SQL storage fields.
    if ($field_info['storage']['type'] != 'field_sql_storage') {
      continue;
    }
    $tables = array(
      key($field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT']),
      key($field_info['storage']['details']['sql']['FIELD_LOAD_REVISION']),
    );
    $data = unserialize($info->data);
    $field_name = $info->field_name . '_value';
    $field_name2 = $info->field_name . '_value2';

    // Build indexes.
    $indexes = $data['indexes'] = array();
    if (!empty($data['settings']['todate'])) {
      $indexes[$field_name] = array(
        $field_name,
        $field_name2,
      );
      $indexes[$field_name2] = array(
        $field_name2,
      );
      $data['indexes']['value'] = array(
        'value',
        'value2',
      );
      $data['indexes']['value2'] = array(
        'value2',
      );
    }
    else {
      $indexes[$field_name] = array(
        $field_name,
      );
      $data['indexes']['value'] = array(
        'value',
      );
    }

    // Add missed indexes to tables.
    foreach ($indexes as $name => $index) {
      foreach ($tables as $table) {
        if (!db_index_exists($table, $name)) {
          db_add_index($table, $name, $index);
        }
      }
    }

    // Fix date fields storage 'field_config' indexes.
    db_update('field_config')
      ->fields(array(
      'data' => serialize($data),
    ))
      ->condition('id', $id)
      ->execute();
  }
}