diff --git a/UPGRADE.md b/UPGRADE.md index dab5b16032b..5396a313e35 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,13 @@ awareness about deprecated code. # Upgrade to 3.4 +# Deprecated `Type::getName()` + +This will method is not useful for the DBAL anymore, and will be removed in 4.0. +As a consequence, depending on the name of a type being `json` for `jsonb` to +be used for the Postgres platform is deprecated in favor of extending +`Doctrine\DBAL\Types\JsonType`. + # Deprecated `AbstractPlatform::getColumnComment()` and `AbstractPlatform::getDoctrineTypeComment()` DBAL no longer needs column comments to ensure proper diffing. Note that both diff --git a/psalm.xml.dist b/psalm.xml.dist index e9c0cf2bf18..a3b83f1c29c 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -229,6 +229,11 @@ See https://github.com/doctrine/dbal/pull/5204 --> + + diff --git a/src/Schema/PostgreSQLSchemaManager.php b/src/Schema/PostgreSQLSchemaManager.php index 03239bde5c2..d722993efa5 100644 --- a/src/Schema/PostgreSQLSchemaManager.php +++ b/src/Schema/PostgreSQLSchemaManager.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; +use Doctrine\DBAL\Types\JsonType; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use Doctrine\Deprecations\Deprecation; @@ -15,6 +16,7 @@ use function array_shift; use function assert; use function explode; +use function get_class; use function implode; use function in_array; use function preg_match; @@ -530,6 +532,20 @@ protected function _getPortableTableColumnDefinition($tableColumn) } if ($column->getType()->getName() === Types::JSON) { + if (! $column->getType() instanceof JsonType) { + Deprecation::trigger( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/5049', + <<<'DEPRECATION' + %s not extending %s while being named %s is deprecated, + and will lead to jsonb never to being used in 4.0., + DEPRECATION, + get_class($column->getType()), + JsonType::class, + Types::JSON + ); + } + $column->setPlatformOption('jsonb', $jsonb); } diff --git a/src/Types/Type.php b/src/Types/Type.php index c2ae2be5ad4..f9fc6d0827e 100644 --- a/src/Types/Type.php +++ b/src/Types/Type.php @@ -102,9 +102,9 @@ abstract public function getSQLDeclaration(array $column, AbstractPlatform $plat /** * Gets the name of this type. * - * @return string + * @deprecated this method will be removed in Doctrine DBAL 4.0. * - * @todo Needed? + * @return string */ abstract public function getName(); diff --git a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php index a7bcf31f139..5d1afd6dbac 100644 --- a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php +++ b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php @@ -16,6 +16,7 @@ use Doctrine\DBAL\Types\DecimalType; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; +use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use function array_map; use function array_merge; @@ -28,6 +29,8 @@ class PostgreSQLSchemaManagerTest extends SchemaManagerFunctionalTestCase { + use VerifyDeprecations; + protected function supportsPlatform(AbstractPlatform $platform): bool { return $platform instanceof PostgreSQLPlatform; @@ -422,6 +425,38 @@ public function testJsonbColumn(): void self::assertTrue(true, $columns['foo']->getPlatformOption('jsonb')); } + public function testItTriggersADeprecationWhenAttemptingToUseJsonbWithATypeNotExtendingJsonType(): void + { + $backedUpType = Type::getType('json'); + try { + Type::getTypeRegistry()->override(Types::JSON, new class extends Type { + /** + * {@inheritdoc} + */ + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return $platform->getJsonTypeDeclarationSQL($column); + } + + public function getName(): string + { + return 'json'; + } + }); + $table = new Table('test_jsonb'); + $table->addColumn('foo', Types::JSON)->setPlatformOption('jsonb', true); + $this->dropAndCreateTable($table); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/5049'); + $columns = $this->schemaManager->listTableColumns('test_jsonb'); + + self::assertSame(Types::JSON, $columns['foo']->getType()->getName()); + self::assertTrue(true, $columns['foo']->getPlatformOption('jsonb')); + } finally { + Type::getTypeRegistry()->override(Types::JSON, $backedUpType); + } + } + public function testListNegativeColumnDefaultValue(): void { $table = new Table('test_default_negative');