Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate Type::getName() #5049

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@
See https://github.com/doctrine/dbal/pull/5204
-->
<referencedMethod name="Doctrine\DBAL\Platforms\AbstractPlatform::getColumnComment"/>
<!--
TODO: remove in 4.0.0
See https://github.com/doctrine/dbal/pull/5049
-->
<referencedMethod name="Doctrine\DBAL\Types\Type::getName"/>
</errorLevel>
</DeprecatedMethod>
<DeprecatedProperty>
Expand Down
16 changes: 16 additions & 0 deletions src/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
35 changes: 35 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,8 @@

class PostgreSQLSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
use VerifyDeprecations;

protected function supportsPlatform(AbstractPlatform $platform): bool
{
return $platform instanceof PostgreSQLPlatform;
Expand Down Expand Up @@ -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');
Expand Down