Skip to content

Commit fa94c6f

Browse files
authored
Merge pull request #1519 from mbabker/dbal-deprecations
Resolve deprecations introduced with DBAL 4.3
2 parents afb1731 + a4dda2b commit fa94c6f

File tree

10 files changed

+129
-17
lines changed

10 files changed

+129
-17
lines changed

phpstan.neon.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ parameters:
4141
-
4242
message: '#^Usage of deprecated trait Symfony\\Component\\VarExporter\\LazyProxyTrait in class Doctrine\\Migrations\\Provider\\LazySchema\:\nsince Symfony 7\.3, use native lazy objects instead$#'
4343
path: src/Provider/LazySchema.php
44+
-
45+
message: '#^Call to deprecated method setPrimaryKey\(\) of class Doctrine\\DBAL\\Schema\\Table\:\nUse \{\@see addPrimaryKeyConstraint\(\)\} instead\.$#'
46+
paths:
47+
- src/Metadata/Storage/TableMetadataStorage.php
48+
- tests/Metadata/Storage/ExistingTableMetadataStorageTest.php
49+
- tests/Metadata/Storage/TableMetadataStorageTest.php
50+
-
51+
message: '#^Call to deprecated method getName\(\) of class Doctrine\\DBAL\\Schema\\Schema\.$#'
52+
path: src/Generator/DiffGenerator.php
53+
-
54+
message: '#^Call to deprecated method getQuotedName\(\) of class Doctrine\\DBAL\\Schema\\AbstractAsset\:\nUse \{@see NamedObject\:\:getObjectName\(\)\} or \{@see OptionallyQualifiedName\:\:getObjectName\(\)\} followed\nby \{@see Name\:\:toSQL\(\)\} instead\.$#'
55+
path: src/SchemaDumper.php
56+
-
57+
message: '#^Call to function method_exists\(\) with Doctrine\\DBAL\\Schema\\Table and ''getObjectName'' will always evaluate to true\.$#'
58+
path: src/SchemaDumper.php
4459

4560
symfony:
4661
consoleApplicationLoader: tests/doctrine-migrations-phpstan-app.php

src/Finder/RecursiveRegexFinder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function findMigrations(string $directory, string|null $namespace = null)
4444
/** @return RegexIterator<mixed, mixed, Traversable<mixed, mixed>> */
4545
private function createIterator(string $dir): RegexIterator
4646
{
47+
/** @phpstan-ignore return.type (https://github.com/phpstan/phpstan/issues/13325) */
4748
return new RegexIterator(
4849
new RecursiveIteratorIterator(
4950
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),

src/Generator/DiffGenerator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use Doctrine\DBAL\Platforms\AbstractPlatform;
99
use Doctrine\DBAL\Schema\AbstractAsset;
1010
use Doctrine\DBAL\Schema\AbstractSchemaManager;
11+
use Doctrine\DBAL\Schema\ComparatorConfig;
1112
use Doctrine\DBAL\Schema\Schema;
1213
use Doctrine\Migrations\Generator\Exception\NoChangesDetected;
1314
use Doctrine\Migrations\Provider\SchemaProvider;
1415

16+
use function class_exists;
1517
use function method_exists;
1618
use function preg_match;
1719
use function strpos;
@@ -77,7 +79,11 @@ static function ($assetName) use ($filterExpression) {
7779
}
7880
}
7981

80-
$comparator = $this->schemaManager->createComparator();
82+
if (class_exists(ComparatorConfig::class)) {
83+
$comparator = $this->schemaManager->createComparator((new ComparatorConfig())->withReportModifiedIndexes(false));
84+
} else {
85+
$comparator = $this->schemaManager->createComparator();
86+
}
8187

8288
$upSql = $this->platform->getAlterSchemaSQL($comparator->compareSchemas($fromSchema, $toSchema));
8389

src/Metadata/Storage/TableMetadataStorage.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
1010
use Doctrine\DBAL\Platforms\AbstractPlatform;
1111
use Doctrine\DBAL\Schema\AbstractSchemaManager;
12+
use Doctrine\DBAL\Schema\ComparatorConfig;
13+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
14+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1215
use Doctrine\DBAL\Schema\Table;
1316
use Doctrine\DBAL\Schema\TableDiff;
1417
use Doctrine\DBAL\Types\Types;
@@ -25,6 +28,7 @@
2528
use InvalidArgumentException;
2629

2730
use function array_change_key_case;
31+
use function class_exists;
2832
use function floatval;
2933
use function round;
3034
use function sprintf;
@@ -112,7 +116,7 @@ public function reset(): void
112116
$this->connection->executeStatement(
113117
sprintf(
114118
'DELETE FROM %s WHERE 1 = 1',
115-
$this->platform->quoteIdentifier($this->configuration->getTableName()),
119+
$this->configuration->getTableName(),
116120
),
117121
);
118122
}
@@ -196,8 +200,14 @@ private function needsUpdate(Table $expectedTable): TableDiff|null
196200
return null;
197201
}
198202

