Skip to content

Commit

Permalink
Fixed more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Feb 20, 2019
1 parent 3b51e17 commit a7c97ea
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
22 changes: 19 additions & 3 deletions lib/Doctrine/DBAL/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function array_unique;
use function assert;
use function count;
use function strcasecmp;
use function strtolower;

/**
Expand Down Expand Up @@ -259,7 +260,10 @@ public function diffTable(Table $table1, Table $table2)
if ($this->diffForeignKey($constraint1, $constraint2) === false) {
unset($fromFkeys[$key1], $toFkeys[$key2]);
} else {
if (strtolower($constraint1->getName()) === strtolower($constraint2->getName())) {
$name1 = $constraint1->getName();
$name2 = $constraint2->getName();

if ($name1 !== null && $name2 !== null && strcasecmp($name1, $name2) === 0) {
$tableDifferences->changedForeignKeys[] = $constraint2;
$changes++;
unset($fromFkeys[$key1], $toFkeys[$key2]);
Expand Down Expand Up @@ -349,8 +353,20 @@ private function detectIndexRenamings(TableDiff $tableDifferences) : void

[$removedIndex, $addedIndex] = $candidateIndexes[0];

$removedIndexName = strtolower($removedIndex->getName());
$addedIndexName = strtolower($addedIndex->getName());
$removedIndexName = $removedIndex->getName();

if ($removedIndexName === null) {
continue;
}

$addedIndexName = $addedIndex->getName();

if ($addedIndexName === null) {
continue;
}

$removedIndexName = strtolower($removedIndexName);
$addedIndexName = strtolower($addedIndexName);

if (isset($tableDifferences->renamedIndexes[$removedIndexName])) {
continue;
Expand Down
11 changes: 11 additions & 0 deletions lib/Doctrine/DBAL/Schema/SchemaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ public static function namedForeignKeyRequired(Table $localTable, ForeignKeyCons
);
}

public static function namedIndexRequired(Table $table, Index $index) : self
{
return new self(
sprintf(
'The performed schema operation on %s requires a named index, but the given index on (%s) is currently unnamed.',
$table->getName(),
implode(', ', $index->getColumns())
)
);
}

public static function alterTableChangeNotSupported(string $changeName) : self
{
return new self(
Expand Down
22 changes: 20 additions & 2 deletions lib/Doctrine/DBAL/Schema/Visitor/RemoveNamespacedAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;

Expand Down Expand Up @@ -57,14 +58,17 @@ public function acceptSequence(Sequence $sequence) : void

/**
* {@inheritdoc}
*
* @throws SchemaException
*/
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) : void
{
// The table may already be deleted in a previous
// RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
// point to nowhere.
if (! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
$localTable->removeForeignKey($fkConstraint->getName());
$this->removeForeignKey($localTable, $fkConstraint);

return;
}

Expand All @@ -73,6 +77,20 @@ public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkCons
return;
}

$localTable->removeForeignKey($fkConstraint->getName());
$this->removeForeignKey($localTable, $fkConstraint);
}

/**
* @throws SchemaException
*/
private function removeForeignKey(Table $table, ForeignKeyConstraint $constraint) : void
{
$name = $constraint->getName();

if ($name === null) {
throw SchemaException::namedForeignKeyRequired($table, $constraint);
}

$table->removeForeignKey($name);
}
}
15 changes: 12 additions & 3 deletions lib/Doctrine/DBAL/Sharding/SQLAzure/Schema/MultiTenantVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Visitor\Visitor;
Expand Down Expand Up @@ -62,6 +63,8 @@ public function __construct(array $excludedTables = [], string $tenantColumnName

/**
* {@inheritdoc}
*
* @throws SchemaException
*/
public function acceptTable(Table $table) : void
{
Expand All @@ -82,9 +85,15 @@ public function acceptTable(Table $table) : void
$table->dropPrimaryKey();
$table->setPrimaryKey($indexColumns);
} else {
$table->dropIndex($clusteredIndex->getName());
$table->addIndex($indexColumns, $clusteredIndex->getName());
$table->getIndex($clusteredIndex->getName())->addFlag('clustered');
$name = $clusteredIndex->getName();

if ($name === null) {
throw SchemaException::namedIndexRequired($table, $clusteredIndex);
}

$table->dropIndex($name);
$table->addIndex($indexColumns, $name);
$table->getIndex($name)->addFlag('clustered');
}
}

Expand Down

0 comments on commit a7c97ea

Please sign in to comment.