DropDatabaseDoctrineCommand.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\DoctrineBundle\Command;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. * Database tool allows you to easily drop and create your configured databases.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Jonathan H. Wage <jonwage@gmail.com>
  19. */
  20. class DropDatabaseDoctrineCommand extends DoctrineCommand
  21. {
  22. protected function configure()
  23. {
  24. $this
  25. ->setName('doctrine:database:drop')
  26. ->setDescription('Drops the configured databases')
  27. ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
  28. ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action')
  29. ->setHelp(<<<EOT
  30. The <info>doctrine:database:drop</info> command drops the default connections
  31. database:
  32. <info>php app/console doctrine:database:drop</info>
  33. The --force parameter has to be used to actually drop the database.
  34. You can also optionally specify the name of a connection to drop the database
  35. for:
  36. <info>php app/console doctrine:database:drop --connection=default</info>
  37. <error>Be careful: All data in a given database will be lost when executing
  38. this command.</error>
  39. EOT
  40. );
  41. }
  42. protected function execute(InputInterface $input, OutputInterface $output)
  43. {
  44. $connection = $this->getDoctrineConnection($input->getOption('connection'));
  45. $params = $connection->getParams();
  46. $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
  47. if (!$name) {
  48. throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
  49. }
  50. if ($input->getOption('force')) {
  51. try {
  52. $connection->getSchemaManager()->dropDatabase($name);
  53. $output->writeln(sprintf('<info>Dropped database for connection named <comment>%s</comment></info>', $name));
  54. } catch (\Exception $e) {
  55. $output->writeln(sprintf('<error>Could not drop database for connection named <comment>%s</comment></error>', $name));
  56. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  57. }
  58. } else {
  59. $output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.');
  60. $output->writeln('');
  61. $output->writeln(sprintf('<info>Would drop the database named <comment>%s</comment>.</info>', $name));
  62. $output->writeln('Please run the operation with --force to execute');
  63. $output->writeln('<error>All data will be lost!</error>');
  64. }
  65. }
  66. }