Created
March 15, 2024 10:18
-
-
Save Brewal/d0fe0792a69e7e5fdf3fb06898b20d35 to your computer and use it in GitHub Desktop.
Fix doctrine migrations schema table for Doctrine 3 and DBAL 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Shared\Infrastructure\Doctrine\EventListener; | |
use Doctrine\DBAL\Exception; | |
use Doctrine\DBAL\Schema\SchemaException; | |
use Doctrine\Migrations\DependencyFactory; | |
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; | |
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | |
use Webmozart\Assert\Assert; | |
/** @see https://github.com/doctrine/migrations/issues/1406 */ | |
final class FixDoctrineMigrationTableSchema | |
{ | |
private TableMetadataStorageConfiguration $configuration; | |
public function __construct( | |
private readonly DependencyFactory $dependencyFactory, | |
) { | |
$configuration = $this->dependencyFactory->getConfiguration()->getMetadataStorageConfiguration(); | |
Assert::notNull($configuration); | |
Assert::isInstanceOf($configuration, TableMetadataStorageConfiguration::class); | |
$this->configuration = $configuration; | |
} | |
/** | |
* @throws SchemaException | |
* @throws Exception | |
*/ | |
public function postGenerateSchema(GenerateSchemaEventArgs $args): void | |
{ | |
$schema = $args->getSchema(); | |
$table = $schema->createTable($this->configuration->getTableName()); | |
$table->addColumn( | |
$this->configuration->getVersionColumnName(), | |
'string', | |
['notnull' => true, 'length' => $this->configuration->getVersionColumnLength()], | |
); | |
$table->addColumn($this->configuration->getExecutedAtColumnName(), 'datetime', ['notnull' => false]); | |
$table->addColumn($this->configuration->getExecutionTimeColumnName(), 'integer', ['notnull' => false]); | |
$table->setPrimaryKey([$this->configuration->getVersionColumnName()]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use App\Shared\Infrastructure\Doctrine\EventListener\FixDoctrineMigrationTableSchema; | |
use function Symfony\Component\DependencyInjection\Loader\Configurator\service; | |
return static function (ContainerConfigurator $containerConfigurator): void { | |
$services = $containerConfigurator->services(); | |
$services->defaults() | |
->autowire() | |
->autoconfigure(); | |
if ('prod' !== $containerConfigurator->env()) { | |
$services->set(FixDoctrineMigrationTableSchema::class) | |
->autoconfigure(false) | |
->arg('$dependencyFactory', service('doctrine.migrations.dependency_factory')) | |
->tag('doctrine.event_listener', ['event' => 'postGenerateSchema']); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment