SchemaTest.php 44.2 KB
Newer Older
1 2
<?php

3
namespace Drupal\KernelTests\Core\Database;
4 5

use Drupal\Core\Database\Database;
6
use Drupal\Core\Database\SchemaException;
7 8
use Drupal\Core\Database\SchemaObjectDoesNotExistException;
use Drupal\Core\Database\SchemaObjectExistsException;
9
use Drupal\KernelTests\KernelTestBase;
10
use Drupal\Component\Utility\Unicode;
11

12
/**
13 14
 * Tests table creation and modification via the schema API.
 *
15 16
 * @coversDefaultClass \Drupal\Core\Database\Schema
 *
17
 * @group Database
18
 */
19
class SchemaTest extends KernelTestBase {
20

21 22
  /**
   * A global counter for table and field creation.
23 24
   *
   * @var int
25
   */
26
  protected $counter;
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  /**
   * Connection to the database.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Database schema instance.
   *
   * @var \Drupal\Core\Database\Schema
   */
  protected $schema;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->connection = Database::getConnection();
    $this->schema = $this->connection->schema();
  }

51
  /**
52
   * Tests database interactions.
53
   */
54
  public function testSchema() {
55
    // Try creating a table.
56
    $table_specification = [
57
      'description' => 'Schema table description may contain "quotes" and could be long—very long indeed.',
58 59
      'fields' => [
        'id'  => [
60 61
          'type' => 'int',
          'default' => NULL,
62 63
        ],
        'test_field'  => [
64 65
          'type' => 'int',
          'not null' => TRUE,
66
          'description' => 'Schema table description may contain "quotes" and could be long—very long indeed. There could be "multiple quoted regions".',
67 68
        ],
        'test_field_string'  => [
69 70 71 72 73
          'type' => 'varchar',
          'length' => 20,
          'not null' => TRUE,
          'default' => "'\"funky default'\"",
          'description' => 'Schema column description for string.',
74 75
        ],
        'test_field_string_ascii'  => [
76 77 78
          'type' => 'varchar_ascii',
          'length' => 255,
          'description' => 'Schema column description for ASCII string.',
79 80 81
        ],
      ],
    ];
82
    $this->schema->createTable('test_table', $table_specification);
83 84

    // Assert that the table exists.
85
    $this->assertTrue($this->schema->tableExists('test_table'), 'The table exists.');
86

87 88 89 90 91 92
    // Assert that the table comment has been set.
    $this->checkSchemaComment($table_specification['description'], 'test_table');

    // Assert that the column comment has been set.
    $this->checkSchemaComment($table_specification['fields']['test_field']['description'], 'test_table', 'test_field');

93
    if ($this->connection->databaseType() === 'mysql') {
94
      // Make sure that varchar fields have the correct collation.
95
      $columns = $this->connection->query('SHOW FULL COLUMNS FROM {test_table}');
96 97
      foreach ($columns as $column) {
        if ($column->Field == 'test_field_string') {
98
          $string_check = ($column->Collation == 'utf8mb4_general_ci' || $column->Collation == 'utf8mb4_0900_ai_ci');
99 100 101 102 103 104 105 106 107
        }
        if ($column->Field == 'test_field_string_ascii') {
          $string_ascii_check = ($column->Collation == 'ascii_general_ci');
        }
      }
      $this->assertTrue(!empty($string_check), 'string field has the right collation.');
      $this->assertTrue(!empty($string_ascii_check), 'ASCII string field has the right collation.');
    }

108
    // An insert without a value for the column 'test_table' should fail.
109
    $this->assertFalse($this->tryInsert(), 'Insert without a default failed.');
110 111

    // Add a default value to the column.
112
    $this->schema->fieldSetDefault('test_table', 'test_field', 0);
113
    // The insert should now succeed.
114
    $this->assertTrue($this->tryInsert(), 'Insert with a default succeeded.');
115 116

    // Remove the default.
117
    $this->schema->fieldSetNoDefault('test_table', 'test_field');
118
    // The insert should fail again.
119
    $this->assertFalse($this->tryInsert(), 'Insert without a default failed.');
120

121
    // Test for fake index and test for the boolean result of indexExists().
122
    $index_exists = $this->schema->indexExists('test_table', 'test_field');
123
    $this->assertIdentical($index_exists, FALSE, 'Fake index does not exist');
124
    // Add index.
125
    $this->schema->addIndex('test_table', 'test_field', ['test_field'], $table_specification);
126
    // Test for created index and test for the boolean result of indexExists().
127
    $index_exists = $this->schema->indexExists('test_table', 'test_field');
128
    $this->assertIdentical($index_exists, TRUE, 'Index created.');
129

130
    // Rename the table.
131
    $this->schema->renameTable('test_table', 'test_table2');
132 133

    // Index should be renamed.
134
    $index_exists = $this->schema->indexExists('test_table2', 'test_field');
135
    $this->assertTrue($index_exists, 'Index was renamed.');
136

137
    // We need the default so that we can insert after the rename.
138
    $this->schema->fieldSetDefault('test_table2', 'test_field', 0);
139 140
    $this->assertFalse($this->tryInsert(), 'Insert into the old table failed.');
    $this->assertTrue($this->tryInsert('test_table2'), 'Insert into the new table succeeded.');
141 142

    // We should have successfully inserted exactly two rows.
143
    $count = $this->connection->query('SELECT COUNT(*) FROM {test_table2}')->fetchField();
144
    $this->assertEqual($count, 2, 'Two fields were successfully inserted.');
145 146

    // Try to drop the table.
147
    $this->schema->dropTable('test_table2');
148
    $this->assertFalse($this->schema->tableExists('test_table2'), 'The dropped table does not exist.');
149 150

    // Recreate the table.
151 152 153
    $this->schema->createTable('test_table', $table_specification);
    $this->schema->fieldSetDefault('test_table', 'test_field', 0);
    $this->schema->addField('test_table', 'test_serial', ['type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'Added column description.']);
154 155 156

    // Assert that the column comment has been set.
    $this->checkSchemaComment('Added column description.', 'test_table', 'test_serial');
157 158

    // Change the new field to a serial column.
159
    $this->schema->changeField('test_table', 'test_serial', 'test_serial', ['type' => 'serial', 'not null' => TRUE, 'description' => 'Changed column description.'], ['primary key' => ['test_serial']]);
160 161 162

    // Assert that the column comment has been set.
    $this->checkSchemaComment('Changed column description.', 'test_table', 'test_serial');
163

164
    $this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
165
    $max1 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
166
    $this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
167
    $max2 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
168
    $this->assertTrue($max2 > $max1, 'The serial is monotone.');
169

170
    $count = $this->connection->query('SELECT COUNT(*) FROM {test_table}')->fetchField();
171
    $this->assertEqual($count, 2, 'There were two rows.');
172

173
    // Test adding a serial field to an existing table.
174
    $this->schema->dropTable('test_table');
175 176 177
    $this->schema->createTable('test_table', $table_specification);
    $this->schema->fieldSetDefault('test_table', 'test_field', 0);
    $this->schema->addField('test_table', 'test_serial', ['type' => 'serial', 'not null' => TRUE], ['primary key' => ['test_serial']]);
178

179
    // Test the primary key columns.
180
    $method = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
181
    $method->setAccessible(TRUE);
182
    $this->assertSame(['test_serial'], $method->invoke($this->schema, 'test_table'));
183 184

    $this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
185
    $max1 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
186
    $this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
187
    $max2 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
188 189
    $this->assertTrue($max2 > $max1, 'The serial is monotone.');

190
    $count = $this->connection->query('SELECT COUNT(*) FROM {test_table}')->fetchField();
191 192 193
    $this->assertEqual($count, 2, 'There were two rows.');

    // Test adding a new column and form a composite primary key with it.
194
    $this->schema->addField('test_table', 'test_composite_primary_key', ['type' => 'int', 'not null' => TRUE, 'default' => 0], ['primary key' => ['test_serial', 'test_composite_primary_key']]);
195

196
    // Test the primary key columns.
197
    $this->assertSame(['test_serial', 'test_composite_primary_key'], $method->invoke($this->schema, 'test_table'));
198

199
    // Test renaming of keys and constraints.
200
    $this->schema->dropTable('test_table');
201 202 203
    $table_specification = [
      'fields' => [
        'id'  => [
204 205
          'type' => 'serial',
          'not null' => TRUE,
206 207
        ],
        'test_field'  => [
208 209
          'type' => 'int',
          'default' => 0,
210 211 212 213 214 215 216
        ],
      ],
      'primary key' => ['id'],
      'unique keys' => [
        'test_field' => ['test_field'],
      ],
    ];
217
    $this->schema->createTable('test_table', $table_specification);
218 219

    // Tests for indexes are Database specific.
220
    $db_type = $this->connection->databaseType();
221 222 223 224

    // Test for existing primary and unique keys.
    switch ($db_type) {
      case 'pgsql':
225 226
        $primary_key_exists = $this->schema->constraintExists('test_table', '__pkey');
        $unique_key_exists = $this->schema->constraintExists('test_table', 'test_field' . '__key');
227
        break;
228

229 230 231
      case 'sqlite':
        // SQLite does not create a standalone index for primary keys.
        $primary_key_exists = TRUE;
232
        $unique_key_exists = $this->schema->indexExists('test_table', 'test_field');
233
        break;
234

235
      default:
236 237
        $primary_key_exists = $this->schema->indexExists('test_table', 'PRIMARY');
        $unique_key_exists = $this->schema->indexExists('test_table', 'test_field');
238 239 240 241 242
        break;
    }
    $this->assertIdentical($primary_key_exists, TRUE, 'Primary key created.');
    $this->assertIdentical($unique_key_exists, TRUE, 'Unique key created.');

243
    $this->schema->renameTable('test_table', 'test_table2');
244 245 246 247

    // Test for renamed primary and unique keys.
    switch ($db_type) {
      case 'pgsql':
248 249
        $renamed_primary_key_exists = $this->schema->constraintExists('test_table2', '__pkey');
        $renamed_unique_key_exists = $this->schema->constraintExists('test_table2', 'test_field' . '__key');
250
        break;
251

252 253 254
      case 'sqlite':
        // SQLite does not create a standalone index for primary keys.
        $renamed_primary_key_exists = TRUE;
255
        $renamed_unique_key_exists = $this->schema->indexExists('test_table2', 'test_field');
256
        break;
257

258
      default:
259 260
        $renamed_primary_key_exists = $this->schema->indexExists('test_table2', 'PRIMARY');
        $renamed_unique_key_exists = $this->schema->indexExists('test_table2', 'test_field');
261 262 263 264 265 266 267 268
        break;
    }
    $this->assertIdentical($renamed_primary_key_exists, TRUE, 'Primary key was renamed.');
    $this->assertIdentical($renamed_unique_key_exists, TRUE, 'Unique key was renamed.');

    // For PostgreSQL check in addition that sequence was renamed.
    if ($db_type == 'pgsql') {
      // Get information about new table.
269 270
      $info = $this->schema->queryTableInformation('test_table2');
      $sequence_name = $this->schema->prefixNonTable('test_table2', 'id', 'seq');
271 272 273
      $this->assertEqual($sequence_name, current($info->sequences), 'Sequence was renamed.');
    }

274
    // Use database specific data type and ensure that table is created.
275
    $table_specification = [
276
      'description' => 'Schema table description.',
277 278
      'fields' => [
        'timestamp'  => [
279 280 281 282 283
          'mysql_type' => 'timestamp',
          'pgsql_type' => 'timestamp',
          'sqlite_type' => 'datetime',
          'not null' => FALSE,
          'default' => NULL,
284 285 286
        ],
      ],
    ];
287
    try {
288
      $this->schema->createTable('test_timestamp', $table_specification);
289
    }
290 291
    catch (\Exception $e) {
    }
292
    $this->assertTrue($this->schema->tableExists('test_timestamp'), 'Table with database specific datatype was created.');
293 294
  }

295 296 297 298 299
  /**
   * Tests that indexes on string fields are limited to 191 characters on MySQL.
   *
   * @see \Drupal\Core\Database\Driver\mysql\Schema::getNormalizedIndexes()
   */
300
  public function testIndexLength() {
301 302
    if ($this->connection->databaseType() !== 'mysql') {
      $this->markTestSkipped("The '{$this->connection->databaseType()}' database type does not support setting column length for indexes.");
303
    }
304

305 306 307
    $table_specification = [
      'fields' => [
        'id'  => [
308 309
          'type' => 'int',
          'default' => NULL,
310 311
        ],
        'test_field_text'  => [
312 313
          'type' => 'text',
          'not null' => TRUE,
314 315
        ],
        'test_field_string_long'  => [
316 317 318
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
319 320
        ],
        'test_field_string_ascii_long'  => [
321 322
          'type' => 'varchar_ascii',
          'length' => 255,
323 324
        ],
        'test_field_string_short'  => [
325 326 327
          'type' => 'varchar',
          'length' => 128,
          'not null' => TRUE,
328 329 330 331
        ],
      ],
      'indexes' => [
        'test_regular' => [
332 333 334 335
          'test_field_text',
          'test_field_string_long',
          'test_field_string_ascii_long',
          'test_field_string_short',
336 337 338 339 340 341 342 343 344
        ],
        'test_length' => [
          ['test_field_text', 128],
          ['test_field_string_long', 128],
          ['test_field_string_ascii_long', 128],
          ['test_field_string_short', 128],
        ],
        'test_mixed' => [
          ['test_field_text', 200],
345
          'test_field_string_long',
346
          ['test_field_string_ascii_long', 200],
347
          'test_field_string_short',
348 349 350
        ],
      ],
    ];
351
    $this->schema->createTable('test_table_index_length', $table_specification);
352 353 354 355 356 357

    // Ensure expected exception thrown when adding index with missing info.
    $expected_exception_message = "MySQL needs the 'test_field_text' field specification in order to normalize the 'test_regular' index";
    $missing_field_spec = $table_specification;
    unset($missing_field_spec['fields']['test_field_text']);
    try {
358
      $this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $missing_field_spec);
359 360 361 362 363 364 365
      $this->fail('SchemaException not thrown when adding index with missing information.');
    }
    catch (SchemaException $e) {
      $this->assertEqual($expected_exception_message, $e->getMessage());
    }

    // Add a separate index.
366
    $this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
367 368 369 370 371
    $table_specification_with_new_index = $table_specification;
    $table_specification_with_new_index['indexes']['test_separate'] = [['test_field_text', 200]];

    // Ensure that the exceptions of addIndex are thrown as expected.
    try {
372
      $this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
373 374 375 376 377 378 379
      $this->fail('\Drupal\Core\Database\SchemaObjectExistsException exception missed.');
    }
    catch (SchemaObjectExistsException $e) {
      $this->pass('\Drupal\Core\Database\SchemaObjectExistsException thrown when index already exists.');
    }

    try {
380
      $this->schema->addIndex('test_table_non_existing', 'test_separate', [['test_field_text', 200]], $table_specification);
381 382 383 384 385 386
      $this->fail('\Drupal\Core\Database\SchemaObjectDoesNotExistException exception missed.');
    }
    catch (SchemaObjectDoesNotExistException $e) {
      $this->pass('\Drupal\Core\Database\SchemaObjectDoesNotExistException thrown when index already exists.');
    }

387
    // Get index information.
388
    $results = $this->connection->query('SHOW INDEX FROM {test_table_index_length}');
389 390
    $expected_lengths = [
      'test_regular' => [
391 392 393 394
        'test_field_text' => 191,
        'test_field_string_long' => 191,
        'test_field_string_ascii_long' => NULL,
        'test_field_string_short' => NULL,
395 396
      ],
      'test_length' => [
397 398 399 400
        'test_field_text' => 128,
        'test_field_string_long' => 128,
        'test_field_string_ascii_long' => 128,
        'test_field_string_short' => NULL,
401 402
      ],
      'test_mixed' => [
403 404 405 406
        'test_field_text' => 191,
        'test_field_string_long' => 191,
        'test_field_string_ascii_long' => 200,
        'test_field_string_short' => NULL,
407 408
      ],
      'test_separate' => [
409
        'test_field_text' => 191,
410 411
      ],
    ];
412 413 414

    // Count the number of columns defined in the indexes.
    $column_count = 0;
415
    foreach ($table_specification_with_new_index['indexes'] as $index) {
416 417 418 419 420 421 422 423 424 425 426 427
      foreach ($index as $field) {
        $column_count++;
      }
    }
    $test_count = 0;
    foreach ($results as $result) {
      $this->assertEqual($result->Sub_part, $expected_lengths[$result->Key_name][$result->Column_name], 'Index length matches expected value.');
      $test_count++;
    }
    $this->assertEqual($test_count, $column_count, 'Number of tests matches expected value.');
  }

428 429 430
  /**
   * Tests inserting data into an existing table.
   *
431
   * @param string $table
432 433
   *   The database table to insert data into.
   *
434
   * @return bool
435 436
   *   TRUE if the insert succeeded, FALSE otherwise.
   */
437
  public function tryInsert($table = 'test_table') {
438
    try {
439 440
      $this->connection
        ->insert($table)
441
        ->fields(['id' => mt_rand(10, 20)])
442
        ->execute();
443 444
      return TRUE;
    }
445
    catch (\Exception $e) {
446 447 448
      return FALSE;
    }
  }
449 450 451 452 453 454 455 456 457 458 459

  /**
   * Checks that a table or column comment matches a given description.
   *
   * @param $description
   *   The asserted description.
   * @param $table
   *   The table to test.
   * @param $column
   *   Optional column to test.
   */
460
  public function checkSchemaComment($description, $table, $column = NULL) {
461 462
    if (method_exists($this->schema, 'getComment')) {
      $comment = $this->schema->getComment($table, $column);
463
      // The schema comment truncation for mysql is different.
464
      if ($this->connection->databaseType() === 'mysql') {
465 466 467
        $max_length = $column ? 255 : 60;
        $description = Unicode::truncate($description, $max_length, TRUE, TRUE);
      }
468
      $this->assertEqual($comment, $description, 'The comment matches the schema description.');
469 470
    }
  }
471

472 473 474
  /**
   * Tests creating unsigned columns and data integrity thereof.
   */
475
  public function testUnsignedColumns() {
476 477
    // First create the table with just a serial column.
    $table_name = 'unsigned_table';
478 479 480 481
    $table_spec = [
      'fields' => ['serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE]],
      'primary key' => ['serial_column'],
    ];
482
    $this->schema->createTable($table_name, $table_spec);
483 484

    // Now set up columns for the other types.
485
    $types = ['int', 'float', 'numeric'];
486
    foreach ($types as $type) {
487
      $column_spec = ['type' => $type, 'unsigned' => TRUE];
488
      if ($type == 'numeric') {
489
        $column_spec += ['precision' => 10, 'scale' => 0];
490 491 492
      }
      $column_name = $type . '_column';
      $table_spec['fields'][$column_name] = $column_spec;
493
      $this->schema->addField($table_name, $column_name, $column_spec);
494 495 496
    }

    // Finally, check each column and try to insert invalid values into them.
497
    foreach ($table_spec['fields'] as $column_name => $column_spec) {
498
      $this->assertTrue($this->schema->fieldExists($table_name, $column_name), format_string('Unsigned @type column was created.', ['@type' => $column_spec['type']]));
499
      $this->assertFalse($this->tryUnsignedInsert($table_name, $column_name), format_string('Unsigned @type column rejected a negative value.', ['@type' => $column_spec['type']]));
500 501 502 503 504 505
    }
  }

  /**
   * Tries to insert a negative value into columns defined as unsigned.
   *
506
   * @param string $table_name
507
   *   The table to insert.
508
   * @param string $column_name
509 510
   *   The column to insert.
   *
511
   * @return bool
512
   *   TRUE if the insert succeeded, FALSE otherwise.
513
   */
514
  public function tryUnsignedInsert($table_name, $column_name) {
515
    try {
516 517
      $this->connection
        ->insert($table_name)
518
        ->fields([$column_name => -1])
519
        ->execute();
520 521
      return TRUE;
    }
522
    catch (\Exception $e) {
523 524 525
      return FALSE;
    }
  }
526 527

  /**
528
   * Tests adding columns to an existing table with default and initial value.
529
   */
530
  public function testSchemaAddFieldDefaultInitial() {
531
    // Test varchar types.
532 533
    foreach ([1, 32, 128, 256, 512] as $length) {
      $base_field_spec = [
534 535
        'type' => 'varchar',
        'length' => $length,
536 537 538 539 540 541 542 543 544 545
      ];
      $variations = [
        ['not null' => FALSE],
        ['not null' => FALSE, 'default' => '7'],
        ['not null' => FALSE, 'default' => substr('"thing"', 0, $length)],
        ['not null' => FALSE, 'default' => substr("\"'hing", 0, $length)],
        ['not null' => TRUE, 'initial' => 'd'],
        ['not null' => FALSE, 'default' => NULL],
        ['not null' => TRUE, 'initial' => 'd', 'default' => '7'],
      ];
546 547 548 549 550 551 552 553

      foreach ($variations as $variation) {
        $field_spec = $variation + $base_field_spec;
        $this->assertFieldAdditionRemoval($field_spec);
      }
    }

    // Test int and float types.
554 555 556
    foreach (['int', 'float'] as $type) {
      foreach (['tiny', 'small', 'medium', 'normal', 'big'] as $size) {
        $base_field_spec = [
557 558
          'type' => $type,
          'size' => $size,
559 560 561 562 563 564 565
        ];
        $variations = [
          ['not null' => FALSE],
          ['not null' => FALSE, 'default' => 7],
          ['not null' => TRUE, 'initial' => 1],
          ['not null' => TRUE, 'initial' => 1, 'default' => 7],
          ['not null' => TRUE, 'initial_from_field' => 'serial_column'],
566 567 568 569 570
          [
            'not null' => TRUE,
            'initial_from_field' => 'test_nullable_field',
            'initial'  => 100,
          ],
571
        ];
572 573 574 575 576 577 578 579 580

        foreach ($variations as $variation) {
          $field_spec = $variation + $base_field_spec;
          $this->assertFieldAdditionRemoval($field_spec);
        }
      }
    }

    // Test numeric types.
581 582
    foreach ([1, 5, 10, 40, 65] as $precision) {
      foreach ([0, 2, 10, 30] as $scale) {
583
        // Skip combinations where precision is smaller than scale.
584 585 586 587
        if ($precision <= $scale) {
          continue;
        }

588
        $base_field_spec = [
589 590 591
          'type' => 'numeric',
          'scale' => $scale,
          'precision' => $precision,
592 593 594 595 596 597 598 599
        ];
        $variations = [
          ['not null' => FALSE],
          ['not null' => FALSE, 'default' => 7],
          ['not null' => TRUE, 'initial' => 1],
          ['not null' => TRUE, 'initial' => 1, 'default' => 7],
          ['not null' => TRUE, 'initial_from_field' => 'serial_column'],
        ];
600 601 602 603 604 605 606 607 608 609

        foreach ($variations as $variation) {
          $field_spec = $variation + $base_field_spec;
          $this->assertFieldAdditionRemoval($field_spec);
        }
      }
    }
  }

  /**
610
   * Asserts that a given field can be added and removed from a table.
611 612 613 614 615 616 617 618 619 620
   *
   * The addition test covers both defining a field of a given specification
   * when initially creating at table and extending an existing table.
   *
   * @param $field_spec
   *   The schema specification of the field.
   */
  protected function assertFieldAdditionRemoval($field_spec) {
    // Try creating the field on a new table.
    $table_name = 'test_table_' . ($this->counter++);
621 622 623
    $table_spec = [
      'fields' => [
        'serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
624
        'test_nullable_field' => ['type' => 'int', 'not null' => FALSE],
625
        'test_field' => $field_spec,
626 627 628
      ],
      'primary key' => ['serial_column'],
    ];
629
    $this->schema->createTable($table_name, $table_spec);
630
    $this->pass(format_string('Table %table created.', ['%table' => $table_name]));
631 632 633 634 635

    // Check the characteristics of the field.
    $this->assertFieldCharacteristics($table_name, 'test_field', $field_spec);

    // Clean-up.
636
    $this->schema->dropTable($table_name);
637 638 639

    // Try adding a field to an existing table.
    $table_name = 'test_table_' . ($this->counter++);
640 641 642
    $table_spec = [
      'fields' => [
        'serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
643
        'test_nullable_field' => ['type' => 'int', 'not null' => FALSE],
644 645 646
      ],
      'primary key' => ['serial_column'],
    ];
647
    $this->schema->createTable($table_name, $table_spec);
648
    $this->pass(format_string('Table %table created.', ['%table' => $table_name]));
649 650 651

    // Insert some rows to the table to test the handling of initial values.
    for ($i = 0; $i < 3; $i++) {
652 653
      $this->connection
        ->insert($table_name)
654
        ->useDefaults(['serial_column'])
655
        ->fields(['test_nullable_field' => 100])
656 657 658
        ->execute();
    }

659
    // Add another row with no value for the 'test_nullable_field' column.
660 661
    $this->connection
      ->insert($table_name)
662 663 664
      ->useDefaults(['serial_column'])
      ->execute();

665
    $this->schema->addField($table_name, 'test_field', $field_spec);
666
    $this->pass(format_string('Column %column created.', ['%column' => 'test_field']));
667 668 669 670 671

    // Check the characteristics of the field.
    $this->assertFieldCharacteristics($table_name, 'test_field', $field_spec);

    // Clean-up.
672
    $this->schema->dropField($table_name, 'test_field');
673 674 675

    // Add back the field and then try to delete a field which is also a primary
    // key.
676 677
    $this->schema->addField($table_name, 'test_field', $field_spec);
    $this->schema->dropField($table_name, 'serial_column');
678
    $this->schema->dropTable($table_name);
679 680 681
  }

  /**
682
   * Asserts that a newly added field has the correct characteristics.
683 684 685 686 687
   */
  protected function assertFieldCharacteristics($table_name, $field_name, $field_spec) {
    // Check that the initial value has been registered.
    if (isset($field_spec['initial'])) {
      // There should be no row with a value different then $field_spec['initial'].
688 689
      $count = $this->connection
        ->select($table_name)
690
        ->fields($table_name, ['serial_column'])
691 692 693 694
        ->condition($field_name, $field_spec['initial'], '<>')
        ->countQuery()
        ->execute()
        ->fetchField();
695
      $this->assertEqual($count, 0, 'Initial values filled out.');
696 697
    }

698
    // Check that the initial value from another field has been registered.
699
    if (isset($field_spec['initial_from_field']) && !isset($field_spec['initial'])) {
700 701
      // There should be no row with a value different than
      // $field_spec['initial_from_field'].
702 703
      $count = $this->connection
        ->select($table_name)
704
        ->fields($table_name, ['serial_column'])
705 706 707 708 709 710
        ->where($table_name . '.' . $field_spec['initial_from_field'] . ' <> ' . $table_name . '.' . $field_name)
        ->countQuery()
        ->execute()
        ->fetchField();
      $this->assertEqual($count, 0, 'Initial values from another field filled out.');
    }
711 712
    elseif (isset($field_spec['initial_from_field']) && isset($field_spec['initial'])) {
      // There should be no row with a value different than '100'.
713 714
      $count = $this->connection
        ->select($table_name)
715 716 717 718 719 720 721
        ->fields($table_name, ['serial_column'])
        ->condition($field_name, 100, '<>')
        ->countQuery()
        ->execute()
        ->fetchField();
      $this->assertEqual($count, 0, 'Initial values from another field or a default value filled out.');
    }
722

723 724 725
    // Check that the default value has been registered.
    if (isset($field_spec['default'])) {
      // Try inserting a row, and check the resulting value of the new column.
726 727
      $id = $this->connection
        ->insert($table_name)
728
        ->useDefaults(['serial_column'])
729
        ->execute();
730 731
      $field_value = $this->connection
        ->select($table_name)
732
        ->fields($table_name, [$field_name])
733 734 735
        ->condition('serial_column', $id)
        ->execute()
        ->fetchField();
736
      $this->assertEqual($field_value, $field_spec['default'], 'Default value registered.');
737 738
    }
  }
739 740

  /**
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
   * Tests various schema changes' effect on the table's primary key.
   *
   * @param array $initial_primary_key
   *   The initial primary key of the test table.
   * @param array $renamed_primary_key
   *   The primary key of the test table after renaming the test field.
   *
   * @dataProvider providerTestSchemaCreateTablePrimaryKey
   *
   * @covers ::addField
   * @covers ::changeField
   * @covers ::dropField
   * @covers ::findPrimaryKeyColumns
   */
  public function testSchemaChangePrimaryKey(array $initial_primary_key, array $renamed_primary_key) {
756
    $find_primary_key_columns = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
757 758 759 760 761 762
    $find_primary_key_columns->setAccessible(TRUE);

    // Test making the field the primary key of the table upon creation.
    $table_name = 'test_table';
    $table_spec = [
      'fields' => [
763 764
        'test_field' => ['type' => 'int', 'not null' => TRUE],
        'other_test_field' => ['type' => 'int', 'not null' => TRUE],
765 766 767
      ],
      'primary key' => $initial_primary_key,
    ];
768 769 770
    $this->schema->createTable($table_name, $table_spec);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
771 772

    // Change the field type and make sure the primary key stays in place.
773 774 775
    $this->schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'varchar', 'length' => 32, 'not null' => TRUE]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
776 777 778

    // Add some data and change the field type back, to make sure that changing
    // the type leaves the primary key in place even with existing data.
779 780
    $this->connection
      ->insert($table_name)
781 782
      ->fields(['test_field' => 1, 'other_test_field' => 2])
      ->execute();
783 784 785
    $this->schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
786 787 788

    // Make sure that adding the primary key can be done as part of changing
    // a field, as well.
789 790 791 792 793
    $this->schema->dropPrimaryKey($table_name);
    $this->assertEquals([], $find_primary_key_columns->invoke($this->schema, $table_name));
    $this->schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE], ['primary key' => $initial_primary_key]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
794 795

    // Rename the field and make sure the primary key was updated.
796 797 798
    $this->schema->changeField($table_name, 'test_field', 'test_field_renamed', ['type' => 'int', 'not null' => TRUE]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field_renamed'));
    $this->assertEquals($renamed_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
799 800

    // Drop the field and make sure the primary key was dropped, as well.
801 802 803
    $this->schema->dropField($table_name, 'test_field_renamed');
    $this->assertFalse($this->schema->fieldExists($table_name, 'test_field_renamed'));
    $this->assertEquals([], $find_primary_key_columns->invoke($this->schema, $table_name));
804 805 806

    // Add the field again and make sure adding the primary key can be done at
    // the same time.
807 808 809
    $this->schema->addField($table_name, 'test_field', ['type' => 'int', 'default' => 0, 'not null' => TRUE], ['primary key' => $initial_primary_key]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
810 811

    // Drop the field again and explicitly add a primary key.
812 813 814 815
    $this->schema->dropField($table_name, 'test_field');
    $this->schema->addPrimaryKey($table_name, ['other_test_field']);
    $this->assertFalse($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals(['other_test_field'], $find_primary_key_columns->invoke($this->schema, $table_name));
816 817 818

    // Test that adding a field with a primary key will work even with a
    // pre-existing primary key.
819 820 821
    $this->schema->addField($table_name, 'test_field', ['type' => 'int', 'default' => 0, 'not null' => TRUE], ['primary key' => $initial_primary_key]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
  }

  /**
   * Provides test cases for SchemaTest::testSchemaCreateTablePrimaryKey().
   *
   * @return array
   *   An array of test cases for SchemaTest::testSchemaCreateTablePrimaryKey().
   */
  public function providerTestSchemaCreateTablePrimaryKey() {
    $tests = [];

    $tests['simple_primary_key'] = [
      'initial_primary_key' => ['test_field'],
      'renamed_primary_key' => ['test_field_renamed'],
    ];
    $tests['composite_primary_key'] = [
      'initial_primary_key' => ['test_field', 'other_test_field'],
      'renamed_primary_key' => ['test_field_renamed', 'other_test_field'],
    ];
    $tests['composite_primary_key_different_order'] = [
      'initial_primary_key' => ['other_test_field', 'test_field'],
      'renamed_primary_key' => ['other_test_field', 'test_field_renamed'],
    ];

    return $tests;
  }

849 850 851 852 853 854 855 856 857 858 859 860
  /**
   * Tests an invalid field specification as a primary key on table creation.
   */
  public function testInvalidPrimaryKeyOnTableCreation() {
    // Test making an invalid field the primary key of the table upon creation.
    $table_name = 'test_table';
    $table_spec = [
      'fields' => [
        'test_field' => ['type' => 'int'],
      ],
      'primary key' => ['test_field'],
    ];
861
    $this->setExpectedException(SchemaException::class, "The 'test_field' field specification does not define 'not null' as TRUE.");
862
    $this->schema->createTable($table_name, $table_spec);
863 864 865 866 867 868 869 870 871 872 873 874 875 876
  }

  /**
   * Tests adding an invalid field specification as a primary key.
   */
  public function testInvalidPrimaryKeyAddition() {
    // Test adding a new invalid field to the primary key.
    $table_name = 'test_table';
    $table_spec = [
      'fields' => [
        'test_field' => ['type' => 'int', 'not null' => TRUE],
      ],
      'primary key' => ['test_field'],
    ];
877
    $this->schema->createTable($table_name, $table_spec);
878

879
    $this->setExpectedException(SchemaException::class, "The 'new_test_field' field specification does not define 'not null' as TRUE.");
880
    $this->schema->addField($table_name, 'new_test_field', ['type' => 'int'], ['primary key' => ['test_field', 'new_test_field']]);
881 882 883 884 885 886 887 888 889 890 891 892 893 894
  }

  /**
   * Tests changing the primary key with an invalid field specification.
   */
  public function testInvalidPrimaryKeyChange() {
    // Test adding a new invalid field to the primary key.
    $table_name = 'test_table';
    $table_spec = [
      'fields' => [
        'test_field' => ['type' => 'int', 'not null' => TRUE],
      ],
      'primary key' => ['test_field'],
    ];
895
    $this->schema->createTable($table_name, $table_spec);
896

897
    $this->setExpectedException(SchemaException::class, "The 'changed_test_field' field specification does not define 'not null' as TRUE.");
898 899
    $this->schema->dropPrimaryKey($table_name);
    $this->schema->changeField($table_name, 'test_field', 'changed_test_field', ['type' => 'int'], ['primary key' => ['changed_test_field']]);
900 901
  }

902 903
  /**
   * Tests changing columns between types with default and initial values.
904
   */
905
  public function testSchemaChangeFieldDefaultInitial() {
906 907 908 909 910 911 912 913
    $field_specs = [
      ['type' => 'int', 'size' => 'normal', 'not null' => FALSE],
      ['type' => 'int', 'size' => 'normal', 'not null' => TRUE, 'initial' => 1, 'default' => 17],
      ['type' => 'float', 'size' => 'normal', 'not null' => FALSE],
      ['type' => 'float', 'size' => 'normal', 'not null' => TRUE, 'initial' => 1, 'default' => 7.3],
      ['type' => 'numeric', 'scale' => 2, 'precision' => 10, 'not null' => FALSE],
      ['type' => 'numeric', 'scale' => 2, 'precision' => 10, 'not null' => TRUE, 'initial' => 1, 'default' => 7],
    ];
914 915 916 917 918 919 920 921 922 923

    foreach ($field_specs as $i => $old_spec) {
      foreach ($field_specs as $j => $new_spec) {
        if ($i === $j) {
          // Do not change a field into itself.
          continue;
        }
        $this->assertFieldChange($old_spec, $new_spec);
      }
    }
924

925 926 927 928 929 930
    $field_specs = [
      ['type' => 'varchar_ascii', 'length' => '255'],
      ['type' => 'varchar', 'length' => '255'],
      ['type' => 'text'],
      ['type' => 'blob', 'size' => 'big'],
    ];
931 932 933 934 935 936 937 938 939 940 941 942 943 944

    foreach ($field_specs as $i => $old_spec) {
      foreach ($field_specs as $j => $new_spec) {
        if ($i === $j) {
          // Do not change a field into itself.
          continue;
        }
        // Note if the serialized data contained an object this would fail on
        // Postgres.
        // @see https://www.drupal.org/node/1031122
        $this->assertFieldChange($old_spec, $new_spec, serialize(['string' => "This \n has \\\\ some backslash \"*string action.\\n"]));
      }
    }

945 946 947 948 949 950 951 952 953 954
  }

  /**
   * Asserts that a field can be changed from one spec to another.
   *
   * @param $old_spec
   *   The beginning field specification.
   * @param $new_spec
   *   The ending field specification.
   */
955
  protected function assertFieldChange($old_spec, $new_spec, $test_data = NULL) {
956
    $table_name = 'test_table_' . ($this->counter++);
957 958 959
    $table_spec = [
      'fields' => [
        'serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
960
        'test_field' => $old_spec,
961 962 963
      ],
      'primary key' => ['serial_column'],
    ];
964
    $this->schema->createTable($table_name, $table_spec);
965
    $this->pass(format_string('Table %table created.', ['%table' => $table_name]));
966 967 968 969 970

    // Check the characteristics of the field.
    $this->assertFieldCharacteristics($table_name, 'test_field', $old_spec);

    // Remove inserted rows.
971
    $this->connection->truncate($table_name)->execute();
972

973
    if ($test_data) {
974 975
      $id = $this->connection
        ->insert($table_name)
976 977 978 979
        ->fields(['test_field'], [$test_data])
        ->execute();
    }

980
    // Change the field.
981
    $this->schema->changeField($table_name, 'test_field', 'test_field', $new_spec);
982

983
    if ($test_data) {
984 985
      $field_value = $this->connection
        ->select($table_name)
986 987 988 989 990 991 992
        ->fields($table_name, ['test_field'])
        ->condition('serial_column', $id)
        ->execute()
        ->fetchField();
      $this->assertIdentical($field_value, $test_data);
    }

993 994 995 996
    // Check the field was changed.
    $this->assertFieldCharacteristics($table_name, 'test_field', $new_spec);

    // Clean-up.
997
    $this->schema->dropTable($table_name);
998
  }
999