You are here

function search_api_update_7102 in Search API 7

Update replacing IDs with machine names for foreign keys. {search_api_index}.server and {search_api_item}.index_id are altered.

File

./search_api.install, line 543
Install, update and uninstall functions for the Search API module.

Code

function search_api_update_7102() {

  // Update of search_api_index:
  $indexes = array();
  $select = db_select('search_api_index', 'i')
    ->fields('i');
  foreach ($select
    ->execute() as $index) {
    $indexes[$index->id] = $index;
  }
  $servers = db_select('search_api_server', 's')
    ->fields('s', array(
    'id',
    'machine_name',
  ))
    ->execute()
    ->fetchAllKeyed();
  db_drop_index('search_api_index', 'server');
  db_drop_field('search_api_index', 'server');
  $spec = array(
    'description' => 'The {search_api_server}.machine_name with which data should be indexed.',
    'type' => 'varchar',
    'length' => 50,
    'not null' => FALSE,
  );
  db_add_field('search_api_index', 'server', $spec);
  foreach ($indexes as $index) {
    db_update('search_api_index')
      ->fields(array(
      'server' => $servers[$index->server],
    ))
      ->condition('id', $index->id)
      ->execute();
  }
  db_add_index('search_api_index', 'server', array(
    'server',
  ));

  // Update of search_api_item:
  db_drop_index('search_api_item', 'indexing');
  db_drop_primary_key('search_api_item');
  $spec = array(
    'description' => 'The {search_api_index}.machine_name this item belongs to.',
    'type' => 'varchar',
    'length' => 50,
    'not null' => TRUE,
  );
  $keys_new = array(
    'indexes' => array(
      'indexing' => array(
        'index_id',
        'changed',
      ),
    ),
    'primary key' => array(
      'item_id',
      'index_id',
    ),
  );
  db_change_field('search_api_item', 'index_id', 'index_id', $spec, $keys_new);
  foreach ($indexes as $index) {

    // We explicitly forbid numeric machine names, therefore we don't have to
    // worry about conflicts here.
    db_update('search_api_item')
      ->fields(array(
      'index_id' => $index->machine_name,
    ))
      ->condition('index_id', $index->id)
      ->execute();
  }
}