View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\pgsql\Kernel\pgsql;
use Drupal\KernelTests\Core\Database\DriverSpecificSchemaTestBase;
class SchemaTest extends DriverSpecificSchemaTestBase {
public function checkSchemaComment(string $description, string $table, string $column = NULL) : void {
$this
->assertSame($description, $this->schema
->getComment($table, $column), 'The comment matches the schema description.');
}
protected function checkSequenceRenaming(string $tableName) : void {
$sequenceExists = (bool) $this->connection
->query("SELECT pg_get_serial_sequence('{" . $tableName . "}', 'id')")
->fetchField();
$this
->assertTrue($sequenceExists, 'Sequence was renamed.');
$anotherTableName = strtolower($this
->getRandomGenerator()
->name(63 - strlen($this
->getDatabasePrefix())));
$this->schema
->renameTable($tableName, $anotherTableName);
$sequenceExists = (bool) $this->connection
->query("SELECT pg_get_serial_sequence('{" . $anotherTableName . "}', 'id')")
->fetchField();
$this
->assertTrue($sequenceExists, 'Sequence was renamed.');
}
public function testTableWithSpecificDataType() : void {
$table_specification = [
'description' => 'Schema table description.',
'fields' => [
'timestamp' => [
'pgsql_type' => 'timestamp',
'not null' => FALSE,
'default' => NULL,
],
],
];
$this->schema
->createTable('test_timestamp', $table_specification);
$this
->assertTrue($this->schema
->tableExists('test_timestamp'));
}
public function testIntrospectIndexSchema() : void {
$table_specification = [
'fields' => [
'id' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'test_field_1' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'test_field_2' => [
'type' => 'int',
'default' => 0,
],
'test_field_3' => [
'type' => 'int',
'default' => 0,
],
'test_field_4' => [
'type' => 'int',
'default' => 0,
],
'test_field_5' => [
'type' => 'int',
'default' => 0,
],
],
'primary key' => [
'id',
'test_field_1',
],
'unique keys' => [
'test_field_2' => [
'test_field_2',
],
'test_field_3_test_field_4' => [
'test_field_3',
'test_field_4',
],
],
'indexes' => [
'test_field_4' => [
'test_field_4',
],
'test_field_4_test_field_5' => [
'test_field_4',
'test_field_5',
],
],
];
$table_name = strtolower($this
->getRandomGenerator()
->name());
$this->schema
->createTable($table_name, $table_specification);
unset($table_specification['fields']);
$introspect_index_schema = new \ReflectionMethod(get_class($this->schema), 'introspectIndexSchema');
$index_schema = $introspect_index_schema
->invoke($this->schema, $table_name);
$ensure_identifier_length = new \ReflectionMethod(get_class($this->schema), 'ensureIdentifiersLength');
foreach ($table_specification['unique keys'] as $original_index_name => $columns) {
unset($table_specification['unique keys'][$original_index_name]);
$new_index_name = $ensure_identifier_length
->invoke($this->schema, $table_name, $original_index_name, 'key');
$table_specification['unique keys'][$new_index_name] = $columns;
}
foreach ($table_specification['indexes'] as $original_index_name => $columns) {
unset($table_specification['indexes'][$original_index_name]);
$new_index_name = $ensure_identifier_length
->invoke($this->schema, $table_name, $original_index_name, 'idx');
$table_specification['indexes'][$new_index_name] = $columns;
}
$this
->assertEquals($table_specification, $index_schema);
}
public function testReservedKeywordsForNaming() : void {
$table_specification = [
'description' => 'A test table with an ANSI reserved keywords for naming.',
'fields' => [
'primary' => [
'description' => 'Simple unique ID.',
'type' => 'int',
'not null' => TRUE,
],
'update' => [
'description' => 'A column with reserved name.',
'type' => 'varchar',
'length' => 255,
],
],
'primary key' => [
'primary',
],
'unique keys' => [
'having' => [
'update',
],
],
'indexes' => [
'in' => [
'primary',
'update',
],
],
];
$table_name = 'select';
$this->schema
->createTable($table_name, $table_specification);
$this
->assertTrue($this->schema
->tableExists($table_name));
$tables = $this->schema
->findTables('%');
sort($tables);
$this
->assertEquals([
'config',
'select',
], $tables);
$table_name_new = 'from';
$this->schema
->renameTable($table_name, $table_name_new);
$this
->assertFalse($this->schema
->tableExists($table_name));
$this
->assertTrue($this->schema
->tableExists($table_name_new));
$field_name = 'delete';
$this->schema
->addField($table_name_new, $field_name, [
'type' => 'int',
'not null' => TRUE,
]);
$this
->assertTrue($this->schema
->fieldExists($table_name_new, $field_name));
$this->schema
->dropPrimaryKey($table_name_new);
$this->schema
->addPrimaryKey($table_name_new, [
$field_name,
]);
$find_primary_key_columns = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
$this
->assertEquals([
$field_name,
], $find_primary_key_columns
->invoke($this->schema, $table_name_new));
$this->schema
->dropPrimaryKey($table_name_new);
$field_name_new = 'where';
$this->schema
->changeField($table_name_new, $field_name, $field_name_new, [
'type' => 'int',
'not null' => FALSE,
]);
$this
->assertFalse($this->schema
->fieldExists($table_name_new, $field_name));
$this
->assertTrue($this->schema
->fieldExists($table_name_new, $field_name_new));
$unique_key_name = $unique_key_introspect_name = 'unique';
$this->schema
->addUniqueKey($table_name_new, $unique_key_name, [
$field_name_new,
]);
$introspect_index_schema = new \ReflectionMethod(get_class($this->schema), 'introspectIndexSchema');
$ensure_identifiers_length = new \ReflectionMethod(get_class($this->schema), 'ensureIdentifiersLength');
$unique_key_introspect_name = $ensure_identifiers_length
->invoke($this->schema, $table_name_new, $unique_key_name, 'key');
$this
->assertEquals([
$field_name_new,
], $introspect_index_schema
->invoke($this->schema, $table_name_new)['unique keys'][$unique_key_introspect_name]);
$this->schema
->dropUniqueKey($table_name_new, $unique_key_name);
$this->schema
->dropField($table_name_new, $field_name_new);
$this
->assertFalse($this->schema
->fieldExists($table_name_new, $field_name_new));
$index_name = $index_introspect_name = 'index';
$this->schema
->addIndex($table_name_new, $index_name, [
'update',
], $table_specification);
$this
->assertTrue($this->schema
->indexExists($table_name_new, $index_name));
$index_introspect_name = $ensure_identifiers_length
->invoke($this->schema, $table_name_new, $index_name, 'idx');
$this
->assertEquals([
'update',
], $introspect_index_schema
->invoke($this->schema, $table_name_new)['indexes'][$index_introspect_name]);
$this->schema
->dropIndex($table_name_new, $index_name);
$this
->assertFalse($this->schema
->indexExists($table_name_new, $index_name));
$this->schema
->dropTable($table_name_new);
$this
->assertFalse($this->schema
->tableExists($table_name_new));
}
public function testPgsqlExtensionExists() : void {
$this
->assertFalse($this->schema
->extensionExists('non_existing_extension'));
$this
->assertTrue($this->schema
->extensionExists('pg_trgm'));
}
public function testPgsqlSequences() : void {
$table_specification = [
'description' => 'A test table with an ANSI reserved keywords for naming.',
'fields' => [
'uid' => [
'description' => 'Simple unique ID.',
'type' => 'serial',
'not null' => TRUE,
],
'update' => [
'description' => 'A column with reserved name.',
'type' => 'varchar',
'length' => 255,
],
],
'primary key' => [
'uid',
],
'unique keys' => [
'having' => [
'update',
],
],
'indexes' => [
'in' => [
'uid',
'update',
],
],
];
$table_name = 'sequence_test';
$this->schema
->createTable($table_name, $table_specification);
$this
->assertTrue($this->schema
->tableExists($table_name));
$sequence_name = $this->connection
->query("SELECT pg_get_serial_sequence(:table, :column)", [
':table' => $this->connection
->getPrefix() . 'sequence_test',
':column' => 'uid',
])
->fetchField();
$schema = $this->connection
->getConnectionOptions()['schema'] ?? 'public';
$this
->assertEquals($schema . '.' . $this->connection
->getPrefix() . 'sequence_test_uid_seq', $sequence_name);
$this
->assertTrue((bool) \Drupal::database()
->query("SELECT c.relname FROM pg_class as c WHERE c.relkind = 'S' AND c.relname = :name", [
':name' => $this->connection
->getPrefix() . 'sequence_test_uid_seq',
])
->fetchField());
$sequence_owner = \Drupal::database()
->query("SELECT d.refobjid::regclass as table_name, a.attname as field_name\n FROM pg_depend d\n JOIN pg_attribute a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid\n WHERE d.objid = :seq_name::regclass\n AND d.refobjsubid > 0\n AND d.classid = 'pg_class'::regclass", [
':seq_name' => $sequence_name,
])
->fetchObject();
$this
->assertEquals($this->connection
->getPrefix() . 'sequence_test', $sequence_owner->table_name);
$this
->assertEquals('uid', $sequence_owner->field_name, 'New sequence is owned by its table.');
}
public function testTableExists() {
$table_name = 'test_table';
$table_specification = [
'fields' => [
'id' => [
'type' => 'int',
'default' => NULL,
],
],
];
$this->schema
->createTable($table_name, $table_specification);
$prefixed_table_name = $this->connection
->getPrefix($table_name) . $table_name;
$this
->assertTrue($this->schema
->tableExists($table_name));
$this
->assertTrue($this->schema
->tableExists($table_name, TRUE));
$this
->assertFalse($this->schema
->tableExists($table_name, FALSE));
$this
->assertFalse($this->schema
->tableExists($prefixed_table_name));
$this
->assertFalse($this->schema
->tableExists($prefixed_table_name, TRUE));
$this
->assertTrue($this->schema
->tableExists($prefixed_table_name, FALSE));
}
public function testRenameTableWithNewIndexNameEqualsTableName() {
$table_name_old = 'some_new_table_name__id__idx';
$table_name_new = 'some_new_table_name';
$table_specification = [
'fields' => [
'id' => [
'type' => 'int',
'default' => NULL,
],
],
'indexes' => [
'id' => [
'id',
],
],
];
$this->schema
->createTable($table_name_old, $table_specification);
$this->schema
->renameTable($table_name_old, $table_name_new);
$this
->assertTrue($this->schema
->tableExists($table_name_new));
}
public function testUnsignedField() : void {
$table_name = 'unsigned_table';
$table_spec = [
'fields' => [
'order' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
],
'primary key' => [
'order',
],
];
$this->schema
->createTable($table_name, $table_spec);
}
}