Skip to content

Instantly share code, notes, and snippets.

@vudaltsov
Last active August 21, 2025 06:44
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 }
@dbu
Copy link

dbu commented Aug 21, 2025

with recent migrations library, i have to skip adding the public namespace (or whatever your default is) because migrations blindly adds the default namespace https://github.com/doctrine/migrations/blob/1b88fcb812f2cd6e77c83d16db60e3cf1e35c66c/src/Generator/DiffGenerator.php#L76

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment