function NonPublicSchemaTest::testUniqueKey

Same name in other branches
  1. 11.x core/modules/pgsql/tests/src/Kernel/pgsql/NonPublicSchemaTest.php \Drupal\Tests\pgsql\Kernel\pgsql\NonPublicSchemaTest::testUniqueKey()

@covers ::addUniqueKey @covers ::indexExists @covers ::dropUniqueKey

File

core/modules/pgsql/tests/src/Kernel/pgsql/NonPublicSchemaTest.php, line 293

Class

NonPublicSchemaTest
Tests schema API for non-public schema for the PostgreSQL driver.

Namespace

Drupal\Tests\pgsql\Kernel\pgsql

Code

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->testingFakeConnection->schema()->indexExists('faking_table', 'test_field'));
    $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->testingFakeConnection
        ->getPrefix() . 'faking_table', $results[0]->tablename);
    $this->assertStringContainsString('USING btree (test_field)', $results[0]->indexdef);
    $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->testingFakeConnection->schema()->indexExists('faking_table', 'test_field'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.