diff --git a/core/includes/schema.inc b/core/includes/schema.inc index 05fe519a97..314939471d 100644 --- a/core/includes/schema.inc +++ b/core/includes/schema.inc @@ -113,8 +113,14 @@ function drupal_set_installed_schema_version($module, $version) { * * @param string $module * The module for which the tables will be created. + * + * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct + * replacement is provided. + * + * @see https://www.drupal.org/node/2970993 */ function drupal_install_schema($module) { + @trigger_error('drupal_install_schema() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); $schema = drupal_get_module_schema($module); _drupal_schema_initialize($schema, $module, FALSE); @@ -129,14 +135,22 @@ function drupal_install_schema($module) { * @param string $module * The module for which the tables will be removed. * - * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use - * Calling this function has no effect + * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct + * replacement is provided. * * @see https://www.drupal.org/node/2970993 */ function drupal_uninstall_schema($module) { @trigger_error('drupal_uninstall_schema() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED); + $tables = drupal_get_module_schema($module); + _drupal_schema_initialize($tables, $module, FALSE); + $schema = \Drupal::database()->schema(); + foreach ($tables as $table) { + if ($schema->tableExists($table['name'])) { + $schema->dropTable($table['name']); } + } +} /** * Returns a module's schema. @@ -181,8 +195,8 @@ function drupal_get_module_schema($module, $table = NULL) { * and fields to improve performance of serialize() and unserialize(). * Defaults to TRUE. * - * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No - * direct replacement is provided. + * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct + * replacement is provided. * * @see https://www.drupal.org/node/2970993 */ diff --git a/core/lib/Drupal/Core/Database/database.api.php b/core/lib/Drupal/Core/Database/database.api.php index a7ccdc32a0..9743bbe3ae 100644 --- a/core/lib/Drupal/Core/Database/database.api.php +++ b/core/lib/Drupal/Core/Database/database.api.php @@ -390,7 +390,7 @@ * ]; * @endcode * - * @see drupal_install_schema() + * @see \Drupal\Core\Extension\ModuleInstaller::installSchema() * * @} */ diff --git a/core/lib/Drupal/Core/Extension/ModuleInstaller.php b/core/lib/Drupal/Core/Extension/ModuleInstaller.php index 534ca41e47..cd66f9607b 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstaller.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstaller.php @@ -240,7 +240,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) { $this->moduleHandler->invokeAll('module_preinstall', [$module]); // Now install the module's schema if necessary. - $this->schemaInstall($module); + $this->installSchema($module); // Clear plugin manager caches. \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions(); @@ -479,7 +479,7 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { } // Remove the schema. - $this->schemaUninstall($module); + $this->uninstallSchema($module); // Remove the module's entry from the config. Don't check schema when // uninstalling a module since we are only clearing a key. @@ -617,27 +617,6 @@ public function validateUninstall(array $module_list) { return $reasons; } - /** - * 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. - */ - private function schemaInitialize(array &$schema, $module) { - // 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; - } - } - } - /** * Creates all tables defined in a module's hook_schema(). * @@ -648,13 +627,12 @@ private function schemaInitialize(array &$schema, $module) { * @param string $module * The module for which the tables will be created. */ - private function schemaInstall($module) { - $schema = drupal_get_module_schema($module); - $this->schemaInitialize($schema, $module); - $connection_schema = $this->connection->schema(); + private function installSchema($module) { + $tables = $this->getSchemaTables($module); + $schema = $this->connection->schema(); - foreach ($schema as $name => $table) { - $connection_schema->createTable($name, $table); + foreach ($tables as $name => $table) { + $schema->createTable($name, $table); } } @@ -668,16 +646,38 @@ private function schemaInstall($module) { * @param string $module * The module for which the tables will be removed. */ - private function schemaUninstall($module) { - $tables = drupal_get_module_schema($module); - $this->schemaInitialize($tables, $module); - $connection_schema = $this->connection->schema(); + private function uninstallSchema($module) { + $tables = $this->getSchemaTables($module); + $schema = $this->connection->schema(); foreach ($tables as $table) { - if ($connection_schema->tableExists($table['name'])) { - $connection_schema->dropTable($table['name']); + if ($schema->tableExists($table['name'])) { + $schema->dropTable($table['name']); + } + } + } + + /** + * Fills in required default values for table definitions from hook_schema(). + * + * @param string $module + * The module for which hook_schema() should be invoked. + * + * @return array + * An array of schema definition provided by hook_schema(). + */ + private function getSchemaTables($module) { + $tables = $this->moduleHandler->invoke($module, 'schema'); + // Set the name and module key for all tables. + foreach ($tables as $name => &$table) { + if (empty($table['module'])) { + $table['module'] = $module; + } + if (!isset($table['name'])) { + $table['name'] = $name; } } + return $tables; } }