From e81e6f869deee63538dd26e37103aee9eb60dd6d Mon Sep 17 00:00:00 2001 From: Dave Long Date: Wed, 3 Jul 2024 10:48:53 +0100 Subject: [PATCH] Issue #3156439 by alexpott, catch, bonsaipuppy, mkalkbrenner, longwave, xjm: Add an index on locales_location on type and name --- core/modules/locale/locale.install | 62 ++++++++++ .../LocalesLocationAddIndexUpdateTest.php | 114 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 core/modules/locale/tests/src/Functional/LocalesLocationAddIndexUpdateTest.php diff --git a/core/modules/locale/locale.install b/core/modules/locale/locale.install index d8abf2234c8..6053bfb158e 100644 --- a/core/modules/locale/locale.install +++ b/core/modules/locale/locale.install @@ -190,6 +190,7 @@ function locale_schema() { ], 'indexes' => [ 'string_type' => ['sid', 'type'], + 'type_name' => ['type', 'name'], ], ]; @@ -321,3 +322,64 @@ function locale_requirements($phase) { function locale_update_last_removed() { return 10100; } + +/** + * Add an index on locales_location on type and name. + */ +function locale_update_10300() { + $spec = []; + $spec['locales_location'] = [ + 'description' => 'Location information for source strings.', + 'fields' => [ + 'lid' => [ + 'type' => 'serial', + 'not null' => TRUE, + 'description' => 'Unique identifier of this location.', + ], + 'sid' => [ + 'type' => 'int', + 'not null' => TRUE, + 'description' => 'Unique identifier of this string.', + ], + 'type' => [ + 'type' => 'varchar_ascii', + 'length' => 50, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The location type (file, config, path, etc).', + ], + 'name' => [ + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Type dependent location information (file name, path, etc).', + ], + 'version' => [ + 'type' => 'varchar_ascii', + 'length' => 20, + 'not null' => TRUE, + 'default' => 'none', + 'description' => 'Version of Drupal where the location was found.', + ], + ], + 'primary key' => ['lid'], + 'foreign keys' => [ + 'locales_source' => [ + 'table' => 'locales_source', + 'columns' => ['sid' => 'lid'], + ], + ], + 'indexes' => [ + 'string_type' => ['sid', 'type'], + 'type_name' => ['type', 'name'], + ], + ]; + $schema = \Drupal::database()->schema(); + // If the update has been manually applied, recreate the index using the + // current schema. + if ($schema->indexExists('locales_location', 'type_name')) { + $schema->dropIndex('locales_location', 'type_name'); + } + $schema->addIndex('locales_location', 'type_name', ['type', 'name'], $spec['locales_location']); +} diff --git a/core/modules/locale/tests/src/Functional/LocalesLocationAddIndexUpdateTest.php b/core/modules/locale/tests/src/Functional/LocalesLocationAddIndexUpdateTest.php new file mode 100644 index 00000000000..a63960f8737 --- /dev/null +++ b/core/modules/locale/tests/src/Functional/LocalesLocationAddIndexUpdateTest.php @@ -0,0 +1,114 @@ +databaseDumpFiles[] = $this->root . '/core/modules/system/tests/fixtures/update/drupal-10.3.0.filled.standard.php.gz'; + } + + /** + * Tests locale_update_10300(). + * + * @see locale_update_10300 + */ + public function testIndex(): void { + $this->assertFalse(\Drupal::database() + ->schema() + ->indexExists('locales_location', 'type_name')); + + // Run updates and test them. + $this->runUpdates(); + + $this->assertTrue(\Drupal::database() + ->schema() + ->indexExists('locales_location', 'type_name')); + } + + /** + * Tests locale_update_10300(). + * + * @see locale_update_10300 + */ + public function testExistingIndex(): void { + $spec = []; + $spec['locales_location'] = [ + 'description' => 'Location information for source strings.', + 'fields' => [ + 'lid' => [ + 'type' => 'serial', + 'not null' => TRUE, + 'description' => 'Unique identifier of this location.', + ], + 'sid' => [ + 'type' => 'int', + 'not null' => TRUE, + 'description' => 'Unique identifier of this string.', + ], + 'type' => [ + 'type' => 'varchar_ascii', + 'length' => 50, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The location type (file, config, path, etc).', + ], + 'name' => [ + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Type dependent location information (file name, path, etc).', + ], + 'version' => [ + 'type' => 'varchar_ascii', + 'length' => 20, + 'not null' => TRUE, + 'default' => 'none', + 'description' => 'Version of Drupal where the location was found.', + ], + ], + 'primary key' => ['lid'], + 'foreign keys' => [ + 'locales_source' => [ + 'table' => 'locales_source', + 'columns' => ['sid' => 'lid'], + ], + ], + 'indexes' => [ + 'string_type' => ['sid', 'type'], + 'type_name' => ['type', 'name'], + ], + ]; + \Drupal::database()->schema()->addIndex('locales_location', 'type_name', ['type', 'name', 'sid'], $spec['locales_location']); + + // Run updates and test them. + $this->runUpdates(); + + $schema = \Drupal::database()->schema(); + $this->assertTrue($schema->indexExists('locales_location', 'type_name')); + + // Ensure that index specification matches our expectations. + $introspect_index_schema = new \ReflectionMethod(get_class($schema), 'introspectIndexSchema'); + $index_schema = $introspect_index_schema->invoke($schema, 'locales_location'); + $this->assertSame(['type', 'name'], $index_schema['indexes']['type_name']); + } + +}