You are here

protected function SchemaMigrator::recreatePrimaryKey in Schema 8

Parameters

$table TableComparison:

1 call to SchemaMigrator::recreatePrimaryKey()
SchemaMigrator::execute in src/Migration/SchemaMigrator.php

File

src/Migration/SchemaMigrator.php, line 192
Contains Drupal\schema\Migration\SchemaMigrator.

Class

SchemaMigrator
Modifies the database schema to match the declared schema.

Namespace

Drupal\schema\Migration

Code

protected function recreatePrimaryKey($table) {
  $primary_key = $table
    ->getDeclaredPrimaryKey();
  $msg_args = array(
    'table' => $table
      ->getTableName(),
    'key' => is_array($primary_key) ? implode(', ', $primary_key) : '[]',
  );

  // If primary key exists already, recreate it.
  if ($this->dbschema
    ->indexExists($table
    ->getTableName(), 'PRIMARY') && is_array($primary_key)) {
    $this->dbschema
      ->recreatePrimaryKey($table
      ->getTableName(), $primary_key);
    $this
      ->logSuccess("Recreated primary key for {table} on {key}.", $msg_args);
  }
  elseif ($this->dbschema
    ->indexExists($table
    ->getTableName(), 'PRIMARY')) {
    log_statement("TABLE %s DROP PRIMARY KEY", $table
      ->getTableName());
    if ($this->dbschema
      ->dropPrimaryKey($table
      ->getTableName())) {
      $this
        ->logSuccess("Dropped primary key for {table}.", $msg_args);
    }
    else {
      $this
        ->logError("Failed to drop primary key for {table}.", $msg_args);
    }
  }
  elseif (is_array($primary_key)) {
    $this->dbschema
      ->addPrimaryKey($table
      ->getTableName(), $primary_key);
    $this
      ->logSuccess("Created primary key for {table} on {key}.", $msg_args);
  }
}