Skip to content

Commit b72f7d6

Browse files
committed
Deprecate dropping columns referenced by constraints
1 parent 8078003 commit b72f7d6

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

UPGRADE.md

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

99
# Upgrade to 4.3
1010

11+
## Deprecated dropping columns referenced by constraints
12+
13+
Dropping columns that are referenced by constraints is deprecated. The constraints should be dropped first.
14+
1115
## Deprecated `Table::removeForeignKey()` and `::removeUniqueConstraint()`
1216

1317
The usage of `Table::removeForeignKey()` and `::removeUniqueConstraint()` is deprecated. Use `Table::dropForeignKey()`

src/Schema/Table.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
use function array_merge;
2121
use function array_values;
22+
use function count;
23+
use function implode;
24+
use function in_array;
2225
use function preg_match;
2326
use function sprintf;
2427
use function strtolower;
@@ -333,6 +336,30 @@ public function dropColumn(string $name): self
333336
{
334337
$name = $this->normalizeIdentifier($name);
335338

339+
$foreignKeyConstraintNames = $this->getForeignKeyConstraintNamesByLocalColumnName($name);
340+
$uniqueConstraintNames = $this->getUniqueConstraintNamesByColumnName($name);
341+
342+
if (count($foreignKeyConstraintNames) > 0 || count($uniqueConstraintNames) > 0) {
343+
$constraints = [];
344+
345+
if (count($foreignKeyConstraintNames) > 0) {
346+
$constraints[] = 'foreign key constraints: ' . implode(', ', $foreignKeyConstraintNames);
347+
}
348+
349+
if (count($uniqueConstraintNames) > 0) {
350+
$constraints[] = 'unique constraints: ' . implode(', ', $uniqueConstraintNames);
351+
}
352+
353+
Deprecation::trigger(
354+
'doctrine/dbal',
355+
'https://github.com/doctrine/dbal/pull/6559',
356+
'Dropping columns referenced by constraints is deprecated.'
357+
. ' Column %s is used by the following constraints: %s ',
358+
$name,
359+
implode('; ', $constraints),
360+
);
361+
}
362+
336363
unset($this->_columns[$name]);
337364

338365
return $this;
@@ -833,4 +860,36 @@ private function _createIndex(
833860

834861
return new Index($indexName, $columns, $isUnique, $isPrimary, $flags, $options);
835862
}
863+
864+
/** @return list<string> */
865+
private function getForeignKeyConstraintNamesByLocalColumnName(string $columnName): array
866+
{
867+
$names = [];
868+
869+
foreach ($this->_fkConstraints as $name => $constraint) {
870+
if (! in_array($columnName, $constraint->getLocalColumns(), true)) {
871+
continue;
872+
}
873+
874+
$names[] = $name;
875+
}
876+
877+
return $names;
878+
}
879+
880+
/** @return list<string> */
881+
private function getUniqueConstraintNamesByColumnName(string $columnName): array
882+
{
883+
$names = [];
884+
885+
foreach ($this->uniqueConstraints as $name => $constraint) {
886+
if (! in_array($columnName, $constraint->getColumns(), true)) {
887+
continue;
888+
}
889+
890+
$names[] = $name;
891+
}
892+
893+
return $names;
894+
}
836895
}

tests/Schema/TableTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\Schema\UniqueConstraint;
1616
use Doctrine\DBAL\Types\Type;
1717
use Doctrine\DBAL\Types\Types;
18+
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
1819
use LogicException;
1920
use PHPUnit\Framework\Attributes\DataProvider;
2021
use PHPUnit\Framework\TestCase;
@@ -24,6 +25,8 @@
2425

2526
class TableTest extends TestCase
2627
{
28+
use VerifyDeprecations;
29+
2730
public function testCreateWithInvalidTableName(): void
2831
{
2932
$this->expectException(Exception::class);
@@ -905,4 +908,33 @@ public function testRemoveUniqueConstraintUnknownNameThrowsException(): void
905908

906909
$table->removeUniqueConstraint('unique_constraint');
907910
}
911+
912+
public function testDropColumnWithForeignKeyConstraint(): void
913+
{
914+
$table = new Table('t1');
915+
$table->addColumn('id', Types::INTEGER);
916+
$table->addForeignKeyConstraint('t2', ['id'], ['id']);
917+
918+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6559');
919+
$table->dropColumn('id');
920+
}
921+
922+
public function testDropColumnWithUniqueConstraint(): void
923+
{
924+
$table = new Table('t');
925+
$table->addColumn('id', Types::INTEGER);
926+
$table->addUniqueConstraint(['id']);
927+
928+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6559');
929+
$table->dropColumn('id');
930+
}
931+
932+
public function testDropColumnWithoutConstraints(): void
933+
{
934+
$table = new Table('t');
935+
$table->addColumn('id', Types::INTEGER);
936+
937+
$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6559');
938+
$table->dropColumn('id');
939+
}
908940
}

0 commit comments

Comments
 (0)