Skip to content

Commit 2f4809a

Browse files
guilhermeblancomorozov
authored andcommitted
Decouple unique index from unique constraint
1 parent 3a0a1d1 commit 2f4809a

File tree

11 files changed

+518
-175
lines changed

11 files changed

+518
-175
lines changed

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Doctrine\DBAL\Schema\Sequence;
2424
use Doctrine\DBAL\Schema\Table;
2525
use Doctrine\DBAL\Schema\TableDiff;
26+
use Doctrine\DBAL\Schema\UniqueConstraint;
2627
use Doctrine\DBAL\TransactionIsolationLevel;
2728
use Doctrine\DBAL\Types;
2829
use Doctrine\DBAL\Types\Type;
@@ -1546,15 +1547,30 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
15461547
$options['indexes'] = [];
15471548
$options['primary'] = [];
15481549

1549-
if (($createFlags&self::CREATE_INDEXES) > 0) {
1550+
if (($createFlags & self::CREATE_INDEXES) > 0) {
15501551
foreach ($table->getIndexes() as $index) {
15511552
/** @var $index Index */
1552-
if ($index->isPrimary()) {
1553-
$options['primary'] = $index->getQuotedColumns($this);
1554-
$options['primary_index'] = $index;
1555-
} else {
1553+
if (! $index->isPrimary()) {
15561554
$options['indexes'][$index->getQuotedName($this)] = $index;
1555+
1556+
continue;
15571557
}
1558+
1559+
$options['primary'] = $index->getQuotedColumns($this);
1560+
$options['primary_index'] = $index;
1561+
}
1562+
1563+
foreach ($table->getUniqueConstraints() as $uniqueConstraint) {
1564+
/** @var UniqueConstraint $uniqueConstraint */
1565+
$options['uniqueConstraints'][$uniqueConstraint->getQuotedName($this)] = $uniqueConstraint;
1566+
}
1567+
}
1568+
1569+
if (($createFlags & self::CREATE_FOREIGNKEYS) > 0) {
1570+
$options['foreignKeys'] = [];
1571+
1572+
foreach ($table->getForeignKeys() as $fkConstraint) {
1573+
$options['foreignKeys'][] = $fkConstraint;
15581574
}
15591575
}
15601576

@@ -1589,13 +1605,6 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
15891605
$columns[$columnData['name']] = $columnData;
15901606
}
15911607

1592-
if (($createFlags&self::CREATE_FOREIGNKEYS) > 0) {
1593-
$options['foreignKeys'] = [];
1594-
foreach ($table->getForeignKeys() as $fkConstraint) {
1595-
$options['foreignKeys'][] = $fkConstraint;
1596-
}
1597-
}
1598-
15991608
if ($this->_eventManager !== null && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
16001609
$eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
16011610
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);

lib/Doctrine/DBAL/Platforms/SqlitePlatform.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,10 +898,11 @@ public function getAlterTableSQL(TableDiff $diff)
898898

899899
$sql = [];
900900
$tableSql = [];
901+
901902
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
902903
$dataTable = new Table('__temp__' . $table->getName());
903904

904-
$newTable = new Table($table->getQuotedName($this), $columns, $this->getPrimaryIndexInAlteredTable($diff), $this->getForeignKeysInAlteredTable($diff), 0, $table->getOptions());
905+
$newTable = new Table($table->getQuotedName($this), $columns, $this->getPrimaryIndexInAlteredTable($diff), [], $this->getForeignKeysInAlteredTable($diff), 0, $table->getOptions());
905906
$newTable->addOption('alter', true);
906907

907908
$sql = $this->getPreAlterTableIndexForeignKeySQL($diff);

lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,14 @@ public function listTableDetails($tableName)
268268
{
269269
$columns = $this->listTableColumns($tableName);
270270
$foreignKeys = [];
271+
271272
if ($this->_platform->supportsForeignKeyConstraints()) {
272273
$foreignKeys = $this->listTableForeignKeys($tableName);
273274
}
275+
274276
$indexes = $this->listTableIndexes($tableName);
275277

276-
return new Table($tableName, $columns, $indexes, $foreignKeys);
278+
return new Table($tableName, $columns, $indexes, [], $foreignKeys);
277279
}
278280

279281
/**
@@ -592,6 +594,7 @@ public function dropAndCreateView(View $view)
592594
public function alterTable(TableDiff $tableDiff)
593595
{
594596
$queries = $this->_platform->getAlterTableSQL($tableDiff);
597+
595598
if (! is_array($queries) || ! count($queries)) {
596599
return;
597600
}

lib/Doctrine/DBAL/Schema/Index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ public function __construct($indexName, array $columns, $isUnique = false, $isPr
5858
$isUnique = $isUnique || $isPrimary;
5959

6060
$this->_setName($indexName);
61+
6162
$this->_isUnique = $isUnique;
6263
$this->_isPrimary = $isPrimary;
6364
$this->options = $options;
6465

6566
foreach ($columns as $column) {
6667
$this->_addColumn($column);
6768
}
69+
6870
foreach ($flags as $flag) {
6971
$this->addFlag($flag);
7072
}

lib/Doctrine/DBAL/Schema/SchemaException.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class SchemaException extends DBALException
1818
public const SEQUENCE_ALREADY_EXISTS = 80;
1919
public const INDEX_INVALID_NAME = 90;
2020
public const FOREIGNKEY_DOESNT_EXIST = 100;
21-
public const NAMESPACE_ALREADY_EXISTS = 110;
21+
public const CONSTRAINT_DOESNT_EXIST = 110;
22+
public const NAMESPACE_ALREADY_EXISTS = 120;
2223

2324
/**
2425
* @param string $tableName
@@ -142,6 +143,21 @@ public static function sequenceDoesNotExist($sequenceName)
142143
return new self("There exists no sequence with the name '" . $sequenceName . "'.", self::SEQUENCE_DOENST_EXIST);
143144
}
144145

146+
/**
147+
* @param string $constraintName
148+
* @param string $table
149+
*
150+
* @return self
151+
*/
152+
public static function uniqueConstraintDoesNotExist($constraintName, $table)
153+
{
154+
return new self(sprintf(
155+
'There exists no unique constraint with the name "%s" on table "%s".',
156+
$constraintName,
157+
$table
158+
), self::CONSTRAINT_DOESNT_EXIST);
159+
}
160+
145161
/**
146162
* @param string $fkName
147163
* @param string $table

0 commit comments

Comments
 (0)