Skip to content

Commit fc9f036

Browse files
committed
Add alter enum basic support
1 parent 6a77717 commit fc9f036

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
/vendor
2-
/composer.lock
3-
/tools
4-
.phpunit.result.cache
2+
/composer.lock

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'declare_strict_types' => true,
2525
'no_superfluous_phpdoc_tags' => false,
2626
'yoda_style' => ['equal' => false, 'identical'=>false, 'less_and_greater' => false],
27+
'no_unused_imports' => true,
2728
])
2829
->setUsingCache(false)
2930
->setFinder($finder)

src/DBAL/Platform/PostgreSQLPlatform.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\DBAL\Connection;
88
use Doctrine\DBAL\Exception;
9+
use Doctrine\DBAL\Exception\InvalidArgumentException;
910
use Doctrine\DBAL\Platforms\PostgreSQLPlatform as BasePlatform;
1011
use Pfilsx\PostgreSQLDoctrine\DBAL\Schema\EnumTypeAsset;
1112
use Pfilsx\PostgreSQLDoctrine\DBAL\Schema\PostgreSQLSchemaManager;
@@ -44,16 +45,45 @@ public function getCommentOnTypeSql(EnumTypeAsset $type): string
4445
return "COMMENT ON TYPE {$type->getQuotedName($this)} IS '{$type->getEnumClass()}'";
4546
}
4647

47-
public function getAlterTypeSql(EnumTypeAsset $from, EnumTypeAsset $to): string
48+
/**
49+
* @param EnumTypeAsset $from
50+
* @param EnumTypeAsset $to
51+
* @throws Exception
52+
* @return string[]
53+
*/
54+
public function getAlterTypeSql(EnumTypeAsset $from, EnumTypeAsset $to): array
4855
{
49-
throw Exception::notSupported(__METHOD__);
56+
$fromLabels = $from->getLabels();
57+
$toLabels = $to->getLabels();
58+
59+
$result = [];
60+
foreach (array_diff($toLabels, $fromLabels) as $label) {
61+
$result[] = "ALTER TYPE {$to->getQuotedName($this)} ADD VALUE {$this->quoteEnumLabel($label)}";
62+
}
63+
64+
if (count(array_diff($fromLabels, $toLabels)) > 0) {
65+
throw new Exception('Enum labels reduction is not supported in automatic generation');
66+
}
67+
68+
return $result;
5069
}
5170

5271
public function getDropTypeSql(EnumTypeAsset $type): string
5372
{
5473
return "DROP TYPE {$type->getQuotedName($this)}";
5574
}
5675

76+
public function quoteEnumLabel(mixed $label): int|string
77+
{
78+
if (\is_string($label)) {
79+
return $this->quoteStringLiteral($label);
80+
} elseif (\is_int($label)) {
81+
return $label;
82+
} else {
83+
throw new InvalidArgumentException('Invalid custom type labels specified. Only string and integers are supported');
84+
}
85+
}
86+
5787
protected function initializeDoctrineTypeMappings()
5888
{
5989
parent::initializeDoctrineTypeMappings();

src/DBAL/Schema/EnumTypeAsset.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Platforms\AbstractPlatform;
99
use Doctrine\DBAL\Schema\AbstractAsset;
1010
use Pfilsx\PostgreSQLDoctrine\DBAL\Contract\EnumInterface;
11+
use Pfilsx\PostgreSQLDoctrine\DBAL\Platform\PostgreSQLPlatform;
1112
use Pfilsx\PostgreSQLDoctrine\Tools\EnumTool;
1213

1314
final class EnumTypeAsset extends AbstractAsset
@@ -69,15 +70,13 @@ public function getEnumClass(): string
6970
*/
7071
public function getQuotedLabels(AbstractPlatform $platform): array
7172
{
73+
if (!$platform instanceof PostgreSQLPlatform) {
74+
return array_map(static fn ($label) => $platform->quoteStringLiteral((string) $label), $this->labels);
75+
}
76+
7277
$result = [];
7378
foreach ($this->labels as $label) {
74-
if (\is_string($label)) {
75-
$result[] = $platform->quoteStringLiteral($label);
76-
} elseif (\is_int($label)) {
77-
$result[] = $label;
78-
} else {
79-
throw new InvalidArgumentException('Invalid custom type labels specified. Only string and integers are supported');
80-
}
79+
$result[] = $platform->quoteEnumLabel($label);
8180
}
8281

8382
return $result;

src/DBAL/Schema/SchemaDiff.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public function toSql(AbstractPlatform $platform): array
7070
}
7171

7272
foreach ($this->getAlteredTypes() as $alterTypeArray) {
73-
$sql[] = $platform->getAlterTypeSql($alterTypeArray['from'], $alterTypeArray['to']);
73+
$sql = array_merge(
74+
$sql,
75+
$platform->getAlterTypeSql($alterTypeArray['from'], $alterTypeArray['to'])
76+
);
7477
}
7578

7679
$sql = \array_merge($sql, $this->_toSql($platform, false));

0 commit comments

Comments
 (0)