* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ /** * Database tool allows you to easily drop and create your configured databases. * * @package symfony * @subpackage console * @author Fabien Potencier * @author Jonathan H. Wage */ class DatabaseToolDoctrineCommand extends DoctrineCommand { /** * @see Command */ protected function configure() { $this ->setName('doctrine:database-tool') ->setDescription('Create and drop the configured databases.') ->addOption('re-create', null, null, 'Drop and re-create your databases.') ->addOption('drop', null, null, 'Drop your databases.') ->addOption('create', null, null, 'Create your databases.') ->addOption('connection', null, null, 'The connection name to work on.') ; } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { if ($input->getOption('re-create')) { $input->setOption('drop', true); $input->setOption('create', true); } if (!$input->getOption('drop') && !$input->getOption('create')) { throw new \InvalidArgumentException('You must specify one of the --drop and --create options or both.'); } $found = false; $connections = $this->getDoctrineConnections(); foreach ($connections as $name => $connection) { if ($input->getOption('connection') && $name != $input->getOption('connection')) { continue; } if ($input->getOption('drop')) { $this->dropDatabaseForConnection($connection, $output); } if ($input->getOption('create')) { $this->createDatabaseForConnection($connection, $output); } $found = true; } if ($found === false) { if ($input->getOption('connection')) { throw new \InvalidArgumentException(sprintf('Could not find a connection named %s', $input->getOption('connection'))); } else { throw new \InvalidArgumentException(sprintf('Could not find any configured connections', $input->getOption('connection'))); } } } protected function dropDatabaseForConnection(Connection $connection, OutputInterface $output) { $params = $connection->getParams(); $name = isset($params['path']) ? $params['path']:$params['dbname']; try { $connection->getSchemaManager()->dropDatabase($name); $output->writeln(sprintf('Dropped database for connection named %s', $name)); } catch (\Exception $e) { $output->writeln(sprintf('Could not drop database for connection named %s', $name)); $output->writeln(sprintf('%s', $e->getMessage())); } } protected function createDatabaseForConnection(Connection $connection, OutputInterface $output) { $params = $connection->getParams(); $name = isset($params['path']) ? $params['path']:$params['dbname']; unset($params['dbname']); $tmpConnection = \Doctrine\DBAL\DriverManager::getConnection($params); try { $tmpConnection->getSchemaManager()->createDatabase($name); $output->writeln(sprintf('Created database for connection named %s', $name)); } catch (\Exception $e) { $output->writeln(sprintf('Could not create database for connection named %s', $name)); $output->writeln(sprintf('%s', $e->getMessage())); } $tmpConnection->close(); } }