Skip to content

Commit

Permalink
Merge branch '3.5.x' into 4.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Sep 1, 2022
2 parents c33e15a + 0a272ef commit 171759e
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 28 deletions.
21 changes: 21 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,27 @@ The following methods have been removed.

# Upgrade to 3.5

## Deprecated relying on the default precision and scale of decimal columns.

Relying on the default precision and scale of decimal columns provided by the DBAL is deprecated.
When declaring decimal columns, specify the precision and scale explicitly.

## Marked `ColumnDiff::hasChanged()` as internal.

The `ColumnDiff::hasChanged()` method has been marked as internal. Use one of the following `ColumnDiff` methods
in order to check if a given column property has changed:

- `hasTypeChanged()`,
- `hasLengthChanged()`,
- `hasPrecisionChanged()`,
- `hasScaleChanged()`,
- `hasUnsignedChanged()`,
- `hasFixedChanged()`,
- `hasNotNullChanged()`,
- `hasDefaultChanged()`,
- `hasAutoIncrementChanged()`,
- `hasCommentChanged()`.

## Deprecated `ColumnDiff` APIs dedicated to the old column name.

The `$oldColumnName` property and the `getOldColumnName()` method of the `ColumnDiff` class have been deprecated.
Expand Down
36 changes: 31 additions & 5 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Doctrine\DBAL\Types;
use Doctrine\DBAL\Types\Exception\TypeNotFound;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use InvalidArgumentException;
use UnexpectedValueException;

Expand Down Expand Up @@ -1577,12 +1578,37 @@ public function getColumnDeclarationSQL(string $name, array $column): string
*/
public function getDecimalTypeDeclarationSQL(array $column): string
{
$column['precision'] = ! isset($column['precision']) || empty($column['precision'])
? 10 : $column['precision'];
$column['scale'] = ! isset($column['scale']) || empty($column['scale'])
? 0 : $column['scale'];
if (empty($column['precision'])) {
if (! isset($column['precision'])) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5637',
'Relying on the default decimal column precision is deprecated'
. ', specify the precision explicitly.',
);
}

$precision = 10;
} else {
$precision = $column['precision'];
}

if (empty($column['scale'])) {
if (! isset($column['scale'])) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5637',
'Relying on the default decimal column scale is deprecated'
. ', specify the scale explicitly.',
);
}

$scale = 0;
} else {
$scale = $column['scale'];
}

return 'NUMERIC(' . $column['precision'] . ', ' . $column['scale'] . ')';
return 'NUMERIC(' . $precision . ', ' . $scale . ')';
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function getAlterTableSQL(TableDiff $diff): array
continue;
}

