Skip to content

Instantly share code, notes, and snippets.

@mvmaasakkers
Created September 5, 2022 06:14
Show Gist options
  • Save mvmaasakkers/7c28355715850d991fb9feb649e60463 to your computer and use it in GitHub Desktop.
Save mvmaasakkers/7c28355715850d991fb9feb649e60463 to your computer and use it in GitHub Desktop.
Doctrine PostgreSQL Default Schema Fix For Symfony - Based on https://gist.github.com/vudaltsov/ec01012d3fe27c9eed59aa7fd9089cf7
<?php
declare(strict_types=1);
namespace App\Doctrine\EventListener;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
final class FixPostgreSQLDefaultSchemaListener
{
/**
* @throws SchemaException
* @throws 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);
}
}
}
}
parameters:
services:
when@dev:
services:
App\Doctrine\EventListener\FixPostgreSQLDefaultSchemaListener:
tags:
- { name: doctrine.event_listener, event: postGenerateSchema }
@mvmaasakkers
Copy link
Author

@dxshindeo
Copy link

Legend!

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