From c04b62d61d703219efbff3afcf0d0d70ba04ca63 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 23 Jun 2020 17:13:38 +0100
Subject: [PATCH] Issue #3143713 by andypost, Kumar Kundan, codersukanta,
 alexpott, martin107: drupal_get_schema_versions() should return integer
 versions

---
 core/includes/schema.inc                      |  2 +-
 core/modules/system/system.post_update.php    | 14 +++++++
 .../drupal-8.update-schema-version-int.php    | 21 ++++++++++
 .../Update/SchemaVersionUpdateTest.php        | 39 +++++++++++++++++++
 .../Core/Extension/UpdateSchemaTest.php       | 33 ++++++++++++++++
 5 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/system/tests/fixtures/update/drupal-8.update-schema-version-int.php
 create mode 100644 core/modules/system/tests/src/Functional/Update/SchemaVersionUpdateTest.php
 create mode 100644 core/tests/Drupal/KernelTests/Core/Extension/UpdateSchemaTest.php

diff --git a/core/includes/schema.inc b/core/includes/schema.inc
index e168e89556ef..9dc190e3cdfa 100644
--- a/core/includes/schema.inc
+++ b/core/includes/schema.inc
@@ -47,7 +47,7 @@ function drupal_get_schema_versions($module) {
       // If this function is a module update function, add it to the list of
       // module updates.
       if (preg_match($regexp, $function, $matches)) {
-        $updates[$matches['module']][] = $matches['version'];
+        $updates[$matches['module']][] = (int) $matches['version'];
       }
     }
     // Ensure that updates are applied in numerical order.
diff --git a/core/modules/system/system.post_update.php b/core/modules/system/system.post_update.php
index 4cbd93f5a948..cf9a0cb807e6 100644
--- a/core/modules/system/system.post_update.php
+++ b/core/modules/system/system.post_update.php
@@ -147,3 +147,17 @@ function system_post_update_uninstall_stable() {
     // depending on it.
   }
 }
+
+/**
+ * Update schema version to integers.
+ *
+ * @see https://www.drupal.org/project/drupal/issues/3143713
+ */
+function system_post_update_schema_version_int() {
+  $registry = \Drupal::keyValue('system.schema');
+  foreach ($registry->getAll() as $name => $schema) {
+    if (is_string($schema)) {
+      $registry->set($name, (int) $schema);
+    }
+  }
+}
diff --git a/core/modules/system/tests/fixtures/update/drupal-8.update-schema-version-int.php b/core/modules/system/tests/fixtures/update/drupal-8.update-schema-version-int.php
new file mode 100644
index 000000000000..8f5283b2b0bd
--- /dev/null
+++ b/core/modules/system/tests/fixtures/update/drupal-8.update-schema-version-int.php
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * @file
+ * Database to mimic the installation of the update_test_schema module.
+ */
+
+use Drupal\Core\Database\Database;
+
+$connection = Database::getConnection();
+
+// Set the schema version.
+$connection->merge('key_value')
+  ->condition('collection', 'system.schema')
+  ->condition('name', 'update_test_schema')
+  ->fields([
+    'collection' => 'system.schema',
+    'name' => 'update_test_schema',
+    'value' => 's:4:"8901";',
+  ])
+  ->execute();
diff --git a/core/modules/system/tests/src/Functional/Update/SchemaVersionUpdateTest.php b/core/modules/system/tests/src/Functional/Update/SchemaVersionUpdateTest.php
new file mode 100644
index 000000000000..a0390f6a17d0
--- /dev/null
+++ b/core/modules/system/tests/src/Functional/Update/SchemaVersionUpdateTest.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Drupal\Tests\system\Functional\Update;
+
+use Drupal\FunctionalTests\Update\UpdatePathTestBase;
+
+/**
+ * Tests that updates clean-up non-integer schema version.
+ *
+ * @group Update
+ * @see system_post_update_schema_version_int()
+ */
+class SchemaVersionUpdateTest extends UpdatePathTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $modules = ['update_test_schema'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setDatabaseDumpFiles() {
+    $this->databaseDumpFiles = [
+      __DIR__ . '/../../../fixtures/update/drupal-8.8.0.bare.standard.php.gz',
+      __DIR__ . '/../../../fixtures/update/drupal-8.update-schema-version-int.php',
+    ];
+  }
+
+  /**
+   * Tests that upgrade converted string value to integer.
+   */
+  public function testSchemaVersionsIsInt() {
+    $this->assertSame('8901', \Drupal::keyValue('system.schema')->get('update_test_schema'));
+    $this->runUpdates();
+    $this->assertSame(8901, \Drupal::keyValue('system.schema')->get('update_test_schema'));
+  }
+
+}
diff --git a/core/tests/Drupal/KernelTests/Core/Extension/UpdateSchemaTest.php b/core/tests/Drupal/KernelTests/Core/Extension/UpdateSchemaTest.php
new file mode 100644
index 000000000000..607935a92d7c
--- /dev/null
+++ b/core/tests/Drupal/KernelTests/Core/Extension/UpdateSchemaTest.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace Drupal\KernelTests\Core\Extension;
+
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * Tests for schema and update includes.
+ *
+ * @group Core
+ */
+class UpdateSchemaTest extends KernelTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $modules = ['update_test_schema'];
+
+  /**
+   * Tests the function parses schema updates as integer numbers.
+   *
+   * @see drupal_get_schema_versions()
+   */
+  public function testDrupalGetSchemaVersionsInt() {
+    \Drupal::state()->set('update_test_schema_version', 8001);
+    $this->installSchema('update_test_schema', ['update_test_schema_table']);
+    $schema = drupal_get_schema_versions('update_test_schema');
+    foreach ($schema as $version) {
+      $this->assertIsInt($version);
+    }
+  }
+
+}
-- 
GitLab