diff --git a/core/lib/Drupal/Core/Extension/SchemaInstaller.php b/core/lib/Drupal/Core/Extension/SchemaInstaller.php index a18198e8d8..1aff3add12 100644 --- a/core/lib/Drupal/Core/Extension/SchemaInstaller.php +++ b/core/lib/Drupal/Core/Extension/SchemaInstaller.php @@ -12,27 +12,6 @@ */ class SchemaInstaller implements SchemaInstallerInterface { - /** - * Statically cached schema data. - * - * @var array - */ - protected $schema; - - /** - * Statically cached complete schema data. - * - * @var array - */ - protected $completeSchema; - - /** - * A static cache of schema currentVersions per module. - * - * @var array - */ - protected $allVersions = []; - /** * A static cache of current module schema currentVersions. * @@ -101,7 +80,7 @@ public function __construct(ModuleHandlerInterface $module_handler, CacheBackend * {@inheritdoc} */ public function getInstalledVersion($module) { - $this->initCurrentVersions(); + $this->ensureInitialized(); return $this->currentVersions[$module] ?? static::UNINSTALLED; } @@ -109,7 +88,7 @@ public function getInstalledVersion($module) { * {@inheritdoc} */ public function getInstalledVersions() { - $this->initCurrentVersions(); + $this->ensureInitialized(); return $this->currentVersions; } @@ -117,10 +96,9 @@ public function getInstalledVersions() { * {@inheritdoc} */ public function resetCache() { - $this->currentVersions = []; - if (!$this->currentVersions = $this->keyValue->get('system.schema')->getAll()) { - $this->currentVersions = []; - } + // Setting to NULL to re-read data on first use in initCurrentVersions(). + $this->currentVersions = NULL; + return $this; } /** @@ -128,9 +106,7 @@ public function resetCache() { */ public function setInstalledVersion($module, $version) { $this->keyValue->get('system.schema')->set($module, $version); - // Reset the static cache of module schema currentVersions. - $this->resetCache(); - $this->getInstalledVersions(); + $this->resetCache()->ensureInitialized(); } /** @@ -194,7 +170,7 @@ public function getSchema($module, $table = NULL) { * tables and fields to improve performance of serialize() and * unserialize(). Defaults to TRUE. */ - private function initialize(array &$schema, $module, $remove_descriptions = 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'])) { @@ -213,9 +189,11 @@ private function initialize(array &$schema, $module, $remove_descriptions = TRUE } /** - * Initialize the schema versions. + * Check if the static cache populated and initialize it if not. + * + * @see resetCache() */ - protected function initCurrentVersions() { + protected function ensureInitialized() { if (is_null($this->currentVersions)) { if (!$this->currentVersions = $this->keyValue->get('system.schema')->getAll()) { $this->currentVersions = []; diff --git a/core/lib/Drupal/Core/Extension/SchemaInstallerInterface.php b/core/lib/Drupal/Core/Extension/SchemaInstallerInterface.php index 02ac1d1820..9c7f99f3e8 100644 --- a/core/lib/Drupal/Core/Extension/SchemaInstallerInterface.php +++ b/core/lib/Drupal/Core/Extension/SchemaInstallerInterface.php @@ -18,9 +18,9 @@ interface SchemaInstallerInterface { * @param string $module * A module name. * - * @return string|int|array - * The currently installed schema version, static::UNINSTALLED if the - * module is not installed, or an array for all modules. + * @return string|int + * The currently installed schema version or static::UNINSTALLED if the + * module is not installed. */ public function getInstalledVersion($module); @@ -34,6 +34,8 @@ public function getInstalledVersions(); /** * Reset the version cache. + * + * @return $this */ public function resetCache(); diff --git a/core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php b/core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php index d57446e05e..748277b72f 100644 --- a/core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php +++ b/core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php @@ -143,7 +143,8 @@ public function testRequirements() { // successfully. $this->drupalLogin($this->updateUser); $update_script_test_config->set('requirement_type', REQUIREMENT_WARNING)->save(); - \Drupal::service('schema_installer')->setInstalledVersion('update_script_test', \Drupal::service('schema_installer')->getInstalledVersion('update_script_test') - 1); + $schema_installer = \Drupal::service('schema_installer'); + $schema_installer->setInstalledVersion('update_script_test', $schema_installer->getInstalledVersion('update_script_test') - 1); $this->drupalGet($this->updateUrl, ['external' => TRUE]); $this->assertText('This is a requirements warning provided by the update_script_test module.'); $this->clickLink('try again'); @@ -486,15 +487,13 @@ public function testSuccessfulUpdateFunctionality() { $final_maintenance_mode = $this->container->get('state')->get('system.maintenance_mode'); $this->assertEqual($final_maintenance_mode, $initial_maintenance_mode, 'Maintenance mode should not have changed after database updates.'); - // Reset the static cache to ensure we have the most current setting. + // Reset the static cache to get the current schema after running updates. $schema_installer = \Drupal::service('schema_installer'); $this->assertTrue($schema_installer instanceof SchemaInstallerInterface); - $schema_installer->resetCache(); - $schema_version = $schema_installer->getInstalledVersion('update_script_test'); + $schema_version = $schema_installer->resetCache()->getInstalledVersion('update_script_test'); $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_installer->resetCache(); $schema_installer->setInstalledVersion('update_script_test', $schema_version - 1); $schema_version = $schema_installer->getInstalledVersion('update_script_test'); $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.'); @@ -550,16 +549,13 @@ public function testSuccessfulMultilingualUpdateFunctionality() { $config->set('url.prefixes.en', 'en'); $config->save(); - // Reset the static cache to ensure we have the most current setting. /** @var \Drupal\Core\Extension\SchemaInstallerInterface $schema_installer */ $schema_installer = \Drupal::service('schema_installer'); - $schema_installer->resetCache(); $schema_version = $schema_installer->getInstalledVersion('update_script_test'); $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_installer->setInstalledVersion('update_script_test', $schema_version - 1); - $schema_installer->resetCache(); $schema_version = $schema_installer->getInstalledVersion('update_script_test'); $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.'); @@ -599,7 +595,6 @@ protected function runUpdates($maintenance_mode) { // Set the installed schema version to one less than the current update. $schema_installer->setInstalledVersion('update_script_test', $schema_version - 1); - $schema_installer->resetCache(); $schema_version = $schema_installer->getInstalledVersion('update_script_test'); $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.'); diff --git a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php index ac19cc44aa..4c1986124d 100644 --- a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php @@ -38,7 +38,6 @@ public function testDatabaseLoaded() { $schema_installer = \Drupal::service('schema_installer'); foreach (['user' => 8100, 'node' => 8700, 'system' => 8805, 'update_test_schema' => 8000] as $module => $schema) { $this->assertEqual($schema_installer->getInstalledVersion($module), $schema, new FormattableMarkup('Module @module schema is @schema', ['@module' => $module, '@schema' => $schema])); - } // Ensure that all {router} entries can be unserialized. If they cannot be diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index 4b5752c15f..be0175862f 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -699,7 +699,7 @@ protected function installConfig($modules) { * If $module is not enabled or the table schema cannot be found. */ protected function installSchema($module, $tables) { - // drupal_get_module_schema() is technically able to install a schema + // Schema installer is technically able to install a schema // of a non-enabled module, but its ability to load the module's .install // file depends on many other factors. To prevent differences in test // behavior and non-reproducible test failures, we only allow the schema of