diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php index 8f8e3df..f3cc3ff 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php @@ -490,6 +490,13 @@ public function dropUniqueKey($table, $name) { * {@inheritdoc} */ public function addIndex($table, $name, $fields, array $spec) { + if (!$this->tableExists($table)) { + throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name))); + } + if ($this->indexExists($table, $name)) { + throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name))); + } + $spec['indexes'][$name] = $fields; $indexes = $this->getNormalizedIndexes($spec); diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php index cfbf43a..07f504c 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php @@ -641,6 +641,13 @@ public function dropUniqueKey($table, $name) { * {@inheritdoc} */ public function addIndex($table, $name, $fields, array $spec) { + if (!$this->tableExists($table)) { + throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name))); + } + if ($this->indexExists($table, $name)) { + throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name))); + } + $spec['indexes'][$name] = $fields; $this->validateIndex($table, $name); @@ -778,7 +785,7 @@ protected function _createKeys($table, $new_keys) { } if (isset($new_keys['indexes'])) { foreach ($new_keys['indexes'] as $name => $fields) { - $this->addIndex($table, $name, $fields); + $this->addIndex($table, $name, $fields, $new_keys); } } } diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php index 89c98d5..9ef0750 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php @@ -586,6 +586,13 @@ protected function mapKeyDefinition(array $key_definition, array $mapping) { * {@inheritdoc} */ public function addIndex($table, $name, $fields, array $spec) { + if (!$this->tableExists($table)) { + throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name))); + } + if ($this->indexExists($table, $name)) { + throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name))); + } + $spec['indexes'][$name] = $fields; $this->validateIndex($table, $name); diff --git a/core/lib/Drupal/Core/Database/Schema.php b/core/lib/Drupal/Core/Database/Schema.php index e7cf65d..45c7e5a 100644 --- a/core/lib/Drupal/Core/Database/Schema.php +++ b/core/lib/Drupal/Core/Database/Schema.php @@ -424,32 +424,6 @@ public function fieldExists($table, $column) { */ abstract public function addIndex($table, $name, $fields, array $spec); - - /** - * Validates whether the index is valid. - * - * This method ensures that the table exists, that the new index was specified - * and that there is no index with the same name yet. - * - * @param string $table - * The table of the index name. - * @param string $name - * The name of the index. - * - * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException - * If the specified table doesn't exist. - * @throws \Drupal\Core\Database\SchemaObjectExistsException - * If the specified table already has an index by that name. - */ - protected function validateIndex($table, $name) { - if (!$this->tableExists($table)) { - throw new SchemaObjectDoesNotExistException("Cannot add index '$name' to table '$table': table doesn't exist."); - } - if ($this->indexExists($table, $name)) { - throw new SchemaObjectExistsException("Cannot add index '$name' to table '$table': index already exists."); - } - } - /** * Gets normalized indexes from a table specification. *