Commit 535fb843 authored by catch's avatar catch
Browse files

Issue #2908886 by andypost, martin107, kim.pepper, daffie, aleevas, vacho,...

Issue #2908886 by andypost, martin107, kim.pepper, daffie, aleevas, vacho, catch, longwave, alexpott, voleger, dww: Split off schema management out of schema.inc
parent aea84a7c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -524,7 +524,7 @@ services:
    class: Drupal\Core\Extension\ModuleInstaller
    tags:
      - { name: service_collector, tag: 'module_install.uninstall_validator', call: addUninstallValidator }
    arguments: ['%app.root%', '@module_handler', '@kernel']
    arguments: ['%app.root%', '@module_handler', '@kernel', '@database']
    lazy: true
  extension.list.module:
    class: Drupal\Core\Extension\ModuleExtensionList
+28 −0
Original line number Diff line number Diff line
@@ -112,8 +112,15 @@ function drupal_set_installed_schema_version($module, $version) {
 *
 * @param string $module
 *   The module for which the tables will be created.
 *
 * @deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. No direct
 *   replacement is provided.
 *
 * @see https://www.drupal.org/node/2970993
 * @see \Drupal\Core\Extension\ModuleInstaller::installSchema()
 */
function drupal_install_schema($module) {
  @trigger_error('drupal_install_schema() is deprecated in drupal:9.2.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);

@@ -127,8 +134,15 @@ function drupal_install_schema($module) {
 *
 * @param string $module
 *   The module for which the tables will be removed.
 *
 * @deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. No direct
 *   replacement is provided.
 *
 * @see https://www.drupal.org/node/2970993
 * @see \Drupal\Core\Extension\ModuleInstaller::uninstallSchema()
 */
function drupal_uninstall_schema($module) {
  @trigger_error('drupal_uninstall_schema() is deprecated in drupal:9.2.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();
@@ -151,8 +165,16 @@ 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:9.2.0 and is removed from drupal:10.0.0. No direct
 *   replacement is provided. Testing classes could use
 *   \Drupal\TestTools\Extension\SchemaInspector for introspection.
 *
 * @see https://www.drupal.org/node/2970993
 * @see \Drupal\TestTools\Extension\SchemaInspector::getTablesSpecification()
 */
function drupal_get_module_schema($module, $table = NULL) {
  @trigger_error('drupal_get_module_schema() is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. No direct replacement is provided. Testing classes could use \Drupal\TestTools\Extension\SchemaInspector for introspection. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED);
  // Load the .install file to get hook_schema.
  module_load_install($module);
  $schema = \Drupal::moduleHandler()->invoke($module, 'schema');
@@ -181,8 +203,14 @@ function drupal_get_module_schema($module, $table = NULL) {
 *   (optional) Whether to additionally remove 'description' keys of all tables
 *   and fields to improve performance of serialize() and unserialize().
 *   Defaults to TRUE.
 *
 * @deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. No direct
 *   replacement is provided.
 *
 * @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:9.2.0 and is removed from drupal:10.0.0. No direct replacement is 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'])) {
+3 −1
Original line number Diff line number Diff line
@@ -390,7 +390,9 @@
 * ];
 * @endcode
 *
 * @see drupal_install_schema()
 * @see \Drupal\Core\Extension\ModuleInstaller::installSchema()
 * @see \Drupal\Core\Extension\ModuleInstaller::uninstallSchema()
 * @see \Drupal\TestTools\Extension\SchemaInspector::getTablesSpecification()
 *
 * @}
 */
+53 −3
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\DrupalKernelInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\FieldableEntityInterface;
@@ -44,6 +45,13 @@ class ModuleInstaller implements ModuleInstallerInterface {
   */
  protected $root;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The uninstall validators.
   *
@@ -60,14 +68,21 @@ class ModuleInstaller implements ModuleInstallerInterface {
   *   The module handler.
   * @param \Drupal\Core\DrupalKernelInterface $kernel
   *   The drupal kernel.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   *
   * @see \Drupal\Core\DrupalKernel
   * @see \Drupal\Core\CoreServiceProvider
   */
  public function __construct($root, ModuleHandlerInterface $module_handler, DrupalKernelInterface $kernel) {
  public function __construct($root, ModuleHandlerInterface $module_handler, DrupalKernelInterface $kernel, Connection $connection = NULL) {
    $this->root = $root;
    $this->moduleHandler = $module_handler;
    $this->kernel = $kernel;
    if (!$connection) {
      @trigger_error('The database connection must be passed to ' . __METHOD__ . '(). Creating ' . __CLASS__ . ' without it is deprecated in drupal:9.2.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/2970993', E_USER_DEPRECATED);
      $connection = \Drupal::service('database');
    }
    $this->connection = $connection;
  }

  /**
@@ -227,7 +242,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
        $this->moduleHandler->invokeAll('module_preinstall', [$module]);

        // Now install the module's schema if necessary.
        drupal_install_schema($module);
        $this->installSchema($module);

        // Clear plugin manager caches.
        \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
@@ -468,7 +483,7 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
      }

      // Remove the schema.
      drupal_uninstall_schema($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.
@@ -585,6 +600,7 @@ protected function updateKernel($module_filenames) {
    // dependencies.
    $container = $this->kernel->getContainer();
    $this->moduleHandler = $container->get('module_handler');
    $this->connection = $container->get('database');
  }

  /**
@@ -606,4 +622,38 @@ public function validateUninstall(array $module_list) {
    return $reasons;
  }

  /**
   * Creates all tables defined in a module's hook_schema().
   *
   * @param string $module
   *   The module for which the tables will be created.
   *
   * @internal
   */
  protected function installSchema(string $module): void {
    $tables = $this->moduleHandler->invoke($module, 'schema') ?? [];
    $schema = $this->connection->schema();
    foreach ($tables as $name => $table) {
      $schema->createTable($name, $table);
    }
  }

  /**
   * Removes all tables defined in a module's hook_schema().
   *
   * @param string $module
   *   The module for which the tables will be removed.
   *
   * @internal
   */
  protected function uninstallSchema(string $module): void {
    $tables = $this->moduleHandler->invoke($module, 'schema') ?? [];
    $schema = $this->connection->schema();
    foreach (array_keys($tables) as $table) {
      if ($schema->tableExists($table)) {
        $schema->dropTable($table);
      }
    }
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -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 = module_test_schema()['module_test'];
  Database::getConnection()->insert('module_test')
    ->fields([
      'data' => $schema['fields']['data']['type'],
Loading