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

Segregated support of unique index and unique constraint #3980

Merged
merged 2 commits into from
Sep 1, 2020
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
11 changes: 11 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Upgrade to 3.0

## BC BREAK: Doctrine\DBAL\Schema\Table constructor new parameter

Deprecated parameter `$idGeneratorType` removed and added a new parameter `$uniqueConstraints`.
Constructor changed from:

`__construct($name, array $columns = [], array $indexes = [], array $fkConstraints = [], $idGeneratorType = 0, array $options = [])`

To the new constructor:

`__construct($name, array $columns = [], array $indexes = [], array $uniqueConstraints = [], array $fkConstraints = [], array $options = [])`

## BC BREAK: change in the behavior of `SchemaManager::dropDatabase()`

When dropping a database, the DBAL no longer attempts to kill the client sessions that use the database.
Expand Down
58 changes: 35 additions & 23 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types;
use Doctrine\DBAL\Types\Type;
Expand Down Expand Up @@ -1495,12 +1496,26 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE

if (($createFlags & self::CREATE_INDEXES) > 0) {
foreach ($table->getIndexes() as $index) {
if ($index->isPrimary()) {
$options['primary'] = $index->getQuotedColumns($this);
$options['primary_index'] = $index;
} else {
if (! $index->isPrimary()) {
$options['indexes'][$index->getQuotedName($this)] = $index;

continue;
}

$options['primary'] = $index->getQuotedColumns($this);
$options['primary_index'] = $index;
}

foreach ($table->getUniqueConstraints() as $uniqueConstraint) {
$options['uniqueConstraints'][$uniqueConstraint->getQuotedName($this)] = $uniqueConstraint;
}
}

if (($createFlags & self::CREATE_FOREIGNKEYS) > 0) {
$options['foreignKeys'] = [];

foreach ($table->getForeignKeys() as $fkConstraint) {
$options['foreignKeys'][] = $fkConstraint;
}
}

Expand All @@ -1513,6 +1528,7 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
&& $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)
) {
$eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);

$this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);

$columnSql = array_merge($columnSql, $eventArgs->getSql());
Expand All @@ -1539,15 +1555,9 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
$columns[$columnData['name']] = $columnData;
}

if (($createFlags & self::CREATE_FOREIGNKEYS) > 0) {
$options['foreignKeys'] = [];
foreach ($table->getForeignKeys() as $fkConstraint) {
$options['foreignKeys'][] = $fkConstraint;
}
}

if ($this->_eventManager !== null && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
$eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);

$this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);

if ($eventArgs->isDefaultPrevented()) {
Expand All @@ -1556,6 +1566,7 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
}

$sql = $this->_getCreateTableSQL($tableName, $columns, $options);

