Last active
November 9, 2024 10:35
-
-
Save vudaltsov/ec01012d3fe27c9eed59aa7fd9089cf7 to your computer and use it in GitHub Desktop.
Doctrine PostgreSQL Default Schema Fix For Symfony
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\Doctrine\EventListener; | |
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager; | |
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | |
final class FixPostgreSQLDefaultSchemaListener | |
{ | |
/** | |
* @throws \Doctrine\DBAL\Exception | |
*/ | |
public function postGenerateSchema(GenerateSchemaEventArgs $args): void | |
{ | |
$schemaManager = $args | |
->getEntityManager() | |
->getConnection() | |
->createSchemaManager(); | |
if (!$schemaManager instanceof PostgreSQLSchemaManager) { | |
return; | |
} | |
$schema = $args->getSchema(); | |
foreach ($schemaManager->listSchemaNames() as $namespace) { | |
if (!$schema->hasNamespace($namespace)) { | |
$schema->createNamespace($namespace); | |
} | |
} | |
} | |
} |
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\Doctrine\EventListener\FixPostgreSQLDefaultSchemaListener; | |
use Doctrine\ORM\Tools\ToolEvents; | |
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | |
return static function (ContainerConfigurator $configurator): void { | |
$services = $configurator->services(); | |
$services | |
->set(FixPostgreSQLDefaultSchemaListener::class) | |
->tag('doctrine.event_listener', ['event' => ToolEvents::postGenerateSchema]); | |
}; |
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
services: | |
App\Doctrine\EventListener\FixPostgreSQLDefaultSchemaListener: | |
tags: | |
- { name: doctrine.event_listener, event: postGenerateSchema } |
Using attributes to register the event listener per the documentation:
// FixPostgreSQLDefaultSchemaListener.php
#[AsDoctrineListener(event: ToolEvents::postGenerateSchema, connection: 'default')]
final class FixPostgreSQLDefaultSchemaListener
{
// implementation
}
With a WhenDev
<?php
declare(strict_types=1);
namespace App\Shared\Doctrine;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use Doctrine\ORM\Tools\ToolEvents;
use Symfony\Component\DependencyInjection\Attribute\When;
#[When('dev')]
#[AsDoctrineListener(event: ToolEvents::postGenerateSchema, connection: 'default')]
final class FixPostgreSQLDefaultSchemaListener
{
public function __invoke(GenerateSchemaEventArgs $args)
{
$schemaManager = $args
->getEntityManager()
->getConnection()
->createSchemaManager();
if (!$schemaManager instanceof PostgreSQLSchemaManager) {
return;
}
$schema = $args->getSchema();
foreach ($schemaManager->listSchemaNames() as $namespace) {
if (!$schema->hasNamespace($namespace)) {
$schema->createNamespace($namespace);
}
}
}
}
Im not proud of this - but for anyone wanting to not break schema tool operations doctrine:schema:create
you can add a guard statement to the event listener limiting the fix to only be applied when running doctrine:migrations:diff
final class FixPostgreSQLDefaultSchemaListener
{
/**
* @throws \Doctrine\DBAL\Exception
*/
public function postGenerateSchema(GenerateSchemaEventArgs $args): void
{
// Only apply this fix when using the doctrine:migrations:diff command
if (
!isset($_SERVER['argv'])
|| !is_array($_SERVER['argv'])
|| !in_array('doctrine:migrations:diff', $_SERVER['argv'], true)
) {
return;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be configured right in attributes.