diff -u b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php --- b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php @@ -73,6 +73,12 @@ * Constructs a connection object. */ public function __construct(\PDO $connection, array $connection_options) { + // Sanitize the schema name here, so we do not have to do it in other + // functions. + if (isset($connection_options['schema']) && ($connection_options['schema'] !== 'public')) { + $connection_options['schema'] = preg_replace('/[^A-Za-z0-9_]+/', '', $connection_options['schema']); + } + // We need to set the connectionOptions before the parent, because setPrefix // needs this. $this->connectionOptions = $connection_options; @@ -99,8 +105,7 @@ // default schema name. $quoted_schema = ''; if (isset($this->connectionOptions['schema']) && ($this->connectionOptions['schema'] !== 'public')) { - $sanitizedSchema = preg_replace('/[^A-Za-z0-9_]+/', '', $this->connectionOptions['schema']); - $quoted_schema = $this->identifierQuotes[0] . $sanitizedSchema . $this->identifierQuotes[1] . '.'; + $quoted_schema = $this->identifierQuotes[0] . $this->connectionOptions['schema'] . $this->identifierQuotes[1] . '.'; } $this->tablePlaceholderReplacements = [ diff -u b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php --- b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php @@ -59,7 +59,6 @@ // If the schema is not set in the connection options then schema defaults // to public. $this->defaultSchema = $connection->getConnectionOptions()['schema'] ?? 'public'; - $this->defaultSchema = preg_replace('/[^A-Za-z0-9_]+/', '', $this->defaultSchema); } /** @@ -134,7 +133,7 @@ // Take into account that temporary tables are stored in a different schema. // \Drupal\Core\Database\Connection::generateTemporaryTableName() sets the // 'db_temporary_' prefix to all temporary tables. - if (strpos($table, 'db_temporary_') !== FALSE) { + if (str_contains($table, 'db_temporary_')) { $key = $quoted_key = $this->getTempNamespaceName() . '.' . $prefixed_table; } else { @@ -1104,7 +1103,7 @@ protected function getSequenceName(string $table, string $column): ?string { return $this->connection ->query("SELECT pg_get_serial_sequence(:table, :column)", [ - ':table' => $this->connection->getPrefix() . $table, + ':table' => $this->defaultSchema . '.' . $this->connection->getPrefix() . $table, ':column' => $column, ]) ->fetchField(); diff -u b/core/modules/pgsql/tests/src/Kernel/pgsql/NonPublicSchemaTest.php b/core/modules/pgsql/tests/src/Kernel/pgsql/NonPublicSchemaTest.php --- b/core/modules/pgsql/tests/src/Kernel/pgsql/NonPublicSchemaTest.php +++ b/core/modules/pgsql/tests/src/Kernel/pgsql/NonPublicSchemaTest.php @@ -15,7 +15,6 @@ * * @group Database * @coversDefaultClass \Drupal\pgsql\Driver\Database\pgsql\Schema - * @coversDefaultClass \Drupal\pgsql\Driver\Database\pgsql\Connection */ class NonPublicSchemaTest extends DriverSpecificKernelTestBase { @@ -138,8 +137,8 @@ } /** - * @covers ::insert - * @covers ::select + * @covers \Drupal\Core\Database\Connection::insert + * @covers \Drupal\Core\Database\Connection::select */ public function testInsert(): void { $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); @@ -162,7 +161,7 @@ } /** - * @covers ::update + * @covers \Drupal\Core\Database\Connection::update */ public function testUpdate(): void { $updated_record = $this->testingFakeConnection->update('faking_table') @@ -178,7 +177,7 @@ } /** - * @covers ::upsert + * @covers \Drupal\Core\Database\Connection::upsert */ public function testUpsert(): void { $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); @@ -217,7 +216,7 @@ } /** - * @covers ::merge + * @covers \Drupal\Core\Database\Connection::merge */ public function testMerge(): void { $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); @@ -238,8 +237,8 @@ } /** - * @covers ::delete - * @covers ::truncate + * @covers \Drupal\Core\Database\Connection::delete + * @covers \Drupal\Core\Database\Connection::truncate */ public function testDelete(): void { $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField();