|
14 | 14 | use Doctrine\DBAL\Schema\Exception\PrimaryKeyAlreadyExists;
|
15 | 15 | use Doctrine\DBAL\Schema\Exception\UniqueConstraintDoesNotExist;
|
16 | 16 | use Doctrine\DBAL\Types\Type;
|
| 17 | +use Doctrine\Deprecations\Deprecation; |
17 | 18 | use LogicException;
|
18 | 19 |
|
19 | 20 | use function array_merge;
|
20 | 21 | use function array_values;
|
| 22 | +use function count; |
| 23 | +use function implode; |
| 24 | +use function in_array; |
21 | 25 | use function preg_match;
|
22 | 26 | use function sprintf;
|
23 | 27 | use function strtolower;
|
@@ -332,6 +336,30 @@ public function dropColumn(string $name): self
|
332 | 336 | {
|
333 | 337 | $name = $this->normalizeIdentifier($name);
|
334 | 338 |
|
| 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/6558', |
| 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 | + |
335 | 363 | unset($this->_columns[$name]);
|
336 | 364 |
|
337 | 365 | return $this;
|
@@ -800,4 +828,36 @@ private function _createIndex(
|
800 | 828 |
|
801 | 829 | return new Index($indexName, $columns, $isUnique, $isPrimary, $flags, $options);
|
802 | 830 | }
|
| 831 | + |
| 832 | + /** @return list<string> */ |
| 833 | + private function getForeignKeyConstraintNamesByLocalColumnName(string $columnName): array |
| 834 | + { |
| 835 | + $names = []; |
| 836 | + |
| 837 | + foreach ($this->_fkConstraints as $name => $constraint) { |
| 838 | + if (! in_array($columnName, $constraint->getLocalColumns(), true)) { |
| 839 | + continue; |
| 840 | + } |
| 841 | + |
| 842 | + $names[] = $name; |
| 843 | + } |
| 844 | + |
| 845 | + return $names; |
| 846 | + } |
| 847 | + |
| 848 | + /** @return list<string> */ |
| 849 | + private function getUniqueConstraintNamesByColumnName(string $columnName): array |
| 850 | + { |
| 851 | + $names = []; |
| 852 | + |
| 853 | + foreach ($this->uniqueConstraints as $name => $constraint) { |
| 854 | + if (! in_array($columnName, $constraint->getColumns(), true)) { |
| 855 | + continue; |
| 856 | + } |
| 857 | + |
| 858 | + $names[] = $name; |
| 859 | + } |
| 860 | + |
| 861 | + return $names; |
| 862 | + } |
803 | 863 | }
|
0 commit comments