Skip to content

Commit 217ce8d

Browse files
committed
Deprecate renaming tables via TableDiff
1 parent 7727e46 commit 217ce8d

11 files changed

+135
-22
lines changed

UPGRADE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ awareness about deprecated code.
1313
Relying on the default precision and scale of decimal columns provided by the DBAL is deprecated.
1414
When declaring decimal columns, specify the precision and scale explicitly.
1515

16+
## Deprecated renaming tables via `TableDiff` and `AbstractPlatform::alterTable()`.
17+
18+
Renaming tables via setting the `$newName` property on a `TableDiff` and passing it to `AbstractPlatform::alterTable()`
19+
is deprecated. The implementations of `AbstractSchemaManager::alterTable()` should use `AbstractPlatform::renameTable()`
20+
instead.
21+
22+
The `TableDiff::$newName` property and the `TableDiff::getNewName()` method have been deprecated.
23+
1624
## Marked `Comparator` methods as internal.
1725

1826
The following `Comparator` methods have been marked as internal:

psalm.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@
423423
-->
424424
<referencedMethod name="Doctrine\DBAL\Schema\ColumnDiff::hasChanged"/>
425425
<referencedMethod name="Doctrine\DBAL\Schema\Comparator::diffColumn"/>
426+
<!--
427+
TODO: remove in 4.0.0
428+
-->
429+
<referencedMethod name="Doctrine\DBAL\Schema\TableDiff::getNewName"/>
426430
</errorLevel>
427431
</DeprecatedMethod>
428432
<DeprecatedProperty>
@@ -464,6 +468,10 @@
464468
TODO: remove in 4.0.0
465469
-->
466470
<referencedProperty name="Doctrine\DBAL\Schema\ColumnDiff::$changedProperties"/>
471+
<!--
472+
TODO: remove in 4.0.0
473+
-->
474+
<referencedProperty name="Doctrine\DBAL\Schema\TableDiff::$newName"/>
467475
</errorLevel>
468476
</DeprecatedProperty>
469477
<DocblockTypeContradiction>

src/Platforms/AbstractMySQLPlatform.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,13 @@ public function getAlterTableSQL(TableDiff $diff)
610610
$newName = $diff->getNewName();
611611

612612
if ($newName !== false) {
613+
Deprecation::trigger(
614+
'doctrine/dbal',
615+
'https://github.com/doctrine/dbal/pull/5663',
616+
'Generation of SQL that renames a table using %s is deprecated. Use getRenameTableSQL() instead.',
617+
__METHOD__,
618+
);
619+
613620
$queryParts[] = 'RENAME TO ' . $newName->getQuotedName($this);
614621
}
615622

src/Platforms/AbstractPlatform.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,14 @@ public function getAlterTableSQL(TableDiff $diff)
25922592
throw Exception::notSupported(__METHOD__);
25932593
}
25942594

