Skip to content

Commit 5c2c764

Browse files
committed
Migrate to schema and column editor APIs
1 parent 2f396b8 commit 5c2c764

2 files changed

Lines changed: 150 additions & 12 deletions

File tree

phpstan-dbal3.neon

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,36 @@ parameters:
164164
-
165165
message: '~inferType.*never returns~'
166166
path: src/Query/ParameterTypeInferer.php
167+
168+
# DBAL 4/5 Schema Editor API compatibility
169+
-
170+
message: '~^Class Doctrine\\DBAL\\Schema\\Table constructor invoked with 7 parameters, 1-6 required\.$~'
171+
path: src/Tools/SchemaTool.php
172+
173+
-
174+
message: '~^Call to an undefined method Doctrine\\DBAL\\Schema\\SchemaConfig::toTableConfiguration\(\)\.$~'
175+
path: src/Tools/SchemaTool.php
176+
177+
-
178+
message: '~^Call to method setAutoincrement\(\) on an unknown class Doctrine\\DBAL\\Schema\\ColumnEditor\.$~'
179+
path: src/Tools/SchemaTool.php
180+
181+
-
182+
message: '~^Parameter \$column of anonymous function has invalid type Doctrine\\DBAL\\Schema\\ColumnEditor\.$~'
183+
path: src/Tools/SchemaTool.php
184+
185+
-
186+
message: '~^Call to an undefined method Doctrine\\DBAL\\Schema\\Table::edit\(\)\.$~'
187+
path: src/Tools/SchemaTool.php
188+
189+
-
190+
message: '~^Call to an undefined method Doctrine\\DBAL\\Schema\\Table::modifyColumnByUnquotedName\(\)\.$~'
191+
path: src/Tools/SchemaTool.php
192+
193+
-
194+
message: '~^Instantiated class Doctrine\\DBAL\\Schema\\ColumnEditor not found\.$~'
195+
path: src/Tools/SchemaTool.php
196+
197+
-
198+
message: '~^Unknown parameter \$configuration in call to Doctrine\\DBAL\\Schema\\Table constructor\.$~'
199+
path: src/Tools/SchemaTool.php

src/Tools/SchemaTool.php

Lines changed: 117 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Platforms\AbstractPlatform;
99
use Doctrine\DBAL\Schema\AbstractAsset;
1010
use Doctrine\DBAL\Schema\AbstractSchemaManager;
11+
use Doctrine\DBAL\Schema\ColumnEditor;
1112
use Doctrine\DBAL\Schema\ComparatorConfig;
1213
use Doctrine\DBAL\Schema\DefaultExpression;
1314
use Doctrine\DBAL\Schema\DefaultExpression\CurrentDate;
@@ -21,6 +22,7 @@
2122
use Doctrine\DBAL\Schema\NamedObject;
2223
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
2324
use Doctrine\DBAL\Schema\Schema;
25+
use Doctrine\DBAL\Schema\SchemaConfig;
2426
use Doctrine\DBAL\Schema\Table;
2527
use Doctrine\DBAL\Types\Types;
2628
use Doctrine\Deprecations\Deprecation;
@@ -204,11 +206,36 @@ public function getSchemaFromMetadata(array $classes): Schema
204206
continue;
205207
}
206208

207-
$table = $schema->createTable($this->quoteStrategy->getTableName($class, $this->platform));
209+
$tableName = $this->quoteStrategy->getTableName($class, $this->platform);
210+
211+
// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
212+
if (method_exists(Schema::class, 'edit')) {
213+
$table = new Table(
214+
name: $tableName,
215+
configuration: $metadataSchemaConfig->toTableConfiguration(),
216+
);
217+
// Add default table options (charset, collation, engine, etc.)
218+
foreach ($metadataSchemaConfig->getDefaultTableOptions() as $option => $value) {
219+
$table->addOption($option, $value);
220+
}
221+
} else {
222+
$table = $schema->createTable($tableName);
223+
}
208224

209225
if ($class->isInheritanceTypeSingleTable()) {
226+
// For new schema API: collect join tables to add after this entity table
227+
$joinTablesToAdd = [];
228+
210229
$this->gatherColumns($class, $table);
211-
$this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
230+
$this->gatherRelationsSql(
231+
$class,
232+
$table,
233+
$schema,
234+
$addedFks,
235+
$blacklistedFks,
236+
$metadataSchemaConfig,
237+
$joinTablesToAdd,
238+
);
212239

213240
// Add the discriminator column
214241
$this->addDiscriminatorColumnDefinition($class, $table);
@@ -222,18 +249,37 @@ public function getSchemaFromMetadata(array $classes): Schema
222249
foreach ($class->subClasses as $subClassName) {
223250
$subClass = $this->em->getClassMetadata($subClassName);
224251
$this->gatherColumns($subClass, $table);
225-
$this->gatherRelationsSql($subClass, $table, $schema, $addedFks, $blacklistedFks);
252+
$this->gatherRelationsSql(
253+
$subClass,
254+
$table,
255+
$schema,
256+
$addedFks,
257+
$blacklistedFks,
258+
$metadataSchemaConfig,
259+
$joinTablesToAdd,
260+
);
226261
$processedClasses[$subClassName] = true;
227262
}
228263
} elseif ($class->isInheritanceTypeJoined()) {
264+
// For new schema API: collect join tables to add after this entity table
265+
$joinTablesToAdd = [];
266+
229267
// Add all non-inherited fields as columns
230268
foreach ($class->fieldMappings as $fieldName => $mapping) {
231269
if (! isset($mapping->inherited)) {
232270
$this->gatherColumn($class, $mapping, $table);
233271
}
234272
}
235273

236-
$this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
274+
$this->gatherRelationsSql(
275+
$class,
276+
$table,
277+
$schema,
278+
$addedFks,
279+
$blacklistedFks,
280+
$metadataSchemaConfig,
281+
$joinTablesToAdd,
282+
);
237283

238284
// Add the discriminator column only to the root table
239285
if ($class->name === $class->rootEntityName) {
@@ -253,7 +299,17 @@ public function getSchemaFromMetadata(array $classes): Schema
253299
$this->platform,
254300
);
255301
// TODO: This seems rather hackish, can we optimize it?
256-
$table->getColumn($columnName)->setAutoincrement(false);
302+
// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API for version detection)
303+
if (method_exists(Schema::class, 'edit')) {
304+
// New API: modify column using table editor (creates new table object)
305+
// This is safe because we'll add the table to schema later after all modifications
306+
$table = $table->edit()->modifyColumnByUnquotedName(
307+
$columnName,
308+
static fn (ColumnEditor $column) => $column->setAutoincrement(false),
309+
)->create();
310+
} else {
311+
$table->getColumn($columnName)->setAutoincrement(false);
312+
}
257313

258314
$pkColumns[] = $columnName;
259315
$inheritedKeyColumns[] = $columnName;
@@ -305,8 +361,19 @@ public function getSchemaFromMetadata(array $classes): Schema
305361
}
306362
}
307363
} else {
364+
// For new schema API: collect join tables to add after this entity table
365+
$joinTablesToAdd = [];
366+
308367
$this->gatherColumns($class, $table);
309-
$this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
368+
$this->gatherRelationsSql(
369+
$class,
370+
$table,
371+
$schema,
372+
$addedFks,
373+
$blacklistedFks,
374+
$metadataSchemaConfig,
375+
$joinTablesToAdd,
376+
);
310377
}
311378

312379
$pkColumns = [];
@@ -377,6 +444,23 @@ public function getSchemaFromMetadata(array $classes): Schema
377444

378445
$processedClasses[$class->name] = true;
379446

447+
// Add the fully populated table to the schema
448+
// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
449+
if (method_exists(Schema::class, 'edit')) {
450+
// @phpstan-ignore method.notFound (Using unreleased Schema::edit() API)
451+
$schemaEditor = $schema->edit();
452+
$schemaEditor->addTable($table);
453+
$schema = $schemaEditor->create();
454+
455+
// Add any join tables collected during relation processing
456+
// This ensures join tables appear right after their owning entity table
457+
foreach ($joinTablesToAdd as $joinTable) {
458+
$schemaEditor = $schema->edit();
459+
$schemaEditor->addTable($joinTable);
460+
$schema = $schemaEditor->create();
461+
}
462+
}
463+
380464
if ($class->isIdGeneratorSequence() && $class->name === $class->rootEntityName) {
381465
$seqDef = $class->sequenceGeneratorDefinition;
382466
$quotedName = $this->quoteStrategy->getSequenceName($seqDef, $class, $this->platform);
@@ -614,15 +698,18 @@ private function gatherColumn(
614698
* fkOptions: array{onDelete?: string, deferrable?: bool, deferred?: bool}
615699
* }> $addedFks
616700
* @phpstan-param array<string, bool> $blacklistedFks
701+
* @phpstan-param list<Table> $joinTablesToAdd
617702
*
618703
* @throws NotSupported
619704
*/
620705
private function gatherRelationsSql(
621706
ClassMetadata $class,
622707
Table $table,
623-
Schema $schema,
708+
Schema &$schema,
624709
array &$addedFks,
625710
array &$blacklistedFks,
711+
SchemaConfig $schemaConfig,
712+
array &$joinTablesToAdd,
626713
): void {
627714
foreach ($class->associationMappings as $id => $mapping) {
628715
if (isset($mapping->inherited) && ! in_array($id, $class->identifier, true)) {
@@ -647,12 +734,25 @@ private function gatherRelationsSql(
647734
// create join table
648735
$joinTable = $mapping->joinTable;
649736

650-
$theJoinTable = $schema->createTable(
651-
$this->quoteStrategy->getJoinTableName($mapping, $foreignClass, $this->platform),
652-
);
737+
$tableName = $this->quoteStrategy->getJoinTableName($mapping, $foreignClass, $this->platform);
653738

654-
foreach ($joinTable->options as $key => $val) {
655-
$theJoinTable->addOption($key, $val);
739+
// Create the join table object
740+
// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
741+
if (method_exists(Schema::class, 'edit')) {
742+
$theJoinTable = new Table(
743+
name: $tableName,
744+
options: $joinTable->options,
745+
configuration: $schemaConfig->toTableConfiguration(),
746+
);
747+
// Add default table options (charset, collation, engine, etc.)
748+
foreach ($schemaConfig->getDefaultTableOptions() as $option => $value) {
749+
$theJoinTable->addOption($option, $value);
750+
}
751+
} else {
752+
$theJoinTable = $schema->createTable($tableName);
753+
foreach ($joinTable->options as $key => $val) {
754+
$theJoinTable->addOption($key, $val);
755+
}
656756
}
657757

658758
$primaryKeyColumns = [];
@@ -680,6 +780,11 @@ private function gatherRelationsSql(
680780
);
681781

682782
self::addPrimaryKeyConstraint($theJoinTable, $primaryKeyColumns);
783+
784+
// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
785+
if (method_exists(Schema::class, 'edit')) {
786+
$joinTablesToAdd[] = $theJoinTable;
787+
}
683788
}
684789
}
685790
}

0 commit comments

Comments
 (0)