From 2a40c8fc16ddad22ff9f24089662d04ac951d700 Mon Sep 17 00:00:00 2001 From: catch Date: Tue, 18 Jan 2022 11:32:59 +0000 Subject: [PATCH] =?UTF-8?q?Issue=20#3192487=20by=20andypost,=20murilohp,?= =?UTF-8?q?=20G=C3=A1bor=20Hojtsy,=20daffie,=20pobster,=20apaderno:=20Warn?= =?UTF-8?q?=20about=20upcoming=20JSON=20database=20support=20requirement?= =?UTF-8?q?=20in=20Drupal=209?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 206d6ee6587671a34267b14ac7aa2440e85dace2) --- core/lib/Drupal/Core/Database/Connection.php | 15 +++++++++++++++ .../src/Driver/Database/pgsql/Connection.php | 12 ++++++++++++ core/modules/system/system.install | 16 ++++++++++++++++ .../tests/src/Functional/System/StatusTest.php | 8 ++++++++ 4 files changed, 51 insertions(+) diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index cea7fc4028e..da910131e11 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -2185,4 +2185,19 @@ abstract class Connection { return \Drupal::service('pager.manager'); } + /** + * Runs a simple query to validate json datatype support. + * + * @return bool + * Returns the query result. + */ + public function hasJson(): bool { + try { + return (bool) $this->query('SELECT JSON_TYPE(\'1\')'); + } + catch (\Exception $e) { + return FALSE; + } + } + } diff --git a/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php index fc2ab6508f8..b36525b337e 100644 --- a/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php @@ -368,6 +368,18 @@ class Connection extends DatabaseConnection { } } + /** + * {@inheritDoc} + */ + public function hasJson(): bool { + try { + return (bool) $this->query('SELECT JSON_TYPEOF(\'1\')'); + } + catch (\Exception $e) { + return FALSE; + } + } + } /** diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 3a8a5b2dcb6..b24d7d1651a 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -487,6 +487,22 @@ function system_requirements($phase) { } } + if ($phase == 'runtime') { + // Test JSON support. To be required from Drupal 10.0. + // @todo remove in https://www.drupal.org/project/drupal/issues/3203193 + $requirements['database_support_json'] = [ + 'title' => t('Database support for JSON'), + 'severity' => REQUIREMENT_OK, + 'value' => t('Available'), + 'description' => t('Is required in Drupal 10.0.'), + ]; + + if (!Database::getConnection()->hasJson()) { + $requirements['database_support_json']['value'] = t('Not available'); + $requirements['database_support_json']['severity'] = REQUIREMENT_WARNING; + } + } + // Test PHP memory_limit $memory_limit = ini_get('memory_limit'); $requirements['php_memory_limit'] = [ diff --git a/core/modules/system/tests/src/Functional/System/StatusTest.php b/core/modules/system/tests/src/Functional/System/StatusTest.php index 562c491d1cf..f2d9fb479eb 100644 --- a/core/modules/system/tests/src/Functional/System/StatusTest.php +++ b/core/modules/system/tests/src/Functional/System/StatusTest.php @@ -91,6 +91,14 @@ class StatusTest extends BrowserTestBase { $this->drupalGet('admin/reports/status'); $this->assertSession()->elementExists('xpath', '//details[contains(@class, "system-status-report__entry")]//div[contains(text(), "Cron has not run recently")]'); \Drupal::state()->set('system.cron_last', $cron_last_run); + + // Check if JSON database support is enabled. + $this->assertSession()->pageTextContains('Database support for JSON'); + $elements = $this->xpath('//details[@class="system-status-report__entry"]//div[contains(text(), :text)]', [ + ':text' => 'Is required in Drupal 10.0.', + ]); + $this->assertCount(1, $elements); + $this->assertStringStartsWith('Available', $elements[0]->getParent()->getText()); } }