Skip to content

Commit

Permalink
Merge pull request doctrine#10153 from greg0ire/address-dbal-deprecat…
Browse files Browse the repository at this point in the history
…ions

Address dbal deprecations
  • Loading branch information
greg0ire committed Oct 24, 2022
2 parents f3f4532 + 7cb96fc commit cf91ce6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
9 changes: 9 additions & 0 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -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"
},
Expand Down
Expand Up @@ -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.
<comment>Hint:</comment> 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
Expand All @@ -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.');
Expand Down
26 changes: 25 additions & 1 deletion lib/Doctrine/ORM/Tools/SchemaTool.php
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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);

Expand All @@ -959,7 +979,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);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions phpstan-dbal2.neon
Expand Up @@ -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\(\)\.$/'
-
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
});
Expand Down
Expand Up @@ -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());
}
Expand Down

0 comments on commit cf91ce6

Please sign in to comment.