Skip to content

Commit 6a72a34

Browse files
authored
Merge pull request #6975 from morozov/remove-column-platform-option-getters
Remove column platform option getters
2 parents 4e0b51e + b172a88 commit 6a72a34

File tree

8 files changed

+88
-89
lines changed

8 files changed

+88
-89
lines changed

UPGRADE.md

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

99
# Upgrade to 5.0
1010

11+
## BC BREAK: Removed `Column` methods
12+
13+
The following `Column` methods have been removed:
14+
15+
- `Column::getPlatformOption()`
16+
- `Column::getPlatformOptions()`
17+
- `Column::hasPlatformOption()`
18+
1119
## BC BREAK: Removed support for the `version` column platform option
1220

1321
The `version` column platform option is no longer supported.

src/Platforms/MySQL/Comparator.php

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
namespace Doctrine\DBAL\Platforms\MySQL;
66

77
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
8-
use Doctrine\DBAL\Schema\Column;
8+
use Doctrine\DBAL\Schema\ColumnEditor;
99
use Doctrine\DBAL\Schema\Comparator as BaseComparator;
1010
use Doctrine\DBAL\Schema\ComparatorConfig;
1111
use Doctrine\DBAL\Schema\Table;
1212
use Doctrine\DBAL\Schema\TableDiff;
1313

14-
use function array_diff_assoc;
14+
use function count;
1515

1616
/**
1717
* Compares schemas in the context of MySQL platform.
1818
*
1919
* In MySQL, unless specified explicitly, the column's character set and collation are inherited from its containing
2020
* table. So during comparison, an omitted value and the value that matches the default value of table in the
2121
* desired schema must be considered equal.
22-
*
23-
* @phpstan-import-type PlatformOptions from Column
2422
*/
2523
class Comparator extends BaseComparator
2624
{
@@ -57,43 +55,55 @@ private function normalizeTable(Table $table): Table
5755
$collation = $this->defaultTableOptions->getCollation();
5856
}
5957

60-
$tableOptions = [
61-
'charset' => $charset,
62-
'collation' => $collation,
63-
];
64-
65-
$table = clone $table;
58+
$editor = null;
6659

6760
foreach ($table->getColumns() as $column) {
68-
$originalOptions = $column->getPlatformOptions();
69-
$normalizedOptions = $this->normalizeOptions($originalOptions);
61+
$originalCharset = $column->getCharset();
62+
$originalCollation = $column->getCollation();
7063

71-
$overrideOptions = array_diff_assoc($normalizedOptions, $tableOptions);
64+
$normalizedCharset = $originalCharset;
65+
$normalizedCollation = $originalCollation;
7266

73-
if ($overrideOptions === $originalOptions) {
74-
continue;
67+
if ($originalCharset !== null && $originalCollation === null) {
68+
$normalizedCollation = $this->charsetMetadataProvider->getDefaultCharsetCollation($originalCharset);
69+
} elseif ($originalCollation !== null && $originalCharset === null) {
70+
$normalizedCharset = $this->collationMetadataProvider->getCollationCharset($originalCollation);
7571
}
7672

77-
/** @phpstan-ignore argument.type */
78-
$column->setPlatformOptions($overrideOptions);
79-
}
73+
$modifications = [];
8074

81-
return $table;
82-
}
75+
if ($normalizedCharset === $charset) {
76+
$modifications[] = static function (ColumnEditor $editor): void {
77+
$editor->setCharset(null);
78+
};
79+
} elseif ($normalizedCharset !== $originalCharset) {
80+
$modifications[] = static function (ColumnEditor $editor) use ($normalizedCharset): void {
81+
$editor->setCharset($normalizedCharset);
82+
};
83+
}
8384

84-
/**
85-
* @param PlatformOptions $options
86-
*
87-
* @return PlatformOptions
88-
*/
89-
private function normalizeOptions(array $options): array
90-
{
91-
if (isset($options['charset']) && ! isset($options['collation'])) {
92-
$options['collation'] = $this->charsetMetadataProvider->getDefaultCharsetCollation($options['charset']);
93-
} elseif (isset($options['collation']) && ! isset($options['charset'])) {
94-
$options['charset'] = $this->collationMetadataProvider->getCollationCharset($options['collation']);
85+
if ($normalizedCollation === $collation) {
86+
$modifications[] = static function (ColumnEditor $editor): void {
87+
$editor->setCollation(null);
88+
};
89+
} elseif ($normalizedCollation !== $originalCollation) {
90+
$modifications[] = static function (ColumnEditor $editor) use ($normalizedCollation): void {
91+
$editor->setCollation($normalizedCollation);
92+
};
93+
}
94+
95+
if (count($modifications) === 0) {
96+
continue;
97+
}
98+
99+
$editor ??= $table->edit();
100+
$name = $column->getObjectName();
101+
102+
foreach ($modifications as $modification) {
103+
$editor->modifyColumn($name, $modification);
104+
}
95105
}
96106

97-
return $options;
107+
return $editor === null ? $table : $editor->create();
98108
}
99109
}

src/Platforms/SQLServer/Comparator.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\DBAL\Platforms\SQLServer;
66

77
use Doctrine\DBAL\Platforms\SQLServerPlatform;
8+
use Doctrine\DBAL\Schema\ColumnEditor;
89
use Doctrine\DBAL\Schema\Comparator as BaseComparator;
910
use Doctrine\DBAL\Schema\ComparatorConfig;
1011
use Doctrine\DBAL\Schema\Table;
@@ -36,19 +37,21 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
3637

