|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Core\App\Doctrine; |
| 4 | + |
| 5 | +use Core\App\DBAL\Types\AbstractEnumType; |
| 6 | +use Doctrine\Common\EventSubscriber; |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Doctrine\DBAL\Exception; |
| 9 | +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; |
| 10 | +use Doctrine\Migrations\Events; |
| 11 | +use Doctrine\ORM\EntityManagerInterface; |
| 12 | +use Psr\Container\ContainerExceptionInterface; |
| 13 | +use Psr\Container\ContainerInterface; |
| 14 | +use Psr\Container\NotFoundExceptionInterface; |
| 15 | + |
| 16 | +use function array_column; |
| 17 | +use function array_key_exists; |
| 18 | +use function implode; |
| 19 | +use function in_array; |
| 20 | +use function sprintf; |
| 21 | + |
| 22 | +class MigrationsMigratedSubscriber implements EventSubscriber |
| 23 | +{ |
| 24 | + private EntityManagerInterface $entityManager; |
| 25 | + private Connection $connection; |
| 26 | + |
| 27 | + /** |
| 28 | + * @throws ContainerExceptionInterface |
| 29 | + * @throws NotFoundExceptionInterface |
| 30 | + */ |
| 31 | + public function __construct( |
| 32 | + private readonly ContainerInterface $container, |
| 33 | + ) { |
| 34 | + $this->entityManager = $container->get('doctrine.entity_manager.orm_default'); |
| 35 | + $this->connection = $this->entityManager->getConnection(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @throws Exception |
| 40 | + */ |
| 41 | + public function getSubscribedEvents(): array |
| 42 | + { |
| 43 | + if (! $this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + return [ |
| 48 | + Events::onMigrationsMigrating, |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @throws ContainerExceptionInterface |
| 54 | + * @throws NotFoundExceptionInterface |
| 55 | + * @throws Exception |
| 56 | + */ |
| 57 | + public function onMigrationsMigrating(): void |
| 58 | + { |
| 59 | + $dbEnumTypes = $this->getCustomEnumTypesFromTheDatabase(); |
| 60 | + $fsEnumTypes = $this->getCustomEnumTypesFromTheFileSystem(); |
| 61 | + |
| 62 | + $enumTypes = $this->mergeCustomEnumTypes($dbEnumTypes, $fsEnumTypes); |
| 63 | + foreach ($enumTypes as $action => $enums) { |
| 64 | + foreach ($enums as $type => $values) { |
| 65 | + match ($action) { |
| 66 | + 'create' => $this->createDatabaseType($type, $values), |
| 67 | + 'delete' => $this->deleteDatabaseType($type), |
| 68 | + 'update' => $this->updateDatabaseType($type, $values), |
| 69 | + default => null, |
| 70 | + }; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array<non-empty-string, object<AbstractEnumType>> |
| 77 | + * @throws ContainerExceptionInterface |
| 78 | + * @throws NotFoundExceptionInterface |
| 79 | + */ |
| 80 | + private function getCustomEnumTypesFromTheFileSystem(): array |
| 81 | + { |
| 82 | + $enumTypes = []; |
| 83 | + |
| 84 | + $customTypes = $this->container->get('config')['doctrine']['types'] ?? []; |
| 85 | + foreach ($customTypes as $type => $class) { |
| 86 | + $class = new $class(); |
| 87 | + if (! $class instanceof AbstractEnumType) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + $enumTypes[$type] = $class; |
| 91 | + } |
| 92 | + |
| 93 | + return $enumTypes; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @throws Exception |
| 98 | + */ |
| 99 | + private function getDatabaseTypeValues(string $type): array |
| 100 | + { |
| 101 | + $results = $this->connection->executeQuery( |
| 102 | + "SELECT e.enumlabel FROM pg_type t JOIN pg_enum e ON t.oid = e.enumtypid WHERE t.typname = '$type';" |
| 103 | + )->fetchAllAssociative(); |
| 104 | + |
| 105 | + return array_column($results, 'enumlabel'); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return list<non-empty-string> |
| 110 | + * @throws Exception |
| 111 | + */ |
| 112 | + private function getCustomEnumTypesFromTheDatabase(): array |
| 113 | + { |
| 114 | + return $this->connection->executeQuery( |
| 115 | + 'SELECT t.typname FROM pg_type t JOIN pg_enum e ON t.oid = e.enumtypid GROUP BY t.typname' |
| 116 | + )->fetchFirstColumn(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param list<non-empty-string> $dbEnumTypes |
| 121 | + * @param array<non-empty-string, object<AbstractEnumType>> $fsEnumTypes |
| 122 | + * @return array<non-empty-string, array<non-empty-string, list<non-empty-string>>> |
| 123 | + * @throws Exception |
| 124 | + */ |
| 125 | + private function mergeCustomEnumTypes(array $dbEnumTypes, array $fsEnumTypes): array |
| 126 | + { |
| 127 | + $enumTypes = [ |
| 128 | + 'create' => [], |
| 129 | + 'delete' => [], |
| 130 | + 'skip' => [], |
| 131 | + 'update' => [], |
| 132 | + ]; |
| 133 | + |
| 134 | + /** @var AbstractEnumType $class */ |
| 135 | + foreach ($fsEnumTypes as $type => $class) { |
| 136 | + $fsTypeValues = $class->getEnumValues(); |
| 137 | + if (in_array($type, $dbEnumTypes)) { |
| 138 | + $dbTypeValues = $this->getDatabaseTypeValues($type); |
| 139 | + if ($dbTypeValues === $fsTypeValues) { |
| 140 | + $enumTypes['skip'][$type] = $fsTypeValues; |
| 141 | + } else { |
| 142 | + $enumTypes['update'][$type] = $fsTypeValues; |
| 143 | + } |
| 144 | + } else { |
| 145 | + $enumTypes['create'][$type] = $fsTypeValues; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + foreach ($dbEnumTypes as $type) { |
| 150 | + if (! array_key_exists($type, $fsEnumTypes)) { |
| 151 | + $enumTypes['delete'][$type] = $this->getDatabaseTypeValues($type); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return $enumTypes; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @throws Exception |
| 160 | + */ |
| 161 | + private function createDatabaseType(string $type, array $values): void |
| 162 | + { |
| 163 | + $sql = sprintf("CREATE TYPE %s AS ENUM ('%s');", $type, implode("', '", $values)); |
| 164 | + $this->connection->executeQuery($sql); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @throws Exception |
| 169 | + */ |
| 170 | + private function deleteDatabaseType(string $type): void |
| 171 | + { |
| 172 | + $sql = sprintf('DROP TYPE %s;', $type); |
| 173 | + $this->connection->executeQuery($sql); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @throws Exception |
| 178 | + */ |
| 179 | + private function updateDatabaseType(string $type, array $values): void |
| 180 | + { |
| 181 | + $this->deleteDatabaseType($type); |
| 182 | + $this->createDatabaseType($type, $values); |
| 183 | + } |
| 184 | +} |
0 commit comments