-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Make SchemaTool use the new DBAL API #12113
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
base: 3.5.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,7 +12,7 @@ | |||||
use Doctrine\DBAL\Schema\ForeignKeyConstraintEditor; | ||||||
use Doctrine\DBAL\Schema\Index; | ||||||
use Doctrine\DBAL\Schema\Index\IndexedColumn; | ||||||
use Doctrine\DBAL\Schema\Name\Identifier; | ||||||
use Doctrine\DBAL\Schema\IndexEditor; | ||||||
use Doctrine\DBAL\Schema\Name\UnqualifiedName; | ||||||
use Doctrine\DBAL\Schema\PrimaryKeyConstraint; | ||||||
use Doctrine\DBAL\Schema\Schema; | ||||||
|
@@ -38,6 +38,7 @@ | |||||
use function array_flip; | ||||||
use function array_intersect_key; | ||||||
use function array_map; | ||||||
use function array_values; | ||||||
use function assert; | ||||||
use function class_exists; | ||||||
use function count; | ||||||
|
@@ -60,6 +61,7 @@ class SchemaTool | |||||
|
||||||
private readonly AbstractPlatform $platform; | ||||||
private readonly QuoteStrategy $quoteStrategy; | ||||||
/** @var AbstractSchemaManager<AbstractPlatform> */ | ||||||
private readonly AbstractSchemaManager $schemaManager; | ||||||
|
||||||
/** | ||||||
|
@@ -128,9 +130,10 @@ private function processingNotRequired( | |||||
/** | ||||||
* Resolves fields in index mapping to column names | ||||||
* | ||||||
* @param mixed[] $indexData index or unique constraint data | ||||||
* @param ClassMetadata<object> $class | ||||||
* @param mixed[] $indexData index or unique constraint data | ||||||
* | ||||||
* @return list<string> Column names from combined fields and columns mappings | ||||||
* @return non-empty-list<non-empty-string> Column names from combined fields and columns mappings | ||||||
*/ | ||||||
private function getIndexColumns(ClassMetadata $class, array $indexData): array | ||||||
{ | ||||||
|
@@ -167,6 +170,13 @@ private function getIndexColumns(ClassMetadata $class, array $indexData): array | |||||
} | ||||||
} | ||||||
|
||||||
if ($columns === []) { | ||||||
throw MappingException::invalidIndexConfiguration( | ||||||
(string) $class, | ||||||
$indexData['name'] ?? 'unnamed', | ||||||
); | ||||||
} | ||||||
|
||||||
return $columns; | ||||||
} | ||||||
|
||||||
|
@@ -314,17 +324,13 @@ public function getSchemaFromMetadata(array $classes): Schema | |||||
} | ||||||
} | ||||||
|
||||||
if (! $table->hasIndex('primary')) { | ||||||
self::addPrimaryKeyConstraint($table, $pkColumns); | ||||||
} | ||||||
$primaryKey = $this->getPrimaryKeyConstraint($table) ?? $this->addPrimaryKeyConstraint($table, $pkColumns); | ||||||
|
||||||
// there can be unique indexes automatically created for join column | ||||||
// if join column is also primary key we should keep only primary key on this column | ||||||
// so, remove indexes overruled by primary key | ||||||
$primaryKey = $table->getIndex('primary'); | ||||||
|
||||||
foreach ($table->getIndexes() as $idxKey => $existingIndex) { | ||||||
if ($existingIndex !== $primaryKey && $primaryKey->spansColumns(self::getIndexedColumns($existingIndex))) { | ||||||
if ($idxKey !== 'primary' && $this->doesIndexOverlapWithPrimaryKey($existingIndex, $primaryKey)) { | ||||||
$table->dropIndex($idxKey); | ||||||
} | ||||||
} | ||||||
|
@@ -346,7 +352,7 @@ public function getSchemaFromMetadata(array $classes): Schema | |||||
|
||||||
if (isset($class->table['uniqueConstraints'])) { | ||||||
foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) { | ||||||
$uniqIndex = new Index('tmp__' . $indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []); | ||||||
$uniqIndex = $this->createIndexForComparison('tmp__' . $indexName, $this->getIndexColumns($class, $indexData), $indexData['options'] ?? []); | ||||||
|
||||||
foreach ($table->getIndexes() as $tableIndexName => $tableIndex) { | ||||||
if ($tableIndex->isFulfilledBy($uniqIndex)) { | ||||||
|
@@ -872,11 +878,7 @@ public function getDropSchemaSQL(array $classes): array | |||||
} | ||||||
|
||||||
foreach ($schema->getTables() as $table) { | ||||||
if (method_exists($table, 'getPrimaryKeyConstraint')) { | ||||||
$primaryKey = $table->getPrimaryKeyConstraint(); | ||||||
} else { | ||||||
$primaryKey = $table->getPrimaryKey(); | ||||||
} | ||||||
$primaryKey = $this->getPrimaryKeyConstraint($table); | ||||||
|
||||||
if ($primaryKey === null) { | ||||||
continue; | ||||||
|
@@ -969,29 +971,84 @@ private function createSchemaForComparison(Schema $toSchema): Schema | |||||
} | ||||||
} | ||||||
|
||||||
/** @param string[] $primaryKeyColumns */ | ||||||
private function addPrimaryKeyConstraint(Table $table, array $primaryKeyColumns): void | ||||||
private function getPrimaryKeyConstraint(Table $table): PrimaryKeyConstraint|Index|null | ||||||
{ | ||||||
if (class_exists(PrimaryKeyConstraint::class)) { | ||||||
$primaryKeyColumnNames = []; | ||||||
// DBAL < 4.3 | ||||||
if (! method_exists($table, 'getPrimaryKeyConstraint')) { | ||||||
return $table->getPrimaryKey(); | ||||||
} | ||||||
|
||||||
foreach ($primaryKeyColumns as $primaryKeyColumn) { | ||||||
$primaryKeyColumnNames[] = new UnqualifiedName(Identifier::unquoted($primaryKeyColumn)); | ||||||
} | ||||||
return $table->getPrimaryKeyConstraint(); | ||||||
} | ||||||
|
||||||
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, $primaryKeyColumnNames, true)); | ||||||
} else { | ||||||
/** @param string[] $primaryKeyColumns */ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use the same advanced types you used somewhere else in this PR? Maybe it will address the baselined issue.
Suggested change
|
||||||
private function addPrimaryKeyConstraint(Table $table, array $primaryKeyColumns): PrimaryKeyConstraint|Index | ||||||
{ | ||||||
// DBAL < 4.3 | ||||||
if (! class_exists(PrimaryKeyConstraint::class)) { | ||||||
$table->setPrimaryKey($primaryKeyColumns); | ||||||
|
||||||
return $table->getPrimaryKey(); | ||||||
} | ||||||
|
||||||
$primaryKeyConstraint = PrimaryKeyConstraint::editor() | ||||||
->setUnquotedColumnNames(...array_values($primaryKeyColumns)) | ||||||
->setIsClustered(true) | ||||||
->create(); | ||||||
|
||||||
$table->addPrimaryKeyConstraint($primaryKeyConstraint); | ||||||
|
||||||
return $table->getPrimaryKeyConstraint(); | ||||||
} | ||||||
|
||||||
/** @return string[] */ | ||||||
/** @return non-empty-list<string> */ | ||||||
private static function getIndexedColumns(Index $index): array | ||||||
{ | ||||||
if (method_exists(Index::class, 'getIndexedColumns')) { | ||||||
return array_map(static fn (IndexedColumn $indexedColumn) => $indexedColumn->getColumnName()->toString(), $index->getIndexedColumns()); | ||||||
// DBAL < 4.3 | ||||||
if (! method_exists(Index::class, 'getIndexedColumns')) { | ||||||
return $index->getColumns(); | ||||||
} | ||||||
|
||||||
return array_map(static fn (IndexedColumn $indexedColumn) => $indexedColumn->getColumnName()->getIdentifier()->getValue(), $index->getIndexedColumns()); | ||||||
} | ||||||
|
||||||
private function doesIndexOverlapWithPrimaryKey(Index $index, PrimaryKeyConstraint|Index $primaryKey): bool | ||||||
{ | ||||||
// DBAL < 4.3 | ||||||
if ($primaryKey instanceof Index) { | ||||||
return $index !== $primaryKey && $primaryKey->spansColumns(self::getIndexedColumns($index)); | ||||||
} | ||||||
|
||||||
$indexedColumns = $index->getIndexedColumns(); | ||||||
foreach ($primaryKey->getColumnNames() as $i => $column) { | ||||||
if ( | ||||||
! isset($indexedColumns[$i]) | ||||||
|| strtolower($column->getIdentifier()->getValue()) | ||||||
!== strtolower($indexedColumns[$i]->getColumnName()->getIdentifier()->getValue()) | ||||||
) { | ||||||
return false; | ||||||
} | ||||||
} | ||||||
|
||||||
return true; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @param non-empty-string $indexName | ||||||
* @param non-empty-list<non-empty-string> $indexColumns | ||||||
* @param array<string, mixed> $indexOptions | ||||||
*/ | ||||||
private function createIndexForComparison(string $indexName, array $indexColumns, mixed $indexOptions): Index | ||||||
{ | ||||||
// DBAL < 4.3 | ||||||
if (! class_exists(IndexEditor::class)) { | ||||||
return new Index($indexName, $indexColumns, true, false, [], $indexOptions); | ||||||
} | ||||||
|
||||||
return $index->getColumns(); | ||||||
return Index::editor() | ||||||
->setUnquotedName($indexName) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would double-check how this code behaves if the index name contains quotes (e.g. If that's the case, you'll need to parse the string with |
||||||
->setUnquotedColumnNames(...$indexColumns) | ||||||
->setType(Index\IndexType::UNIQUE) | ||||||
->create(); | ||||||
} | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we have identifiers at our disposal, I think we should not add new issues to the baseline, and instead resort to e.g.
/** @phpstan-ignore argument.type (reason why we ignore this) */
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll try to do that when I rework the PR.