203+
if (class_exists(ComparatorConfig::class)) {
204+
$comparator = $this->schemaManager->createComparator((new ComparatorConfig())->withReportModifiedIndexes(false));
205+
} else {
206+
$comparator = $this->schemaManager->createComparator();
207+
}
208+
199209
$currentTable = $this->schemaManager->introspectTable($this->configuration->getTableName());
200-
$diff = $this->schemaManager->createComparator()->compareTables($currentTable, $expectedTable);
210+
$diff = $comparator->compareTables($currentTable, $expectedTable);
201211

202212
return $diff->isEmpty() ? null : $diff;
203213
}
@@ -240,7 +250,15 @@ private function getExpectedTable(): Table
240250
$schemaChangelog->addColumn($this->configuration->getExecutedAtColumnName(), 'datetime', ['notnull' => false]);
241251
$schemaChangelog->addColumn($this->configuration->getExecutionTimeColumnName(), 'integer', ['notnull' => false]);
242252

243-
$schemaChangelog->setPrimaryKey([$this->configuration->getVersionColumnName()]);
253+
if (class_exists(PrimaryKeyConstraint::class)) {
254+
$constraint = PrimaryKeyConstraint::editor()
255+
->setColumnNames(UnqualifiedName::unquoted($this->configuration->getVersionColumnName()))
256+
->create();
257+
258+
$schemaChangelog->addPrimaryKeyConstraint($constraint);
259+
} else {
260+
$schemaChangelog->setPrimaryKey([$this->configuration->getVersionColumnName()]);
261+
}
244262

245263
return $schemaChangelog;
246264
}

src/Metadata/Storage/TableMetadataStorageConfiguration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ final class TableMetadataStorageConfiguration implements MetadataStorageConfigur
88
{
99
private string $tableName = 'doctrine_migration_versions';
1010

11+
/** @var non-empty-string */
1112
private string $versionColumnName = 'version';
1213

1314
private int $versionColumnLength = 191;
@@ -26,11 +27,13 @@ public function setTableName(string $tableName): void
2627
$this->tableName = $tableName;
2728
}
2829

30+
/** @return non-empty-string */
2931
public function getVersionColumnName(): string
3032
{
3133
return $this->versionColumnName;
3234
}
3335

36+
/** @param non-empty-string $versionColumnName */
3437
public function setVersionColumnName(string $versionColumnName): void
3538
{
3639
$this->versionColumnName = $versionColumnName;

src/Provider/DBALSchemaDiffProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
88
use Doctrine\DBAL\Schema\AbstractSchemaManager;
9+
use Doctrine\DBAL\Schema\ComparatorConfig;
910
use Doctrine\DBAL\Schema\Schema;
1011

12+
use function class_exists;
13+
1114
/**
1215
* The SchemaDiffProvider class is responsible for providing a Doctrine\DBAL\Schema\Schema instance that
1316
* represents the current state of your database. A clone of this Schema instance is passed to each of your migrations
@@ -40,8 +43,14 @@ public function createToSchema(Schema $fromSchema): Schema
4043
/** @return string[] */
4144
public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema): array
4245
{
46+
if (class_exists(ComparatorConfig::class)) {
47+
$comparator = $this->schemaManager->createComparator((new ComparatorConfig())->withReportModifiedIndexes(false));
48+
} else {
49+
$comparator = $this->schemaManager->createComparator();
50+
}
51+
4352
return $this->platform->getAlterSchemaSQL(
44-
$this->schemaManager->createComparator()->compareSchemas($fromSchema, $toSchema),
53+
$comparator->compareSchemas($fromSchema, $toSchema),
4554
);
4655
}
4756
}

src/SchemaDumper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function array_merge;
1616
use function count;
1717
use function implode;
18+
use function method_exists;
1819
use function preg_last_error;
1920
use function preg_last_error_msg;
2021
use function preg_match;
@@ -82,8 +83,13 @@ public function dump(
8283
$up[] = $upCode;
8384
}
8485

