Skip to content

Instantly share code, notes, and snippets.

@vudaltsov
Last active November 9, 2024 10:35
Show Gist options
  • Save vudaltsov/ec01012d3fe27c9eed59aa7fd9089cf7 to your computer and use it in GitHub Desktop.
Save vudaltsov/ec01012d3fe27c9eed59aa7fd9089cf7 to your computer and use it in GitHub Desktop.
Doctrine PostgreSQL Default Schema Fix For Symfony
<?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);
}
}
}
}
<?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]);
};
services:
App\Doctrine\EventListener\FixPostgreSQLDefaultSchemaListener:
tags:
- { name: doctrine.event_listener, event: postGenerateSchema }
@vudaltsov
Copy link
Author

vudaltsov commented Jan 15, 2019

Listener is not implemented as a subscriber according to performance considerations described at the end of the "Doctrine Lifecycle Subscribers" section.

@vasilvestre
Copy link

This is causing some fail in my behat test. :/ (Actually tring to fix it)

@vudaltsov
Copy link
Author

@vasilvestre , could you provide some more information or a reproducer?

@vasilvestre
Copy link

@vasilvestre , could you provide some more information or a reproducer?

    /**
     * @BeforeScenario @resetDatabase
     * @throws \Doctrine\ORM\Tools\ToolsException
     */
    public function resetDatabase()
    {
        $this->schemaTool->dropSchema($this->classes);
        $this->doctrine->getManager()->clear();
        $this->schemaTool->createSchema($this->classes);
    }

This part of code fail, telling me that namespace already exist and that some table already exist

@flolivaud
Copy link

flolivaud commented Apr 19, 2019

@vasilvestre, had the same problem, i do this instead, and it work

        exec('bin/console doctrine:database:drop --force --env=test');
        exec('bin/console doctrine:database:create --env=test');
        exec('bin/console doctrine:migrations:migrate --env=test -n');

@fancyweb
Copy link

Thank you @vudaltsov :)

@romaricdrigon
Copy link

It works, thank you for the snippet!
For optimization, you may want to add the listener declaration to services_dev.yaml instead - so service is never called but when you generate a migration on your machine.

@vudaltsov
Copy link
Author

@romaricdrigon, agree, thank you! Updated :)

@zomberg
Copy link

zomberg commented Oct 14, 2020

@vudaltsov, thank you! It works great)

@shahariaazam
Copy link

@vasilvestre, had the same problem, i do this instead, and it work

        exec('bin/console doctrine:database:drop --force --env=test');
        exec('bin/console doctrine:database:create --env=test');
        exec('bin/console doctrine:migrations:migrate --env=test -n');

Your commands will work for new migration. But for existing db+already migrated migrations, it won't work. This gist will fix that problem too, I guess.

@stetodd
Copy link

stetodd commented Feb 16, 2022

I've just upgraded from v2 to v3 and had to make changes to this listener. The main change was to:
foreach ($schemaManager->getExistingSchemaSearchPaths() as $namespace) {

to use listSchemaNames() instead:
foreach ($schemaManager->listSchemaNames() as $namespace) {

getExistingSchemaSearchPaths() is internal and listSchemaNames() is public. getExistingSchemaSearchPaths() also wasn't providing the expected set of namespaces.

@lhapaipai
Copy link

Thank you @stetodd .

@mvmaasakkers
Copy link

mvmaasakkers commented Sep 5, 2022

One more addition to the changes @stetodd suggested to remove usage of a deprecated methods from doctrine/orm 2.13: change ->getSchemaManager() into ->createSchemaManager().

Also, when using Symfony 5.3 or up (and yaml config) you can use a when@dev statement to add the listener like so:

when@dev:
    services:
        App\Doctrine\EventListener\FixPostgreSQLDefaultSchemaListener:
            tags:
                - { name: doctrine.event_listener, event: postGenerateSchema }

Created a new gist for a complete overview: https://gist.github.com/mvmaasakkers/7c28355715850d991fb9feb649e60463

@vudaltsov
Copy link
Author

Thank you all for the interest! I've updated the snippet to match the latest Doctrine contracts.

@luzrain
Copy link

luzrain commented Jun 10, 2023

Can be configured right in attributes.

use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\DependencyInjection\Attribute\When;

#[When('dev')]
#[AutoconfigureTag('doctrine.event_listener', ['event' => ToolEvents::postGenerateSchema])]
final class FixPostgreSQLDefaultSchemaListener
{
}

@jdevinemt
Copy link

Using attributes to register the event listener per the documentation:

// FixPostgreSQLDefaultSchemaListener.php

#[AsDoctrineListener(event: ToolEvents::postGenerateSchema, connection: 'default')] 
final class FixPostgreSQLDefaultSchemaListener
{
    // implementation
}

@SalvadorCardona
Copy link

SalvadorCardona commented May 15, 2024

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);
            }
        }
    }
}

@JParkinson1991
Copy link

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