From 53db41026b757034bfb050a1c27abd985b4d3b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Mon, 4 Nov 2013 21:29:16 +0000 Subject: [PATCH] Implemented the table drop command --- Command/AbstractCommand.php | 30 ++++++++ Command/TableDropCommand.php | 129 +++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 Command/TableDropCommand.php diff --git a/Command/AbstractCommand.php b/Command/AbstractCommand.php index f22c8ec..3442094 100644 --- a/Command/AbstractCommand.php +++ b/Command/AbstractCommand.php @@ -380,6 +380,36 @@ EOT; return $this->cacheDir; } + + /** + * Comes from the SensioGeneratorBundle. + * @see https://github.com/sensio/SensioGeneratorBundle/blob/master/Command/Helper/DialogHelper.php#L52 + * + * @param OutputInterface $output The output. + * @param string $text A text message. + * @param string $style A style to apply on the section. + */ + protected function writeSection(OutputInterface $output, $text, $style = 'bg=blue;fg=white') + { + $output->writeln(array( + '', + $this->getHelperSet()->get('formatter')->formatBlock($text, $style, true), + '', + )); + } + + /** + * Ask confirmation from the user. + * + * @param OutputInterface $output The output. + * @param string $question A given question. + * @param string $default A default response. + */ + protected function askConfirmation(OutputInterface $output, $question, $default = null) + { + return $this->getHelperSet()->get('dialog')->askConfirmation($output, $question, $default); + } + protected function arrayToIni(array $data) { $lines = array(); diff --git a/Command/TableDropCommand.php b/Command/TableDropCommand.php new file mode 100644 index 0000000..916616a --- /dev/null +++ b/Command/TableDropCommand.php @@ -0,0 +1,129 @@ + + */ +class TableDropCommand extends AbstractCommand +{ + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('propel:table:drop') + ->setDescription('Drop a given table or all tables in the database.') + + ->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.') + ->addArgument('table', InputArgument::IS_ARRAY, 'Set this parameter to défine which table to delete (default all the table in the database.') + ; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + if (!$input->getOption('force')) { + $output->writeln('You have to use the "--force" option to drop some tables.'); + return; + } + + $tablesToDelete = $input->getArgument('table'); + $nbTable = count($tablesToDelete); + $tablePlural = (($nbTable > 1 || $nbTable == 0) ? 's' : '' ); + + if ('prod' === $this->getApplication()->getKernel()->getEnvironment()) { + $count = $nbTable ?: 'all'; + + $this->writeSection( + $output, + 'WARNING: you are about to drop ' . $count . ' table' . $tablePlural . ' in production !', + 'bg=red;fg=white' + ); + + if (false === $this->askConfirmation($output, 'Are you sure ? (y/n) ', false)) { + $output->writeln('Aborted, nice decision !'); + + return -2; + } + } + + try { + $connection = Propel::getConnection($input->getOption('connection')); + $adapter = Propel::getAdapter($connection->getName()); + + $showStatement = $connection->prepare('SHOW TABLES;'); + $showStatement->execute(); + + $allTables = $showStatement->fetchAll(\PDO::FETCH_COLUMN); + + if ($nbTable) { + foreach ($tablesToDelete as $tableToDelete) { + if (!array_search($tableToDelete, $allTables)) { + throw new \InvalidArgumentException(sprintf('Table %s doesn\'t exist in the database.', $tableToDelete)); + } + } + } else { + $tablesToDelete = $allTables; + } + + $connection->exec('SET FOREIGN_KEY_CHECKS = 0;'); + + array_walk($tablesToDelete, function(&$table, $key, $dbAdapter) { + $table = $dbAdapter->quoteIdentifierTable($table); + }, $adapter); + + $tablesToDelete = join(', ', $tablesToDelete); + + if ('' !== $tablesToDelete) { + $connection->exec('DROP TABLE ' . $tablesToDelete . ' ;'); + + $output->writeln(sprintf('Table' . $tablePlural . ' %s has been dropped.', $tablesToDelete)); + } else { + $output->writeln('No tables have been dropped'); + } + + $connection->exec('SET FOREIGN_KEY_CHECKS = 1;'); + } catch (\Exception $e) { + $this->writeSection($output, array( + '[Propel] Exception caught', + '', + $e->getMessage() + ), 'fg=white;bg=red'); + } + } + + /** + * {@inheritdoc} + */ + protected function createSubCommandInstance() + { + // useless for this command + } + + /** + * {@inheritdoc} + */ + protected function getSubCommandArguments(InputInterface $input) + { + // useless for this command + } +}