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

Add runtime deprecations for default string column length #5377

Merged
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
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ awareness about deprecated code.

# Upgrade to 3.4

## Added runtime deprecations for the default string column length.

In addition to the formal deprecation introduced in DBAL 3.2, the library will now emit a deprecation message at runtime
if the string or binary column length is omitted, but it's required by the target database platform.

## Deprecated `AbstractPlatform::getVarcharTypeDeclarationSQL()`

The `AbstractPlatform::getVarcharTypeDeclarationSQL()` method has been deprecated.
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,12 @@ parameters:
paths:
- src/Query/QueryBuilder.php

# https://github.com/doctrine/dbal/issues/3263
# TODO: remove in 4.0.0
-
message: '~^Method Doctrine\\DBAL\\Platforms\\AbstractPlatform::get(Binary|Varchar)TypeDeclarationSQLSnippet\(\) invoked with 3 parameters, 2 required\.$~'
paths:
- src/Platforms/AbstractPlatform.php

includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
5 changes: 5 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@
</ReservedWord>
<TooManyArguments>
<errorLevel type="suppress">
<!--
This suppression should be removed in 4.0.x
See https://github.com/doctrine/dbal/issues/3263
-->
<file name="src/Platforms/AbstractPlatform.php"/>
<!-- See https://github.com/doctrine/dbal/pull/3562 -->
<file name="src/Schema/AbstractSchemaManager.php"/>
<file name="src/Schema/SqliteSchemaManager.php"/>
Expand Down
24 changes: 22 additions & 2 deletions src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
use function array_unique;
use function array_values;
use function count;
use function func_get_arg;
use function func_get_args;
use function func_num_args;
use function implode;
use function in_array;
use function is_numeric;
Expand Down Expand Up @@ -203,17 +205,35 @@ public function getListTableForeignKeysSQL($table, $database = null)
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
if ($length <= 0 || (func_num_args() > 2 && func_get_arg(2))) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3263',
'Relying on the default string column length on MySQL is deprecated'
. ', specify the length explicitly.'
);
}

return $fixed ? ($length > 0 ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length > 0 ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
}

/**
* {@inheritdoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
if ($length <= 0 || (func_num_args() > 2 && func_get_arg(2))) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3263',
'Relying on the default binary column length on MySQL is deprecated'
. ', specify the length explicitly.'
);
}

return $fixed
? 'BINARY(' . ($length > 0 ? $length : 255) . ')'
: 'VARBINARY(' . ($length > 0 ? $length : 255) . ')';
Expand Down
26 changes: 13 additions & 13 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ public function getAsciiStringTypeDeclarationSQL(array $column): string
*/
public function getVarcharTypeDeclarationSQL(array $column)
{
if (! isset($column['length'])) {
if (isset($column['length'])) {
$lengthOmitted = false;
} else {
$column['length'] = $this->getVarcharDefaultLength();
$lengthOmitted = true;
}

$fixed = $column['fixed'] ?? false;
Expand All @@ -219,7 +222,7 @@ public function getVarcharTypeDeclarationSQL(array $column)
return $this->getClobTypeDeclarationSQL($column);
}

return $this->getVarcharTypeDeclarationSQLSnippet($column['length'], $fixed);
return $this->getVarcharTypeDeclarationSQLSnippet($column['length'], $fixed, $lengthOmitted);
}

/**
Expand All @@ -243,8 +246,11 @@ public function getStringTypeDeclarationSQL(array $column)
*/
public function getBinaryTypeDeclarationSQL(array $column)
{
if (! isset($column['length'])) {
if (isset($column['length'])) {
$lengthOmitted = false;
} else {
$column['length'] = $this->getBinaryDefaultLength();
$lengthOmitted = true;
}

$fixed = $column['fixed'] ?? false;
Expand All @@ -266,7 +272,7 @@ public function getBinaryTypeDeclarationSQL(array $column)
return $this->getBlobTypeDeclarationSQL($column);
}

return $this->getBinaryTypeDeclarationSQLSnippet($column['length'], $fixed);
return $this->getBinaryTypeDeclarationSQLSnippet($column['length'], $fixed, $lengthOmitted);
}