2595+
/** @return list<string> */
2596+
public function getRenameTableSQL(string $oldName, string $newName): array
2597+
{
2598+
return [
2599+
sprintf('ALTER TABLE %s RENAME TO %s', $oldName, $newName),
2600+
];
2601+
}
2602+
25952603
/**
25962604
* @param mixed[] $columnSql
25972605
*

src/Platforms/DB2Platform.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,13 @@ public function getAlterTableSQL(TableDiff $diff)
643643
$newName = $diff->getNewName();
644644

645645
if ($newName !== false) {
646+
Deprecation::trigger(
647+
'doctrine/dbal',
648+
'https://github.com/doctrine/dbal/pull/5663',
649+
'Generation of "rename table" SQL using %s is deprecated. Use getRenameTableSQL() instead.',
650+
__METHOD__,
651+
);
652+
646653
$sql[] = sprintf(
647654
'RENAME TABLE %s TO %s',
648655
$diff->getName($this)->getQuotedName($this),
@@ -660,6 +667,16 @@ public function getAlterTableSQL(TableDiff $diff)
660667
return array_merge($sql, $tableSql, $columnSql);
661668
}
662669

670+
/**
671+
* {@inheritDoc}
672+
*/
673+
public function getRenameTableSQL(string $oldName, string $newName): array
674+
{
675+
return [
676+
sprintf('RENAME TABLE %s TO %s', $oldName, $newName),
677+
];
678+
}
679+
663680
/**
664681
* Gathers the table alteration SQL for a given column diff.
665682
*

src/Platforms/OraclePlatform.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,13 @@ public function getAlterTableSQL(TableDiff $diff)
977977
$newName = $diff->getNewName();
978978

979979
if ($newName !== false) {
980+
Deprecation::trigger(
981+
'doctrine/dbal',
982+
'https://github.com/doctrine/dbal/pull/5663',
983+
'Generation of "rename table" SQL using %s is deprecated. Use getRenameTableSQL() instead.',
984+
__METHOD__,
985+
);
986+
980987
$sql[] = sprintf(
981988
'ALTER TABLE %s RENAME TO %s',
982989
$diff->getName($this)->getQuotedName($this),

src/Platforms/PostgreSQLPlatform.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ public function getAlterTableSQL(TableDiff $diff)
656656
$newName = $diff->getNewName();
657657

658658
if ($newName !== false) {
659+
Deprecation::trigger(
660+
'doctrine/dbal',
661+
'https://github.com/doctrine/dbal/pull/5663',
662+
'Generation of "rename table" SQL using %s is deprecated. Use getRenameTableSQL() instead.',
663+
__METHOD__,
664+
);
665+
659666
$sql[] = sprintf(
660667
'ALTER TABLE %s RENAME TO %s',
661668
$diff->getName($this)->getQuotedName($this),

src/Platforms/SQLServerPlatform.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -661,24 +661,14 @@ public function getAlterTableSQL(TableDiff $diff)
661661
$newName = $diff->getNewName();
662662

663663
if ($newName !== false) {
664-
$sql[] = "sp_rename '" . $diff->getName($this)->getQuotedName($this) . "', '" . $newName->getName() . "'";
664+
Deprecation::trigger(
665+
'doctrine/dbal',
666+
'https://github.com/doctrine/dbal/pull/5663',
667+
'Generation of "rename table" SQL using %s is deprecated. Use getRenameTableSQL() instead.',
668+
__METHOD__,
669+
);
665670

666-
/**
667-
* Rename table's default constraints names
668-
* to match the new table name.
669-
* This is necessary to ensure that the default
670-
* constraints can be referenced in future table
671-
* alterations as the table name is encoded in
672-
* default constraints' names.
673-
*/
674-
$sql[] = "DECLARE @sql NVARCHAR(MAX) = N''; " .
675-
"SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N''' " .
676-
"+ REPLACE(dc.name, '" . $this->generateIdentifierName($diff->name) . "', " .
677-
"'" . $this->generateIdentifierName($newName->getName()) . "') + ''', ''OBJECT'';' " .
678-
'FROM sys.default_constraints dc ' .
679-
'JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id ' .
680-
"WHERE tbl.name = '" . $newName->getName() . "';" .
681-
'EXEC sp_executesql @sql';
671+
$sql = array_merge($sql, $this->getRenameTableSQL($diff->name, $newName->getName()));
682672
}
683673

684674
$sql = array_merge(
@@ -690,6 +680,38 @@ public function getAlterTableSQL(TableDiff $diff)
690680
return array_merge($sql, $tableSql, $columnSql);
691681
}
692682

