Commit 410d8384 authored by webchick's avatar webchick
Browse files

Issue #2555665 by jhedstrom, Gábor Hojtsy, stefan.r, plach: When index is...

Issue #2555665 by jhedstrom, Gábor Hojtsy, stefan.r, plach: When index is added for content_translation_uid, the corresponding stored schema definition is not updated
parent f0ba2d7c
......@@ -353,7 +353,9 @@ public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeI
if (!empty($schema['indexes'])) {
foreach ($schema['indexes'] as $name => $specifier) {
$schema_handler->addIndex($table_name, $name, $specifier, $schema);
// Check if the index exists because it might already have been
// created as part of the earlier entity type update event.
$this->addIndexIfNotExists($table_name, $name, $specifier, $schema);
}
}
if (!empty($schema['unique keys'])) {
......@@ -1159,9 +1161,7 @@ protected function createSharedTableSchema(FieldStorageDefinitionInterface $stor
foreach ($schema[$table_name]['indexes'] as $name => $specifier) {
// Check if the index exists because it might already have been
// created as part of the earlier entity type update event.
if (!$schema_handler->indexExists($table_name, $name)) {
$schema_handler->addIndex($table_name, $name, $specifier, $schema[$table_name]);
}
$this->addIndexIfNotExists($table_name, $name, $specifier, $schema[$table_name]);
}
}
if (!empty($schema[$table_name]['unique keys'])) {
......@@ -1327,8 +1327,10 @@ protected function updateDedicatedTableSchema(FieldStorageDefinitionInterface $s
$real_columns[] = $table_mapping->getFieldColumnName($storage_definition, $column_name);
}
}
$this->database->schema()->addIndex($table, $real_name, $real_columns, $actual_schema[$table]);
$this->database->schema()->addIndex($revision_table, $real_name, $real_columns, $actual_schema[$revision_table]);
// Check if the index exists because it might already have been
// created as part of the earlier entity type update event.
$this->addIndexIfNotExists($table, $real_name, $real_columns, $actual_schema[$table]);
$this->addIndexIfNotExists($revision_table, $real_name, $real_columns, $actual_schema[$revision_table]);
}
}
$this->saveFieldSchemaData($storage_definition, $this->getDedicatedTableSchema($storage_definition));
......@@ -1419,7 +1421,10 @@ protected function updateSharedTableSchema(FieldStorageDefinitionInterface $stor
// Create new indexes and unique keys.
if (!empty($schema[$table_name]['indexes'])) {
foreach ($schema[$table_name]['indexes'] as $name => $specifier) {
$schema_handler->addIndex($table_name, $name, $specifier, $schema[$table_name]);
// Check if the index exists because it might already have been
// created as part of the earlier entity type update event.
$this->addIndexIfNotExists($table_name, $name, $specifier, $schema[$table_name]);
}
}
if (!empty($schema[$table_name]['unique keys'])) {
......@@ -1867,4 +1872,25 @@ protected function getColumnSchemaRelevantKeys() {
return ['type', 'size', 'length', 'unsigned'];
}
/**
* Create an index if it doesn't already exist.
*
* @param string $table
* The table name.
* @param string $name
* The index name.
* @param array $fields
* The fields to index.
* @param array $spec
* The table specification.
*
* For the full parameter descriptions see
* \Drupal\Core\Database\Schema::addIndex().
*/
protected function addIndexIfNotExists($table, $name, array $fields, array $spec) {
if (!$this->database->schema()->indexExists($table, $name)) {
$this->database->schema()->addIndex($table, $name, $fields, $spec);
}
}
}
......@@ -1446,7 +1446,24 @@ public function testonEntityTypeUpdateWithNewIndex() {
$this->storageSchema->expects($this->any())
->method('loadEntitySchemaData')
->willReturn([]);
->willReturn([
'entity_test' => [
'indexes' => [
// A changed index definition.
'entity_test__b588603cb9' => ['longer_index_name'],
// An index that has been removed.
'entity_test__removed_field' => ['removed_field'],
],
],
]);
// The original indexes should be dropped before the new one is added.
$this->dbSchemaHandler->expects($this->at(0))
->method('dropIndex')
->with('entity_test', 'entity_test__b588603cb9');
$this->dbSchemaHandler->expects($this->at(1))
->method('dropIndex')
->with('entity_test', 'entity_test__removed_field');
$this->dbSchemaHandler->expects($this->atLeastOnce())
->method('addIndex')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment