From a5cd6b86b6ecbcc123e21713ea2f7659c4f3a552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Mon, 4 Nov 2013 21:41:59 +0000 Subject: [PATCH] Implemented the DatabaseDrop command --- Command/AbstractCommand.php | 18 +++++ Command/DatabaseDropCommand.php | 113 ++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 Command/DatabaseDropCommand.php diff --git a/Command/AbstractCommand.php b/Command/AbstractCommand.php index 3fb8057..e2623e4 100644 --- a/Command/AbstractCommand.php +++ b/Command/AbstractCommand.php @@ -394,4 +394,22 @@ EOT; return implode(PHP_EOL, $lines); } + + /** + * Extract the database name from a given DSN + * + * @param string $dsn A DSN + * @return string The database name extracted from the given DSN + */ + protected function parseDbName($dsn) + { + preg_match('#dbname=([a-zA-Z0-9\_]+)#', $dsn, $matches); + + if (isset($matches[1])) { + return $matches[1]; + } + + // e.g. SQLite + return null; + } } diff --git a/Command/DatabaseDropCommand.php b/Command/DatabaseDropCommand.php new file mode 100644 index 0000000..a20ecf9 --- /dev/null +++ b/Command/DatabaseDropCommand.php @@ -0,0 +1,113 @@ +setName('propel:database:drop') + ->setDescription('Drop a given database or the default one.') + ->setHelp(<<propel:database:drop command will drop your database. + + php app/console propel:database:drop + +The --force parameter has to be used to actually drop the database. +The --connection parameter allows you to change the connection to use. +The default connection is the active connection (propel.dbal.default_connection). +EOT + ) + + ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Connection to use. Example: default, bookstore') + ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action.') + ; + } + + /** + * @see Command + * + * @throws \InvalidArgumentException When the target directory does not exist + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + if (!$input->getOption('force')) { + $output->writeln('You have to use the "--force" option to drop the database.'); + return; + } + + if ('prod' === $this->getApplication()->getKernel()->getEnvironment()) { + $this->writeSection($output, 'WARNING: you are about to drop a database in production !', 'bg=red;fg=white'); + + if (false === $this->askConfirmation($output, 'Are you sure ? (y/n) ', false)) { + $output->writeln('Aborted, nice decision !'); + + return -2; + } + } + + $connectionName = $input->getOption('connection') ?: $this->getContainer()->getParameter('propel.dbal.default_connection'); + $config = $this->getConnectionData($connectionName); + $connection = Propel::getConnection($connectionName); + $dbName = $this->parseDbName($config['connection']['dsn']); + + if (null === $dbName) { + return $output->writeln('No database name found.'); + } else { + $query = 'DROP DATABASE '. $dbName .';'; + } + + try { + $statement = $connection->prepare($query); + $statement->execute(); + + $output->writeln(sprintf('Database %s has been dropped.', $dbName)); + } catch (\Exception $e) { + $this->writeSection($output, array( + '[Propel] Exception caught', + '', + $e->getMessage() + ), 'fg=white;bg=red'); + } + } + + /** + * {@inheritdoc} + */ + protected function createSubCommandInstance() + { + // useless here + } + + /** + * {@inheritdoc} + */ + protected function getSubCommandArguments(InputInterface $input) + { + // useless here + } +}