Commit b74bb8fa authored by catch's avatar catch
Browse files

Issue #3214922 by murilohp, daffie: Add a requirements error in Drupal 10 when...

Issue #3214922 by murilohp, daffie: Add a requirements error in Drupal 10 when PostgreSQL is used and the pg_trgm extension is not installed or created
parent 1f171b93
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -41,6 +41,10 @@ public function __construct() {
      'function' => 'checkStandardConformingStrings',
      'arguments' => [],
    ];
    $this->tasks[] = [
      'function' => 'checkExtensions',
      'arguments' => [],
    ];
    $this->tasks[] = [
      'function' => 'initializeDatabase',
      'arguments' => [],
@@ -238,6 +242,27 @@ protected function checkStandardConformingStringsSuccess() {
    return ($standard_conforming_strings == 'on');
  }

  /**
   * Generic function to check postgresql extensions.
   */
  public function checkExtensions() {
    $connection = Database::getConnection();
    try {
      if ($connection->schema()->extensionExists('pg_trgm')) {
        $this->pass(t('PostgreSQL has the pg_trgm extension enabled.'));
      }
      else {
        $this->fail(t('The <a href=":pg_trgm">pg_trgm</a> PostgreSQL extension is not present. The extension is 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',
        ]));
      }
    }
    catch (\Exception $e) {
      $this->fail(t('Drupal could not check for the pg_trgm extension: @error.', ['@error' => $e->getMessage()]));
    }
  }

  /**
   * Make PostgreSQL Drupal friendly.
   */
+24 −11
Original line number Diff line number Diff line
@@ -539,17 +539,30 @@ 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')) {
  if ($phase === 'runtime' || $phase === 'update') {
    if (Database::isActiveConnection()) {
      $connection = Database::getConnection();

      // Set the requirement just for postgres.
      if ($connection->driver() == 'pgsql') {
        $requirements['pgsql_extension_pg_trgm'] = [
        'severity' => REQUIREMENT_WARNING,
          'severity' => REQUIREMENT_OK,
          '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.', [
          'value' => t('Available'),
          'description' => 'The pg_trgm PostgreSQL extension is present.',
        ];

        // If the extension is not available, set the requirement error.
        if (!$connection->schema()->extensionExists('pg_trgm')) {
          $requirements['pgsql_extension_pg_trgm']['severity'] = REQUIREMENT_ERROR;
          $requirements['pgsql_extension_pg_trgm']['value'] = t('Not created');
          $requirements['pgsql_extension_pg_trgm']['description'] = t('The <a href=":pg_trgm">pg_trgm</a> PostgreSQL extension is not present. The extension is 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',
        ]),
      ];
          ]);
        }

      }
    }
  }

+10 −0
Original line number Diff line number Diff line
@@ -143,6 +143,16 @@ public function testStatusPage() {
    $session->pageTextNotContains('Deprecated themes enabled');
    $session->pageTextNotContains('Deprecated themes found: Test deprecated theme.');
    $this->assertSession()->elementNotExists('xpath', "//a[contains(@href, 'http://example.com/deprecated_theme')]");

    // Check if pg_trgm extension is enabled on postgres.
    if ($this->getDatabaseConnection()->databaseType() == 'pgsql') {
      $this->assertSession()->pageTextContains('PostgreSQL pg_trgm extension');
      $elements = $this->xpath('//details[@class="system-status-report__entry"]//div[contains(text(), :text)]', [
        ':text' => 'The pg_trgm PostgreSQL extension is present.',
      ]);
      $this->assertCount(1, $elements);
      $this->assertStringStartsWith('Available', $elements[0]->getParent()->getText());
    }
  }

}