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

8.7.x
Alex Pott 2018-07-30 11:20:50 +01:00
parent ee56c7d8ae
commit f58b7ce3da
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
1 changed files with 164 additions and 163 deletions

View File

@ -20,6 +20,8 @@ class SchemaTest extends KernelTestBase {
/**
* A global counter for table and field creation.
*
* @var int
*/
protected $counter;
@ -50,8 +52,6 @@ class SchemaTest extends KernelTestBase {
* 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 @@ class SchemaTest extends KernelTestBase {
],
],
];
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 @@ class SchemaTest extends KernelTestBase {
// 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 @@ class SchemaTest extends KernelTestBase {
$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 @@ class SchemaTest extends KernelTestBase {
'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 @@ class SchemaTest extends KernelTestBase {
// 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 @@ class SchemaTest extends KernelTestBase {
],
];
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 @@ class SchemaTest extends KernelTestBase {
* @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 @@ class SchemaTest extends KernelTestBase {
],
],
];
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 @@ class SchemaTest extends KernelTestBase {
}
// 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 @@ class SchemaTest extends KernelTestBase {
}
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 @@ class SchemaTest extends KernelTestBase {
}
// 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 @@ class SchemaTest extends KernelTestBase {
/**
* 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 @@ class SchemaTest extends KernelTestBase {
* 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 @@ class SchemaTest extends KernelTestBase {
'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 @@ class SchemaTest extends KernelTestBase {
}
$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 @@ class SchemaTest extends KernelTestBase {
/**
* 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 @@ class SchemaTest extends KernelTestBase {
],
'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 @@ class SchemaTest extends KernelTestBase {
],
'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 @@ class SchemaTest extends KernelTestBase {
// 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 @@ class SchemaTest extends KernelTestBase {
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 @@ class SchemaTest extends KernelTestBase {
}
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 @@ class SchemaTest extends KernelTestBase {
// 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 @@ class SchemaTest extends KernelTestBase {
* @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 @@ class SchemaTest extends KernelTestBase {
],
'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 @@ class SchemaTest extends KernelTestBase {
* 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 @@ class SchemaTest extends KernelTestBase {
'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 @@ class SchemaTest extends KernelTestBase {
],
'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 @@ class SchemaTest extends KernelTestBase {
],
'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 @@ class SchemaTest extends KernelTestBase {
],
'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.
db_change_field($table_name, 'test_field', 'test_field', $new_spec);
$this->schema->changeField($table_name, 'test_field', 'test_field', $new_spec);
if ($test_data) {
$field_value = db_select($table_name)
$field_value = $this->connection
->select($table_name)
->fields($table_name, ['test_field'])
->condition('serial_column', $id)
->execute()
@ -998,12 +1001,11 @@ class SchemaTest extends KernelTestBase {
* @covers ::findPrimaryKeyColumns
*/
public function testFindPrimaryKeyColumns() {
$schema = Database::getConnection()->schema();
$method = new \ReflectionMethod(get_class($schema), 'findPrimaryKeyColumns');
$method = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
$method->setAccessible(TRUE);
// Test with single column primary key.
$schema->createTable('table_with_pk_0', [
$this->schema->createTable('table_with_pk_0', [
'description' => 'Table with primary key.',
'fields' => [
'id' => [
@ -1017,10 +1019,10 @@ class SchemaTest extends KernelTestBase {
],
'primary key' => ['id'],
]);
$this->assertSame(['id'], $method->invoke($schema, 'table_with_pk_0'));
$this->assertSame(['id'], $method->invoke($this->schema, 'table_with_pk_0'));
// Test with multiple column primary key.
$schema->createTable('table_with_pk_1', [
$this->schema->createTable('table_with_pk_1', [
'description' => 'Table with primary key with multiple columns.',
'fields' => [
'id0' => [
@ -1038,11 +1040,11 @@ class SchemaTest extends KernelTestBase {
],
'primary key' => ['id0', 'id1'],
]);
$this->assertSame(['id0', 'id1'], $method->invoke($schema, 'table_with_pk_1'));
$this->assertSame(['id0', 'id1'], $method->invoke($this->schema, 'table_with_pk_1'));
// Test with multiple column primary key and not being the first column of
// the table definition.
$schema->createTable('table_with_pk_2', [
$this->schema->createTable('table_with_pk_2', [
'description' => 'Table with primary key with multiple columns at the end and in reverted sequence.',
'fields' => [
'test_field_1' => [
@ -1064,12 +1066,12 @@ class SchemaTest extends KernelTestBase {
],
'primary key' => ['id4', 'id3'],
]);
$this->assertSame(['id4', 'id3'], $method->invoke($schema, 'table_with_pk_2'));
$this->assertSame(['id4', 'id3'], $method->invoke($this->schema, 'table_with_pk_2'));
// Test with multiple column primary key in a different order. For the
// PostgreSQL and the SQLite drivers is sorting used to get the primary key
// columns in the right order.
$schema->createTable('table_with_pk_3', [
$this->schema->createTable('table_with_pk_3', [
'description' => 'Table with primary key with multiple columns at the end and in reverted sequence.',
'fields' => [
'test_field_1' => [
@ -1091,10 +1093,10 @@ class SchemaTest extends KernelTestBase {
],
'primary key' => ['id3', 'test_field_2', 'id4'],
]);
$this->assertSame(['id3', 'test_field_2', 'id4'], $method->invoke($schema, 'table_with_pk_3'));
$this->assertSame(['id3', 'test_field_2', 'id4'], $method->invoke($this->schema, 'table_with_pk_3'));
// Test with table without a primary key.
$schema->createTable('table_without_pk_1', [
$this->schema->createTable('table_without_pk_1', [
'description' => 'Table without primary key.',
'fields' => [
'id' => [
@ -1107,10 +1109,10 @@ class SchemaTest extends KernelTestBase {
],
],
]);
$this->assertSame([], $method->invoke($schema, 'table_without_pk_1'));
$this->assertSame([], $method->invoke($this->schema, 'table_without_pk_1'));
// Test with table with an empty primary key.
$schema->createTable('table_without_pk_2', [
$this->schema->createTable('table_without_pk_2', [
'description' => 'Table without primary key.',
'fields' => [
'id' => [
@ -1124,10 +1126,10 @@ class SchemaTest extends KernelTestBase {
],
'primary key' => [],
]);
$this->assertSame([], $method->invoke($schema, 'table_without_pk_2'));
$this->assertSame([], $method->invoke($this->schema, 'table_without_pk_2'));
// Test with non existing table.
$this->assertFalse($method->invoke($schema, 'non_existing_table'));
$this->assertFalse($method->invoke($this->schema, 'non_existing_table'));
}
/**
@ -1136,7 +1138,6 @@ class SchemaTest extends KernelTestBase {
public function testFindTables() {
// We will be testing with three tables, two of them using the default
// prefix and the third one with an individually specified prefix.
// Set up a new connection with different connection info.
$connection_info = Database::getConnectionInfo();
@ -1144,8 +1145,8 @@ class SchemaTest extends KernelTestBase {
$new_connection_info = $connection_info['default'];
$new_connection_info['prefix']['test_2_table'] = $new_connection_info['prefix']['default'] . '_shared_';
Database::addConnectionInfo('test', 'default', $new_connection_info);
Database::setActiveConnection('test');
$test_schema = Database::getConnection()->schema();
// Create the tables.
$table_specification = [
@ -1157,12 +1158,12 @@ class SchemaTest extends KernelTestBase {
],
],
];
Database::getConnection()->schema()->createTable('test_1_table', $table_specification);
Database::getConnection()->schema()->createTable('test_2_table', $table_specification);
Database::getConnection()->schema()->createTable('the_third_table', $table_specification);
$test_schema->createTable('test_1_table', $table_specification);
$test_schema->createTable('test_2_table', $table_specification);
$test_schema->createTable('the_third_table', $table_specification);
// Check the "all tables" syntax.
$tables = Database::getConnection()->schema()->findTables('%');
$tables = $test_schema->findTables('%');
sort($tables);
$expected = [
// The 'config' table is added by
@ -1176,7 +1177,7 @@ class SchemaTest extends KernelTestBase {
$this->assertEqual($tables, $expected, 'All tables were found.');
// Check the restrictive syntax.
$tables = Database::getConnection()->schema()->findTables('test_%');
$tables = $test_schema->findTables('test_%');
sort($tables);
$expected = [
'test_1_table',