3738
private function normalizeColumns(Table $table): Table
3839
{
39-
$table = clone $table;
40+
$editor = null;
4041

4142
foreach ($table->getColumns() as $column) {
42-
$options = $column->getPlatformOptions();
43+
$collation = $column->getCollation();
4344

44-
if (! isset($options['collation']) || $options['collation'] !== $this->databaseCollation) {
45+
if ($collation !== $this->databaseCollation) {
4546
continue;
4647
}
4748

48-
unset($options['collation']);
49-
$column->setPlatformOptions($options);
49+
($editor ??= $table->edit())
50+
->modifyColumn($column->getObjectName(), static function (ColumnEditor $editor): void {
51+
$editor->setCollation(null);
52+
});
5053
}
5154

52-
return $table;
55+
return $editor === null ? $table : $editor->create();
5356
}
5457
}

src/Platforms/SQLServerPlatform.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,16 @@ private function getAlterTableAddDefaultConstraintClause(string $tableName, Colu
494494
*/
495495
private function getAlterTableDropDefaultConstraintClause(Column $column): string
496496
{
497-
if (! $column->hasPlatformOption(self::OPTION_DEFAULT_CONSTRAINT_NAME)) {
497+
$constraintName = $column->getDefaultConstraintName();
498+
499+
if ($constraintName === null) {
498500
throw new InvalidArgumentException(
499501
'Column ' . $column->getName() . ' was not properly introspected as it has a default value'
500502
. ' but does not have the default constraint name.',
501503
);
502504
}
503505

504-
return 'DROP CONSTRAINT ' . $this->quoteSingleIdentifier(
505-
$column->getPlatformOption(self::OPTION_DEFAULT_CONSTRAINT_NAME),
506-
);
506+
return 'DROP CONSTRAINT ' . $this->quoteSingleIdentifier($constraintName);
507507
}
508508

509509
/**

src/Platforms/SQLite/Comparator.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\DBAL\Platforms\SQLite;
66

77
use Doctrine\DBAL\Platforms\SQLitePlatform;
8+
use Doctrine\DBAL\Schema\ColumnEditor;
89
use Doctrine\DBAL\Schema\Comparator as BaseComparator;
910
use Doctrine\DBAL\Schema\ComparatorConfig;
1011
use Doctrine\DBAL\Schema\Table;
@@ -35,19 +36,21 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
3536

3637
private function normalizeColumns(Table $table): Table
3738
{
38-
$table = clone $table;
39+
$editor = null;
3940

4041
foreach ($table->getColumns() as $column) {
41-
$options = $column->getPlatformOptions();
42+
$collation = $column->getCollation();
4243

43-
if (! isset($options['collation']) || strcasecmp($options['collation'], 'binary') !== 0) {
44+
if ($collation === null || strcasecmp($collation, 'binary') !== 0) {
4445
continue;
4546
}
4647

47-
unset($options['collation']);
48-
$column->setPlatformOptions($options);
48+
($editor ??= $table->edit())
49+
->modifyColumn($column->getObjectName(), static function (ColumnEditor $editor): void {
50+
$editor->setCollation(null);
51+
});
4952
}
5053

51-
return $table;
54+
return $editor === null ? $table : $editor->create();
5255
}
5356
}

src/Schema/Column.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -270,40 +270,6 @@ public function getDefaultConstraintName(): ?string
270270
return $this->_platformOptions[SQLServerPlatform::OPTION_DEFAULT_CONSTRAINT_NAME] ?? null;
271271
}
272272

273-
/**
274-
* @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()}
275-
* instead.
276-
*
277-
* @return PlatformOptions
278-
*/
279-
public function getPlatformOptions(): array
280-
{
281-
return $this->_platformOptions;
282-
}
283-
284-
/**
285-
* @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()}
286-
* instead.
287-
*
288-
* @param key-of<PlatformOptions> $name
289-
*/
290-
public function hasPlatformOption(string $name): bool
291-
{
292-
return isset($this->_platformOptions[$name]);
293-
}
294-
295-
/**
296-
* @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()}
297-
* instead.
298-
*
299-
* @param key-of<PlatformOptions> $name
300-
*/
301-
public function getPlatformOption(string $name): mixed
302-
{
303-
/** @phpstan-ignore offsetAccess.notFound */
304-
return $this->_platformOptions[$name];
305-
}
306-
307273
public function getColumnDefinition(): ?string
308274
{
309275
return $this->_columnDefinition;

src/Schema/ColumnDiff.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,20 @@ public function hasCommentChanged(): bool
127127

128128
public function hasPlatformOptionsChanged(): bool
129129
{
130-
return $this->hasPropertyChanged(static function (Column $column): array {
131-
return $column->getPlatformOptions();
132-
});
130+
foreach (
131+
[
132+
static fn (Column $column): ?string => $column->getCharset(),
133+
static fn (Column $column): ?string => $column->getCollation(),
134+
static fn (Column $column): mixed => $column->getMinimumValue(),
135+
static fn (Column $column): mixed => $column->getMaximumValue(),
136+
] as $property
137+
) {
138+
if ($this->hasPropertyChanged($property)) {
139+
return true;
140+
}
141+
}
142+
143+
return false;
133144
}
134145

135146
private function hasPropertyChanged(callable $property): bool

tests/Schema/ColumnTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ public function testGet(): void
3535
self::assertTrue($column->getFixed());
3636
self::assertEquals('baz', $column->getDefault());
3737

38-
self::assertEquals(['charset' => 'utf8'], $column->getPlatformOptions());
39-
self::assertTrue($column->hasPlatformOption('charset'));
40-
self::assertEquals('utf8', $column->getPlatformOption('charset'));
41-
self::assertFalse($column->hasPlatformOption('collation'));
38+
self::assertEquals('utf8', $column->getCharset());
39+
self::assertNull($column->getCollation());
4240
}
4341

4442
public function testToArray(): void

0 commit comments

Comments
 (0)