diff -u b/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php --- b/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -382,7 +382,8 @@ assert(is_string($prefix), 'The \'$prefix\' argument to ' . __METHOD__ . '() must be a string'); $this->prefix = $prefix; - // Add the schema name of it is not set to public, else it will use the default schema name. + // Add the schema name of it is not set to public, else it will use the + // default schema name. $quoted_schema = ''; if (isset($this->connectionOptions['schema']) && ($this->connectionOptions['schema'] != 'public')) { $quoted_schema = $this->identifierQuotes[0] . $this->connectionOptions['schema'] . $this->identifierQuotes[1] . '.'; reverted: --- b/core/lib/Drupal/Core/Database/Schema.php +++ a/core/lib/Drupal/Core/Database/Schema.php @@ -81,7 +81,7 @@ * @return array * A keyed array with information about the schema, table name and prefix. */ + protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) { - public function getPrefixInfo($table = 'default', $add_prefix = TRUE) { $info = [ 'schema' => $this->defaultSchema, 'prefix' => $this->connection->getPrefix(), reverted: --- b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php +++ a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php @@ -22,7 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; +// cspell:ignore destid idmap sourceid -// cspell:ignore destid idmap sourceid psql /** * Defines the sql based ID map implementation. reverted: --- b/core/modules/mysql/src/Driver/Database/mysql/Schema.php +++ a/core/modules/mysql/src/Driver/Database/mysql/Schema.php @@ -47,7 +47,7 @@ * @return array * A keyed array with information about the database, table name and prefix. */ + protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) { - public function getPrefixInfo($table = 'default', $add_prefix = TRUE) { $info = ['prefix' => $this->connection->getPrefix()]; if ($add_prefix) { $table = $info['prefix'] . $table; 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 @@ -270,7 +270,6 @@ * * PostgreSQL has built in sequences. We'll use these instead of inserting * and updating a sequences table. - * @todo This has been changed, look back for this change */ public function nextId($existing = 0) { 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 @@ -56,11 +56,9 @@ public function __construct($connection) { parent::__construct($connection); - $connectionInfo = $connection->getConnectionOptions(); - // If the schema is not set in the connection options than schema defaults // to public. - $this->defaultSchema = $connectionInfo['schema'] ?? 'public'; + $this->defaultSchema = $connection->getConnectionOptions()['schema'] ?? 'public'; } /** @@ -216,8 +214,7 @@ * The non-prefixed name of the table. */ protected function resetTableInformation($table) { - $prefixInfo = $this->getPrefixInfo($table); - $key = $this->defaultSchema . '.' . $prefixInfo['table']; + $key = $this->defaultSchema . '.' . $this->connection->getPrefix() . $table; unset($this->tableInformation[$key]); } @@ -557,18 +554,16 @@ } // Get the schema and tablename for the old table. - $prefixInfo = $this->getPrefixInfo($table); - $schema = $this->defaultSchema; - $table_name = $prefixInfo['table']; + $table_name = $this->connection->getPrefix() . $table; // Index names and constraint names are global in PostgreSQL, so we need to // rename them when renaming the table. - $indexes = $this->connection->query('SELECT indexname FROM pg_indexes WHERE schemaname = :schema AND tablename = :table', [':schema' => $schema, ':table' => $table_name]); + $indexes = $this->connection->query('SELECT indexname FROM pg_indexes WHERE schemaname = :schema AND tablename = :table', [':schema' => $this->defaultSchema, ':table' => $table_name]); foreach ($indexes as $index) { // Append the schema to the index name. $index_short_name = $index->indexname; - if ($schema != 'public') { - $index_full_name = $schema . '.' . $index_short_name; + if ($this->defaultSchema != 'public') { + $index_full_name = $this->defaultSchema . '.' . $index_short_name; $index_full_name = $this->connection->escapeTable($index_full_name); } else { @@ -606,7 +601,7 @@ // The initial name of the sequence is generated automatically by // PostgreSQL when the table is created, so we need to use // pg_get_serial_sequence() to retrieve it. - $old_sequence = $this->connection->query("SELECT pg_get_serial_sequence('" . $schema . '.' . $table_name . "', '" . $field . "')")->fetchField(); + $old_sequence = $this->connection->query("SELECT pg_get_serial_sequence('" . $this->defaultSchema . '.' . $table_name . "', '" . $field . "')")->fetchField(); // If the new sequence name exceeds the maximum identifier length limit, // it will not match the pattern that is automatically applied by @@ -731,10 +726,9 @@ $index_name = str_replace('"', '', $index_name); // Get the schema and tablename for the old table. - $info = $this->getPrefixInfo($table); $sql_params = [ - ':schema' => $info['schema'], - ':table' => $info['table'], + ':schema' => $this->defaultSchema, + ':table' => $this->connection->getPrefix() . $table, ':index' => $index_name, ]; return (bool) $this->connection->query("SELECT 1 FROM pg_indexes WHERE schemaname = :schema AND tablename = :table AND indexname = :index", $sql_params)->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 @@ -8,12 +8,14 @@ use Drupal\KernelTests\Core\Database\DatabaseTestSchemaInstallTrait; use Drupal\KernelTests\Core\Database\DriverSpecificKernelTestBase; -// cSpell:ignore nspname schemaname Upserting indexdef +// cSpell:ignore nspname schemaname upserting indexdef /** - * Tests schema API for the PostgreSQL driver. + * Tests schema API for non-public schema for the PostgreSQL driver. * * @group Database + * @coversDefaultClass \Drupal\pgsql\Driver\Database\pgsql\Schema + * @coversDefaultClass \Drupal\pgsql\Driver\Database\pgsql\Connection */ class NonPublicSchemaTest extends DriverSpecificKernelTestBase { @@ -25,7 +27,7 @@ * * @var \Drupal\Core\Database\Connection */ - protected Connection $connection; + protected Connection $testingFakeConnection; /** * {@inheritdoc} @@ -33,12 +35,12 @@ protected function setUp(): void { parent::setUp(); - // Test the method for a non existing extension. + // Create a connection to the non-public schema. $info = Database::getConnectionInfo('default'); $info['default']['schema'] = 'testing_fake'; Database::addConnectionInfo('default', 'testing_fake', $info['default']); - $this->connection = Database::getConnection('testing_fake', 'default'); + $this->testingFakeConnection = Database::getConnection('testing_fake', 'default'); $table_specification = [ 'description' => 'Schema table description may contain "quotes" and could be long—very long indeed.', @@ -55,34 +57,9 @@ 'primary key' => ['id'], ]; - $this->connection->schema()->createTable('faking_table', $table_specification); + $this->testingFakeConnection->schema()->createTable('faking_table', $table_specification); - $this->createSampleData(); - } - - /** - * {@inheritdoc} - */ - protected function tearDown(): void { - // We overwrite this function because the regular teardown will not drop the tables from a specified schema. - $tables = $this->connection->schema()->findTables('%'); - foreach ($tables as $table) { - if ($this->connection->schema()->dropTable($table)) { - unset($tables[$table]); - } - } - - $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') + $this->testingFakeConnection->insert('faking_table') ->fields( [ 'id' => '1', @@ -90,7 +67,7 @@ ] )->execute(); - $this->connection->insert('faking_table') + $this->testingFakeConnection->insert('faking_table') ->fields( [ 'id' => '2', @@ -98,7 +75,7 @@ ] )->execute(); - $this->connection->insert('faking_table') + $this->testingFakeConnection->insert('faking_table') ->fields( [ 'id' => '3', @@ -108,56 +85,76 @@ } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::extensionExists - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::tableExists + * {@inheritdoc} */ - public function testPgsqlExtensionExists(): void { + protected function tearDown(): void { + // We overwrite this function because the regular teardown will not drop the + // tables from a specified schema. + $tables = $this->testingFakeConnection->schema()->findTables('%'); + foreach ($tables as $table) { + if ($this->testingFakeConnection->schema()->dropTable($table)) { + unset($tables[$table]); + } + } + + $this->assertEmpty($this->testingFakeConnection->schema()->findTables('%')); + + Database::removeConnection('testing_fake'); + + parent::tearDown(); + } + + /** + * @covers ::extensionExists + * @covers ::tableExists + */ + public function testExtensionExists(): void { // Check if PG_trgm extension is present. - $this->assertTrue($this->connection->schema()->extensionExists('pg_trgm')); + $this->assertTrue($this->testingFakeConnection->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')); + $this->assertTrue($this->testingFakeConnection->schema()->tableExists('faking_table')); // Hardcoded assertion that we created the table in the non-public schema. - $this->assertCount(1, $this->connection->query("SELECT * FROM pg_tables WHERE schemaname = 'testing_fake' AND tablename = :prefixedTable", [':prefixedTable' => $this->connection->getPrefix() . "faking_table"])->fetchAll()); + $this->assertCount(1, $this->testingFakeConnection->query("SELECT * FROM pg_tables WHERE schemaname = 'testing_fake' AND tablename = :prefixedTable", [':prefixedTable' => $this->testingFakeConnection->getPrefix() . "faking_table"])->fetchAll()); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::addField - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::fieldExists - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::dropField - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::changeField + * @covers ::addField + * @covers ::fieldExists + * @covers ::dropField + * @covers ::changeField */ - public function testPgsqlField(): void { - $this->connection->schema()->addField('faking_table', 'added_field', ['type' => 'int', 'not null' => FALSE]); - $this->assertTrue($this->connection->schema()->fieldExists('faking_table', 'added_field')); + public function testField(): void { + $this->testingFakeConnection->schema()->addField('faking_table', 'added_field', ['type' => 'int', 'not null' => FALSE]); + $this->assertTrue($this->testingFakeConnection->schema()->fieldExists('faking_table', 'added_field')); - $this->connection->schema()->changeField('faking_table', 'added_field', 'changed_field', ['type' => 'int', 'not null' => FALSE]); - $this->assertFalse($this->connection->schema()->fieldExists('faking_table', 'added_field')); - $this->assertTrue($this->connection->schema()->fieldExists('faking_table', 'changed_field')); + $this->testingFakeConnection->schema()->changeField('faking_table', 'added_field', 'changed_field', ['type' => 'int', 'not null' => FALSE]); + $this->assertFalse($this->testingFakeConnection->schema()->fieldExists('faking_table', 'added_field')); + $this->assertTrue($this->testingFakeConnection->schema()->fieldExists('faking_table', 'changed_field')); - $this->connection->schema()->dropField('faking_table', 'changed_field'); - $this->assertFalse($this->connection->schema()->fieldExists('faking_table', 'changed_field')); + $this->testingFakeConnection->schema()->dropField('faking_table', 'changed_field'); + $this->assertFalse($this->testingFakeConnection->schema()->fieldExists('faking_table', 'changed_field')); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Connection::insert - * @covers \Drupal\Core\Database\Driver\pgsql\Connection::select + * @covers ::insert + * @covers ::select */ - public function testPgsqlInsert(): void { - $num_records_before = $this->connection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); + public function testInsert(): void { + $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); - $this->connection->insert('faking_table') + $this->testingFakeConnection->insert('faking_table') ->fields([ 'id' => '123', 'test_field' => '55', ])->execute(); // Testing that the insert is correct. - $results = $this->connection->select('faking_table')->fields('faking_table')->condition('id', '123')->execute()->fetchAll(); + $results = $this->testingFakeConnection->select('faking_table')->fields('faking_table')->condition('id', '123')->execute()->fetchAll(); $this->assertIsArray($results); - $num_records_after = $this->connection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); + $num_records_after = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); $this->assertEquals($num_records_before + 1, $num_records_after, 'Merge inserted properly.'); $this->assertSame('123', $results[0]->id); @@ -165,28 +162,28 @@ } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Connection::update + * @covers ::update */ - public function testPgsqlUpdate(): void { - $updated_record = $this->connection->update('faking_table') + public function testUpdate(): void { + $updated_record = $this->testingFakeConnection->update('faking_table') ->fields(['test_field' => 321]) ->condition('id', 1) ->execute(); $this->assertSame(1, $updated_record, 'Updated 1 record.'); - $updated_results = $this->connection->select('faking_table')->fields('faking_table')->condition('id', '1')->execute()->fetchAll(); + $updated_results = $this->testingFakeConnection->select('faking_table')->fields('faking_table')->condition('id', '1')->execute()->fetchAll(); $this->assertSame('321', $updated_results[0]->test_field); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Connection::upsert + * @covers ::upsert */ - public function testPgsqlUpsert(): void { - $num_records_before = $this->connection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); + public function testUpsert(): void { + $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); - $upsert = $this->connection->upsert('faking_table') + $upsert = $this->testingFakeConnection->upsert('faking_table') ->key('id') ->fields(['id', 'test_field']); @@ -203,167 +200,162 @@ ]); $result = $upsert->execute(); - $this->assertIsInt($result); - $this->assertGreaterThanOrEqual(2, $result, 'The result of the upsert operation should report that at least two rows were affected.'); + $this->assertSame(2, $result, 'The result of the upsert operation should report that at exactly two rows were affected.'); - $num_records_after = $this->connection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); + $num_records_after = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); $this->assertEquals($num_records_before + 1, $num_records_after, 'Merge inserted properly.'); // Check if new row has been added with upsert. - $result = $this->connection->query('SELECT * FROM {faking_table} WHERE [id] = :id', [':id' => '456'])->fetch(); + $result = $this->testingFakeConnection->query('SELECT * FROM {faking_table} WHERE [id] = :id', [':id' => '456'])->fetch(); $this->assertEquals('456', $result->id, 'ID set correctly.'); $this->assertEquals('444', $result->test_field, 'test_field set correctly.'); // Check if new row has been edited with upsert. - $result = $this->connection->query('SELECT * FROM {faking_table} WHERE [id] = :id', [':id' => '1'])->fetch(); + $result = $this->testingFakeConnection->query('SELECT * FROM {faking_table} WHERE [id] = :id', [':id' => '1'])->fetch(); $this->assertEquals('1', $result->id, 'ID set correctly.'); $this->assertEquals('898', $result->test_field, 'test_field set correctly.'); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Connection::merge + * @covers ::merge */ - public function testPgsqlMerge(): void { - $num_records_before = $this->connection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); + public function testMerge(): void { + $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); - $this->connection->merge('faking_table') + $this->testingFakeConnection->merge('faking_table') ->key('id', '789') ->fields([ 'test_field' => 343, ]) ->execute(); - $num_records_after = $this->connection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); + $num_records_after = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); $this->assertEquals($num_records_before + 1, $num_records_after, 'Merge inserted properly.'); - $merge_results = $this->connection->select('faking_table')->fields('faking_table')->condition('id', '789')->execute()->fetchAll(); + $merge_results = $this->testingFakeConnection->select('faking_table')->fields('faking_table')->condition('id', '789')->execute()->fetchAll(); $this->assertSame('789', $merge_results[0]->id); $this->assertSame('343', $merge_results[0]->test_field); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Connection::delete - * @covers \Drupal\Core\Database\Driver\pgsql\Connection::truncate + * @covers ::delete + * @covers ::truncate */ - public function testPgsqlDelete(): void { - $num_records_before = $this->connection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); + public function testDelete(): void { + $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); - $num_deleted = $this->connection->delete('faking_table') + $num_deleted = $this->testingFakeConnection->delete('faking_table') ->condition('id', 3) ->execute(); $this->assertSame(1, $num_deleted, 'Deleted 1 record.'); - $num_records_after = $this->connection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); + $num_records_after = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField(); $this->assertEquals($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.'); - $num_records_before = $this->connection->query("SELECT COUNT(*) FROM {faking_table}")->fetchField(); + $num_records_before = $this->testingFakeConnection->query("SELECT COUNT(*) FROM {faking_table}")->fetchField(); $this->assertNotEmpty($num_records_before); - $this->connection->truncate('faking_table')->execute(); + $this->testingFakeConnection->truncate('faking_table')->execute(); - $num_records_after = $this->connection->query("SELECT COUNT(*) FROM {faking_table}")->fetchField(); + $num_records_after = $this->testingFakeConnection->query("SELECT COUNT(*) FROM {faking_table}")->fetchField(); $this->assertEquals(0, $num_records_after, 'Truncate really deletes everything.'); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::addIndex - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::indexExists - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::dropIndex + * @covers ::addIndex + * @covers ::indexExists + * @covers ::dropIndex */ - public function testPgsqlIndex(): void { - $this->connection->schema()->addIndex('faking_table', 'test_field', ['test_field'], []); + public function testIndex(): void { + $this->testingFakeConnection->schema()->addIndex('faking_table', 'test_field', ['test_field'], []); - $this->assertTrue($this->connection->schema()->indexExists('faking_table', 'test_field')); + $this->assertTrue($this->testingFakeConnection->schema()->indexExists('faking_table', 'test_field')); - $results = $this->connection->query("SELECT * FROM pg_indexes WHERE indexname = :indexname", [':indexname' => $this->connection->getPrefix() . 'faking_table__test_field__idx'])->fetchAll(); + $results = $this->testingFakeConnection->query("SELECT * FROM pg_indexes WHERE indexname = :indexname", [':indexname' => $this->testingFakeConnection->getPrefix() . 'faking_table__test_field__idx'])->fetchAll(); $this->assertCount(1, $results); $this->assertSame('testing_fake', $results[0]->schemaname); - $this->assertSame($this->connection->getPrefix() . 'faking_table', $results[0]->tablename); + $this->assertSame($this->testingFakeConnection->getPrefix() . 'faking_table', $results[0]->tablename); $this->assertStringContainsString('USING btree (test_field)', $results[0]->indexdef); - $this->connection->schema()->dropIndex('faking_table', 'test_field'); + $this->testingFakeConnection->schema()->dropIndex('faking_table', 'test_field'); - $this->assertFalse($this->connection->schema()->indexExists('faking_table', 'test_field')); + $this->assertFalse($this->testingFakeConnection->schema()->indexExists('faking_table', 'test_field')); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::addUniqueKey - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::indexExists - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::dropUniqueKey + * @covers ::addUniqueKey + * @covers ::indexExists + * @covers ::dropUniqueKey */ - public function testPgsqlUniqueKey(): void { - $this->connection->schema()->addUniqueKey('faking_table', 'test_field', ['test_field']); + public function testUniqueKey(): void { + $this->testingFakeConnection->schema()->addUniqueKey('faking_table', 'test_field', ['test_field']); // This should work, but currently indexExist() only searches for keys that end with idx. // @todo remove comments when: https://www.drupal.org/project/drupal/issues/3325358 is committed. - // $this->assertTrue($this->connection->schema()->indexExists('faking_table', 'test_field')); + // $this->assertTrue($this->testingFakeConnection->schema()->indexExists('faking_table', 'test_field')); - $results = $this->connection->query("SELECT * FROM pg_indexes WHERE indexname = :indexname", [':indexname' => $this->connection->getPrefix() . 'faking_table__test_field__key'])->fetchAll(); + $results = $this->testingFakeConnection->query("SELECT * FROM pg_indexes WHERE indexname = :indexname", [':indexname' => $this->testingFakeConnection->getPrefix() . 'faking_table__test_field__key'])->fetchAll(); // Check the unique key columns. $this->assertCount(1, $results); $this->assertSame('testing_fake', $results[0]->schemaname); - $this->assertSame($this->connection->getPrefix() . 'faking_table', $results[0]->tablename); + $this->assertSame($this->testingFakeConnection->getPrefix() . 'faking_table', $results[0]->tablename); $this->assertStringContainsString('USING btree (test_field)', $results[0]->indexdef); - $this->connection->schema()->dropUniqueKey('faking_table', 'test_field'); + $this->testingFakeConnection->schema()->dropUniqueKey('faking_table', 'test_field'); // This function will not work due to a the fact that indexExist() does not search for keys without idx tag. // @todo remove comments when: https://www.drupal.org/project/drupal/issues/3325358 is committed. - // $this->assertFalse($this->connection->schema()->indexExists('faking_table', 'test_field')); + // $this->assertFalse($this->testingFakeConnection->schema()->indexExists('faking_table', 'test_field')); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::addPrimaryKey - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::dropPrimaryKey + * @covers ::addPrimaryKey + * @covers ::dropPrimaryKey */ - public function testPgsqlPrimaryKey(): void { - $this->connection->schema()->dropPrimaryKey('faking_table'); - $results = $this->connection->query("SELECT * FROM pg_indexes WHERE schemaname = 'testing_fake'")->fetchAll(); + public function testPrimaryKey(): void { + $this->testingFakeConnection->schema()->dropPrimaryKey('faking_table'); + $results = $this->testingFakeConnection->query("SELECT * FROM pg_indexes WHERE schemaname = 'testing_fake'")->fetchAll(); $this->assertCount(0, $results); - $this->connection->schema()->addPrimaryKey('faking_table', ['id']); - $results = $this->connection->query("SELECT * FROM pg_indexes WHERE schemaname = 'testing_fake'")->fetchAll(); + $this->testingFakeConnection->schema()->addPrimaryKey('faking_table', ['id']); + $results = $this->testingFakeConnection->query("SELECT * FROM pg_indexes WHERE schemaname = 'testing_fake'")->fetchAll(); $this->assertCount(1, $results); $this->assertSame('testing_fake', $results[0]->schemaname); - $this->assertSame($this->connection->getPrefix() . 'faking_table', $results[0]->tablename); + $this->assertSame($this->testingFakeConnection->getPrefix() . 'faking_table', $results[0]->tablename); $this->assertStringContainsString('USING btree (id)', $results[0]->indexdef); - $find_primary_keys_columns = new \ReflectionMethod(get_class($this->connection->schema()), 'findPrimaryKeyColumns'); - $results = $find_primary_keys_columns->invoke($this->connection->schema(), 'faking_table'); + $find_primary_keys_columns = new \ReflectionMethod(get_class($this->testingFakeConnection->schema()), 'findPrimaryKeyColumns'); + $results = $find_primary_keys_columns->invoke($this->testingFakeConnection->schema(), 'faking_table'); $this->assertCount(1, $results); $this->assertSame('id', $results[0]); } /** - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::renameTable - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::tableExists - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::findTables - * @covers \Drupal\Core\Database\Driver\pgsql\Schema::dropTable + * @covers ::renameTable + * @covers ::tableExists + * @covers ::findTables + * @covers ::dropTable */ - public function testPgsqlTable(): void { - $this->connection->schema()->renameTable('faking_table', 'new_faking_table'); + public function testTable(): void { + $this->testingFakeConnection->schema()->renameTable('faking_table', 'new_faking_table'); - $oldTableExists = $this->connection->schema()->tableExists('faking_table'); - $newTableExists = $this->connection->schema()->tableExists('new_faking_table'); - $tables = $this->connection->schema()->findTables('%'); - $result = $this->connection->query("SELECT * FROM information_schema.tables WHERE table_schema = 'testing_fake'")->fetchAll(); - $this->assertFalse($oldTableExists); - $this->assertTrue($newTableExists); - $this->assertEquals($this->connection->getPrefix() . 'new_faking_table', $result[0]->table_name); + $tables = $this->testingFakeConnection->schema()->findTables('%'); + $result = $this->testingFakeConnection->query("SELECT * FROM information_schema.tables WHERE table_schema = 'testing_fake'")->fetchAll(); + $this->assertFalse($this->testingFakeConnection->schema()->tableExists('faking_table')); + $this->assertTrue($this->testingFakeConnection->schema()->tableExists('new_faking_table')); + $this->assertEquals($this->testingFakeConnection->getPrefix() . 'new_faking_table', $result[0]->table_name); $this->assertEquals('testing_fake', $result[0]->table_schema); sort($tables); $this->assertEquals(['new_faking_table'], $tables); - $this->connection->schema()->dropTable('new_faking_table'); - $newTableExists = $this->connection->schema()->tableExists('new_faking_table'); - $result = $this->connection->query("SELECT * FROM information_schema.tables WHERE table_schema = 'testing_fake'")->fetchAll(); - $this->assertFalse($newTableExists); - $this->assertCount(0, $result); + $this->testingFakeConnection->schema()->dropTable('new_faking_table'); + $this->assertFalse($this->testingFakeConnection->schema()->tableExists('new_faking_table')); + $this->assertCount(0, $this->testingFakeConnection->query("SELECT * FROM information_schema.tables WHERE table_schema = 'testing_fake'")->fetchAll()); } } reverted: --- b/core/modules/pgsql/tests/src/Unit/SchemaTest.php +++ a/core/modules/pgsql/tests/src/Unit/SchemaTest.php @@ -51,32 +51,20 @@ $statement = $this->createMock('\Drupal\Core\Database\StatementInterface'); $statement->expects($this->any()) ->method('fetchField') + ->willReturn($max_identifier_length); - ->will($this->onConsecutiveCalls($max_identifier_length, 1)); + $this->connection->expects($this->exactly(2)) - $this->connection->expects($this->any()) ->method('query') ->withConsecutive( [$this->anything()], ["SELECT 1 FROM pg_constraint WHERE conname = '$expected'"], ) + ->willReturnOnConsecutiveCalls( + $statement, + $this->createMock('\Drupal\Core\Database\StatementInterface'), + ); - ->will($this->returnValueMap([ - ["SHOW max_identifier_length", [], [], $statement], - ["SELECT 1 FROM pg_constraint WHERE conname = '$expected'", [], [], $statement], - ])); + $schema->constraintExists($table_name, $name); - $this->connection->expects($this->any()) - ->method('tablePrefix') - ->willReturn(''); - - $this->connection->expects($this->any()) - ->method('escapeTable') - ->willReturnMap([ - ['user_field_data____pkey' , '"user_field_data____pkey"'], - ['user_field_data__name__key', '"user_field_data__name__key"'], - ['drupal_BGGYAXgbqlAF1rMOyFTdZGj9zIMXZtSvEjMAKZ9wGIk_key', '"drupal_BGGYAXgbqlAF1rMOyFTdZGj9zIMXZtSvEjMAKZ9wGIk_key"'], - ]); - - $this->assertTrue($schema->constraintExists($table_name, $name)); } /**