Skip to content

Commit

Permalink
Automatic database migration when enum definition changes #9951
Browse files Browse the repository at this point in the history
Previously, we had to manually craft a DB migration when we modified the
enum definition in PHP. This is now entirely automated.

This will migrate all pre-existing enums.
  • Loading branch information
PowerKiKi committed Nov 9, 2023
1 parent 2967b49 commit 0ef5dad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public function __invoke(): array
\Laminas\View\Renderer\RendererInterface::class => \Ecodev\Felix\Service\RendererFactory::class,
],
],
'doctrine' => [
'event_manager' => [
'orm_default' => [
'listeners' => [
[
'events' => [\Doctrine\ORM\Tools\ToolEvents::postGenerateSchema],
'listener' => \Ecodev\Felix\Service\EnumAutoMigrator::class,
],
],
],
],
],
];
}
}
27 changes: 27 additions & 0 deletions src/Service/EnumAutoMigrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Ecodev\Felix\Service;

use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use Ecodev\Felix\DBAL\Types\EnumType;

/**
* When we update our enum values in PHP (native or non-native), a DB migration will be created automatically.
*/
class EnumAutoMigrator
{
public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs): void
{
foreach ($eventArgs->getSchema()->getTables() as $table) {
foreach ($table->getColumns() as $column) {
$type = $column->getType();
if ($type instanceof EnumType) {
$hash = md5($type->getQuotedPossibleValues());

Check failure on line 21 in src/Service/EnumAutoMigrator.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Ecodev\Felix\DBAL\Types\EnumType::getQuotedPossibleValues().
$column->setComment("(FelixEnum:$hash)");
}
}
}
}
}

0 comments on commit 0ef5dad

Please sign in to comment.