setName('doctrine:database:drop') ->setDescription('Drops the configured database') ->addOption('shard', 's', InputOption::VALUE_REQUIRED, 'The shard connection to use for this command') ->addOption('connection', 'c', InputOption::VALUE_OPTIONAL, 'The connection to use for this command') ->addOption('if-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database doesn\'t exist') ->addOption('force', 'f', InputOption::VALUE_NONE, 'Set this parameter to execute this action') ->setHelp(<<%command.name% command drops the default connections database: php %command.full_name% The --force parameter has to be used to actually drop the database. You can also optionally specify the name of a connection to drop the database for: php %command.full_name% --connection=default Be careful: All data in a given database will be lost when executing this command. EOT ); } /** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $connectionName = $input->getOption('connection'); if (empty($connectionName)) { $connectionName = $this->getDoctrine()->getDefaultConnectionName(); } $connection = $this->getDoctrineConnection($connectionName); $ifExists = $input->getOption('if-exists'); $driverOptions = []; $params = $connection->getParams(); if (isset($params['driverOptions'])) { $driverOptions = $params['driverOptions']; } // Since doctrine/dbal 2.11 master has been replaced by primary if (isset($params['primary'])) { $params = $params['primary']; $params['driverOptions'] = $driverOptions; } if (isset($params['master'])) { $params = $params['master']; $params['driverOptions'] = $driverOptions; } if (isset($params['shards'])) { $shards = $params['shards']; // Default select global $params = array_merge($params, $params['global'] ?? []); if ($input->getOption('shard')) { foreach ($shards as $shard) { if ($shard['id'] === (int) $input->getOption('shard')) { // Select sharded database $params = array_merge($params, $shard); unset($params['id']); break; } } } } $name = $params['path'] ?? ($params['dbname'] ?? false); if (! $name) { throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped."); } unset($params['dbname'], $params['url']); if (! $input->getOption('force')) { $output->writeln('ATTENTION: This operation should not be executed in a production environment.'); $output->writeln(''); $output->writeln(sprintf('Would drop the database %s for connection named %s.', $name, $connectionName)); $output->writeln('Please run the operation with --force to execute'); $output->writeln('All data will be lost!'); return self::RETURN_CODE_NO_FORCE; } // Reopen connection without database name set // as some vendors do not allow dropping the database connected to. $connection->close(); $connection = DriverManager::getConnection($params); $shouldDropDatabase = ! $ifExists || in_array($name, $connection->getSchemaManager()->listDatabases()); // Only quote if we don't have a path if (! isset($params['path'])) { $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name); } try { if ($shouldDropDatabase) { $connection->getSchemaManager()->dropDatabase($name); $output->writeln(sprintf('Dropped database %s for connection named %s', $name, $connectionName)); } else { $output->writeln(sprintf('Database %s for connection named %s doesn\'t exist. Skipped.', $name, $connectionName)); } return 0; } catch (Throwable $e) { $output->writeln(sprintf('Could not drop database %s for connection named %s', $name, $connectionName)); $output->writeln(sprintf('%s', $e->getMessage())); return self::RETURN_CODE_NOT_DROP; } } }