You are here

public function SchemaTestExtended::testAddChangeWithIndex in Drupal driver for SQL Server and SQL Azure 8.2

Test db_add_field() and db_change_field() with indexes.

File

tests/src/Kernel/SchemaTestExtended.php, line 266

Class

SchemaTestExtended
Tests table creation and modification via the schema API.

Namespace

Drupal\Tests\sqlsrv\Kernel

Code

public function testAddChangeWithIndex() {
  $table_spec = array(
    'fields' => array(
      'id' => array(
        'type' => 'int',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $this->connection
    ->schema()
    ->createTable('test_table', $table_spec);

  // Add a default value.
  $this->connection
    ->schema()
    ->addField('test_table', 'test', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 1,
  ), array(
    'indexes' => array(
      'id_test' => array(
        'id, test',
      ),
    ),
  ));
  $this
    ->assertTrue($this->connection
    ->schema()
    ->indexExists('test_table', 'id_test'), t('The index has been created by db_add_field().'));

  // Change the definition, we have by contract to remove the indexes before.
  $this->connection
    ->schema()
    ->dropIndex('test_table', 'id_test');
  $this
    ->assertFalse($this->connection
    ->schema()
    ->indexExists('test_table', 'id_test'), t('The index has been dropped.'));
  $this->connection
    ->schema()
    ->changeField('test_table', 'test', 'test', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 1,
  ), array(
    'indexes' => array(
      'id_test' => array(
        'id, test',
      ),
    ),
  ));
  $this
    ->assertTrue($this->connection
    ->schema()
    ->indexExists('test_table', 'id_test'), t('The index has been recreated by db_change_field().'));
}