if ($columnDiff->hasChanged('comment')) {
if ($columnDiff->hasCommentChanged()) {
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->getName($this)->getQuotedName($this),
$columnDiff->column->getQuotedName($this),
Expand Down Expand Up @@ -420,20 +420,20 @@ private function getAlterColumnClausesSQL(ColumnDiff $columnDiff): array
$clauses = [];

if (
$columnDiff->hasChanged('type') ||
$columnDiff->hasChanged('length') ||
$columnDiff->hasChanged('precision') ||
$columnDiff->hasChanged('scale') ||
$columnDiff->hasChanged('fixed')
$columnDiff->hasTypeChanged() ||
$columnDiff->hasLengthChanged() ||
$columnDiff->hasPrecisionChanged() ||
$columnDiff->hasScaleChanged() ||
$columnDiff->hasFixedChanged()
) {
$clauses[] = $alterClause . ' SET DATA TYPE ' . $column['type']->getSQLDeclaration($column, $this);
}

if ($columnDiff->hasChanged('notnull')) {
if ($columnDiff->hasNotNullChanged()) {
$clauses[] = $column['notnull'] ? $alterClause . ' SET NOT NULL' : $alterClause . ' DROP NOT NULL';
}

if ($columnDiff->hasChanged('default')) {
if ($columnDiff->hasDefaultChanged()) {
if (isset($column['default'])) {
$defaultClause = $this->getDefaultValueDeclarationSQL($column);

Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,15 +565,15 @@ public function getAlterTableSQL(TableDiff $diff): array
$currentSQL = $this->getColumnDeclarationSQL('', $columnInfo);

if ($currentSQL !== $fromSQL) {
if (! $columnDiff->hasChanged('notnull')) {
if (! $columnDiff->hasNotNullChanged()) {
unset($columnInfo['notnull']);
$currentSQL = $this->getColumnDeclarationSQL('', $columnInfo);
}

$modifyColumnSQL[] = $column->getQuotedName($this) . $currentSQL;
}

if (! $columnDiff->hasChanged('comment')) {
if (! $columnDiff->hasCommentChanged()) {
continue;
}

Expand Down
18 changes: 9 additions & 9 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ public function getAlterTableSQL(TableDiff $diff): array
$column = $columnDiff->column;

if (
$columnDiff->hasChanged('type')
|| $columnDiff->hasChanged('precision')
|| $columnDiff->hasChanged('scale')
|| $columnDiff->hasChanged('fixed')
$columnDiff->hasTypeChanged()
|| $columnDiff->hasPrecisionChanged()
|| $columnDiff->hasScaleChanged()
|| $columnDiff->hasFixedChanged()
) {
$type = $column->getType();

Expand All @@ -283,20 +283,20 @@ public function getAlterTableSQL(TableDiff $diff): array
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
}

if ($columnDiff->hasChanged('default')) {
if ($columnDiff->hasDefaultChanged()) {
$defaultClause = $column->getDefault() === null
? ' DROP DEFAULT'
: ' SET' . $this->getDefaultValueDeclarationSQL($column->toArray());
$query = 'ALTER ' . $oldColumnName . $defaultClause;
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
}

if ($columnDiff->hasChanged('notnull')) {
if ($columnDiff->hasNotNullChanged()) {
$query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotnull() ? 'SET' : 'DROP') . ' NOT NULL';
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
}

if ($columnDiff->hasChanged('autoincrement')) {
if ($columnDiff->hasAutoIncrementChanged()) {
if ($column->getAutoincrement()) {
$query = 'ADD GENERATED BY DEFAULT AS IDENTITY';
} else {
Expand All @@ -310,15 +310,15 @@ public function getAlterTableSQL(TableDiff $diff): array
$newComment = $column->getComment();
$oldComment = $columnDiff->fromColumn->getComment();

if ($columnDiff->hasChanged('comment') || $oldComment !== $newComment) {
if ($columnDiff->hasCommentChanged() || $oldComment !== $newComment) {
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
$newComment,
);
}

if (! $columnDiff->hasChanged('length')) {
if (! $columnDiff->hasLengthChanged()) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public function getAlterTableSQL(TableDiff $diff): array
$newDeclarationSQL = $this->getColumnDeclarationSQL($columnNameSQL, $column->toArray());

$declarationSQLChanged = $newDeclarationSQL !== $oldDeclarationSQL;
$defaultChanged = $columnDiff->hasChanged('default');
$defaultChanged = $columnDiff->hasDefaultChanged();

if (! $declarationSQLChanged && ! $defaultChanged) {
continue;
Expand Down Expand Up @@ -559,13 +559,13 @@ private function alterColumnRequiresDropDefaultConstraint(ColumnDiff $columnDiff

// We need to drop an existing default constraint if the column was
// defined with a default value before and it has changed.
if ($columnDiff->hasChanged('default')) {
if ($columnDiff->hasDefaultChanged()) {
return true;
}

// We need to drop an existing default constraint if the column was
// defined with a default value before and the native column type has changed.
return $columnDiff->hasChanged('type') || $columnDiff->hasChanged('fixed');
return $columnDiff->hasTypeChanged() || $columnDiff->hasFixedChanged();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/SQLitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ private function getSimpleAlterTableSQL(TableDiff $diff): array|false
continue;
}

if (! $columnDiff->hasChanged('type') && $columnDiff->hasChanged('unsigned')) {
if (! $columnDiff->hasTypeChanged() && $columnDiff->hasUnsignedChanged()) {
unset($diff->changedColumns[$oldColumnName]);

continue;
Expand Down
53 changes: 53 additions & 0 deletions src/Schema/ColumnDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,59 @@ public function __construct(
) {
}

public function hasTypeChanged(): bool
{
return $this->hasChanged('type');
}

public function hasLengthChanged(): bool
{
return $this->hasChanged('length');
}

public function hasPrecisionChanged(): bool
{
return $this->hasChanged('precision');
}

public function hasScaleChanged(): bool
{
return $this->hasChanged('scale');
}

public function hasUnsignedChanged(): bool
{
return $this->hasChanged('unsigned');
}

public function hasFixedChanged(): bool
{
return $this->hasChanged('fixed');
}

public function hasNotNullChanged(): bool
{
return $this->hasChanged('notnull');
}

public function hasDefaultChanged(): bool
{
return $this->hasChanged('default');
}

public function hasAutoIncrementChanged(): bool
{
return $this->hasChanged('autoincrement');
}

public function hasCommentChanged(): bool
{
return $this->hasChanged('comment');
}

/**
* @internal
*/
public function hasChanged(string $propertyName): bool
{
return in_array($propertyName, $this->changedProperties, true);
Expand Down

0 comments on commit 171759e

Please sign in to comment.