From 84e8b971c8d65ac93a3848802a1535d45b1585d3 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 8 Nov 2021 14:22:26 +0000
Subject: [PATCH] Issue #3214921 by daffie, xurizaemon, alexpott, mondrake,
 andypost, Taran2L, Mixologic, longwave: Add a requirements warning in Drupal
 9 when PostgreSQL is used and the pg_trgm extension is not created

---
 .../Core/Database/Driver/pgsql/Schema.php       | 17 +++++++++++++++++
 core/modules/system/system.install              | 15 +++++++++++++++
 .../KernelTests/Core/Database/SchemaTest.php    | 15 +++++++++++++++
 3 files changed, 47 insertions(+)

diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
index 106add49efb0..afb7f604fbde 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
@@ -1060,6 +1060,23 @@ protected function hashBase64($data) {
     return strtr($hash, ['+' => '_', '/' => '_', '=' => '']);
   }
 
+  /**
+   * Determines whether the PostgreSQL extension is created.
+   *
+   * @param string $name
+   *   The name of the extension.
+   *
+   * @return bool
+   *   Return TRUE when the extension is created, FALSE otherwise.
+   *
+   * @internal
+   */
+  public function extensionExists($name): bool {
+    return (bool) $this->connection->query('SELECT installed_version FROM pg_available_extensions WHERE name = :name', [
+      ':name' => $name,
+    ])->fetchField();
+  }
+
 }
 
 /**
diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index e19a96853d93..6eae0bb05095 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -472,6 +472,21 @@ function system_requirements($phase) {
     }
   }
 
+  // Test with PostgreSQL databases for the status of the pg_trgm extension.
+  if ($phase === 'runtime') {
+    if (Database::isActiveConnection() && (Database::getConnection()->driver() == 'pgsql') && !Database::getConnection()->schema()->extensionExists('pg_trgm')) {
+      $requirements['pgsql_extension_pg_trgm'] = [
+        'severity' => REQUIREMENT_WARNING,
+        'title' => t('PostgreSQL pg_trgm extension'),
+        'value' => t('Not created'),
+        'description' => t('The <a href=":pg_trgm">pg_trgm</a> PostgreSQL extension is not present. The extension will be required by Drupal 10 to improve performance when using PostgreSQL. See <a href=":requirements">Drupal database server requirements</a> for more information.', [
+          ':pg_trgm' => 'https://www.postgresql.org/docs/current/pgtrgm.html',
+          ':requirements' => 'https://www.drupal.org/docs/system-requirements/database-server-requirements',
+        ]),
+      ];
+    }
+  }
+
   // Test PHP memory_limit
   $memory_limit = ini_get('memory_limit');
   $requirements['php_memory_limit'] = [
diff --git a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
index da8578bc7386..039e916a33ab 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
@@ -1347,4 +1347,19 @@ public function testDefaultAfterAlter() {
     $this->assertSame('default value', $result->column7);
   }
 
+  /**
+   * @covers \Drupal\Core\Database\Driver\pgsql\Schema::extensionExists
+   */
+  public function testPgsqlExtensionExists() {
+    if ($this->connection->databaseType() !== 'pgsql') {
+      $this->markTestSkipped("This test only runs for PostgreSQL.");
+    }
+
+    // Test the method for a non existing extension.
+    $this->assertFalse($this->schema->extensionExists('non_existing_extension'));
+
+    // Test the method for an existing extension.
+    $this->assertTrue($this->schema->extensionExists('pg_trgm'));
+  }
+
 }
-- 
GitLab