85-
$downSql = [$this->platform->getDropTableSQL($table->getQuotedName($this->platform))];
86+
if (method_exists($table, 'getObjectName')) {
87+
$tableName = $table->getObjectName()->toSQL($this->platform);
88+
} else {
89+
$tableName = $table->getQuotedName($this->platform);
90+
}
8691

92+
$downSql = [$this->platform->getDropTableSQL($tableName)];
8793
$downCode = $this->migrationSqlGenerator->generate(
8894
$downSql,
8995
$formatted,

tests/Metadata/Storage/ExistingTableMetadataStorageTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Doctrine\DBAL\DriverManager;
1010
use Doctrine\DBAL\Platforms\AbstractPlatform;
1111
use Doctrine\DBAL\Schema\AbstractSchemaManager;
12+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
13+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1214
use Doctrine\DBAL\Schema\Table;
1315
use Doctrine\Migrations\AbstractMigration;
1416
use Doctrine\Migrations\FilesystemMigrationsRepository;
@@ -22,6 +24,7 @@
2224
use Doctrine\Migrations\Version\Version;
2325
use PHPUnit\Framework\TestCase;
2426

27+
use function class_exists;
2528
use function sprintf;
2629

2730
class ExistingTableMetadataStorageTest extends TestCase
@@ -71,7 +74,17 @@ public function setUp(): void
7174
// create partial table
7275
$table = new Table($this->config->getTableName());
7376
$table->addColumn($this->config->getVersionColumnName(), 'string', ['notnull' => true, 'length' => 24]);
74-
$table->setPrimaryKey([$this->config->getVersionColumnName()]);
77+
78+
if (class_exists(PrimaryKeyConstraint::class)) {
79+
$constraint = PrimaryKeyConstraint::editor()
80+
->setColumnNames(UnqualifiedName::unquoted($this->config->getVersionColumnName()))
81+
->create();
82+
83+
$table->addPrimaryKeyConstraint($constraint);
84+
} else {
85+
$table->setPrimaryKey([$this->config->getVersionColumnName()]);
86+
}
87+
7588
$this->schemaManager->createTable($table);
7689
}
7790

tests/Metadata/Storage/TableMetadataStorageTest.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Doctrine\DBAL\Logging\Middleware;
1414
use Doctrine\DBAL\Platforms\AbstractPlatform;
1515
use Doctrine\DBAL\Schema\AbstractSchemaManager;
16+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
17+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1618
use Doctrine\DBAL\Schema\Table;
1719
use Doctrine\DBAL\Types\DateTimeType;
1820
use Doctrine\DBAL\Types\IntegerType;
@@ -32,6 +34,7 @@
3234
use Psr\Log\Test\TestLogger;
3335
use ReflectionClass;
3436

37+
use function class_exists;
3538
use function sprintf;
3639

3740
class TableMetadataStorageTest extends TestCase
@@ -87,7 +90,17 @@ public function testDifferentTableNotUpdatedOnRead(): void
8790

8891
$table = new Table($this->config->getTableName());
8992
$table->addColumn($this->config->getVersionColumnName(), 'string', ['notnull' => true, 'length' => 10]);
90-
$table->setPrimaryKey([$this->config->getVersionColumnName()]);
93+
94+
if (class_exists(PrimaryKeyConstraint::class)) {
95+
$constraint = PrimaryKeyConstraint::editor()
96+
->setColumnNames(UnqualifiedName::unquoted($this->config->getVersionColumnName()))
97+
->create();
98+
99+
$table->addPrimaryKeyConstraint($constraint);
100+
} else {
101+
$table->setPrimaryKey([$this->config->getVersionColumnName()]);
102+
}
103+
91104
$this->schemaManager->createTable($table);
92105

93106
$this->storage->getExecutedMigrations();
@@ -112,7 +125,17 @@ public function testTableStructureUpdate(): void
112125

113126
$table = new Table($config->getTableName());
114127
$table->addColumn($config->getVersionColumnName(), 'string', ['notnull' => true, 'length' => 10]);
115-
$table->setPrimaryKey([$config->getVersionColumnName()]);
128+
129+
if (class_exists(PrimaryKeyConstraint::class)) {
130+
$constraint = PrimaryKeyConstraint::editor()
131+
->setColumnNames(UnqualifiedName::unquoted($config->getVersionColumnName()))
132+
->create();
133+
134+
$table->addPrimaryKeyConstraint($constraint);
135+
} else {
136+
$table->setPrimaryKey([$config->getVersionColumnName()]);
137+
}
138+
116139
$this->schemaManager->createTable($table);
117140

