EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
DBAL PostgreSQL fulltextd support re-established
Yurii72fcf092ebf1
diff --git a/application/Espo/Core/Utils/Database/Dbal/Drivers/PostgreSQLCustomDriver.php b/application/Espo/Core/Utils/Database/Dbal/Drivers/PostgreSQLCustomDriver.phpnew file mode 100644index 0000000000..d1c5399660--- /dev/null+++ b/application/Espo/Core/Utils/Database/Dbal/Drivers/PostgreSQLCustomDriver.php@@ -0,0 +1,173 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Utils\Database\Dbal\Drivers;++use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;+use Doctrine\DBAL\Driver\PDO\Connection;+use Doctrine\DBAL\Driver\PDO\Exception;+use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;+use Doctrine\DBAL\Driver\PDO\PDOConnect;+use Doctrine\DBAL\Platforms\PostgreSQLPlatform;+use Doctrine\DBAL\ServerVersionProvider;+use Espo\Core\Utils\Database\Dbal\Platforms\PostgresqlCustomPlatform;+use PDO;+use PDOException;+use SensitiveParameter;++/**+ * Code (except the `getDatabasePlatform` method) is from `Doctrine\DBAL\Driver\PDO\PgSQL`.+ * As it is final, it is copy-pasted.+ */+class PostgreSQLCustomDriver extends AbstractPostgreSQLDriver+{+ use PDOConnect;++ public function __construct(+ private ?string $textSearchConfig = null+ ) {}++ public function getDatabasePlatform(ServerVersionProvider $versionProvider): PostgreSQLPlatform+ {+ $platform = new PostgresqlCustomPlatform();++ $platform->setTextSearchConfig($this->textSearchConfig);++ return $platform;+ }++ public function connect(+ #[SensitiveParameter]+ array $params,+ ): Connection {+ $driverOptions = $params['driverOptions'] ?? [];++ if (! empty($params['persistent'])) {+ $driverOptions[PDO::ATTR_PERSISTENT] = true;+ }++ foreach (['user', 'password'] as $key) {+ if (isset($params[$key]) && ! is_string($params[$key])) {+ throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);+ }+ }++ $safeParams = $params;+ unset($safeParams['password']);++ try {+ $pdo = $this->doConnect(+ $this->constructPdoDsn($safeParams),+ $params['user'] ?? '',+ $params['password'] ?? '',+ $driverOptions,+ );+ } catch (PDOException $exception) {+ throw Exception::new($exception);+ }++ if (class_exists("Pdo\\Pgsql")) {+ $className = "Pdo\\Pgsql";++ $disablePreparesAttr = $className::ATTR_DISABLE_PREPARES;+ } else {+ $disablePreparesAttr = PDO::PGSQL_ATTR_DISABLE_PREPARES;+ }++ if (+ ! isset($driverOptions[$disablePreparesAttr])+ || $driverOptions[$disablePreparesAttr] === true+ ) {+ $pdo->setAttribute($disablePreparesAttr, true);+ }++ $connection = new Connection($pdo);++ /* defining client_encoding via SET NAMES to avoid inconsistent DSN support+ * - passing client_encoding via the 'options' param breaks pgbouncer support+ */+ if (isset($params['charset'])) {+ $connection->exec('SET NAMES \'' . $params['charset'] . '\'');+ }++ return $connection;+ }++ /**+ * Constructs the Postgres PDO DSN.+ *+ * @param array<string, mixed> $params+ */+ private function constructPdoDsn(array $params): string+ {+ $dsn = 'pgsql:';++ if (isset($params['host']) && $params['host'] !== '') {+ $dsn .= 'host=' . $params['host'] . ';';+ }++ if (isset($params['port']) && $params['port'] !== '') {+ $dsn .= 'port=' . $params['port'] . ';';+ }++ if (isset($params['dbname'])) {+ $dsn .= 'dbname=' . $params['dbname'] . ';';+ }++ if (isset($params['sslmode'])) {+ $dsn .= 'sslmode=' . $params['sslmode'] . ';';+ }++ if (isset($params['sslrootcert'])) {+ $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';+ }++ if (isset($params['sslcert'])) {+ $dsn .= 'sslcert=' . $params['sslcert'] . ';';+ }++ if (isset($params['sslkey'])) {+ $dsn .= 'sslkey=' . $params['sslkey'] . ';';+ }++ if (isset($params['sslcrl'])) {+ $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';+ }++ if (isset($params['application_name'])) {+ $dsn .= 'application_name=' . $params['application_name'] . ';';+ }++ if (isset($params['gssencmode'])) {+ $dsn .= 'gssencmode=' . $params['gssencmode'] . ';';+ }++ return $dsn;+ }+}diff --git a/application/Espo/Core/Utils/Database/Dbal/Factories/MysqlConnectionFactory.php b/application/Espo/Core/Utils/Database/Dbal/Factories/MysqlConnectionFactory.phpindex 8758def6ef..e3c4f810fb 100644--- a/application/Espo/Core/Utils/Database/Dbal/Factories/MysqlConnectionFactory.php+++ b/application/Espo/Core/Utils/Database/Dbal/Factories/MysqlConnectionFactory.php@@ -31,7 +31,6 @@ namespace Espo\Core\Utils\Database\Dbal\Factories;use Doctrine\DBAL\Connection;use Doctrine\DBAL\Driver\PDO\MySQL\Driver as PDOMySQLDriver;-use Doctrine\DBAL\Exception as DBALException;use Espo\Core\Utils\Database\Dbal\ConnectionFactory;use Espo\ORM\DatabaseParams;@@ -51,9 +50,6 @@ class MysqlConnectionFactory implements ConnectionFactoryprivate PDO $pdo,) {}- /**- * @throws DBALException- */public function create(DatabaseParams $databaseParams): Connection{$driver = new PDOMySQLDriver();diff --git a/application/Espo/Core/Utils/Database/Dbal/Factories/PostgresqlConnectionFactory.php b/application/Espo/Core/Utils/Database/Dbal/Factories/PostgresqlConnectionFactory.phpindex e1dea4c208..08f252c6e9 100644--- a/application/Espo/Core/Utils/Database/Dbal/Factories/PostgresqlConnectionFactory.php+++ b/application/Espo/Core/Utils/Database/Dbal/Factories/PostgresqlConnectionFactory.php@@ -30,10 +30,9 @@namespace Espo\Core\Utils\Database\Dbal\Factories;use Doctrine\DBAL\Connection;-use Doctrine\DBAL\Driver\PDO\PgSQL\Driver as PostgreSQLDriver;use Doctrine\DBAL\Exception as DBALException;use Espo\Core\Utils\Database\Dbal\ConnectionFactory;-use Espo\Core\Utils\Database\Dbal\Platforms\PostgresqlPlatform;+use Espo\Core\Utils\Database\Dbal\Drivers\PostgreSQLCustomDriver;use Espo\Core\Utils\Database\Helper;use Espo\ORM\DatabaseParams;use Espo\ORM\PDO\Options as PdoOptions;@@ -58,20 +57,21 @@ class PostgresqlConnectionFactory implements ConnectionFactory*/public function create(DatabaseParams $databaseParams): Connection{- $driver = new PostgreSQLDriver();+ $textSearchConfig = null;- if (!$databaseParams->getHost()) {- throw new RuntimeException("No database host in config.");+ if ($databaseParams->getName()) {+ $textSearchConfig = $this->helper->getParam('default_text_search_config');}- $platform = new PostgresqlPlatform();+ $driver = new PostgreSQLCustomDriver(+ textSearchConfig: $textSearchConfig,+ );- if ($databaseParams->getName()) {- $platform->setTextSearchConfig($this->helper->getParam('default_text_search_config'));+ if (!$databaseParams->getHost()) {+ throw new RuntimeException("No database host in config.");}$params = [- 'platform' => $platform,'pdo' => $this->pdo,'host' => $databaseParams->getHost(),'driverOptions' => PdoOptions::getOptionsFromDatabaseParams($databaseParams),diff --git a/application/Espo/Core/Utils/Database/Dbal/Platforms/PostgreSQLSchemaManager.php b/application/Espo/Core/Utils/Database/Dbal/Platforms/PostgreSQLSchemaManager.phpindex 452d152328..e195be076c 100644--- a/application/Espo/Core/Utils/Database/Dbal/Platforms/PostgreSQLSchemaManager.php+++ b/application/Espo/Core/Utils/Database/Dbal/Platforms/PostgreSQLSchemaManager.php@@ -29,11 +29,60 @@namespace Espo\Core\Utils\Database\Dbal\Platforms;+use Doctrine\DBAL\Exception;+use Doctrine\DBAL\Result;use Doctrine\DBAL\Schema\Index;use Doctrine\DBAL\Schema\PostgreSQLSchemaManager as BasePostgreSQLSchemaManager;+use RuntimeException;class PostgreSQLSchemaManager extends BasePostgreSQLSchemaManager{+ /**+ * Partially copy-pasted. Added parts to retrieve fulltext indexes too.+ *+ * @throws Exception+ */+ protected function selectIndexColumns(string $databaseName, ?string $tableName = null): Result+ {+ $params = [];++ $sql = sprintf(+ <<<'SQL'+ SELECT+ quote_ident(n.nspname) AS schema_name,+ quote_ident(c.relname) AS table_name,+ quote_ident(ic.relname) AS relname,+ i.indisunique,+ i.indisprimary,+ i.indkey,+ i.indrelid,+ pg_get_expr(indpred, indrelid) AS "where",+ quote_ident(attname) AS attname,+ -- added+ CASE+ WHEN keys.attnum = 0+ THEN pg_get_expr(i.indexprs, i.indrelid)+ ELSE quote_ident(a.attname)+ END AS attname+ FROM pg_index i+ JOIN pg_class AS c ON c.oid = i.indrelid+ JOIN pg_namespace n ON n.oid = c.relnamespace+ JOIN pg_class AS ic ON ic.oid = i.indexrelid+ JOIN LATERAL UNNEST(i.indkey) WITH ORDINALITY AS keys(attnum, ord)+ ON TRUE+ -- LEFT added+ LEFT JOIN pg_attribute a+ ON a.attrelid = c.oid+ AND a.attnum = keys.attnum+ WHERE %s+ ORDER BY 1, 2, keys.ord;+ SQL,+ implode(' AND ', $this->buildQueryConditions($tableName, $params)),+ );++ return $this->connection->executeQuery($sql, $params);+ }+/*** DBAL does not add the 'fulltext' flag on reverse engineering.*@@ -44,11 +93,7 @@ class PostgreSQLSchemaManager extends BasePostgreSQLSchemaManager$indexes = parent::_getPortableTableIndexesList($rows, $tableName);foreach ($rows as $row) {- $key = $row['key_name'];-- if (str_starts_with($tableName, '"') && str_ends_with($tableName, '"')) {- $tableName = substr($tableName, 1, -1);- }+ $key = $row['relname'];if ($key !== "idx_{$tableName}_system_full_text_search") {continue;@@ -56,7 +101,11 @@ class PostgreSQLSchemaManager extends BasePostgreSQLSchemaManager$sql = "SELECT indexdef FROM pg_indexes WHERE indexname = '$key'";- $items = $this->connection->fetchAllAssociative($sql);+ try {+ $items = $this->connection->fetchAllAssociative($sql);+ } catch (Exception $e) {+ throw new RuntimeException($e->getMessage(), previous: $e);+ }if (!$items) {continue;@@ -104,4 +153,33 @@ class PostgreSQLSchemaManager extends BasePostgreSQLSchemaManagerreturn array_map(fn ($item) => trim($item), explode(' ', $part));}++ /**+ * Copy-pasted the private method.+ *+ * @param list<int|string> $params+ * @return non-empty-list<string>+ */+ private function buildQueryConditions(?string $tableName, array &$params): array+ {+ $conditions = [];++ if ($tableName !== null) {+ if (str_contains($tableName, '.')) {+ [$schemaName, $tableName] = explode('.', $tableName);++ $conditions[] = 'n.nspname = ?';+ $params[] = $schemaName;+ } else {+ $conditions[] = 'n.nspname = ANY(current_schemas(false))';+ }++ $conditions[] = 'c.relname = ?';+ $params[] = $tableName;+ }++ $conditions[] = "n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')";++ return $conditions;+ }}diff --git a/application/Espo/Core/Utils/Database/Dbal/Platforms/PostgresqlPlatform.php b/application/Espo/Core/Utils/Database/Dbal/Platforms/PostgresqlCustomPlatform.phpsimilarity index 96%rename from application/Espo/Core/Utils/Database/Dbal/Platforms/PostgresqlPlatform.phprename to application/Espo/Core/Utils/Database/Dbal/Platforms/PostgresqlCustomPlatform.phpindex d762584bd9..f26b48b6a0 100644--- a/application/Espo/Core/Utils/Database/Dbal/Platforms/PostgresqlPlatform.php+++ b/application/Espo/Core/Utils/Database/Dbal/Platforms/PostgresqlCustomPlatform.php@@ -35,7 +35,7 @@ use Doctrine\DBAL\Schema\Index;use InvalidArgumentException;-class PostgresqlPlatform extends PostgreSQLPlatformDbal+class PostgresqlCustomPlatform extends PostgreSQLPlatformDbal{private const string TEXT_SEARCH_CONFIG = 'pg_catalog.simple';@@ -56,7 +56,7 @@ class PostgresqlPlatform extends PostgreSQLPlatformDbal*/public function getCreateIndexSQL(Index $index, string $table): string{- if (!$index->hasFlag('fulltext')) {+ if ($index->getType() !== Index\IndexType::FULLTEXT) {return parent::getCreateIndexSQL($index, $table);}diff --git a/tests/integration/Espo/Core/FulltextIndex/CheckCreatedIndexTest.php b/tests/integration/Espo/Core/FulltextIndex/CheckCreatedIndexTest.phpindex 92ef4557a0..a81a860850 100644--- a/tests/integration/Espo/Core/FulltextIndex/CheckCreatedIndexTest.php+++ b/tests/integration/Espo/Core/FulltextIndex/CheckCreatedIndexTest.php@@ -34,7 +34,6 @@ use Doctrine\DBAL\Schema\Table;use Espo\Core\ORM\DatabaseParamsFactory;use Espo\Core\Utils\Database\Schema\SchemaManager;use Espo\Core\Utils\Util;-use PDO;use PHPUnit\Framework\Attributes\DataProvider;use RuntimeException;use tests\integration\Core\BaseTestCase;@@ -57,6 +56,9 @@ class CheckCreatedIndexTest extends BaseTestCasepublic function testCreatedIndexes(string $entityType): void{if ($this->getPlatform() === 'Postgresql') {+ // DBAL uses two different codes to fetch indexes.+ // `getIndexes` won't work, but `introspectTableIndexes` might work.+ // Consider to implement the test.return;}@@ -72,14 +74,11 @@ class CheckCreatedIndexTest extends BaseTestCase$expectedList = array_map(fn (string $it) => Util::toUnderScore($it), $fulltextFieldList);-$table = $this->getTable(Util::camelCaseToUnderscore($entityType));- $indexes = $table->getIndexes();-$foundIndex = null;- foreach ($indexes as $index) {+ foreach ($table->getIndexes() as $index) {if ($index->getType() !== IndexType::FULLTEXT) {continue;}@@ -112,19 +111,11 @@ class CheckCreatedIndexTest extends BaseTestCase$helper = $manager->getDatabaseHelper();- $tables = $helper->getDbalConnection()+ $table = $helper->getDbalConnection()->createSchemaManager()- ->introspectTables();-- foreach ($tables as $table) {- if ($table->getObjectName()->getUnqualifiedName()->getValue() !== $name) {- continue;- }-- return $table;- }+ ->introspectTableByUnquotedName($name);- throw new RuntimeException("Table '$name' not found.");+ return $table ?? throw new RuntimeException("Table '$name' not found.");}private function getPlatform(): ?string