From ac94d826dcfabe729bcdde8d98e5a36977c3259a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Tue, 18 Oct 2022 22:25:49 +0200 Subject: [PATCH 1/2] Address deprecation of SchemaDiff::toSql() The new method AbstractPlatform::getAlterSchemaSQL() should be preferred when available. --- lib/Doctrine/ORM/Tools/SchemaTool.php | 6 +++++- .../Doctrine/Tests/ORM/Functional/SchemaTool/DDC214Test.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 3a40e9d2013..08a998a2025 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -959,7 +959,11 @@ public function getUpdateSchemaSql(array $classes, $saveMode = false) return $schemaDiff->toSaveSql($this->platform); } - return $schemaDiff->toSql($this->platform); + if (! method_exists(AbstractPlatform::class, 'getAlterSchemaSQL')) { + return $schemaDiff->toSql($this->platform); + } + + return $this->platform->getAlterSchemaSQL($schemaDiff); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/DDC214Test.php b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/DDC214Test.php index 42f14669ae6..2e674d38d63 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/DDC214Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/DDC214Test.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM\Functional\SchemaTool; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Comparator; @@ -82,7 +83,10 @@ public function assertCreatedSchemaNeedsNoUpdates(string ...$classes): void $schemaDiff = $comparator->compareSchemas($fromSchema, $toSchema); - $sql = $schemaDiff->toSql($this->_em->getConnection()->getDatabasePlatform()); + $sql = method_exists(AbstractPlatform::class, 'getAlterSchemaSQL') ? + $this->_em->getConnection()->getDatabasePlatform()->getAlterSchemaSQL($schemaDiff) : + $schemaDiff->toSql($this->_em->getConnection()->getDatabasePlatform()); + $sql = array_filter($sql, static function ($sql) { return ! str_contains($sql, 'DROP'); }); From 7cb96fcf0e3aa151cba463cff4ea2e99a5ba6517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Tue, 18 Oct 2022 22:54:56 +0200 Subject: [PATCH 2/2] Address deprecation of SchemaDiff::toSaveSql() This implies deprecating a feature relying on that method. --- UPGRADE.md | 9 +++++++++ composer.json | 2 +- .../Command/SchemaTool/UpdateCommand.php | 10 +++++++--- lib/Doctrine/ORM/Tools/SchemaTool.php | 20 +++++++++++++++++++ phpstan-dbal2.neon | 3 +++ .../Command/SchemaTool/UpdateCommandTest.php | 5 ++++- 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 052f761cabb..78fa6cff804 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,14 @@ # Upgrade to 2.14 +## Deprecated incomplete schema updates + +Using `orm:schema-tool:update` without passing the `--complete` flag is +deprecated. Use schema asset filtering if you need to preserve assets not +managed by DBAL. + +Likewise, calling `SchemaTool::updateSchema()` or +`SchemaTool::getUpdateSchemaSql()` with a second argument is deprecated. + ## Deprecated annotation mapping driver. Please switch to one of the other mapping drivers. Native attributes which PHP diff --git a/composer.json b/composer.json index 8a224aefc69..746d8c73bc3 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "doctrine/lexer": "^1.2.3", "doctrine/persistence": "^2.4 || ^3", "psr/cache": "^1 || ^2 || ^3", - "symfony/console": "^3.3 || ^4.0 || ^5.0 || ^6.0", + "symfony/console": "^4.2 || ^5.0 || ^6.0", "symfony/polyfill-php72": "^1.23", "symfony/polyfill-php80": "^1.16" }, diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php index 42f5709c20b..743041ecac0 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php @@ -57,7 +57,7 @@ protected function configure() task will drop all database assets (e.g. tables, etc) that are *not* described by the current metadata. In other words, without this option, this task leaves untouched any "extra" tables that exist in the database, but which aren't -described by any metadata. +described by any metadata. Not passing that option is deprecated. Hint: If you have a database with tables that should not be managed by the ORM, you can use a DBAL functionality to filter the tables and sequences down @@ -73,12 +73,16 @@ protected function configure() */ protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui) { + $notificationUi = $ui->getErrorStyle(); + // Defining if update is complete or not (--complete not defined means $saveMode = true) $saveMode = ! $input->getOption('complete'); - $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); + if ($saveMode) { + $notificationUi->warning('Not passing the "--complete" option to "orm:schema-tool:update" is deprecated and will not be supported when using doctrine/dbal 4'); + } - $notificationUi = $ui->getErrorStyle(); + $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); if (empty($sqls)) { $notificationUi->success('Nothing to update - your database is already in sync with the current entity metadata.'); diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 08a998a2025..2597ad656ab 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -14,6 +14,7 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets; +use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\MappingException; @@ -32,6 +33,7 @@ use function assert; use function count; use function current; +use function func_num_args; use function implode; use function in_array; use function is_array; @@ -924,6 +926,15 @@ public function getDropSchemaSQL(array $classes) */ public function updateSchema(array $classes, $saveMode = false) { + if (func_num_args() > 1) { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/orm', + 'https://github.com/doctrine/orm/pull/10153', + 'Passing $saveMode to %s() is deprecated and will not be possible in Doctrine ORM 3.0.', + __METHOD__ + ); + } + $updateSchemaSql = $this->getUpdateSchemaSql($classes, $saveMode); $conn = $this->em->getConnection(); @@ -944,6 +955,15 @@ public function updateSchema(array $classes, $saveMode = false) */ public function getUpdateSchemaSql(array $classes, $saveMode = false) { + if (func_num_args() > 1) { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/orm', + 'https://github.com/doctrine/orm/pull/10153', + 'Passing $saveMode to %s() is deprecated and will not be possible in Doctrine ORM 3.0.', + __METHOD__ + ); + } + $toSchema = $this->getSchemaFromMetadata($classes); $fromSchema = $this->createSchemaForComparison($toSchema); diff --git a/phpstan-dbal2.neon b/phpstan-dbal2.neon index 852987a3bd4..72285bfb227 100644 --- a/phpstan-dbal2.neon +++ b/phpstan-dbal2.neon @@ -11,6 +11,9 @@ parameters: # Class name will change in DBAL 3. - '/^Class Doctrine\\DBAL\\Platforms\\PostgreSQLPlatform not found\.$/' + # Forward compatibility for DBAL 3.5 + - '/^Call to an undefined method Doctrine\\DBAL\\Platforms\\AbstractPlatform::getAlterSchemaSQL\(\).$/' + # Forward compatibility for DBAL 3.4 - '/^Call to an undefined method Doctrine\\DBAL\\Cache\\QueryCacheProfile::[gs]etResultCache\(\)\.$/' - diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/SchemaTool/UpdateCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/SchemaTool/UpdateCommandTest.php index df94ca4256e..a2df3811075 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/SchemaTool/UpdateCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/SchemaTool/UpdateCommandTest.php @@ -12,7 +12,10 @@ class UpdateCommandTest extends AbstractCommandTest public function testItPrintsTheSql(): void { $tester = $this->getCommandTester(UpdateCommand::class); - $tester->execute(['--dump-sql' => true]); + $tester->execute( + ['--dump-sql' => true], + ['capture_stderr_separately' => true] + ); self::$sharedConn->executeStatement($tester->getDisplay()); }