Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting rid of the column name index #3583

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upgrade to 3.0

## BC BREAK: Changes in `Doctrine\DBAL\Event\SchemaCreateTableEventArgs`

Table columns are no longer indexed by column name. Use the `name` attribute of the column instead.

## BC BREAK: Changes in the `Doctrine\DBAL\Schema` API

- Column precision no longer defaults to 10. The default value is NULL.
Expand Down
11 changes: 6 additions & 5 deletions lib/Doctrine/DBAL/Event/SchemaCreateTableEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
use function array_merge;

/**
* Event Arguments used when SQL queries for creating tables are generated inside Doctrine\DBAL\Platform\AbstractPlatform.
* Event Arguments used when SQL queries for creating tables are generated
* inside Doctrine\DBAL\Platform\AbstractPlatform.
*/
class SchemaCreateTableEventArgs extends SchemaEventArgs
{
/** @var Table */
private $table;

/** @var array<string, array<string, mixed>> */
/** @var array<int, array<string, mixed>> */
private $columns;

/** @var array<string, mixed> */
Expand All @@ -29,8 +30,8 @@ class SchemaCreateTableEventArgs extends SchemaEventArgs
private $sql = [];

/**
* @param array<string, array<string, mixed>> $columns
* @param array<string, mixed> $options
* @param array<int, array<string, mixed>> $columns
* @param array<string, mixed> $options
*/
public function __construct(Table $table, array $columns, array $options, AbstractPlatform $platform)
{
Expand All @@ -46,7 +47,7 @@ public function getTable() : Table
}

/**
* @return array<string, array<string, mixed>>
* @return array<int, array<string, mixed>>
*/
public function getColumns() : array
{
Expand Down
21 changes: 9 additions & 12 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1345,10 +1345,7 @@ public function getCreateTableSQL(Table $table, int $createFlags = self::CREATE_
$columnData['primary'] = true;
}

$columnName = $columnData['name'];
assert(is_string($columnName));

$columns[$columnName] = $columnData;
$columns[] = $columnData;
}

if ($this->_eventManager !== null && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
Expand Down Expand Up @@ -1859,9 +1856,9 @@ protected function _getAlterTableIndexForeignKeySQL(TableDiff $diff) : array
/**
* Gets declaration of a number of fields in bulk.
*
* @param mixed[][] $fields A multidimensional associative array.
* The first dimension determines the field name, while the second
* dimension is keyed with the name of the properties
* @param mixed[][] $fields A multidimensional array.
* The first dimension determines the ordinal position of the field,
* while the second dimension is keyed with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
*
Expand All @@ -1887,8 +1884,8 @@ public function getColumnDeclarationListSQL(array $fields) : string
{
$queryFields = [];

foreach ($fields as $fieldName => $field) {
$queryFields[] = $this->getColumnDeclarationSQL($fieldName, $field);
foreach ($fields as $field) {
$queryFields[] = $this->getColumnDeclarationSQL($field['name'], $field);
}

return implode(', ', $queryFields);
Expand Down Expand Up @@ -2034,19 +2031,19 @@ public function getDefaultValueDeclarationSQL(array $field) : string
public function getCheckDeclarationSQL(array $definition) : string
{
$constraints = [];
foreach ($definition as $field => $def) {
foreach ($definition as $def) {
if (is_string($def)) {
$constraints[] = 'CHECK (' . $def . ')';
} else {
if (isset($def['min'])) {
$constraints[] = 'CHECK (' . $field . ' >= ' . $def['min'] . ')';
$constraints[] = 'CHECK (' . $def['name'] . ' >= ' . $def['min'] . ')';
}

if (! isset($def['max'])) {
continue;
}

$constraints[] = 'CHECK (' . $field . ' <= ' . $def['max'] . ')';
$constraints[] = 'CHECK (' . $def['name'] . ' <= ' . $def['max'] . ')';
}
}

Expand Down
7 changes: 3 additions & 4 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,16 @@ protected function _getCreateTableSQL(string $tableName, array $columns, array $
$options['indexes'] = [];
$sql = parent::_getCreateTableSQL($tableName, $columns, $options);

foreach ($columns as $name => $column) {
foreach ($columns as $column) {
if (isset($column['sequence'])) {
$sql[] = $this->getCreateSequenceSQL($column['sequence']);
}

if (! isset($column['autoincrement']) || ! $column['autoincrement'] &&
(! isset($column['autoinc']) || ! $column['autoinc'])) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's worth mentioning the removal of 'autoinc' as a breaking change. I don't see it mentioned in the documentation and this is the only platform which has it. The rest of the usages were removed prior to DBAL 2.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove it in another PR and add it to the upgrade doc. That isn't really related to getting rid of the column name index.

if (empty($column['autoincrement'])) {
continue;
}

$sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $tableName));
$sql = array_merge($sql, $this->getCreateAutoincrementSql($column['name'], $tableName));
}

if (isset($indexes) && ! empty($indexes)) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,10 @@ private function getNonAutoincrementPrimaryKeyDefinition(array $columns, array $
$keyColumns = array_unique(array_values($options['primary']));

foreach ($keyColumns as $keyColumn) {
if (! empty($columns[$keyColumn]['autoincrement'])) {
return '';
foreach ($columns as $column) {
if ($column['name'] === $keyColumn && ! empty($column['autoincrement'])) {
return '';
}
}
}

Expand Down
22 changes: 18 additions & 4 deletions tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ public function testDropTemporaryTableNotAutoCommitTransaction() : void
}

$platform = $this->connection->getDatabasePlatform();
$columnDefinitions = ['id' => ['type' => Type::getType('integer'), 'notnull' => true]];
$tempTable = $platform->getTemporaryTableName('my_temporary');
$columnDefinitions = [
[
'name' => 'id',
'type' => Type::getType('integer'),
'notnull' => true,
],
];

$tempTable = $platform->getTemporaryTableName('my_temporary');

$createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
. $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
Expand Down Expand Up @@ -79,8 +86,15 @@ public function testCreateTemporaryTableNotAutoCommitTransaction() : void
}

$platform = $this->connection->getDatabasePlatform();
$columnDefinitions = ['id' => ['type' => Type::getType('integer'), 'notnull' => true]];
$tempTable = $platform->getTemporaryTableName('my_temporary');
$columnDefinitions = [
[
'name' => 'id',
'type' => Type::getType('integer'),
'notnull' => true,
],
];

$tempTable = $platform->getTemporaryTableName('my_temporary');

$createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
. $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
Expand Down