|
6 | 6 |
|
7 | 7 | use Doctrine\DBAL\Connection; |
8 | 8 | use Doctrine\DBAL\Exception; |
| 9 | +use Doctrine\DBAL\Exception\InvalidArgumentException; |
9 | 10 | use Doctrine\DBAL\Platforms\PostgreSQLPlatform as BasePlatform; |
10 | 11 | use Pfilsx\PostgreSQLDoctrine\DBAL\Schema\EnumTypeAsset; |
11 | 12 | use Pfilsx\PostgreSQLDoctrine\DBAL\Schema\PostgreSQLSchemaManager; |
@@ -44,16 +45,45 @@ public function getCommentOnTypeSql(EnumTypeAsset $type): string |
44 | 45 | return "COMMENT ON TYPE {$type->getQuotedName($this)} IS '{$type->getEnumClass()}'"; |
45 | 46 | } |
46 | 47 |
|
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 |
48 | 55 | { |
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; |
50 | 69 | } |
51 | 70 |
|
52 | 71 | public function getDropTypeSql(EnumTypeAsset $type): string |
53 | 72 | { |
54 | 73 | return "DROP TYPE {$type->getQuotedName($this)}"; |
55 | 74 | } |
56 | 75 |
|
| 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 | + |
57 | 87 | protected function initializeDoctrineTypeMappings() |
58 | 88 | { |
59 | 89 | parent::initializeDoctrineTypeMappings(); |
|
0 commit comments