if ($this->supportsCommentOnStatement()) {
if ($table->hasOption('comment')) {
$sql[] = $this->getCommentOnTableSQL($tableName, $table->getOption('comment'));
Expand Down Expand Up @@ -1654,8 +1665,8 @@ protected function _getCreateTableSQL($name, array $columns, array $options = []
}

$query = 'CREATE TABLE ' . $name . ' (' . $columnListSql;

$check = $this->getCheckDeclarationSQL($columns);

if (! empty($check)) {
$query .= ', ' . $check;
}
Expand Down Expand Up @@ -2307,25 +2318,27 @@ public function getCheckDeclarationSQL(array $definition)
* Obtains DBMS specific SQL code portion needed to set a unique
* constraint declaration to be used in statements like CREATE TABLE.
*
* @param string $name The name of the unique constraint.
* @param Index $index The index definition.
* @param string $name The name of the unique constraint.
* @param UniqueConstraint $constraint The unique constraint definition.
*
* @return string DBMS specific SQL code portion needed to set a constraint.
*
* @throws InvalidArgumentException
*/
public function getUniqueConstraintDeclarationSQL($name, Index $index)
public function getUniqueConstraintDeclarationSQL($name, UniqueConstraint $constraint)
{
$columns = $index->getColumns();
$columns = $constraint->getQuotedColumns($this);
$name = new Identifier($name);

if (count($columns) === 0) {
throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
}

return 'CONSTRAINT ' . $name->getQuotedName($this) . ' UNIQUE ('
. $this->getColumnsFieldDeclarationListSQL($columns)
. ')' . $this->getPartialIndexSQL($index);
$constraintFlags = array_merge(['UNIQUE'], array_map('strtoupper', $constraint->getFlags()));
$constraintName = $name->getQuotedName($this);
$columnListNames = $this->getColumnsFieldDeclarationListSQL($columns);

return sprintf('CONSTRAINT %s %s (%s)', $constraintName, implode(' ', $constraintFlags), $columnListNames);
}

/**
Expand All @@ -2348,9 +2361,8 @@ public function getIndexDeclarationSQL($name, Index $index)
throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
}

return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name->getQuotedName($this) . ' ('
. $this->getIndexFieldDeclarationListSQL($index)
. ')' . $this->getPartialIndexSQL($index);
return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name->getQuotedName($this)
. ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')' . $this->getPartialIndexSQL($index);
}

/**
Expand Down Expand Up @@ -3022,7 +3034,7 @@ public function usesSequenceEmulatedIdentityColumns()
/**
* Returns the name of the sequence for a particular identity column in a particular table.
*
* @see usesSequenceEmulatedIdentityColumns
* @see usesSequenceEmulatedIdentityColumns
*
* @param string $tableName The name of the table to return the sequence name for.
* @param string $columnName The name of the identity column in the table to return the sequence name for.
Expand Down
12 changes: 5 additions & 7 deletions src/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,15 @@ protected function _getCreateTableSQL($name, array $columns, array $options = []
$queryFields = $this->getColumnDeclarationListSQL($columns);

if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $index => $definition) {
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($index, $definition);
foreach ($options['uniqueConstraints'] as $constraintName => $definition) {
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($constraintName, $definition);
}
}

// add all indexes
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach ($options['indexes'] as $index => $definition) {
$queryFields .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
foreach ($options['indexes'] as $indexName => $definition) {
$queryFields .= ', ' . $this->getIndexDeclarationSQL($indexName, $definition);
}
}

Expand Down Expand Up @@ -752,9 +752,7 @@ private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff)
continue;
}

foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName) {
$column = $diff->fromTable->getColumn($columnName);

foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName => $column) {
// Check if an autoincrement column was dropped from the primary key.
if (! $column->getAutoincrement() || in_array($columnName, $changedIndex->getColumns(), true)) {
continue;
Expand Down
16 changes: 2 additions & 14 deletions src/Platforms/SQLServer2012Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ protected function _getCreateTableSQL($name, array $columns, array $options = []
$columnListSql = $this->getColumnDeclarationListSQL($columns);

if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $indexName => $definition) {
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($indexName, $definition);
foreach ($options['uniqueConstraints'] as $constraintName => $definition) {
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($constraintName, $definition);
}
}

Expand Down Expand Up @@ -456,18 +456,6 @@ public function getDefaultConstraintDeclarationSQL($table, array $column)
' FOR ' . $columnName->getQuotedName($this);
}

/**
* {@inheritDoc}
*/
public function getUniqueConstraintDeclarationSQL($name, Index $index)
{
$constraint = parent::getUniqueConstraintDeclarationSQL($name, $index);

$constraint = $this->_appendUniqueConstraintDefinition($constraint, $index);

return $constraint;
}

/**
* {@inheritDoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,8 @@ public function getAlterTableSQL(TableDiff $diff)
$table->getQuotedName($this),
$columns,
$this->getPrimaryIndexInAlteredTable($diff),
[],
$this->getForeignKeysInAlteredTable($diff),
0,
$table->getOptions()
);
$newTable->addOption('alter', true);
Expand Down
3 changes: 2 additions & 1 deletion src/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,14 @@ public function listTableDetails($name)
{
$columns = $this->listTableColumns($name);
$foreignKeys = [];

if ($this->_platform->supportsForeignKeyConstraints()) {
$foreignKeys = $this->listTableForeignKeys($name);
}

$indexes = $this->listTableIndexes($name);

return new Table($name, $columns, $indexes, $foreignKeys);
return new Table($name, $columns, $indexes, [], $foreignKeys);
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/Schema/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use function array_search;
use function array_shift;
use function count;
use function is_string;
use function strtolower;

class Index extends AbstractAsset implements Constraint
Expand Down Expand Up @@ -79,18 +78,10 @@ public function __construct(
}

/**
* @param string $column
*
* @return void
*
* @throws InvalidArgumentException
*/
protected function _addColumn($column)
protected function _addColumn(string $column): void
{
if (! is_string($column)) {
throw new InvalidArgumentException('Expecting a string as Index Column');
}

$this->_columns[$column] = new Identifier($column);
}

Expand Down
17 changes: 16 additions & 1 deletion src/Schema/SchemaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class SchemaException extends DBALException
public const SEQUENCE_ALREADY_EXISTS = 80;
public const INDEX_INVALID_NAME = 90;
public const FOREIGNKEY_DOESNT_EXIST = 100;
public const NAMESPACE_ALREADY_EXISTS = 110;
public const CONSTRAINT_DOESNT_EXIST = 110;
public const NAMESPACE_ALREADY_EXISTS = 120;

/**
* @param string $tableName
Expand Down Expand Up @@ -146,6 +147,20 @@ public static function sequenceDoesNotExist($name)
return new self("There exists no sequence with the name '" . $name . "'.", self::SEQUENCE_DOENST_EXIST);
}

/**
* @param string $constraintName
* @param string $table
*
* @return SchemaException
*/
public static function uniqueConstraintDoesNotExist($constraintName, $table)
{
return new self(
sprintf('There exists no unique constraint with the name "%s" on table "%s".', $constraintName, $table),
self::CONSTRAINT_DOESNT_EXIST
);
}

/**
* @param string $fkName
* @param string $table
Expand Down
Loading