DropDatabaseDoctrineCommand.php 3.6KB

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