/**
Expand Down Expand Up @@ -310,7 +316,7 @@ public function getJsonTypeDeclarationSQL(array $column)
*
* @throws Exception If not supported on this platform.
*/
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
throw Exception::notSupported('VARCHARs not supported by Platform.');
}
Expand All @@ -325,7 +331,7 @@ protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
*
* @throws Exception If not supported on this platform.
*/
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
throw Exception::notSupported('BINARY/VARBINARY column types are not supported by this platform.');
}
Expand Down Expand Up @@ -4027,17 +4033,11 @@ private function columnToArray(Column $column): array
{
$name = $column->getQuotedName($this);

$columnData = array_merge($column->toArray(), [
return array_merge($column->toArray(), [
'name' => $name,
'version' => $column->hasPlatformOption('version') ? $column->getPlatformOption('version') : false,
'comment' => $this->getColumnComment($column),
]);

if ($columnData['type'] instanceof Types\StringType && $columnData['length'] === null) {
$columnData['length'] = $this->getVarcharDefaultLength();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already implemented in getVarcharTypeDeclarationSQL():

if (! isset($column['length'])) {
$column['length'] = $this->getVarcharDefaultLength();
}

If this block is left as is, then it will be impossible to detect whether the column length was specified explicitly or the default value is used.

}

return $columnData;
}

/**
Expand Down
24 changes: 21 additions & 3 deletions src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,35 @@ public function isCommentedDoctrineType(Type $doctrineType)
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
if ($length <= 0 || (func_num_args() > 2 && func_get_arg(2))) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3263',
'Relying on the default string column length on IBM DB2 is deprecated'
. ', specify the length explicitly.'
);
}

return $fixed ? ($length > 0 ? 'CHAR(' . $length . ')' : 'CHAR(254)')
: ($length > 0 ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
}

/**
* {@inheritdoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
if ($length <= 0 || (func_num_args() > 2 && func_get_arg(2))) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3263',
'Relying on the default binary column length on IBM DB2 is deprecated'
. ', specify the length explicitly.'
);
}

return $this->getVarcharTypeDeclarationSQLSnippet($length, $fixed) . ' FOR BIT DATA';
}

Expand Down
24 changes: 21 additions & 3 deletions src/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,35 @@ protected function _getCommonIntegerTypeDeclarationSQL(array $column)
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
if ($length <= 0 || (func_num_args() > 2 && func_get_arg(2))) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3263',
'Relying on the default string column length on Oracle is deprecated'
. ', specify the length explicitly.'
);
}

return $fixed ? ($length > 0 ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
: ($length > 0 ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
}

/**
* {@inheritdoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
if ($length <= 0 || (func_num_args() > 2 && func_get_arg(2))) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3263',
'Relying on the default binary column length on Oracle is deprecated'
. ', specify the length explicitly.'
);
}

return 'RAW(' . ($length > 0 ? $length : $this->getBinaryMaxLength()) . ')';
}

Expand Down
24 changes: 22 additions & 2 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use function crc32;
use function dechex;
use function explode;
use function func_get_arg;
use function func_get_args;
use function func_num_args;
use function implode;
use function is_array;
use function is_bool;
Expand Down Expand Up @@ -1203,8 +1205,17 @@ public function getAsciiStringTypeDeclarationSQL(array $column): string
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
if ($length <= 0 || (func_num_args() > 2 && func_get_arg(2))) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3263',
'Relying on the default string column length on SQL Server is deprecated'
. ', specify the length explicitly.'
);
}

return $fixed
? ($length > 0 ? 'NCHAR(' . $length . ')' : 'CHAR(255)')
: ($length > 0 ? 'NVARCHAR(' . $length . ')' : 'NVARCHAR(255)');
Expand All @@ -1213,8 +1224,17 @@ protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
/**
* {@inheritdoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed/*, $lengthOmitted = false*/)
{
if ($length <= 0 || (func_num_args() > 2 && func_get_arg(2))) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3263',
'Relying on the default binary column length on SQL Server is deprecated'
. ', specify the length explicitly.'
);
}

return $fixed
? 'BINARY(' . ($length > 0 ? $length : 255) . ')'
: 'VARBINARY(' . ($length > 0 ? $length : 255) . ')';
Expand Down