diff --git a/core/lib/Drupal/Core/Extension/ModuleInstaller.php b/core/lib/Drupal/Core/Extension/ModuleInstaller.php index 172075796c..661ca24960 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstaller.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstaller.php @@ -49,9 +49,9 @@ class ModuleInstaller implements ModuleInstallerInterface { * * @var \Drupal\Core\Extension\SchemaVersionHandlerInterface */ - protected $SchemaVersionHandler; + protected $schemaVersionHandler; - /** + /** * The database connection. * * @var \Drupal\Core\Database\Connection @@ -90,10 +90,10 @@ public function __construct($root, ModuleHandlerInterface $module_handler, Drupa @trigger_error('The schema installer service must be passed to ' . __METHOD__ . '. Creating ' . __CLASS__ . ' without it is deprecated in drupal:9.1.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); $schema_version_handler = \Drupal::service('schema_version_handler'); } - $this->SchemaVersionHandler = $schema_version_handler; - if(!$connection) { + $this->schemaVersionHandler = $schema_version_handler; + if (!$connection) { @trigger_error('The database connection must be passed to ' . __METHOD__ . '. Creating ' . __CLASS__ . ' without it is deprecated in drupal:9.1.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); - $connection = \Drupal::service('schema_version_handler'); + $connection = \Drupal::service('database'); } $this->connection = $connection; } @@ -322,7 +322,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) { if ($last_removed = $this->moduleHandler->invoke($module, 'update_last_removed')) { $version = max($version, $last_removed); } - $this->SchemaVersionHandler->setInstalledVersion($module, $version); + $this->schemaVersionHandler->setInstalledVersion($module, $version); // Ensure that all post_update functions are registered already. This // should include existing post-updates, as well as any specified as @@ -543,8 +543,7 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { // fastCGI which executes ::destruct() after the Module uninstallation page // was sent already. \Drupal::service('router.builder')->rebuild(); - $this->SchemaVersionHandler->resetCache(); - $this->SchemaVersionHandler->getInstalledVersions(); + $this->schemaVersionHandler->resetCache()->getInstalledVersions(); // Let other modules react. $this->moduleHandler->invokeAll('modules_uninstalled', [$module_list, $sync_status]); @@ -663,19 +662,12 @@ public function getSchema($module, $table = NULL) { $schema = $this->moduleHandler->invoke($module, 'schema'); if (isset($table)) { - if (isset($schema[$table])) { - return $schema[$table]; - } - return []; + return $schema[$table] ?? []; } - elseif (!empty($schema)) { - return $schema; - } - - return []; + return $schema ?: []; } - /** + /** * Fills in required default values for table definitions from hook_schema(). * * @param array $schema @@ -709,9 +701,10 @@ private function schemaInitialize(array &$schema, $module) { private function schemaInstall($module) { $schema = $this->getSchema($module); $this->schemaInitialize($schema, $module); + $connection_schema = $this->connection->schema(); foreach ($schema as $name => $table) { - $this->connection->schema()->createTable($name, $table); + $connection_schema->createTable($name, $table); } } @@ -726,11 +719,11 @@ private function schemaInstall($module) { * The module for which the tables will be removed. */ private function schemaUninstall($module) { - $schema = $this->getSchema($module); - $this->schemaInitialize($schema, $module); + $tables = $this->getSchema($module); + $this->schemaInitialize($tables, $module); $connection_schema = $this->connection->schema(); - foreach ($schema as $table) { + foreach ($tables as $table) { if ($connection_schema->tableExists($table['name'])) { $connection_schema->dropTable($table['name']); } diff --git a/core/lib/Drupal/Core/Extension/SchemaVersionHandler.php b/core/lib/Drupal/Core/Extension/SchemaVersionHandler.php index 9a6c0cc5bd..4dab0f3a31 100644 --- a/core/lib/Drupal/Core/Extension/SchemaVersionHandler.php +++ b/core/lib/Drupal/Core/Extension/SchemaVersionHandler.php @@ -48,7 +48,7 @@ class SchemaVersionHandler implements SchemaVersionHandlerInterface { * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator * The cache tags invalidator. */ - public function __construct(ModuleHandlerInterface $module_handler, KeyValueFactoryInterface $key_value_factory, $cache_tags_invalidator, CacheTagsInvalidatorInterface $cache_tags_invalidator_legacy = NULL) { + public function __construct(ModuleHandlerInterface $module_handler, KeyValueFactoryInterface $key_value_factory, CacheTagsInvalidatorInterface $cache_tags_invalidator) { $this->moduleHandler = $module_handler; $this->keyValue = $key_value_factory; $this->cacheTagsInvalidator = $cache_tags_invalidator; @@ -84,38 +84,8 @@ public function resetCache() { */ public function setInstalledVersion($module, $version) { $this->keyValue->get('system.schema')->set($module, $version); - $this->resetCache()->ensureInitialized(); - } - - /** - * Fills in required default values for table definitions from hook_schema(). - * - * @param array $schema - * The schema definition array as it was returned by the module's - * hook_schema(). - * @param string $module - * The module for which hook_schema() was invoked. - * @param bool $remove_descriptions - * (optional) Whether to additionally remove 'description' keys of all - * tables and fields to improve performance of serialize() and - * unserialize(). Defaults to TRUE. - */ - protected function initialize(array &$schema, $module, $remove_descriptions = TRUE) { - // Set the name and module key for all tables. - foreach ($schema as $name => &$table) { - if (empty($table['module'])) { - $table['module'] = $module; - } - if (!isset($table['name'])) { - $table['name'] = $name; - } - if ($remove_descriptions) { - unset($table['description']); - foreach ($table['fields'] as &$field) { - unset($field['description']); - } - } - } + $this->currentVersions[$module] = $version; + return $this; } /** diff --git a/core/lib/Drupal/Core/Extension/SchemaVersionHandlerInterface.php b/core/lib/Drupal/Core/Extension/SchemaVersionHandlerInterface.php index b5b4a246a0..cf0881aabe 100644 --- a/core/lib/Drupal/Core/Extension/SchemaVersionHandlerInterface.php +++ b/core/lib/Drupal/Core/Extension/SchemaVersionHandlerInterface.php @@ -46,6 +46,8 @@ public function resetCache(); * A module name. * @param string $version * The new schema version. + * + * @return $this */ public function setInstalledVersion($module, $version); diff --git a/core/modules/system/src/Controller/DbUpdateController.php b/core/modules/system/src/Controller/DbUpdateController.php index 995ea53261..61ffe62102 100644 --- a/core/modules/system/src/Controller/DbUpdateController.php +++ b/core/modules/system/src/Controller/DbUpdateController.php @@ -83,7 +83,7 @@ class DbUpdateController extends ControllerBase { * * @var \Drupal\Core\Extension\SchemaVersionHandlerInterface */ - protected $SchemaVersionHandler; + protected $schemaVersionHandler; /** * Constructs a new UpdateController. @@ -120,7 +120,7 @@ public function __construct($root, KeyValueExpirableFactoryInterface $key_value_ @trigger_error('The schema installer service must be passed to ' . __METHOD__ . '. Creating ' . __CLASS__ . ' without it is deprecated in drupal:9.1.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); $schema_version_handler = \Drupal::service('schema_version_handler'); } - $this->SchemaVersionHandler = $schema_version_handler; + $this->schemaVersionHandler = $schema_version_handler; } /** @@ -616,7 +616,7 @@ protected function triggerBatch(Request $request) { // correct place. (The updates are already sorted, so we can simply base // this on the first one we come across in the above foreach loop.) if (isset($start[$update['module']])) { - $this->SchemaVersionHandler->setInstalledVersion($update['module'], $update['number'] - 1); + $this->schemaVersionHandler->setInstalledVersion($update['module'], $update['number'] - 1); unset($start[$update['module']]); } $operations[] = ['update_do_one', [$update['module'], $update['number'], $dependency_map[$function]]]; diff --git a/core/modules/system/tests/src/Functional/Module/InstallTest.php b/core/modules/system/tests/src/Functional/Module/InstallTest.php index de46ca9583..04269d5052 100644 --- a/core/modules/system/tests/src/Functional/Module/InstallTest.php +++ b/core/modules/system/tests/src/Functional/Module/InstallTest.php @@ -53,10 +53,8 @@ public function testEnableUserTwice() { public function testRequiredModuleSchemaVersions() { /** @var \Drupal\Core\Extension\SchemaVersionHandlerInterface $schema_version_handler */ $schema_version_handler = \Drupal::service('schema_version_handler'); - $schema_version_handler->resetCache(); $version = $schema_version_handler->getInstalledVersion('system'); $this->assertTrue($version > 0, 'System module version is > 0.'); - $schema_version_handler->resetCache(); $version = $schema_version_handler->getInstalledVersion('user'); $this->assertTrue($version > 0, 'User module version is > 0.'); diff --git a/core/modules/system/tests/src/Functional/UpdateSystem/UpdatePathLastRemovedTest.php b/core/modules/system/tests/src/Functional/UpdateSystem/UpdatePathLastRemovedTest.php index c3c198a383..245f77622f 100644 --- a/core/modules/system/tests/src/Functional/UpdateSystem/UpdatePathLastRemovedTest.php +++ b/core/modules/system/tests/src/Functional/UpdateSystem/UpdatePathLastRemovedTest.php @@ -53,8 +53,9 @@ protected function setUp(): void { * Tests that a module with a too old schema version can not be updated. */ public function testLastRemovedVersion() { - $this->getSchemaVersionHandler()->setInstalledVersion('update_test_last_removed', 8000); - $this->getSchemaVersionHandler()->setInstalledVersion('system', 8804); + $this->getSchemaVersionHandler() + ->setInstalledVersion('update_test_last_removed', 8000) + ->setInstalledVersion('system', 8804); // Access the update page with a schema version that is too old for system // and the test module, only the generic core message should be shown. diff --git a/core/modules/system/tests/src/Functional/UpdateSystem/UpdateSchemaTest.php b/core/modules/system/tests/src/Functional/UpdateSystem/UpdateSchemaTest.php index 94cd93ab8d..4480e97fc3 100644 --- a/core/modules/system/tests/src/Functional/UpdateSystem/UpdateSchemaTest.php +++ b/core/modules/system/tests/src/Functional/UpdateSystem/UpdateSchemaTest.php @@ -54,11 +54,9 @@ protected function setUp(): void { */ public function testUpdateHooks() { $connection = Database::getConnection(); - /** @var \Drupal\Core\Extension\SchemaVersionHandlerInterface $schema_version_handler */ - $schema_version_handler = \Drupal::service('schema_version_handler'); // Verify that the 8000 schema is in place. - $this->assertEqual($schema_version_handler->getInstalledVersion('update_test_schema'), 8000); + $this->assertSame(8000, \Drupal::service('schema_version_handler')->getInstalledVersion('update_test_schema')); $this->assertFalse($connection->schema()->indexExists('update_test_schema_table', 'test'), 'Version 8000 of the update_test_schema module is installed.'); // Increment the schema version. @@ -74,8 +72,9 @@ public function testUpdateHooks() { $this->checkForMetaRefresh(); // Ensure schema has changed. - $schema_version_handler->resetCache(); - $this->assertEqual($schema_version_handler->getInstalledVersion('update_test_schema'), 8001); + /** @var \Drupal\Core\Extension\SchemaVersionHandlerInterface $schema_version_handler */ + $schema_version_handler = \Drupal::service('schema_version_handler'); + $this->assertSame(8001, $schema_version_handler->resetCache()->getInstalledVersion('update_test_schema')); // Ensure the index was added for column a. $this->assertTrue($connection->schema()->indexExists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.'); @@ -83,8 +82,7 @@ public function testUpdateHooks() { require_once $this->root . '/core/includes/update.inc'; update_set_schema('update_test_schema', 8003); // Ensure schema has changed. - $this->assertEqual($schema_version_handler->getInstalledVersion('update_test_schema'), 8003); - + $this->assertSame(8003, $schema_version_handler->getInstalledVersion('update_test_schema')); } } diff --git a/core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php b/core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php index 43558a4ad1..b931cec690 100644 --- a/core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php +++ b/core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php @@ -617,8 +617,9 @@ public function testSuccessfulMultilingualUpdateFunctionality() { $this->assertEqual($schema_version, 8001, 'update_script_test schema version is 8001 after updating.'); // Set the installed schema version to one less than the current update. - $schema_version_handler->setInstalledVersion('update_script_test', $schema_version - 1); - $schema_version = $schema_version_handler->getInstalledVersion('update_script_test'); + $schema_version = $schema_version_handler + ->setInstalledVersion('update_script_test', $schema_version - 1) + ->getInstalledVersion('update_script_test'); $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.'); // Create admin user. diff --git a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php index fe7ce4a169..14b5c629d8 100644 --- a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php @@ -101,8 +101,8 @@ public function testUpdateHookN() { /** @var \Drupal\Core\Extension\SchemaVersionHandlerInterface $schema_version_handler */ $schema_version_handler = \Drupal::service('schema_version_handler'); $schema_version_handler->resetCache(); - $this->assertEqual($schema_version_handler->getInstalledVersion('update_test_schema'), 8001); - $this->assertEqual($schema_version_handler->getInstalledVersion('update_test_semver_update_n'), 8001); + $this->assertSame(8001, $schema_version_handler->getInstalledVersion('update_test_schema')); + $this->assertSame(8001, $schema_version_handler->getInstalledVersion('update_test_semver_update_n')); // Ensure the index was added for column a. $this->assertTrue($connection->schema()->indexExists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.'); // Ensure update_test_semver_update_n_update_8001 was run.