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 @@ -314,10 +314,11 @@ */ public function getFullQualifiedTableName($table) { $options = $this->getConnectionOptions(); + $schema = $options['schema'] ?? 'public'; // The fully qualified table name in PostgreSQL is in the form of // ... - return $options['database'] . '.' . $options['schema'] . '.' . $this->getPrefix() . $table; + return $options['database'] . '.' . $schema . '.' . $this->getPrefix() . $table;; } /** 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 @@ -131,16 +131,11 @@ // Generate a key to reference this table's information on. $key = $this->connection->prefixTables('{' . $table . '}'); - $prefixInfo = $this->getPrefixInfo($table); - $prefixed_table = $prefixInfo['table']; // 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($prefixed_table, 'db_temporary_') === FALSE) { - $key = $prefixInfo['schema'] . '.' . $prefixed_table; + if (strpos($table, 'db_temporary_') !== FALSE) { + $key = $this->getTempNamespaceName() . '.' . $key; } - else { - $key = $this->getTempNamespaceName() . '.' . $prefixed_table; - } if (!isset($this->tableInformation[$key])) { @@ -175,7 +170,6 @@ $this->connection->rollbackSavepoint(); throw $e; } - $this->connection->releaseSavepoint(); // If the table information does not yet exist in the PostgreSQL @@ -223,7 +217,7 @@ */ protected function resetTableInformation($table) { $prefixInfo = $this->getPrefixInfo($table); - $key = $prefixInfo['schema'] . '.' . $prefixInfo['table']; + $key = $this->defaultSchema . '.' . $prefixInfo['table']; unset($this->tableInformation[$key]); } @@ -564,7 +558,7 @@ // Get the schema and tablename for the old table. $prefixInfo = $this->getPrefixInfo($table); - $schema = $prefixInfo['schema']; + $schema = $this->defaultSchema; $table_name = $prefixInfo['table']; // Index names and constraint names are global in PostgreSQL, so we need to // rename them when renaming the table. @@ -573,7 +567,13 @@ foreach ($indexes as $index) { // Append the schema to the index name. $index_short_name = $index->indexname; - $index_full_name = $schema . '.' . $index_short_name; + if ($schema != 'public') { + $index_full_name = $schema . '.' . $index_short_name; + $index_full_name = $this->connection->escapeTable($index_full_name); + } + else { + $index_full_name = '"' . $index_short_name . '"'; + } // Get the index type by suffix, e.g. idx/key/pkey. $index_type = substr($index_short_name, strrpos($index_short_name, '_') + 1); @@ -593,7 +593,6 @@ preg_match('/^' . preg_quote($table_name) . '__(.*)__' . preg_quote($index_type) . '/', $index_short_name, $matches); $index_name = $matches[1]; } - $index_full_name = $this->connection->escapeTable($index_full_name); $this->connection->query('ALTER INDEX ' . $index_full_name . ' RENAME TO ' . $this->ensureIdentifiersLength($new_name, $index_name, $index_type)); } 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 @@ -72,9 +72,15 @@ } } + $this->connection = Database::getConnection('default', 'default'); + Database::removeConnection('testing_fake'); + parent::tearDown(); } + /** + * Create sample data to test with alternative schema name. + */ protected function createSampleData(): void { $this->connection->insert('faking_table') ->fields( @@ -103,11 +109,13 @@ /** * @covers \Drupal\Core\Database\Driver\pgsql\Schema::extensionExists + * @covers \Drupal\Core\Database\Driver\pgsql\Schema::tableExists */ public function testPgsqlExtensionExists(): void { + // Check if PG_trgm extension is present. + $this->assertTrue($this->connection->schema()->extensionExists('pg_trgm')); // Asserting that the Schema testing_fake exist in the database. $this->assertCount(1, \Drupal::database()->query("SELECT * FROM pg_catalog.pg_namespace WHERE nspname = 'testing_fake'")->fetchAll()); - $this->assertTrue($this->connection->schema()->tableExists('faking_table')); // Hardcoded assertion that we created the table in the non-public schema.