You are here

function search_api_db_update_8102 in Search API 8

Adds primary keys to denormalized index tables.

File

modules/search_api_db/search_api_db.install, line 50
Install, update and uninstall functions for the Database Search module.

Code

function search_api_db_update_8102() {

  // @see https://www.drupal.org/node/2884451
  $key_value = \Drupal::keyValue('search_api_db.indexes');
  foreach ($key_value
    ->getAll() as $db_info) {

    // Use the correct database from the server's backend configuration.
    $database = \Drupal::config('search_api.server.' . $db_info['server'])
      ->get('backend_config.database');
    if (!$database) {
      continue;
    }
    list($key, $target) = explode(':', $database, 2);
    $schema = Database::getConnection($target, $key)
      ->schema();
    $table = $db_info['index_table'];
    try {
      $schema
        ->addPrimaryKey($table, [
        'item_id',
      ]);
    } catch (SchemaObjectExistsException $e) {

      // Primary key was already added, maybe by a conscientious site admin.
      // Nothing to do here in that case.
    } catch (\Exception $e) {
      throw new UpdateException("Could not add a primary key to table {{$table}}: " . $e
        ->getMessage(), 0, $e);
    }
  }
  return t('Primary keys added to all denormalized index tables.');
}