Skip to content

Commit 50415ff

Browse files
committed
Deprecate Table methods
1 parent 1ebda1c commit 50415ff

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

UPGRADE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ awareness about deprecated code.
88

99
# Upgrade to 3.5
1010

11+
## Deprecated `Table` methods.
12+
13+
The `hasPrimaryKey()` method has been deprecated. Use `getPrimaryKey()` and check if the return value is not null.
14+
The `getPrimaryKeyColumns()` method has been deprecated. Use `getPrimaryKey()` and `Index::getColumns()` instead.
15+
The `getForeignKeyColumns()` method has been deprecated. Use `getForeignKey()`
16+
and `ForeignKeyConstraint::getLocalColumns()` instead.
17+
1118
## Deprecated `SchemaException` error codes.
1219

1320
Relying on the error code of `SchemaException` is deprecated. In order to handle a specific type of exception,

psalm.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,12 @@
435435
TODO: remove in 4.0.0
436436
-->
437437
<referencedMethod name="Doctrine\DBAL\Schema\TableDiff::getName"/>
438+
<!--
439+
TODO: remove in 4.0.0
440+
-->
441+
<referencedMethod name="Doctrine\DBAL\Schema\Table::getForeignKeyColumns"/>
442+
<referencedMethod name="Doctrine\DBAL\Schema\Table::getPrimaryKeyColumns"/>
443+
<referencedMethod name="Doctrine\DBAL\Schema\Table::hasPrimaryKey"/>
438444
</errorLevel>
439445
</DeprecatedMethod>
440446
<DeprecatedProperty>

src/Platforms/AbstractMySQLPlatform.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,26 @@ private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff): array
866866
return [];
867867
}
868868

869+
$primaryKey = $table->getPrimaryKey();
870+
871+
if ($primaryKey === null) {
872+
return [];
873+
}
874+
875+
$primaryKeyColumns = [];
876+
877+
foreach ($primaryKey->getColumns() as $columnName) {
878+
if (! $table->hasColumn($columnName)) {
879+
continue;
880+
}
881+
882+
$primaryKeyColumns[] = $table->getColumn($columnName);
883+
}
884+
885+
if (count($primaryKeyColumns) === 0) {
886+
return [];
887+
}
888+
869889
$sql = [];
870890

871891
$tableNameSQL = $table->getQuotedName($this);
@@ -876,9 +896,9 @@ private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff): array
876896
continue;
877897
}
878898

879-
foreach ($table->getPrimaryKeyColumns() as $columnName => $column) {
899+
foreach ($primaryKeyColumns as $column) {
880900
// Check if an autoincrement column was dropped from the primary key.
881-
if (! $column->getAutoincrement() || in_array($columnName, $changedIndex->getColumns(), true)) {
901+
if (! $column->getAutoincrement() || in_array($column->getName(), $changedIndex->getColumns(), true)) {
882902
continue;
883903
}
884904

src/Schema/Comparator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public function diffTable(Table $fromTable, Table $toTable)
330330

331331
/* See if all the indexes in "from" table exist in "to" table */
332332
foreach ($toTableIndexes as $indexName => $index) {
333-
if (($index->isPrimary() && $fromTable->hasPrimaryKey()) || $fromTable->hasIndex($indexName)) {
333+
if (($index->isPrimary() && $fromTable->getPrimaryKey() !== null) || $fromTable->hasIndex($indexName)) {
334334
continue;
335335
}
336336

@@ -343,7 +343,7 @@ public function diffTable(Table $fromTable, Table $toTable)
343343
foreach ($fromTableIndexes as $indexName => $index) {
344344
// See if index is removed in "to" table.
345345
if (
346-
($index->isPrimary() && ! $toTable->hasPrimaryKey()) ||
346+
($index->isPrimary() && $toTable->getPrimaryKey() === null) ||
347347
! $index->isPrimary() && ! $toTable->hasIndex($indexName)
348348
) {
349349
$droppedIndexes[$indexName] = $index;

src/Schema/Table.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ public function removeUniqueConstraint(string $name): void
683683
*/
684684
public function getColumns()
685685
{
686-
$primaryKeyColumns = $this->hasPrimaryKey() ? $this->getPrimaryKeyColumns() : [];
686+
$primaryKeyColumns = $this->getPrimaryKey() !== null ? $this->getPrimaryKeyColumns() : [];
687687
$foreignKeyColumns = $this->getForeignKeyColumns();
688688
$remainderColumns = $this->filterColumns(
689689
array_merge(array_keys($primaryKeyColumns), array_keys($foreignKeyColumns)),
@@ -696,10 +696,19 @@ public function getColumns()
696696
/**
697697
* Returns the foreign key columns
698698
*
699+
* @deprecated Use {@see getForeignKey()} and {@see ForeignKeyConstraint::getLocalColumns()} instead.
700+
*
699701
* @return Column[]
700702
*/
701703
public function getForeignKeyColumns()
702704
{
705+
Deprecation::trigger(
706+
'doctrine/dbal',
707+
'https://github.com/doctrine/dbal/pull/5731',
708+
'%s is deprecated. Use getForeignKey() and ForeignKeyConstraint::getLocalColumns() instead.',
709+
__METHOD__,
710+
);
711+
703712
$foreignKeyColumns = [];
704713

705714
foreach ($this->getForeignKeys() as $foreignKey) {
@@ -774,12 +783,21 @@ public function getPrimaryKey()
774783
/**
775784
* Returns the primary key columns.
776785
*
786+
* @deprecated Use {@see getPrimaryKey()} and {@see Index::getColumns()} instead.
787+
*
777788
* @return Column[]
778789
*
779790
* @throws Exception
780791
*/
781792
public function getPrimaryKeyColumns()
782793
{
794+
Deprecation::trigger(
795+
'doctrine/dbal',
796+
'https://github.com/doctrine/dbal/pull/5731',
797+
'%s is deprecated. Use getPrimaryKey() and Index::getColumns() instead.',
798+
__METHOD__,
799+
);
800+
783801
$primaryKey = $this->getPrimaryKey();
784802

785803
if ($primaryKey === null) {
@@ -792,10 +810,19 @@ public function getPrimaryKeyColumns()
792810
/**
793811
* Returns whether this table has a primary key.
794812
*
813+
* @deprecated Use {@see getPrimaryKey()} instead.
814+
*
795815
* @return bool
796816
*/
797817
public function hasPrimaryKey()
798818
{
819+
Deprecation::trigger(
820+
'doctrine/dbal',
821+
'https://github.com/doctrine/dbal/pull/5731',
822+
'%s is deprecated. Use getPrimaryKey() instead.',
823+
__METHOD__,
824+
);
825+
799826
return $this->_primaryKeyName !== null && $this->hasIndex($this->_primaryKeyName);
800827
}
801828

0 commit comments

Comments
 (0)