setDescription('Drop a given table or all tables in the database.') ->addArgument('table', InputArgument::IS_ARRAY, 'Set this parameter to défine which table to delete (default all the table in the database.') ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action.') ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use') ->setHelp(<<propel:table:drop command will drop one or several table. php app/console propel:table:drop The table arguments define the list of table which has to be delete (default: all table). The --force parameter has to be used to actually drop the table. The --connection parameter allows you to change the connection to use. The default connection is the active connection (propel.dbal.default_connection). EOT ) ->setName('propel:table:drop'); } /** * @see Command * * @throws \InvalidArgumentException When the target directory does not exist */ protected function execute(InputInterface $input, OutputInterface $output) { $this->writeSection($output, '[Propel] You are running the command: propel:table:drop'); $tablesToDelete = $input->getArgument('table'); if ($input->getOption('force')) { $nbTable = count($tablesToDelete); $tablePlural = (($nbTable > 1 || $nbTable == 0) ? 's' : '' ); if ('prod' === $this->getApplication()->getKernel()->getEnvironment()) { $count = (count($input->getArgument('table')) ?: '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 { list($name, $config) = $this->getConnection($input, $output); $connection = \Propel::getConnection($name); $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;'); $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 catched', '', $e->getMessage() ), 'fg=white;bg=red'); } } else { $output->writeln('You have to use the "--force" option to drop some tables.'); } } }