diff --git a/core/includes/schema.inc b/core/includes/schema.inc index 753cc935dc..b3972c0210 100644 --- a/core/includes/schema.inc +++ b/core/includes/schema.inc @@ -83,10 +83,10 @@ function drupal_get_schema_versions($module) { * \Drupal\Core\Extension\SchemaInstallerInterface::getInstalledVersion() * instead. * - * @see https://www.drupal.org/node/2444417 + * @see https://www.drupal.org/node/2970993 */ function drupal_get_installed_schema_version($module, $reset = FALSE, $array = FALSE) { - @trigger_error('drupal_get_installed_schema_version() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::getInstalledVersion() instead. See https://www.drupal.org/node/2444417', E_USER_DEPRECATED); + @trigger_error('drupal_get_installed_schema_version() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::getInstalledVersion() instead. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); return \Drupal::service('schema_installer')->getInstalledVersion($module, $reset, $array); } @@ -102,10 +102,10 @@ function drupal_get_installed_schema_version($module, $reset = FALSE, $array = F * \Drupal\Core\Extension\SchemaInstallerInterface::setInstalledVersion() * instead. * - * @see https://www.drupal.org/node/2444417 + * @see https://www.drupal.org/node/2970993 */ function drupal_set_installed_schema_version($module, $version) { - @trigger_error('drupal_set_installed_schema_version() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::setInstalledVersion() instead. See https://www.drupal.org/node/2444417', E_USER_DEPRECATED); + @trigger_error('drupal_set_installed_schema_version() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::setInstalledVersion() instead. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); \Drupal::service('schema_installer')->setInstalledVersion($module, $version); } @@ -118,10 +118,10 @@ function drupal_set_installed_schema_version($module, $version) { * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use * \Drupal\Core\Extension\SchemaInstallerInterface::install() instead. * - * @see https://www.drupal.org/node/2444417 + * @see https://www.drupal.org/node/2970993 */ function drupal_install_schema($module) { - @trigger_error('drupal_install_schema() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::install() instead. See https://www.drupal.org/node/2444417', E_USER_DEPRECATED); + @trigger_error('drupal_install_schema() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::install() instead. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); \Drupal::service('schema_installer')->install($module); } @@ -134,10 +134,10 @@ function drupal_install_schema($module) { * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use * \Drupal\Core\Extension\SchemaInstallerInterface::uninstall() instead. * - * @see https://www.drupal.org/node/2444417 + * @see https://www.drupal.org/node/2970993 */ function drupal_uninstall_schema($module) { - @trigger_error('drupal_uninstall_schema() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::uninstall() instead. See https://www.drupal.org/node/2444417', E_USER_DEPRECATED); + @trigger_error('drupal_uninstall_schema() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::uninstall() instead. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); \Drupal::service('schema_installer')->uninstall($module); } @@ -153,22 +153,15 @@ function drupal_uninstall_schema($module) { * @param string $table * The name of the table. If not given, the module's complete schema * is returned. + * + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Extension\SchemaInstallerInterface::getSchema() instead. + * + * @see https://www.drupal.org/node/2970993 */ function drupal_get_module_schema($module, $table = NULL) { - // Load the .install file to get hook_schema. - module_load_install($module); - $schema = \Drupal::moduleHandler()->invoke($module, 'schema'); - - if (isset($table)) { - if (isset($schema[$table])) { - return $schema[$table]; - } - return []; - } - elseif (!empty($schema)) { - return $schema; - } - return []; + @trigger_error('drupal_get_module_schema() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Extension\SchemaInstallerInterface::uninstall() instead. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); + return \Drupal::service('schema_installer')->getSchema($module, $table); } /** @@ -187,10 +180,10 @@ function drupal_get_module_schema($module, $table = NULL) { * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. No * direct replacement provided. * - * @see https://www.drupal.org/node/2444417 + * @see https://www.drupal.org/node/2970993 */ function _drupal_schema_initialize(&$schema, $module, $remove_descriptions = TRUE) { - @trigger_error('_drupal_schema_initialize() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. No direct replacement provided. See https://www.drupal.org/node/2444417', E_USER_DEPRECATED); + @trigger_error('_drupal_schema_initialize() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. No direct replacement provided. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); // Set the name and module key for all tables. foreach ($schema as $name => &$table) { if (empty($table['module'])) { diff --git a/core/lib/Drupal/Core/Extension/SchemaInstaller.php b/core/lib/Drupal/Core/Extension/SchemaInstaller.php index 1b23e101fb..3acdf4b3cb 100644 --- a/core/lib/Drupal/Core/Extension/SchemaInstaller.php +++ b/core/lib/Drupal/Core/Extension/SchemaInstaller.php @@ -152,22 +152,9 @@ public function uninstall($module) { } /** - * Returns the module's schema. - * - * This function can be used to retrieve a schema specification in - * hook_schema(), so it allows you to derive your tables from existing - * specifications. - * - * It is also used by ::install() and ::uninstall() to ensure that a module's - * tables are created exactly as specified. - * - * @param string $module - * The module to which the table belongs. - * @param string $table - * (optional) The name of the table. Defaults to NULL, which means that the - * module's complete schema is returned. + * {@inheritdoc} */ - private function getSchema($module, $table = NULL) { + public function getSchema($module, $table = NULL) { // Load the .install file to get hook_schema. $this->moduleHandler->loadInclude($module, 'install'); $schema = $this->moduleHandler->invoke($module, 'schema'); diff --git a/core/lib/Drupal/Core/Extension/SchemaInstallerInterface.php b/core/lib/Drupal/Core/Extension/SchemaInstallerInterface.php index 4d4a9fd4da..466e612b08 100644 --- a/core/lib/Drupal/Core/Extension/SchemaInstallerInterface.php +++ b/core/lib/Drupal/Core/Extension/SchemaInstallerInterface.php @@ -64,4 +64,27 @@ public function install($module); */ public function uninstall($module); + /** + * Returns the module's schema. + * + * This function can be used to retrieve a schema specification in + * hook_schema(), so it allows you to derive your tables from existing + * specifications. + * + * It is also used by ::install() and ::uninstall() to ensure that a module's + * tables are created exactly as specified. + * + * @param string $module + * The module to which the table belongs. + * @param string $table + * (optional) The name of the table. Defaults to NULL, which means that the + * module's complete schema is returned. + * + * @return array + * An array of schema definition provided by hook_schema(). + * + * @see \hook_schema() + */ + public function getSchema($module, $table = NULL); + } diff --git a/core/modules/node/node.install b/core/modules/node/node.install index 557153eb59..40396a59fb 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -251,7 +251,7 @@ function node_update_8301() { * Fix realm column description on the node_access table. */ function node_update_8400() { - $schema = drupal_get_module_schema('node', 'node_access'); + $schema = \Drupal::service('schema_installer')->getSchema('node', 'node_access'); $schema['fields']['realm']['description'] = 'The realm in which the user must possess the grant ID. Modules can define one or more realms by implementing hook_node_grants().'; Database::getConnection()->schema()->changeField('node_access', 'realm', 'realm', $schema['fields']['realm']); } diff --git a/core/modules/simpletest/src/KernelTestBase.php b/core/modules/simpletest/src/KernelTestBase.php index 80c8eb0b7f..205f92d5a3 100644 --- a/core/modules/simpletest/src/KernelTestBase.php +++ b/core/modules/simpletest/src/KernelTestBase.php @@ -459,8 +459,9 @@ protected function installSchema($module, $tables) { } $tables = (array) $tables; + $schema_installer = \Drupal::service('schema_installer'); foreach ($tables as $table) { - $schema = drupal_get_module_schema($module, $table); + $schema = $schema_installer->getSchema($module, $table); if (empty($schema)) { // BC layer to avoid some contrib tests to fail. // @todo Remove the BC layer before 8.1.x release. diff --git a/core/modules/simpletest/src/Tests/KernelTestBaseTest.php b/core/modules/simpletest/src/Tests/KernelTestBaseTest.php index fc0d918b8a..eb8b2debd6 100644 --- a/core/modules/simpletest/src/Tests/KernelTestBaseTest.php +++ b/core/modules/simpletest/src/Tests/KernelTestBaseTest.php @@ -127,7 +127,7 @@ public function testEnableModulesInstall() { $this->assertTrue(in_array($module, $list), "{$module}_hook_info() in \Drupal::moduleHandler()->getImplementations() found."); $this->assertTrue(Database::getConnection()->schema()->tableExists($table), "'$table' database table found."); - $schema = drupal_get_module_schema($module, $table); + $schema = \Drupal::service('schema_installer')->getSchema($module, $table); $this->assertTrue($schema, "'$table' table schema found."); } @@ -159,7 +159,9 @@ public function testInstallSchema() { $this->assertTrue(Database::getConnection()->schema()->tableExists($table), "'$table' database table found."); // Verify that the schema is known to Schema API. - $schema = drupal_get_module_schema($module, $table); + /** @var \Drupal\Core\Extension\SchemaInstaller $schema_installer */ + $schema_installer = \Drupal::service('schema_installer'); + $schema = $schema_installer->getSchema($module, $table); $this->assertTrue($schema, "'$table' table schema found."); // Verify that a unknown table from an enabled module throws an error. @@ -172,7 +174,7 @@ public function testInstallSchema() { $this->pass('Exception for non-retrievable schema found.'); } $this->assertFalse(Database::getConnection()->schema()->tableExists($table), "'$table' database table not found."); - $schema = drupal_get_module_schema($module, $table); + $schema = $schema_installer->getSchema($module, $table); $this->assertFalse($schema, "'$table' table schema not found."); // Verify that a table from a unknown module cannot be installed. @@ -186,14 +188,14 @@ public function testInstallSchema() { $this->pass('Exception for non-retrievable schema found.'); } $this->assertFalse(Database::getConnection()->schema()->tableExists($table), "'$table' database table not found."); - $schema = drupal_get_module_schema($module, $table); + $schema = $schema_installer->getSchema($module, $table); $this->assertTrue($schema, "'$table' table schema found."); // Verify that the same table can be installed after enabling the module. $this->enableModules([$module]); $this->installSchema($module, $table); $this->assertTrue(Database::getConnection()->schema()->tableExists($table), "'$table' database table found."); - $schema = drupal_get_module_schema($module, $table); + $schema = $schema_installer->getSchema($module, $table); $this->assertTrue($schema, "'$table' table schema found."); } diff --git a/core/modules/system/src/Tests/Module/ModuleTestBase.php b/core/modules/system/src/Tests/Module/ModuleTestBase.php index e733dfa4c8..dae44f59a8 100644 --- a/core/modules/system/src/Tests/Module/ModuleTestBase.php +++ b/core/modules/system/src/Tests/Module/ModuleTestBase.php @@ -62,7 +62,7 @@ public function assertTableCount($base_table, $count = TRUE) { * The name of the module. */ public function assertModuleTablesExist($module) { - $tables = array_keys(drupal_get_module_schema($module)); + $tables = array_keys(\Drupal::service('schema_installer')->getSchema($module)); $tables_exist = TRUE; $schema = Database::getConnection()->schema(); foreach ($tables as $table) { @@ -80,7 +80,7 @@ public function assertModuleTablesExist($module) { * The name of the module. */ public function assertModuleTablesDoNotExist($module) { - $tables = array_keys(drupal_get_module_schema($module)); + $tables = array_keys(\Drupal::service('schema_installer')->getSchema($module)); $tables_exist = FALSE; $schema = Database::getConnection()->schema(); foreach ($tables as $table) { diff --git a/core/modules/system/tests/modules/module_test/module_test.install b/core/modules/system/tests/modules/module_test/module_test.install index 9d80d89605..9ec2788c5d 100644 --- a/core/modules/system/tests/modules/module_test/module_test.install +++ b/core/modules/system/tests/modules/module_test/module_test.install @@ -30,7 +30,7 @@ function module_test_schema() { * Implements hook_install(). */ function module_test_install() { - $schema = drupal_get_module_schema('module_test', 'module_test'); + $schema = \Drupal::service('schema_installer')->getSchema('module_test', 'module_test'); Database::getConnection()->insert('module_test') ->fields([ 'data' => $schema['fields']['data']['type'], diff --git a/core/modules/system/tests/src/Functional/Module/ModuleTestBase.php b/core/modules/system/tests/src/Functional/Module/ModuleTestBase.php index 40ef29fcc9..aede015045 100644 --- a/core/modules/system/tests/src/Functional/Module/ModuleTestBase.php +++ b/core/modules/system/tests/src/Functional/Module/ModuleTestBase.php @@ -55,7 +55,7 @@ public function assertTableCount($base_table, $count = TRUE) { * The name of the module. */ public function assertModuleTablesExist($module) { - $tables = array_keys(drupal_get_module_schema($module)); + $tables = array_keys(\Drupal::service('schema_installer')->getSchema($module)); $tables_exist = TRUE; $schema = Database::getConnection()->schema(); foreach ($tables as $table) { @@ -73,7 +73,7 @@ public function assertModuleTablesExist($module) { * The name of the module. */ public function assertModuleTablesDoNotExist($module) { - $tables = array_keys(drupal_get_module_schema($module)); + $tables = array_keys(\Drupal::service('schema_installer')->getSchema($module)); $tables_exist = FALSE; $schema = Database::getConnection()->schema(); foreach ($tables as $table) { diff --git a/core/modules/system/tests/src/Functional/Update/UpdateScriptTest.php b/core/modules/system/tests/src/Functional/Update/UpdateScriptTest.php index 6e95f07d40..b3ce07014f 100644 --- a/core/modules/system/tests/src/Functional/Update/UpdateScriptTest.php +++ b/core/modules/system/tests/src/Functional/Update/UpdateScriptTest.php @@ -208,12 +208,14 @@ public function testSuccessfulUpdateFunctionality() { $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. - $schema_version = \Drupal::service('schema_installer')->getInstalledVersion('update_script_test', TRUE); + /** @var \Drupal\Core\Extension\SchemaInstallerInterface $schema_installer */ + $schema_installer = \Drupal::service('schema_installer'); + $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. - \Drupal::service('schemainstaller')->setInstalledVersion('update_script_test', $schema_version - 1); - $schema_version = \Drupal::service('schema_installer')->getInstalledVersion('update_script_test', TRUE); + $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.'); // Click through update.php with 'access administration pages' and diff --git a/core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php b/core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php index 13ef94e4cb..98ecb2ae6f 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php @@ -18,7 +18,7 @@ public function testDefaultInsert() { $query = $this->connection->insert('test')->useDefaults(['job']); $id = $query->execute(); - $schema = drupal_get_module_schema('database_test', 'test'); + $schema = \Drupal::service('schema_installer')->getSchema('database_test', 'test'); $job = db_query('SELECT job FROM {test} WHERE id = :id', [':id' => $id])->fetchField(); $this->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.'); @@ -52,7 +52,7 @@ public function testDefaultInsertWithFields() { ->useDefaults(['job']); $id = $query->execute(); - $schema = drupal_get_module_schema('database_test', 'test'); + $schema = \Drupal::service('schema_installer')->getSchema('database_test', 'test'); $job = db_query('SELECT job FROM {test} WHERE id = :id', [':id' => $id])->fetchField(); $this->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.'); diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index acf85fd590..4e34f2388e 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -714,8 +714,9 @@ protected function installSchema($module, $tables) { throw new \LogicException("$module module is not enabled."); } $tables = (array) $tables; + $schema_installer = \Drupal::service('schema_installer'); foreach ($tables as $table) { - $schema = drupal_get_module_schema($module, $table); + $schema = $schema_installer->getSchema($module, $table); if (empty($schema)) { // BC layer to avoid some contrib tests to fail. if ($module == 'system') {