function SchemaTest::testPgsqlSequences

Same name in other branches
  1. 10 core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php \Drupal\Tests\pgsql\Kernel\pgsql\SchemaTest::testPgsqlSequences()

Tests if the new sequences get the right ownership.

File

core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php, line 252

Class

SchemaTest
Tests schema API for the PostgreSQL driver.

Namespace

Drupal\Tests\pgsql\Kernel\pgsql

Code

public function testPgsqlSequences() : void {
    $table_specification = [
        'description' => 'A test table with an ANSI reserved keywords for naming.',
        'fields' => [
            'uid' => [
                'description' => 'Simple unique ID.',
                'type' => 'serial',
                'not null' => TRUE,
            ],
            'update' => [
                'description' => 'A column with reserved name.',
                'type' => 'varchar',
                'length' => 255,
            ],
        ],
        'primary key' => [
            'uid',
        ],
        'unique keys' => [
            'having' => [
                'update',
            ],
        ],
        'indexes' => [
            'in' => [
                'uid',
                'update',
            ],
        ],
    ];
    // Creating a table.
    $table_name = 'sequence_test';
    $this->schema
        ->createTable($table_name, $table_specification);
    $this->assertTrue($this->schema
        ->tableExists($table_name));
    // Retrieves a sequence name that is owned by the table and column.
    $sequence_name = $this->connection
        ->query("SELECT pg_get_serial_sequence(:table, :column)", [
        ':table' => $this->connection
            ->getPrefix() . 'sequence_test',
        ':column' => 'uid',
    ])
        ->fetchField();
    $schema = $this->connection
        ->getConnectionOptions()['schema'] ?? 'public';
    $this->assertEquals($schema . '.' . $this->connection
        ->getPrefix() . 'sequence_test_uid_seq', $sequence_name);
    // Checks if the sequence exists.
    $this->assertTrue((bool) \Drupal::database()->query("SELECT c.relname FROM pg_class as c WHERE c.relkind = 'S' AND c.relname = :name", [
        ':name' => $this->connection
            ->getPrefix() . 'sequence_test_uid_seq',
    ])
        ->fetchField());
    // Retrieves the sequence owner object.
    $sequence_owner = \Drupal::database()->query("SELECT d.refobjid::regclass as table_name, a.attname as field_name\n      FROM pg_depend d\n      JOIN pg_attribute a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid\n      WHERE d.objid = :seq_name::regclass\n      AND d.refobjsubid > 0\n      AND d.classid = 'pg_class'::regclass", [
        ':seq_name' => $sequence_name,
    ])
        ->fetchObject();
    $this->assertEquals($this->connection
        ->getPrefix() . 'sequence_test', $sequence_owner->table_name);
    $this->assertEquals('uid', $sequence_owner->field_name, 'New sequence is owned by its table.');
}

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