Skip to content

Commit 7150c1d

Browse files
committed
Fix doctrine:database:create and doctrine:database:drop for Postgresql on DBAL 4.x
Ref doctrine/dbal#5705 Fixes #1757
1 parent a1b3627 commit 7150c1d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Command/CreateDatabaseDoctrineCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Doctrine\Bundle\DoctrineBundle\Command;
44

55
use Doctrine\DBAL\DriverManager;
6+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
67
use InvalidArgumentException;
78
use Symfony\Component\Console\Input\InputInterface;
89
use Symfony\Component\Console\Input\InputOption;
@@ -60,10 +61,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6061
throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be created.");
6162
}
6263

63-
// Need to get rid of _every_ occurrence of dbname from connection configuration and we have already extracted all relevant info from url
64+
// Need to get rid of _every_ occurrence of dbname from connection configuration as we have already extracted all relevant info from url
6465
/** @psalm-suppress InvalidArrayOffset Need to be compatible with DBAL < 4, which still has `$params['url']` */
6566
unset($params['dbname'], $params['path'], $params['url']);
6667

68+
if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
69+
$params['dbname'] = 'postgres';
70+
}
71+
6772
$tmpConnection = DriverManager::getConnection($params, $connection->getConfiguration());
6873
$schemaManager = $tmpConnection->createSchemaManager();
6974
$shouldNotCreateDatabase = $ifNotExists && in_array($name, $schemaManager->listDatabases());

Command/DropDatabaseDoctrineCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Doctrine\Bundle\DoctrineBundle\Command;
44

55
use Doctrine\DBAL\DriverManager;
6+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
67
use Doctrine\DBAL\Schema\SQLiteSchemaManager;
78
use InvalidArgumentException;
89
use Symfony\Component\Console\Input\InputInterface;
@@ -75,6 +76,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7576
/** @psalm-suppress InvalidArrayOffset Need to be compatible with DBAL < 4, which still has `$params['url']` */
7677
unset($params['dbname'], $params['url']);
7778

79+
if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
80+
$params['dbname'] = 'postgres';
81+
}
82+
7883
if (! $input->getOption('force')) {
7984
$output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.');
8085
$output->writeln('');

0 commit comments

Comments
 (0)