Skip to content

Commit

Permalink
Decouple unique index from unique constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermeblanco authored and morozov committed Jan 4, 2018
1 parent a5d3e33 commit 400d73c
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 166 deletions.
34 changes: 21 additions & 13 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\DBAL\Types;
use Doctrine\DBAL\Types\Type;

Expand Down Expand Up @@ -1522,15 +1523,30 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
$options['indexes'] = [];
$options['primary'] = [];

if (($createFlags&self::CREATE_INDEXES) > 0) {
if (($createFlags & self::CREATE_INDEXES) > 0) {
foreach ($table->getIndexes() as $index) {
/* @var $index 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) {
/** @var UniqueConstraint $uniqueConstraint */
$options['uniqueConstraints'][$uniqueConstraint->getQuotedName($this)] = $uniqueConstraint;
}
}

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

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

Expand All @@ -1539,7 +1555,6 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE

foreach ($table->getColumns() as $column) {
/* @var \Doctrine\DBAL\Schema\Column $column */

if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)) {
$eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);
Expand Down Expand Up @@ -1567,13 +1582,6 @@ 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 (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
$eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,11 @@ public function getAlterTableSQL(TableDiff $diff)

$sql = [];
$tableSql = [];

if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
$dataTable = new Table('__temp__'.$table->getName());

$newTable = new Table($table->getQuotedName($this), $columns, $this->getPrimaryIndexInAlteredTable($diff), $this->getForeignKeysInAlteredTable($diff), 0, $table->getOptions());
$newTable = new Table($table->getQuotedName($this), $columns, $this->getPrimaryIndexInAlteredTable($diff), array(), $this->getForeignKeysInAlteredTable($diff), 0, $table->getOptions());
$newTable->addOption('alter', true);

$sql = $this->getPreAlterTableIndexForeignKeySQL($diff);
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,14 @@ public function listTableDetails($tableName)
{
$columns = $this->listTableColumns($tableName);
$foreignKeys = [];

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

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

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

/**
Expand Down Expand Up @@ -621,6 +623,7 @@ public function dropAndCreateView(View $view)
public function alterTable(TableDiff $tableDiff)
{
$queries = $this->_platform->getAlterTableSQL($tableDiff);

if (is_array($queries) && count($queries)) {
foreach ($queries as $ddlQuery) {
$this->_execSql($ddlQuery);
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/DBAL/Schema/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ public function __construct($indexName, array $columns, $isUnique = false, $isPr
$isUnique = $isUnique || $isPrimary;

$this->_setName($indexName);
$this->_isUnique = $isUnique;

$this->_isUnique = $isUnique;
$this->_isPrimary = $isPrimary;
$this->options = $options;
$this->options = $options;

foreach ($columns as $column) {
$this->_addColumn($column);
}

foreach ($flags as $flag) {
$this->addFlag($flag);
}
Expand Down
14 changes: 13 additions & 1 deletion lib/Doctrine/DBAL/Schema/SchemaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class SchemaException extends \Doctrine\DBAL\DBALException
const SEQUENCE_ALREADY_EXISTS = 80;
const INDEX_INVALID_NAME = 90;
const FOREIGNKEY_DOESNT_EXIST = 100;
const NAMESPACE_ALREADY_EXISTS = 110;
const CONSTRAINT_DOESNT_EXIST = 110;
const NAMESPACE_ALREADY_EXISTS = 120;

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

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

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

0 comments on commit 400d73c

Please sign in to comment.