Issue #3376421 by mstrelan, catch, Berdir: Remove suboptimal path_alias__status index from path_alias table

merge-requests/4485/head
Lee Rowlands 2023-07-27 08:30:43 +10:00
parent 98618df781
commit 1f9798dd81
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
4 changed files with 99 additions and 1 deletions

View File

@ -0,0 +1,16 @@
<?php
/**
* @file
* Post update functions for Path Alias.
*/
/**
* Remove the path_alias__status index.
*/
function path_alias_post_update_drop_path_alias_status_index(): void {
/** @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $update_manager */
$update_manager = \Drupal::service('entity.definition_update_manager');
$entity_type = $update_manager->getEntityType('path_alias');
$update_manager->updateEntityType($entity_type);
}

View File

@ -15,12 +15,17 @@ class PathAliasStorageSchema extends SqlContentEntityStorageSchema {
*/
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$schema = parent::getEntitySchema($entity_type, $reset);
$base_table = $this->storage->getBaseTable();
$schema[$this->storage->getBaseTable()]['indexes'] += [
$schema[$base_table]['indexes'] += [
'path_alias__alias_langcode_id_status' => ['alias', 'langcode', 'id', 'status'],
'path_alias__path_langcode_id_status' => ['path', 'langcode', 'id', 'status'],
];
// Unset the path_alias__status index as it is slower than the above
// indexes and MySQL 5.7 chooses to use it even though it is suboptimal.
unset($schema[$base_table]['indexes']['path_alias__status']);
return $schema;
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\path_alias\Functional;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* Tests update hooks the path_alias module.
*
* @group path_alias
*/
class PathAliasUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles[] = __DIR__ . '/../../../../system/tests/fixtures/update/drupal-9.4.0.bare.standard.php.gz';
}
/**
* Tests path_alias_post_update_drop_path_alias_status_index.
*/
public function testPathAliasStatusIndexRemoved(): void {
$schema = \Drupal::database()->schema();
$this->assertTrue($schema->indexExists('path_alias', 'path_alias__status'));
$this->runUpdates();
$this->assertFalse($schema->indexExists('path_alias', 'path_alias__status'));
}
}

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\path_alias\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the path_alias storage schema.
*
* @coversDefaultClass \Drupal\path_alias\PathAliasStorageSchema
*
* @group path_alias
*/
class PathAliasStorageSchemaTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['path_alias'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('path_alias');
}
/**
* Tests that the path_alias__status index is removed.
*
* @covers ::getEntitySchema
*/
public function testPathAliasStatusIndexRemoved(): void {
$schema = \Drupal::database()->schema();
$table_name = 'path_alias';
$this->assertTrue($schema->indexExists($table_name, 'path_alias__alias_langcode_id_status'));
$this->assertTrue($schema->indexExists($table_name, 'path_alias__path_langcode_id_status'));
$this->assertFalse($schema->indexExists($table_name, 'path_alias__status'));
}
}