Skip to content

Commit 5cb4ca1

Browse files
committed
Migrate call to IndexEditor API
I had to relax a test that checked for the name of an index, which we do not really care about. What we really care about is that there is an index on the column named `quoted-name`. The name changes because the quoting changed from `quoted-name` to "quoted-name".
1 parent a9f633e commit 5cb4ca1

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

phpstan-dbal3.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ parameters:
9696
- '~^Class Doctrine\\DBAL\\Platforms\\SQLitePlatform not found\.$~'
9797

9898
- '~^Class Doctrine\\DBAL\\Schema\\NamedObject not found\.$~'
99+
- '~^Class Doctrine\\DBAL\\Schema\\Name\\Parsers not found\.$~'
99100

100101
-
101102
message: '~sort~'

src/Tools/SchemaTool.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Doctrine\DBAL\Schema\Index;
1919
use Doctrine\DBAL\Schema\Index\IndexedColumn;
2020
use Doctrine\DBAL\Schema\Name\Identifier;
21+
use Doctrine\DBAL\Schema\Name\Parsers;
2122
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
2223
use Doctrine\DBAL\Schema\NamedObject;
2324
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
@@ -409,12 +410,39 @@ public function getSchemaFromMetadata(array $classes): Schema
409410
$indexData['flags'] = [];
410411
}
411412

412-
$table->addIndex(
413-
$this->getIndexColumns($class, $indexData),
414-
is_numeric($indexName) ? null : $indexName,
415-
(array) $indexData['flags'],
416-
$indexData['options'] ?? [],
417-
);
413+
/** @phpstan-ignore function.impossibleType (Parsers::parseUnqualifiedName() is unreleased) */
414+
if (method_exists(Parsers::class, 'parseUnqualifiedName')) {
415+
$indexEditor = Index::editor();
416+
417+
foreach ($this->getIndexColumns($class, $indexData) as $columnName) {
418+
$indexEditor->addColumn(new IndexedColumn(
419+
Parsers::parseUnqualifiedName($columnName),
420+
null,
421+
));
422+
}
423+
424+
if (isset($indexData['flags']['clustered'])) {
425+
$indexEditor->setIsClustered($indexData['flags']['clustered']);
426+
}
427+
428+
if (isset($indexData['options']['where'])) {
429+
$indexEditor->setPredicate($indexData['options']['where']);
430+
}
431+
432+
if (! is_numeric($indexName)) {
433+
$indexEditor->setName(Parsers::parseUnqualifiedName($indexName));
434+
}
435+
436+
/** @phpstan-ignore method.notFound (IndexEditor::addToTable() is unreleased) */
437+
$indexEditor->addToTable($table);
438+
} else {
439+
$table->addIndex(
440+
$this->getIndexColumns($class, $indexData),
441+
is_numeric($indexName) ? null : $indexName,
442+
(array) $indexData['flags'],
443+
$indexData['options'] ?? [],
444+
);
445+
}
418446
}
419447
}
420448

tests/Tests/ORM/Tools/SchemaToolTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\DBAL\Schema\PrimaryKeyConstraintEditor;
1515
use Doctrine\DBAL\Schema\Schema;
1616
use Doctrine\DBAL\Schema\Table as DbalTable;
17+
use Doctrine\DBAL\Schema\TableEditor;
1718
use Doctrine\DBAL\Types\EnumType;
1819
use Doctrine\DBAL\Types\Types;
1920
use Doctrine\ORM\Mapping\ClassMetadata;
@@ -61,6 +62,7 @@
6162
use function current;
6263
use function enum_exists;
6364
use function method_exists;
65+
use function str_contains;
6466

6567
class SchemaToolTest extends OrmTestCase
6668
{
@@ -482,7 +484,21 @@ public function testQuotedIdentifiers(): void
482484

483485
$table = $schema->getTable('quoted-table');
484486

485-
self::assertTrue($table->hasIndex('IDX_AA2790FB50D14D90'));
487+
$foundIndex = false;
488+
if (class_exists(TableEditor::class)) {
489+
foreach ($table->getIndexes() as $index) {
490+
foreach ($index->getIndexedColumns() as $column) {
491+
if (str_contains($column->getColumnName()->toString(), 'quoted-name')) {
492+
$foundIndex = true;
493+
break 2;
494+
}
495+
}
496+
}
497+
} elseif ($table->hasIndex('IDX_AA2790FB50D14D90')) {
498+
$foundIndex = true;
499+
}
500+
501+
self::assertTrue($foundIndex, 'Index on quoted-name should exist.');
486502

487503
// DBAL < 4.3
488504
if (! class_exists(PrimaryKeyConstraint::class)) {

0 commit comments

Comments
 (0)