118141
$storage = new TableMetadataStorage($this->connection, new AlphabeticalComparator(), $config);
@@ -140,7 +163,17 @@ public function testTableNotUpToDateTriggersExcepton(): void
140163

141164
$table = new Table($config->getTableName());
142165
$table->addColumn($config->getVersionColumnName(), 'string', ['notnull' => true, 'length' => 10]);
143-
$table->setPrimaryKey([$config->getVersionColumnName()]);
166+
167+
if (class_exists(PrimaryKeyConstraint::class)) {
168+
$constraint = PrimaryKeyConstraint::editor()
169+
->setColumnNames(UnqualifiedName::unquoted($config->getVersionColumnName()))
170+
->create();
171+
172+
$table->addPrimaryKeyConstraint($constraint);
173+
} else {
174+
$table->setPrimaryKey([$config->getVersionColumnName()]);
175+
}
176+
144177
$this->schemaManager->createTable($table);
145178

146179
$storage = new TableMetadataStorage($this->connection, new AlphabeticalComparator(), $config);
@@ -177,7 +210,7 @@ public function testComplete(): void
177210

178211
$sql = sprintf(
179212
'SELECT * FROM %s',
180-
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName()),
213+
$this->config->getTableName(),
181214
);
182215
$rows = $this->connection->fetchAllAssociative($sql);
183216
self::assertEquals([
@@ -201,7 +234,7 @@ public function testCompleteWithFloatTime(): void
201234

202235
$sql = sprintf(
203236
'SELECT * FROM %s',
204-
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName()),
237+
$this->config->getTableName(),
205238
);
206239
$rows = $this->connection->fetchAllAssociative($sql);
207240
self::assertEquals([
@@ -317,7 +350,7 @@ public function testCompleteDownRemovesTheRow(): void
317350

318351
$sql = sprintf(
319352
'SELECT * FROM %s',
320-
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName()),
353+
$this->config->getTableName(),
321354
);
322355
self::assertCount(1, $this->connection->fetchAllAssociative($sql));
323356

@@ -339,7 +372,7 @@ public function testReset(): void
339372

340373
$sql = sprintf(
341374
'SELECT * FROM %s',
342-
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName()),
375+
$this->config->getTableName(),
343376
);
344377
self::assertCount(1, $this->connection->fetchAllAssociative($sql));
345378

@@ -357,7 +390,7 @@ public function testResetWithEmptySchema(): void
357390

358391
$sql = sprintf(
359392
'SELECT * FROM %s',
360-
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName()),
393+
$this->config->getTableName(),
361394
);
362395
self::assertCount(0, $this->connection->fetchAllAssociative($sql));
363396
}
@@ -384,7 +417,7 @@ public function testGetSql(): void
384417

385418
$sql = sprintf(
386419
'SELECT * FROM %s WHERE version = 2230',
387-
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName()),
420+
$this->config->getTableName(),
388421
);
389422

390423
self::assertCount(1, $this->connection->fetchAllAssociative($sql));

tests/Tools/Console/Command/MigrateCommandTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\Migrations\Tests\Tools\Console\Command;
66

77
use Doctrine\DBAL\Connection;
8+
use Doctrine\DBAL\Schema\ComparatorConfig;
89
use Doctrine\DBAL\Types\Types;
910
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
1011
use Doctrine\Migrations\AbstractMigration;
@@ -39,6 +40,7 @@
3940
use Symfony\Component\Console\Helper\QuestionHelper;
4041
use Symfony\Component\Console\Tester\CommandTester;
4142

43+
use function class_exists;
4244
use function getcwd;
4345
use function in_array;
4446
use function sprintf;
@@ -504,7 +506,13 @@ private function alterMetadataTable(): void
504506
$modifiedTable = clone $originalTable;
505507
$modifiedTable->addColumn('extra', Types::STRING, ['notnull' => false]);
506508

507-
$diff = $schemaManager->createComparator()->compareTables($originalTable, $modifiedTable);
509+
if (class_exists(ComparatorConfig::class)) {
510+
$comparator = $schemaManager->createComparator((new ComparatorConfig())->withReportModifiedIndexes(false));
511+
} else {
512+
$comparator = $schemaManager->createComparator();
513+
}
514+
515+
$diff = $comparator->compareTables($originalTable, $modifiedTable);
508516
if ($diff->isEmpty()) {
509517
return;
510518
}

0 commit comments

Comments
 (0)