Issue #3130579 by daffie, markdorison, pradhumanjainOSL, dragan_bp, jcisio, mondrake, artem_sylchuk, Sutharsan, alexpott, jsst, catch: Make Drupal\Core\Database\Schema work with reserved keywords for naming

merge-requests/2560/merge
Alex Pott 2022-11-01 22:35:09 +00:00
parent bc8ed7fec1
commit 9363b0e7a1
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
5 changed files with 239 additions and 37 deletions

View File

@ -138,7 +138,7 @@ class Schema extends DatabaseSchema {
* The field specification, as per the schema data structure format.
*/
protected function createFieldSql($name, $spec) {
$sql = "`" . $name . "` " . $spec['mysql_type'];
$sql = "[" . $name . "] " . $spec['mysql_type'];
if (in_array($spec['mysql_type'], $this->mysqlStringTypes)) {
if (isset($spec['length'])) {
@ -276,13 +276,13 @@ class Schema extends DatabaseSchema {
}
if (!empty($spec['unique keys'])) {
foreach ($spec['unique keys'] as $key => $fields) {
$keys[] = 'UNIQUE KEY `' . $key . '` (' . $this->createKeySql($fields) . ')';
$keys[] = 'UNIQUE KEY [' . $key . '] (' . $this->createKeySql($fields) . ')';
}
}
if (!empty($spec['indexes'])) {
$indexes = $this->getNormalizedIndexes($spec);
foreach ($indexes as $index => $fields) {
$keys[] = 'INDEX `' . $index . '` (' . $this->createKeySql($fields) . ')';
$keys[] = 'INDEX [' . $index . '] (' . $this->createKeySql($fields) . ')';
}
}
@ -356,10 +356,10 @@ class Schema extends DatabaseSchema {
$return = [];
foreach ($fields as $field) {
if (is_array($field)) {
$return[] = '`' . $field[0] . '`(' . $field[1] . ')';
$return[] = '[' . $field[0] . '] (' . $field[1] . ')';
}
else {
$return[] = '`' . $field . '`';
$return[] = '[' . $field . ']';
}
}
return implode(', ', $return);
@ -377,7 +377,7 @@ class Schema extends DatabaseSchema {
}
$info = $this->getPrefixInfo($new_name);
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO `' . $info['table'] . '`');
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO [' . $info['table'] . ']');
}
/**
@ -471,7 +471,7 @@ class Schema extends DatabaseSchema {
$this->dropPrimaryKey($table);
}
$this->connection->query('ALTER TABLE {' . $table . '} DROP `' . $field . '`');
$this->connection->query('ALTER TABLE {' . $table . '} DROP [' . $field . ']');
return TRUE;
}
@ -533,7 +533,7 @@ class Schema extends DatabaseSchema {
throw new SchemaObjectExistsException("Cannot add unique key '$name' to table '$table': unique key already exists.");
}
$this->connection->query('ALTER TABLE {' . $table . '} ADD UNIQUE KEY `' . $name . '` (' . $this->createKeySql($fields) . ')');
$this->connection->query('ALTER TABLE {' . $table . '} ADD UNIQUE KEY [' . $name . '] (' . $this->createKeySql($fields) . ')');
}
/**
@ -544,7 +544,7 @@ class Schema extends DatabaseSchema {
return FALSE;
}
$this->connection->query('ALTER TABLE {' . $table . '} DROP KEY `' . $name . '`');
$this->connection->query('ALTER TABLE {' . $table . '} DROP KEY [' . $name . ']');
return TRUE;
}
@ -562,7 +562,7 @@ class Schema extends DatabaseSchema {
$spec['indexes'][$name] = $fields;
$indexes = $this->getNormalizedIndexes($spec);
$this->connection->query('ALTER TABLE {' . $table . '} ADD INDEX `' . $name . '` (' . $this->createKeySql($indexes[$name]) . ')');
$this->connection->query('ALTER TABLE {' . $table . '} ADD INDEX [' . $name . '] (' . $this->createKeySql($indexes[$name]) . ')');
}
/**
@ -573,7 +573,7 @@ class Schema extends DatabaseSchema {
return FALSE;
}
$this->connection->query('ALTER TABLE {' . $table . '} DROP INDEX `' . $name . '`');
$this->connection->query('ALTER TABLE {' . $table . '} DROP INDEX [' . $name . ']');
return TRUE;
}
@ -621,7 +621,7 @@ class Schema extends DatabaseSchema {
$this->ensureNotNullPrimaryKey($keys_new['primary key'], [$field_new => $spec]);
}
$sql = 'ALTER TABLE {' . $table . '} CHANGE `' . $field . '` ' . $this->createFieldSql($field_new, $this->processField($spec));
$sql = 'ALTER TABLE {' . $table . '} CHANGE [' . $field . '] ' . $this->createFieldSql($field_new, $this->processField($spec));
if ($keys_sql = $this->createKeysSql($keys_new)) {
$sql .= ', ADD ' . implode(', ADD ', $keys_sql);
}

View File

@ -794,6 +794,7 @@ EOD;
throw new SchemaObjectExistsException("Cannot add unique key '$name' to table '$table': unique key already exists.");
}
$fields = array_map([$this->connection, 'escapeField'], $fields);
$this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT ' . $this->ensureIdentifiersLength($table, $name, 'key') . ' UNIQUE (' . implode(',', $fields) . ')');
$this->resetTableInformation($table);
}
@ -910,11 +911,11 @@ EOD;
$field_info = $this->queryFieldInformation($table, $field);
foreach ($field_info as $check) {
$this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $check . '"');
$this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT [' . $check . ']');
}
// Remove old default.
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT');
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN [' . $field . '] DROP DEFAULT');
// Convert field type.
// Usually, we do this via a simple typecast 'USING fieldname::type'. But
@ -924,10 +925,10 @@ EOD;
$is_bytea = !empty($table_information->blob_fields[$field]);
if ($spec['pgsql_type'] != 'bytea') {
if ($is_bytea) {
$this->connection->query('ALTER TABLE {' . $table . '} ALTER "' . $field . '" TYPE ' . $field_def . ' USING convert_from("' . $field . '"' . ", 'UTF8')");
$this->connection->query('ALTER TABLE {' . $table . '} ALTER [' . $field . '] TYPE ' . $field_def . ' USING convert_from([' . $field . ']' . ", 'UTF8')");
}
else {
$this->connection->query('ALTER TABLE {' . $table . '} ALTER "' . $field . '" TYPE ' . $field_def . ' USING "' . $field . '"::' . $field_def);
$this->connection->query('ALTER TABLE {' . $table . '} ALTER [' . $field . '] TYPE ' . $field_def . ' USING [' . $field . ']::' . $field_def);
}
}
else {
@ -936,7 +937,7 @@ EOD;
// Convert to a bytea type by using the SQL replace() function to
// convert any single backslashes in the field content to double
// backslashes ('\' to '\\').
$this->connection->query('ALTER TABLE {' . $table . '} ALTER "' . $field . '" TYPE ' . $field_def . ' USING decode(replace("' . $field . '"' . ", E'\\\\', E'\\\\\\\\'), 'escape');");
$this->connection->query('ALTER TABLE {' . $table . '} ALTER [' . $field . '] TYPE ' . $field_def . ' USING decode(replace("' . $field . '"' . ", E'\\\\', E'\\\\\\\\'), 'escape');");
}
}
@ -947,7 +948,7 @@ EOD;
else {
$null_action = 'DROP NOT NULL';
}
$this->connection->query('ALTER TABLE {' . $table . '} ALTER "' . $field . '" ' . $null_action);
$this->connection->query('ALTER TABLE {' . $table . '} ALTER [' . $field . '] ' . $null_action);
}
if (in_array($spec['pgsql_type'], ['serial', 'bigserial'])) {
@ -958,28 +959,28 @@ EOD;
$this->connection->query("CREATE SEQUENCE " . $seq);
// Set sequence to maximal field value to not conflict with existing
// entries.
$this->connection->query("SELECT setval('" . $seq . "', MAX(\"" . $field . '")) FROM {' . $table . "}");
$this->connection->query('ALTER TABLE {' . $table . '} ALTER ' . $field . ' SET DEFAULT nextval(' . $this->connection->quote($seq) . ')');
$this->connection->query("SELECT setval('" . $seq . "', MAX([" . $field . "])) FROM {" . $table . "}");
$this->connection->query('ALTER TABLE {' . $table . '} ALTER [' . $field . '] SET DEFAULT nextval(' . $this->connection->quote($seq) . ')');
}
// Rename the column if necessary.
if ($field != $field_new) {
$this->connection->query('ALTER TABLE {' . $table . '} RENAME "' . $field . '" TO "' . $field_new . '"');
$this->connection->query('ALTER TABLE {' . $table . '} RENAME [' . $field . '] TO [' . $field_new . ']');
}
// Add unsigned check if necessary.
if (!empty($spec['unsigned'])) {
$this->connection->query('ALTER TABLE {' . $table . '} ADD CHECK ("' . $field_new . '" >= 0)');
$this->connection->query('ALTER TABLE {' . $table . '} ADD CHECK ([' . $field_new . '] >= 0)');
}
// Add default if necessary.
if (isset($spec['default'])) {
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field_new . '" SET DEFAULT ' . $this->escapeDefaultValue($spec['default']));
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN [' . $field_new . '] SET DEFAULT ' . $this->escapeDefaultValue($spec['default']));
}
// Change description if necessary.
if (!empty($spec['description'])) {
$this->connection->query('COMMENT ON COLUMN {' . $table . '}."' . $field_new . '" IS ' . $this->prepareComment($spec['description']));
$this->connection->query('COMMENT ON COLUMN {' . $table . '}.[' . $field_new . '] IS ' . $this->prepareComment($spec['description']));
}
if (isset($new_keys)) {

View File

@ -112,6 +112,108 @@ class SchemaTest extends DriverSpecificSchemaTestBase {
$this->assertEquals($table_specification, $index_schema);
}
/**
* {@inheritdoc}
*/
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'],
],
];
// Creating a table.
$table_name = 'select';
$this->schema->createTable($table_name, $table_specification);
$this->assertTrue($this->schema->tableExists($table_name));
// Finding all tables.
$tables = $this->schema->findTables('%');
sort($tables);
$this->assertEquals(['config', 'select'], $tables);
// Renaming a table.
$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));
// Adding a field.
$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));
// Dropping a primary key.
$this->schema->dropPrimaryKey($table_name_new);
// Adding a primary key.
$this->schema->addPrimaryKey($table_name_new, [$field_name]);
// Check the primary key columns.
$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));
// Dropping a primary key.
$this->schema->dropPrimaryKey($table_name_new);
// Changing a field.
$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));
// Adding an unique key
$unique_key_name = $unique_key_introspect_name = 'unique';
$this->schema->addUniqueKey($table_name_new, $unique_key_name, [$field_name_new]);
// Check the unique key columns.
$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]);
// Dropping an unique key
$this->schema->dropUniqueKey($table_name_new, $unique_key_name);
// Dropping a field.
$this->schema->dropField($table_name_new, $field_name_new);
$this->assertFalse($this->schema->fieldExists($table_name_new, $field_name_new));
// Adding an index.
$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));
// Check the index columns.
$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]);
// Dropping an index.
$this->schema->dropIndex($table_name_new, $index_name);
$this->assertFalse($this->schema->indexExists($table_name_new, $index_name));
// Dropping a table.
$this->schema->dropTable($table_name_new);
$this->assertFalse($this->schema->tableExists($table_name_new));
}
/**
* @covers \Drupal\Core\Database\Driver\pgsql\Schema::extensionExists
*/