683+
/**
684+
* {@inheritDoc}
685+
*/
686+
public function getRenameTableSQL(string $oldName, string $newName): array
687+
{
688+
return [
689+
sprintf('sp_rename %s, %s', $this->quoteStringLiteral($oldName), $this->quoteStringLiteral($newName)),
690+
691+
/* Rename table's default constraints names
692+
* to match the new table name.
693+
* This is necessary to ensure that the default
694+
* constraints can be referenced in future table
695+
* alterations as the table name is encoded in
696+
* default constraints' names. */
697+
sprintf(
698+
<<<'SQL'
699+
DECLARE @sql NVARCHAR(MAX) = N'';
700+
SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N'''
701+
+ REPLACE(dc.name, '%s', '%s') + ''', ''OBJECT'';'
702+
FROM sys.default_constraints dc
703+
JOIN sys.tables tbl
704+
ON dc.parent_object_id = tbl.object_id
705+
WHERE tbl.name = %s;
706+
EXEC sp_executesql @sql
707+
SQL,
708+
$this->generateIdentifierName($oldName),
709+
$this->generateIdentifierName($newName),
710+
$this->quoteStringLiteral($newName),
711+
),
712+
];
713+
}
714+
693715
/**
694716
* Returns the SQL clause for adding a default constraint in an ALTER TABLE statement.
695717
*

src/Platforms/SqlitePlatform.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,13 @@ public function getAlterTableSQL(TableDiff $diff)
11041104
$newName = $diff->getNewName();
11051105

11061106
if ($newName !== false) {
1107+
Deprecation::trigger(
1108+
'doctrine/dbal',
1109+
'https://github.com/doctrine/dbal/pull/5663',
1110+
'Generation of "rename table" SQL using %s is deprecated. Use getRenameTableSQL() instead.',
1111+
__METHOD__,
1112+
);
1113+
11071114
$sql[] = sprintf(
11081115
'ALTER TABLE %s RENAME TO %s',
11091116
$newTable->getQuotedName($this),
@@ -1234,6 +1241,14 @@ private function getSimpleAlterTableSQL(TableDiff $diff)
12341241

12351242
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
12361243
if ($diff->newName !== false) {
1244+
Deprecation::trigger(
1245+
'doctrine/dbal',
1246+
'https://github.com/doctrine/dbal/pull/5663',
1247+
'Generation of SQL that renames a table using %s is deprecated.'
1248+
. ' Use getRenameTableSQL() instead.',
1249+
__METHOD__,
1250+
);
1251+
12371252
$newTable = new Identifier($diff->newName);
12381253

12391254
$sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' RENAME TO '

src/Schema/AbstractSchemaManager.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,7 @@ public function alterTable(TableDiff $tableDiff)
12551255
*/
12561256
public function renameTable($name, $newName)
12571257
{
1258-
$tableDiff = new TableDiff($name);
1259-
$tableDiff->newName = $newName;
1260-
$this->alterTable($tableDiff);
1258+
$this->_execSql($this->_platform->getRenameTableSQL($name, $newName));
12611259
}
12621260

12631261
/**

src/Schema/TableDiff.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Doctrine\DBAL\Schema;
44

55
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\Deprecations\Deprecation;
67

78
/**
89
* Table Diff.
@@ -12,7 +13,11 @@ class TableDiff
1213
/** @var string */
1314
public $name;
1415

15-
/** @var string|false */
16+
/**
17+
* @deprecated Rename tables via {@link AbstractSchemaManager::renameTable()} instead.
18+
*
19+
* @var string|false
20+
*/
1621
public $newName = false;
1722

1823
/**
@@ -140,9 +145,20 @@ public function getName(AbstractPlatform $platform)
140145
);
141146
}
142147

143-
/** @return Identifier|false */
148+
/**
149+
* @deprecated Rename tables via {@link AbstractSchemaManager::renameTable()} instead.
150+
*
151+
* @return Identifier|false
152+
*/
144153
public function getNewName()
145154
{
155+
Deprecation::triggerIfCalledFromOutside(
156+
'doctrine/dbal',
157+
'https://github.com/doctrine/dbal/pull/5663',
158+
'%s is deprecated. Rename tables via AbstractSchemaManager::renameTable() instead.',
159+
__METHOD__,
160+
);
161+
146162
if ($this->newName === false) {
147163
return false;
148164
}

0 commit comments

Comments
 (0)