Unverified Commit f58b7ce3 authored by alexpott's avatar alexpott

Issue #2987856 by voleger, mondrake, alexpott: Followup: Update Schema test

parent ee56c7d8
......@@ -20,6 +20,8 @@ class SchemaTest extends KernelTestBase {
/**
* A global counter for table and field creation.
*
* @var int
*/
protected $counter;
......@@ -50,8 +52,6 @@ protected function setUp() {
* Tests database interactions.
*/
public function testSchema() {
$schema = Database::getConnection()->schema();
// Try creating a table.
$table_specification = [
'description' => 'Schema table description may contain "quotes" and could be long—very long indeed.',
......@@ -79,10 +79,10 @@ public function testSchema() {
],
],
];
db_create_table('test_table', $table_specification);
$this->schema->createTable('test_table', $table_specification);
// Assert that the table exists.
$this->assertTrue(db_table_exists('test_table'), 'The table exists.');
$this->assertTrue($this->schema->tableExists('test_table'), 'The table exists.');
// Assert that the table comment has been set.
$this->checkSchemaComment($table_specification['description'], 'test_table');
......@@ -90,9 +90,9 @@ public function testSchema() {
// Assert that the column comment has been set.
$this->checkSchemaComment($table_specification['fields']['test_field']['description'], 'test_table', 'test_field');
if (Database::getConnection()->databaseType() == 'mysql') {
if ($this->connection->databaseType() === 'mysql') {
// Make sure that varchar fields have the correct collation.
$columns = db_query('SHOW FULL COLUMNS FROM {test_table}');
$columns = $this->connection->query('SHOW FULL COLUMNS FROM {test_table}');
foreach ($columns as $column) {
if ($column->Field == 'test_field_string') {
$string_check = ($column->Collation == 'utf8mb4_general_ci');
......@@ -109,92 +109,92 @@ public function testSchema() {
$this->assertFalse($this->tryInsert(), 'Insert without a default failed.');
// Add a default value to the column.
db_field_set_default('test_table', 'test_field', 0);
$this->schema->fieldSetDefault('test_table', 'test_field', 0);
// The insert should now succeed.
$this->assertTrue($this->tryInsert(), 'Insert with a default succeeded.');
// Remove the default.
db_field_set_no_default('test_table', 'test_field');
$this->schema->fieldSetNoDefault('test_table', 'test_field');
// The insert should fail again.
$this->assertFalse($this->tryInsert(), 'Insert without a default failed.');
// Test for fake index and test for the boolean result of indexExists().
$index_exists = $schema->indexExists('test_table', 'test_field');
$index_exists = $this->schema->indexExists('test_table', 'test_field');
$this->assertIdentical($index_exists, FALSE, 'Fake index does not exist');
// Add index.
db_add_index('test_table', 'test_field', ['test_field'], $table_specification);
$this->schema->addIndex('test_table', 'test_field', ['test_field'], $table_specification);
// Test for created index and test for the boolean result of indexExists().
$index_exists = $schema->indexExists('test_table', 'test_field');
$index_exists = $this->schema->indexExists('test_table', 'test_field');
$this->assertIdentical($index_exists, TRUE, 'Index created.');
// Rename the table.
db_rename_table('test_table', 'test_table2');
$this->schema->renameTable('test_table', 'test_table2');
// Index should be renamed.
$index_exists = $schema->indexExists('test_table2', 'test_field');
$index_exists = $this->schema->indexExists('test_table2', 'test_field');
$this->assertTrue($index_exists, 'Index was renamed.');
// We need the default so that we can insert after the rename.
db_field_set_default('test_table2', 'test_field', 0);
$this->schema->fieldSetDefault('test_table2', 'test_field', 0);
$this->assertFalse($this->tryInsert(), 'Insert into the old table failed.');
$this->assertTrue($this->tryInsert('test_table2'), 'Insert into the new table succeeded.');
// We should have successfully inserted exactly two rows.
$count = db_query('SELECT COUNT(*) FROM {test_table2}')->fetchField();
$count = $this->connection->query('SELECT COUNT(*) FROM {test_table2}')->fetchField();
$this->assertEqual($count, 2, 'Two fields were successfully inserted.');
// Try to drop the table.
$this->schema->dropTable('test_table2');
$this->assertFalse(db_table_exists('test_table2'), 'The dropped table does not exist.');
$this->assertFalse($this->schema->tableExists('test_table2'), 'The dropped table does not exist.');
// Recreate the table.
db_create_table('test_table', $table_specification);
db_field_set_default('test_table', 'test_field', 0);
db_add_field('test_table', 'test_serial', ['type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'Added column description.']);
$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.']);
// Assert that the column comment has been set.
$this->checkSchemaComment('Added column description.', 'test_table', 'test_serial');
// Change the new field to a serial column.
db_change_field('test_table', 'test_serial', 'test_serial', ['type' => 'serial', 'not null' => TRUE, 'description' => 'Changed column description.'], ['primary key' => ['test_serial']]);
$this->schema->changeField('test_table', 'test_serial', 'test_serial', ['type' => 'serial', 'not null' => TRUE, 'description' => 'Changed column description.'], ['primary key' => ['test_serial']]);
// Assert that the column comment has been set.
$this->checkSchemaComment('Changed column description.', 'test_table', 'test_serial');
$this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
$max1 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
$max1 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
$this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
$max2 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
$max2 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
$this->assertTrue($max2 > $max1, 'The serial is monotone.');
$count = db_query('SELECT COUNT(*) FROM {test_table}')->fetchField();
$count = $this->connection->query('SELECT COUNT(*) FROM {test_table}')->fetchField();
$this->assertEqual($count, 2, 'There were two rows.');
// Test adding a serial field to an existing table.
$this->schema->dropTable('test_table');
db_create_table('test_table', $table_specification);
db_field_set_default('test_table', 'test_field', 0);
db_add_field('test_table', 'test_serial', ['type' => 'serial', 'not null' => TRUE], ['primary key' => ['test_serial']]);
$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']]);
// Test the primary key columns.
$method = new \ReflectionMethod(get_class($schema), 'findPrimaryKeyColumns');
$method = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
$method->setAccessible(TRUE);
$this->assertSame(['test_serial'], $method->invoke($schema, 'test_table'));
$this->assertSame(['test_serial'], $method->invoke($this->schema, 'test_table'));
$this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
$max1 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
$max1 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
$this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
$max2 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
$max2 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
$this->assertTrue($max2 > $max1, 'The serial is monotone.');
$count = db_query('SELECT COUNT(*) FROM {test_table}')->fetchField();
$count = $this->connection->query('SELECT COUNT(*) FROM {test_table}')->fetchField();
$this->assertEqual($count, 2, 'There were two rows.');
// Test adding a new column and form a composite primary key with it.
db_add_field('test_table', 'test_composite_primary_key', ['type' => 'int', 'not null' => TRUE, 'default' => 0], ['primary key' => ['test_serial', 'test_composite_primary_key']]);
$this->schema->addField('test_table', 'test_composite_primary_key', ['type' => 'int', 'not null' => TRUE, 'default' => 0], ['primary key' => ['test_serial', 'test_composite_primary_key']]);
// Test the primary key columns.
$this->assertSame(['test_serial', 'test_composite_primary_key'], $method->invoke($schema, 'test_table'));
$this->assertSame(['test_serial', 'test_composite_primary_key'], $method->invoke($this->schema, 'test_table'));
// Test renaming of keys and constraints.
$this->schema->dropTable('test_table');
......@@ -214,46 +214,50 @@ public function testSchema() {
'test_field' => ['test_field'],
],
];
db_create_table('test_table', $table_specification);
$this->schema->createTable('test_table', $table_specification);
// Tests for indexes are Database specific.
$db_type = Database::getConnection()->databaseType();
$db_type = $this->connection->databaseType();
// Test for existing primary and unique keys.
switch ($db_type) {
case 'pgsql':
$primary_key_exists = $schema->constraintExists('test_table', '__pkey');
$unique_key_exists = $schema->constraintExists('test_table', 'test_field' . '__key');
$primary_key_exists = $this->schema->constraintExists('test_table', '__pkey');
$unique_key_exists = $this->schema->constraintExists('test_table', 'test_field' . '__key');
break;
case 'sqlite':
// SQLite does not create a standalone index for primary keys.
$primary_key_exists = TRUE;
$unique_key_exists = $schema->indexExists('test_table', 'test_field');
$unique_key_exists = $this->schema->indexExists('test_table', 'test_field');
break;
default:
$primary_key_exists = $schema->indexExists('test_table', 'PRIMARY');
$unique_key_exists = $schema->indexExists('test_table', 'test_field');
$primary_key_exists = $this->schema->indexExists('test_table', 'PRIMARY');
$unique_key_exists = $this->schema->indexExists('test_table', 'test_field');
break;
}
$this->assertIdentical($primary_key_exists, TRUE, 'Primary key created.');
$this->assertIdentical($unique_key_exists, TRUE, 'Unique key created.');
db_rename_table('test_table', 'test_table2');
$this->schema->renameTable('test_table', 'test_table2');
// Test for renamed primary and unique keys.
switch ($db_type) {
case 'pgsql':
$renamed_primary_key_exists = $schema->constraintExists('test_table2', '__pkey');
$renamed_unique_key_exists = $schema->constraintExists('test_table2', 'test_field' . '__key');
$renamed_primary_key_exists = $this->schema->constraintExists('test_table2', '__pkey');
$renamed_unique_key_exists = $this->schema->constraintExists('test_table2', 'test_field' . '__key');
break;
case 'sqlite':
// SQLite does not create a standalone index for primary keys.
$renamed_primary_key_exists = TRUE;
$renamed_unique_key_exists = $schema->indexExists('test_table2', 'test_field');
$renamed_unique_key_exists = $this->schema->indexExists('test_table2', 'test_field');
break;
default:
$renamed_primary_key_exists = $schema->indexExists('test_table2', 'PRIMARY');
$renamed_unique_key_exists = $schema->indexExists('test_table2', 'test_field');
$renamed_primary_key_exists = $this->schema->indexExists('test_table2', 'PRIMARY');
$renamed_unique_key_exists = $this->schema->indexExists('test_table2', 'test_field');
break;
}
$this->assertIdentical($renamed_primary_key_exists, TRUE, 'Primary key was renamed.');
......@@ -262,8 +266,8 @@ public function testSchema() {
// For PostgreSQL check in addition that sequence was renamed.
if ($db_type == 'pgsql') {
// Get information about new table.
$info = $schema->queryTableInformation('test_table2');
$sequence_name = $schema->prefixNonTable('test_table2', 'id', 'seq');
$info = $this->schema->queryTableInformation('test_table2');
$sequence_name = $this->schema->prefixNonTable('test_table2', 'id', 'seq');
$this->assertEqual($sequence_name, current($info->sequences), 'Sequence was renamed.');
}
......@@ -281,11 +285,11 @@ public function testSchema() {
],
];
try {
db_create_table('test_timestamp', $table_specification);
$this->schema->createTable('test_timestamp', $table_specification);
}
catch (\Exception $e) {
}
$this->assertTrue(db_table_exists('test_timestamp'), 'Table with database specific datatype was created.');
$this->assertTrue($this->schema->tableExists('test_timestamp'), 'Table with database specific datatype was created.');
}
/**
......@@ -294,9 +298,10 @@ public function testSchema() {
* @see \Drupal\Core\Database\Driver\mysql\Schema::getNormalizedIndexes()
*/
public function testIndexLength() {
if (Database::getConnection()->databaseType() != 'mysql') {
return;
if ($this->connection->databaseType() !== 'mysql') {
$this->markTestSkipped("The '{$this->connection->databaseType()}' database type does not support setting column length for indexes.");
}
$table_specification = [
'fields' => [
'id' => [
......@@ -343,16 +348,14 @@ public function testIndexLength() {
],
],
];
db_create_table('test_table_index_length', $table_specification);
$schema_object = Database::getConnection()->schema();
$this->schema->createTable('test_table_index_length', $table_specification);
// 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 {
$schema_object->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $missing_field_spec);
$this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $missing_field_spec);
$this->fail('SchemaException not thrown when adding index with missing information.');
}
catch (SchemaException $e) {
......@@ -360,14 +363,13 @@ public function testIndexLength() {
}
// Add a separate index.
$schema_object->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
$this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
$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 {
$schema_object->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
$this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
$this->fail('\Drupal\Core\Database\SchemaObjectExistsException exception missed.');
}
catch (SchemaObjectExistsException $e) {
......@@ -375,7 +377,7 @@ public function testIndexLength() {
}
try {
$schema_object->addIndex('test_table_non_existing', 'test_separate', [['test_field_text', 200]], $table_specification);
$this->schema->addIndex('test_table_non_existing', 'test_separate', [['test_field_text', 200]], $table_specification);
$this->fail('\Drupal\Core\Database\SchemaObjectDoesNotExistException exception missed.');
}
catch (SchemaObjectDoesNotExistException $e) {
......@@ -383,7 +385,7 @@ public function testIndexLength() {
}
// Get index information.
$results = db_query('SHOW INDEX FROM {test_table_index_length}');
$results = $this->connection->query('SHOW INDEX FROM {test_table_index_length}');
$expected_lengths = [
'test_regular' => [
'test_field_text' => 191,
......@@ -426,15 +428,16 @@ public function testIndexLength() {
/**
* Tests inserting data into an existing table.
*
* @param $table
* @param string $table
* The database table to insert data into.
*
* @return
* @return bool
* TRUE if the insert succeeded, FALSE otherwise.
*/
public function tryInsert($table = 'test_table') {
try {
db_insert($table)
$this->connection
->insert($table)
->fields(['id' => mt_rand(10, 20)])
->execute();
return TRUE;
......@@ -455,10 +458,10 @@ public function tryInsert($table = 'test_table') {
* Optional column to test.
*/
public function checkSchemaComment($description, $table, $column = NULL) {
if (method_exists(Database::getConnection()->schema(), 'getComment')) {
$comment = Database::getConnection()->schema()->getComment($table, $column);
if (method_exists($this->schema, 'getComment')) {
$comment = $this->schema->getComment($table, $column);
// The schema comment truncation for mysql is different.
if (Database::getConnection()->databaseType() == 'mysql') {
if ($this->connection->databaseType() === 'mysql') {
$max_length = $column ? 255 : 60;
$description = Unicode::truncate($description, $max_length, TRUE, TRUE);
}
......@@ -476,7 +479,7 @@ public function testUnsignedColumns() {
'fields' => ['serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE]],
'primary key' => ['serial_column'],
];
db_create_table($table_name, $table_spec);
$this->schema->createTable($table_name, $table_spec);
// Now set up columns for the other types.
$types = ['int', 'float', 'numeric'];
......@@ -487,12 +490,12 @@ public function testUnsignedColumns() {
}
$column_name = $type . '_column';
$table_spec['fields'][$column_name] = $column_spec;
db_add_field($table_name, $column_name, $column_spec);
$this->schema->addField($table_name, $column_name, $column_spec);
}
// Finally, check each column and try to insert invalid values into them.
foreach ($table_spec['fields'] as $column_name => $column_spec) {
$this->assertTrue(db_field_exists($table_name, $column_name), format_string('Unsigned @type column was created.', ['@type' => $column_spec['type']]));
$this->assertTrue($this->schema->fieldExists($table_name, $column_name), format_string('Unsigned @type column was created.', ['@type' => $column_spec['type']]));
$this->assertFalse($this->tryUnsignedInsert($table_name, $column_name), format_string('Unsigned @type column rejected a negative value.', ['@type' => $column_spec['type']]));
}
}
......@@ -500,17 +503,18 @@ public function testUnsignedColumns() {
/**
* Tries to insert a negative value into columns defined as unsigned.
*
* @param $table_name
* @param string $table_name
* The table to insert.
* @param $column_name
* @param string $column_name
* The column to insert.
*
* @return
* @return bool
* TRUE if the insert succeeded, FALSE otherwise.
*/
public function tryUnsignedInsert($table_name, $column_name) {
try {
db_insert($table_name)
$this->connection
->insert($table_name)
->fields([$column_name => -1])
->execute();
return TRUE;
......@@ -622,7 +626,7 @@ protected function assertFieldAdditionRemoval($field_spec) {
],
'primary key' => ['serial_column'],
];
db_create_table($table_name, $table_spec);
$this->schema->createTable($table_name, $table_spec);
$this->pass(format_string('Table %table created.', ['%table' => $table_name]));
// Check the characteristics of the field.
......@@ -640,35 +644,37 @@ protected function assertFieldAdditionRemoval($field_spec) {
],
'primary key' => ['serial_column'],
];
db_create_table($table_name, $table_spec);
$this->schema->createTable($table_name, $table_spec);
$this->pass(format_string('Table %table created.', ['%table' => $table_name]));
// Insert some rows to the table to test the handling of initial values.
for ($i = 0; $i < 3; $i++) {
db_insert($table_name)
$this->connection
->insert($table_name)
->useDefaults(['serial_column'])
->fields(['test_nullable_field' => 100])
->execute();
}
// Add another row with no value for the 'test_nullable_field' column.
db_insert($table_name)
$this->connection
->insert($table_name)
->useDefaults(['serial_column'])
->execute();
db_add_field($table_name, 'test_field', $field_spec);
$this->schema->addField($table_name, 'test_field', $field_spec);
$this->pass(format_string('Column %column created.', ['%column' => 'test_field']));
// Check the characteristics of the field.
$this->assertFieldCharacteristics($table_name, 'test_field', $field_spec);
// Clean-up.
db_drop_field($table_name, 'test_field');
$this->schema->dropField($table_name, 'test_field');
// Add back the field and then try to delete a field which is also a primary
// key.
db_add_field($table_name, 'test_field', $field_spec);
db_drop_field($table_name, 'serial_column');
$this->schema->addField($table_name, 'test_field', $field_spec);
$this->schema->dropField($table_name, 'serial_column');
$this->schema->dropTable($table_name);
}
......@@ -679,7 +685,8 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s
// 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'].
$count = db_select($table_name)
$count = $this->connection
->select($table_name)
->fields($table_name, ['serial_column'])
->condition($field_name, $field_spec['initial'], '<>')
->countQuery()
......@@ -692,7 +699,8 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s
if (isset($field_spec['initial_from_field']) && !isset($field_spec['initial'])) {
// There should be no row with a value different than
// $field_spec['initial_from_field'].
$count = db_select($table_name)
$count = $this->connection
->select($table_name)
->fields($table_name, ['serial_column'])
->where($table_name . '.' . $field_spec['initial_from_field'] . ' <> ' . $table_name . '.' . $field_name)
->countQuery()
......@@ -702,7 +710,8 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s
}
elseif (isset($field_spec['initial_from_field']) && isset($field_spec['initial'])) {
// There should be no row with a value different than '100'.
$count = db_select($table_name)
$count = $this->connection
->select($table_name)
->fields($table_name, ['serial_column'])
->condition($field_name, 100, '<>')
->countQuery()
......@@ -714,10 +723,12 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s
// 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.
$id = db_insert($table_name)
$id = $this->connection
->insert($table_name)
->useDefaults(['serial_column'])
->execute();
$field_value = db_select($table_name)
$field_value = $this->connection
->select($table_name)
->fields($table_name, [$field_name])
->condition('serial_column', $id)
->execute()
......@@ -742,9 +753,7 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s
* @covers ::findPrimaryKeyColumns
*/
public function testSchemaChangePrimaryKey(array $initial_primary_key, array $renamed_primary_key) {
$connection = Database::getConnection();
$schema = $connection->schema();
$find_primary_key_columns = new \ReflectionMethod(get_class($schema), 'findPrimaryKeyColumns');
$find_primary_key_columns = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
$find_primary_key_columns->setAccessible(TRUE);
// Test making the field the primary key of the table upon creation.
......@@ -756,59 +765,60 @@ public function testSchemaChangePrimaryKey(array $initial_primary_key, array $re
],
'primary key' => $initial_primary_key,
];
$schema->createTable($table_name, $table_spec);
$this->assertTrue($schema->fieldExists($table_name, 'test_field'));
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($schema, $table_name));
$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));
// Change the field type and make sure the primary key stays in place.
$schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'varchar', 'length' => 32, 'not null' => TRUE]);
$this->assertTrue($schema->fieldExists($table_name, 'test_field'));
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($schema, $table_name));
$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));
// 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.
$connection->insert($table_name)
$this->connection
->insert($table_name)
->fields(['test_field' => 1, 'other_test_field' => 2])
->execute();
$schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE]);
$this->assertTrue($schema->fieldExists($table_name, 'test_field'));
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($schema, $table_name));
$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));
// Make sure that adding the primary key can be done as part of changing
// a field, as well.
$schema->dropPrimaryKey($table_name);
$this->assertEquals([], $find_primary_key_columns->invoke($schema, $table_name));
$schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE], ['primary key' => $initial_primary_key]);
$this->assertTrue($schema->fieldExists($table_name, 'test_field'));
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($schema, $table_name));
$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));
// Rename the field and make sure the primary key was updated.
$schema->changeField($table_name, 'test_field', 'test_field_renamed', ['type' => 'int', 'not null' => TRUE]);
$this->assertTrue($schema->fieldExists($table_name, 'test_field_renamed'));
$this->assertEquals($renamed_primary_key, $find_primary_key_columns->invoke($schema, $table_name));
$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));
// Drop the field and make sure the primary key was dropped, as well.
$schema->dropField($table_name, 'test_field_renamed');
$this->assertFalse($schema->fieldExists($table_name, 'test_field_renamed'));
$this->assertEquals([], $find_primary_key_columns->invoke($schema, $table_name));
$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));
// Add the field again and make sure adding the primary key can be done at
// the same time.
$schema->addField($table_name, 'test_field', ['type' => 'int', 'default' => 0, 'not null' => TRUE], ['primary key' => $initial_primary_key]);
$this->assertTrue($schema->fieldExists($table_name, 'test_field'));
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($schema, $table_name));
$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));
// Drop the field again and explicitly add a primary key.
$schema->dropField($table_name, 'test_field');
$schema->addPrimaryKey($table_name, ['other_test_field']);
$this->assertFalse($schema->fieldExists($table_name, 'test_field'));
$this->assertEquals(['other_test_field'], $find_primary_key_columns->invoke($schema, $table_name));
$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));
// Test that adding a field with a primary key will work even with a
// pre-existing primary key.
$schema->addField($table_name, 'test_field', ['type' => 'int', 'default' => 0, 'not null' => TRUE], ['primary key' => $initial_primary_key]);
$this->assertTrue($schema->fieldExists($table_name, 'test_field'));
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($schema, $table_name));
$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));
}
/**
......@@ -840,9 +850,6 @@ public function providerTestSchemaCreateTablePrimaryKey() {
* Tests an invalid field specification as a primary key on table creation.
*/
public function testInvalidPrimaryKeyOnTableCreation() {
$connection = Database::getConnection();
$schema = $connection->schema();
// Test making an invalid field the primary key of the table upon creation.
$table_name = 'test_table';
$table_spec = [
......@@ -852,16 +859,13 @@ public function testInvalidPrimaryKeyOnTableCreation() {
'primary key' => ['test_field'],
];
$this->setExpectedException(SchemaException::class, "The 'test_field' field specification does not define 'not null' as TRUE.");
$schema->createTable($table_name, $table_spec);
$this->schema->createTable($table_name, $table_spec);
}
/**
* Tests adding an invalid field specification as a primary key.
*/
public function testInvalidPrimaryKeyAddition() {
$connection = Database::getConnection();
$schema = $connection->schema();
// Test adding a new invalid field to the primary key.
$table_name = 'test_table';
$table_spec = [
......@@ -870,19 +874,16 @@ public function testInvalidPrimaryKeyAddition() {
],
'primary key' => ['test_field'],
];
$schema->createTable($table_name, $table_spec);
$this->schema->createTable($table_name, $table_spec);
$this->setExpectedException(SchemaException::class, "The 'new_test_field' field specification does not define 'not null' as TRUE.");
$schema->addField($table_name, 'new_test_field', ['type' => 'int'], ['primary key' => ['test_field', 'new_test_field']]);
$this->schema->addField($table_name, 'new_test_field', ['type' => 'int'], ['primary key' => ['test_field', 'new_test_field']]);
}
/**
* Tests changing the primary key with an invalid field specification.
*/
public function testInvalidPrimaryKeyChange() {
$connection = Database::getConnection();
$schema = $connection->schema();
// Test adding a new invalid field to the primary key.
$table_name = 'test_table';
$table_spec = [
......@@ -891,11 +892,11 @@ public function testInvalidPrimaryKeyChange() {
],
'primary key' => ['test_field'],
];
$schema->createTable($table_name, $table_spec);
$this->schema->createTable($table_name, $table_spec);
$this->setExpectedException(SchemaException::class, "The 'changed_test_field' field specification does not define 'not null' as TRUE.");
$schema->dropPrimaryKey($table_name);
$schema->changeField($table_name, 'test_field', 'changed_test_field', ['type' => 'int'], ['primary key' => ['changed_test_field']]);
$this->schema->dropPrimaryKey($table_name);
$this->schema->changeField($table_name, 'test_field', 'changed_test_field', ['type' => 'int'], ['primary key' => ['changed_test_field']]);
}
/**
......@@ -960,26 +961,28 @@ protected function assertFieldChange($old_spec, $new_spec, $test_data = NULL) {
],
'primary key' => ['serial_column'],
];
db_create_table($table_name, $table_spec);
$this->schema->createTable($table_name, $table_spec);
$this->pass(format_string('Table %table created.', ['%table' => $table_name]));
// Check the characteristics of the field.
$this->assertFieldCharacteristics($table_name, 'test_field', $old_spec);
// Remove inserted rows.
db_truncate($table_name)->execute();
$this->connection->truncate($table_name)->execute();
if ($test_data) {
$id = db_insert($table_name)
$id = $this->connection
->insert($table_name)
->fields(['test_field'], [$test_data])
->execute();
}
// Change the field.