View File

@ -32,7 +32,7 @@ class Schema extends DatabaseSchema {
$info = $this->getPrefixInfo($table);
// Don't use {} around sqlite_master table.
return (bool) $this->connection->query('SELECT 1 FROM ' . $info['schema'] . '.sqlite_master WHERE type = :type AND name = :name', [':type' => 'table', ':name' => $info['table']])->fetchField();
return (bool) $this->connection->query('SELECT 1 FROM [' . $info['schema'] . '].sqlite_master WHERE type = :type AND name = :name', [':type' => 'table', ':name' => $info['table']])->fetchField();
}
/**
@ -64,12 +64,12 @@ class Schema extends DatabaseSchema {
$info = $this->getPrefixInfo($tablename);
if (!empty($schema['unique keys'])) {
foreach ($schema['unique keys'] as $key => $fields) {
$sql[] = 'CREATE UNIQUE INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $key . ' ON ' . $info['table'] . ' (' . $this->createKeySql($fields) . ")\n";
$sql[] = 'CREATE UNIQUE INDEX [' . $info['schema'] . '].[' . $info['table'] . '_' . $key . '] ON [' . $info['table'] . '] (' . $this->createKeySql($fields) . ")\n";
}
}
if (!empty($schema['indexes'])) {
foreach ($schema['indexes'] as $key => $fields) {
$sql[] = 'CREATE INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $key . ' ON ' . $info['table'] . ' (' . $this->createKeySql($fields) . ")\n";
$sql[] = 'CREATE INDEX [' . $info['schema'] . '].[' . $info['table'] . '_' . $key . '] ON [' . $info['table'] . '] (' . $this->createKeySql($fields) . ")\n";
}
}
return $sql;
@ -106,10 +106,10 @@ class Schema extends DatabaseSchema {
$return = [];
foreach ($fields as $field) {
if (is_array($field)) {
$return[] = $field[0];
$return[] = '[' . $field[0] . ']';
}
else {
$return[] = $field;
$return[] = '[' . $field . ']';
}
}
return implode(', ', $return);
@ -272,7 +272,7 @@ class Schema extends DatabaseSchema {
// the table with curly braces in case the db_prefix contains a reference
// to a database outside of our existing database.
$info = $this->getPrefixInfo($new_name);
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO ' . $info['table']);
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO [' . $info['table'] . ']');
// Drop the indexes, there is no RENAME INDEX command in SQLite.
if (!empty($schema['unique keys'])) {
@ -483,7 +483,7 @@ class Schema extends DatabaseSchema {
];
$info = $this->getPrefixInfo($table);
$result = $this->connection->query('PRAGMA ' . $info['schema'] . '.table_info(' . $info['table'] . ')');
$result = $this->connection->query('PRAGMA [' . $info['schema'] . '].table_info([' . $info['table'] . '])');
foreach ($result as $row) {
if (preg_match('/^([^(]+)\((.*)\)$/', $row->type, $matches)) {
$type = $matches[1];
@ -539,7 +539,7 @@ class Schema extends DatabaseSchema {
$schema['primary key'] = array_values($schema['primary key']);
$indexes = [];
$result = $this->connection->query('PRAGMA ' . $info['schema'] . '.index_list(' . $info['table'] . ')');
$result = $this->connection->query('PRAGMA [' . $info['schema'] . '].index_list([' . $info['table'] . '])');
foreach ($result as $row) {
if (strpos($row->name, 'sqlite_autoindex_') !== 0) {
$indexes[] = [
@ -552,7 +552,7 @@ class Schema extends DatabaseSchema {
$name = $index['name'];
// Get index name without prefix.
$index_name = substr($name, strlen($info['table']) + 1);
$result = $this->connection->query('PRAGMA ' . $info['schema'] . '.index_info(' . $name . ')');
$result = $this->connection->query('PRAGMA [' . $info['schema'] . '].index_info([' . $name . '])');
foreach ($result as $row) {
$schema[$index['schema_key']][$index_name][] = $row->name;
}
@ -693,7 +693,7 @@ class Schema extends DatabaseSchema {
public function indexExists($table, $name) {
$info = $this->getPrefixInfo($table);
return $this->connection->query('PRAGMA ' . $info['schema'] . '.index_info(' . $info['table'] . '_' . $name . ')')->fetchField() != '';
return $this->connection->query('PRAGMA [' . $info['schema'] . '].index_info([' . $info['table'] . '_' . $name . '])')->fetchField() != '';
}
/**
@ -706,7 +706,7 @@ class Schema extends DatabaseSchema {
$info = $this->getPrefixInfo($table);
$this->connection->query('DROP INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $name);
$this->connection->query('DROP INDEX [' . $info['schema'] . '].[' . $info['table'] . '_' . $name . ']');
return TRUE;
}
@ -738,7 +738,7 @@ class Schema extends DatabaseSchema {
$info = $this->getPrefixInfo($table);
$this->connection->query('DROP INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $name);
$this->connection->query('DROP INDEX [' . $info['schema'] . '].[' . $info['table'] . '_' . $name . ']');
return TRUE;
}
@ -817,7 +817,7 @@ class Schema extends DatabaseSchema {
// Can't use query placeholders for the schema because the query would
// have to be :prefixsqlite_master, which does not work. We also need to
// ignore the internal SQLite tables.
$result = $this->connection->query("SELECT name FROM " . $schema . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
$result = $this->connection->query("SELECT name FROM [" . $schema . "].sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
':type' => 'table',
':table_name' => $table_expression,
':pattern' => 'sqlite_%',

View File

@ -1224,4 +1224,103 @@ abstract class DriverSpecificSchemaTestBase extends DriverSpecificKernelTestBase
$this->assertSame('default value', $result->column7);
}
/**
* Tests handling with reserved keywords for naming tables, fields and more.
*/
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'],
],
];
// Creating a table.
$table_name = 'select';
$this->schema->createTable($table_name, $table_specification);
$this->assertTrue($this->schema->tableExists($table_name));
// Finding all tables.
$tables = $this->schema->findTables('%');
sort($tables);
$this->assertEquals(['config', 'select'], $tables);
// Renaming a table.
$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));
// Adding a field.
$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));
// Dropping a primary key.
$this->schema->dropPrimaryKey($table_name_new);
// Adding a primary key.
$this->schema->addPrimaryKey($table_name_new, [$field_name]);
// Check the primary key columns.
$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));
// Dropping a primary key.
$this->schema->dropPrimaryKey($table_name_new);
// Changing a field.
$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));
// Adding an unique key
$unique_key_name = $unique_key_introspect_name = 'unique';
$this->schema->addUniqueKey($table_name_new, $unique_key_name, [$field_name_new]);
// Check the unique key columns.
$introspect_index_schema = new \ReflectionMethod(get_class($this->schema), 'introspectIndexSchema');
$this->assertEquals([$field_name_new], $introspect_index_schema->invoke($this->schema, $table_name_new)['unique keys'][$unique_key_introspect_name]);
// Dropping an unique key
$this->schema->dropUniqueKey($table_name_new, $unique_key_name);
// Dropping a field.
$this->schema->dropField($table_name_new, $field_name_new);
$this->assertFalse($this->schema->fieldExists($table_name_new, $field_name_new));
// Adding an index.
$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));
// Check the index columns.
$this->assertEquals(['update'], $introspect_index_schema->invoke($this->schema, $table_name_new)['indexes'][$index_introspect_name]);
// Dropping an index.
$this->schema->dropIndex($table_name_new, $index_name);
$this->assertFalse($this->schema->indexExists($table_name_new, $index_name));
// Dropping a table.
$this->schema->dropTable($table_name_new);
$this->assertFalse($this->schema->tableExists($table_name_new));
}
}