function DriverSpecificSchemaTestBase::testChangePrimaryKeyToSerial

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php \Drupal\KernelTests\Core\Database\DriverSpecificSchemaTestBase::testChangePrimaryKeyToSerial()

Tests converting an int to a serial when the int column has data.

File

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php, line 693

Class

DriverSpecificSchemaTestBase
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testChangePrimaryKeyToSerial() : void {
    // Test making an invalid field the primary key of the table upon creation.
    $table_name = 'test_table';
    $table_spec = [
        'fields' => [
            'test_field' => [
                'type' => 'int',
                'not null' => TRUE,
            ],
            'test_field_string' => [
                'type' => 'varchar',
                'length' => 20,
            ],
        ],
        'primary key' => [
            'test_field',
        ],
    ];
    $this->schema
        ->createTable($table_name, $table_spec);
    $this->tryInsertExpectsIntegrityConstraintViolationException($table_name);
    // @todo https://www.drupal.org/project/drupal/issues/3222127 Change the
    //   first item to 0 to test changing a field with 0 to a serial.
    // Create 8 rows in the table. Note that the 5 value is deliberately
    // omitted.
    foreach ([
        1,
        2,
        3,
        4,
        6,
        7,
        8,
        9,
    ] as $value) {
        $this->connection
            ->insert($table_name)
            ->fields([
            'test_field' => $value,
        ])
            ->execute();
    }
    $this->schema
        ->changeField($table_name, 'test_field', 'test_field', [
        'type' => 'serial',
        'not null' => TRUE,
    ]);
    $data = $this->connection
        ->select($table_name)
        ->fields($table_name, [
        'test_field',
    ])
        ->execute()
        ->fetchCol();
    $this->assertEquals([
        1,
        2,
        3,
        4,
        6,
        7,
        8,
        9,
    ], array_values($data));
    try {
        $this->connection
            ->insert($table_name)
            ->fields([
            'test_field' => 1,
        ])
            ->execute();
        $this->fail('Expected IntegrityConstraintViolationException not thrown');
    } catch (IntegrityConstraintViolationException) {
    }
    // Ensure auto numbering now works.
    // We use a >= assertion to allow non-core drivers, that may have specific
    // strategies on automatic incrementing, to run core tests. For example,
    // Oracle will allocate a 10 id with the previous insert that was meant to
    // fail; that id will be discarded, and the insert here will get a new 11
    // id instead.
    $id = $this->connection
        ->insert($table_name)
        ->fields([
        'test_field_string' => 'test',
    ])
        ->execute();
    $this->assertGreaterThanOrEqual(10, $id);
}

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