You are here

public function MySql::alterNewTable in Search API 8

Reacts to a new table being created.

Parameters

string $table: The name of the table.

string $type: (optional) The type of table. One of "index" (for the denormalized table for an entire index), "text" (for an index's fulltext data table) and "field" (for field-specific tables).

Throws

\Drupal\search_api\SearchApiException Thrown if any error occurs that should abort the current action. Internal errors that can be ignored should just be logged.

Overrides GenericDatabase::alterNewTable

File

modules/search_api_db/src/DatabaseCompatibility/MySql.php, line 17

Class

MySql
Represents a MySQL-based database.

Namespace

Drupal\search_api_db\DatabaseCompatibility

Code

public function alterNewTable($table, $type = 'text') {

  // The Drupal MySQL integration defaults to using a 4-byte-per-character
  // encoding, which would make it impossible to use our normal 255 characters
  // long varchar fields in a primary key (since that would exceed the key's
  // maximum size). Therefore, we have to convert all tables to the "utf8"
  // character set – but we only want to make fulltext tables case-sensitive.
  $charset = $type === 'text' ? 'utf8mb4' : 'utf8';
  $collation = $type === 'text' ? 'utf8mb4_bin' : 'utf8_general_ci';
  try {
    $this->database
      ->query("ALTER TABLE {{$table}} CONVERT TO CHARACTER SET '{$charset}' COLLATE '{$collation}'");
  } catch (\PDOException $e) {
    $class = get_class($e);
    $message = $e
      ->getMessage();
    throw new SearchApiException("{$class} while trying to change collation of {$type} search data table '{$table}': {$message}", 0, $e);
  } catch (DatabaseException $e) {
    $class = get_class($e);
    $message = $e
      ->getMessage();
    throw new SearchApiException("{$class} while trying to change collation of {$type} search data table '{$table}': {$message}", 0, $e);
  }
}