Unverified Commit 26189f0a authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2738879 by mr.baileys, trobey, dawehner, alexpott, ravi.shankar, catch,...

Issue #2738879 by mr.baileys, trobey, dawehner, alexpott, ravi.shankar, catch, Fernly: system.schema can end up with missing schema information for some modules, resulting in hook_update_N() not getting called

(cherry picked from commit 8faf10c9)
(cherry picked from commit b57a81c7)
parent 2b977bcc
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -83,9 +83,10 @@ function drupal_get_installed_schema_version($module, $reset = FALSE, $array = F
  }

  if (!$versions) {
    if (!$versions = \Drupal::keyValue('system.schema')->getAll()) {
      $versions = [];
    }
    $versions = \Drupal::keyValue('system.schema')->getAll();
    $enabled_modules = \Drupal::moduleHandler()->getModuleList();
    $enabled_modules = array_fill_keys(array_keys($enabled_modules), \Drupal::CORE_MINIMUM_SCHEMA_VERSION);
    $versions = array_merge($enabled_modules, $versions);
  }

  if ($array) {
+1 −1
Original line number Diff line number Diff line
@@ -607,7 +607,7 @@ function update_retrieve_dependencies() {
  $return = [];
  // Get a list of installed modules, arranged so that we invoke their hooks in
  // the same order that \Drupal::moduleHandler()->invokeAll() does.
  foreach (\Drupal::keyValue('system.schema')->getAll() as $module => $schema) {
  foreach (drupal_get_installed_schema_version(NULL, FALSE, TRUE) as $module => $schema) {
    if ($schema == SCHEMA_UNINSTALLED) {
      // Nothing to upgrade.
      continue;
+5 −0
Original line number Diff line number Diff line
name: 'Update test'
type: module
description: 'Support module for update testing.'
package: Testing
version: VERSION
+13 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Install, update and uninstall functions for the update_test_no_preexisting module.
 */

/**
 * Dummy update_test_no_preexisting update 8001.
 */
function update_test_no_preexisting_update_8001() {
  \Drupal::state()->set('update_test_no_preexisting_update_8001', TRUE);
}
+55 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\system\Functional\UpdateSystem;

use Drupal\Core\Database\Database;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\UpdatePathTestTrait;

/**
 * Tries to update a module which has no pre-existing schema.
 *
 * @group Update
 * @group legacy
 */
class NoPreExistingSchemaUpdateTest extends BrowserTestBase {
  use UpdatePathTestTrait;

  protected function setUp() {
    parent::setUp();
    $connection = Database::getConnection();

    // Enable the update_test_no_preexisting module by altering the
    // core.extension configuration directly, so that the schema version
    // information is missing.
    $extensions = $connection->select('config')
      ->fields('config', ['data'])
      ->condition('name', 'core.extension')
      ->execute()
      ->fetchField();
    $extensions = unserialize($extensions);
    $connection->update('config')
      ->fields([
        'data' => serialize(array_merge_recursive($extensions, ['module' => ['update_test_no_preexisting' => 0]])),
      ])
      ->condition('name', 'core.extension')
      ->execute();
  }

  /**
   * Test the system module updates with no dependencies installed.
   */
  public function testNoPreExistingSchema() {
    $schema = \Drupal::keyValue('system.schema')->getAll();
    $this->assertArrayNotHasKey('update_test_no_preexisting', $schema);
    $this->assertFalse(\Drupal::state()->get('update_test_no_preexisting_update_8001', FALSE));

    $this->runUpdates();

    $schema = \Drupal::keyValue('system.schema')->getAll();
    $this->assertArrayHasKey('update_test_no_preexisting', $schema);
    $this->assertEquals('8001', $schema['update_test_no_preexisting']);
    $this->assertTrue(\Drupal::state()->get('update_test_no_preexisting_update_8001', FALSE));
  }

}