Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix doctrine:database:create and doctrine:database:drop for Postgresql on DBAL 4.x #1761

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion Command/CreateDatabaseDoctrineCommand.php
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Bundle\DoctrineBundle\Command;

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

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

if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$params['dbname'] = 'postgres';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we unset dbname on line 66, only to set it again here? Also, I don't understand the comment on line 64, should it read "as we have already extracted" instead of "and we have already extracted"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the first point, it's because I assume unsetting dbname might still be needed for other db platforms. For the second point: I guess so.


$tmpConnection = DriverManager::getConnection($params, $connection->getConfiguration());
$schemaManager = $tmpConnection->createSchemaManager();
$shouldNotCreateDatabase = $ifNotExists && in_array($name, $schemaManager->listDatabases());
Expand Down
5 changes: 5 additions & 0 deletions Command/DropDatabaseDoctrineCommand.php
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Bundle\DoctrineBundle\Command;

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

if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$params['dbname'] = 'postgres';
}

if (! $input->getOption('force')) {
$output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.');
$output->writeln('');
Expand Down