DropDatabaseDoctrineCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle\Command;
  3. use Doctrine\DBAL\DriverManager;
  4. use Exception;
  5. use InvalidArgumentException;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Input\InputOption;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. /**
  10. * Database tool allows you to easily drop your configured databases.
  11. *
  12. * @final
  13. */
  14. class DropDatabaseDoctrineCommand extends DoctrineCommand
  15. {
  16. const RETURN_CODE_NOT_DROP = 1;
  17. const RETURN_CODE_NO_FORCE = 2;
  18. /**
  19. * {@inheritDoc}
  20. */
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('doctrine:database:drop')
  25. ->setDescription('Drops the configured database')
  26. ->addOption('shard', 's', InputOption::VALUE_REQUIRED, 'The shard connection to use for this command')
  27. ->addOption('connection', 'c', InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
  28. ->addOption('if-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database doesn\'t exist')
  29. ->addOption('force', 'f', InputOption::VALUE_NONE, 'Set this parameter to execute this action')
  30. ->setHelp(<<<EOT
  31. The <info>%command.name%</info> command drops the default connections database:
  32. <info>php %command.full_name%</info>
  33. The <info>--force</info> 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 for:
  35. <info>php %command.full_name% --connection=default</info>
  36. <error>Be careful: All data in a given database will be lost when executing this command.</error>
  37. EOT
  38. );
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. protected function execute(InputInterface $input, OutputInterface $output)
  44. {
  45. $connectionName = $input->getOption('connection');
  46. if (empty($connectionName)) {
  47. $connectionName = $this->getDoctrine()->getDefaultConnectionName();
  48. }
  49. $connection = $this->getDoctrineConnection($connectionName);
  50. $ifExists = $input->getOption('if-exists');
  51. $params = $connection->getParams();
  52. if (isset($params['master'])) {
  53. $params = $params['master'];
  54. }
  55. // Since doctrine/dbal 2.11 master has been replaced by primary
  56. if (isset($params['primary'])) {
  57. $params = $params['primary'];
  58. }
  59. if (isset($params['shards'])) {
  60. $shards = $params['shards'];
  61. // Default select global
  62. $params = array_merge($params, $params['global']);
  63. if ($input->getOption('shard')) {
  64. foreach ($shards as $shard) {
  65. if ($shard['id'] === (int) $input->getOption('shard')) {
  66. // Select sharded database
  67. $params = array_merge($params, $shard);
  68. unset($params['id']);
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
  75. if (! $name) {
  76. throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
  77. }
  78. unset($params['dbname'], $params['url']);
  79. if (! $input->getOption('force')) {
  80. $output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.');
  81. $output->writeln('');
  82. $output->writeln(sprintf('<info>Would drop the database <comment>%s</comment> for connection named <comment>%s</comment>.</info>', $name, $connectionName));
  83. $output->writeln('Please run the operation with --force to execute');
  84. $output->writeln('<error>All data will be lost!</error>');
  85. return self::RETURN_CODE_NO_FORCE;
  86. }
  87. // Reopen connection without database name set
  88. // as some vendors do not allow dropping the database connected to.
  89. $connection->close();
  90. $connection = DriverManager::getConnection($params);
  91. $shouldDropDatabase = ! $ifExists || in_array($name, $connection->getSchemaManager()->listDatabases());
  92. // Only quote if we don't have a path
  93. if (! isset($params['path'])) {
  94. $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name);
  95. }
  96. try {
  97. if ($shouldDropDatabase) {
  98. $connection->getSchemaManager()->dropDatabase($name);
  99. $output->writeln(sprintf('<info>Dropped database <comment>%s</comment> for connection named <comment>%s</comment></info>', $name, $connectionName));
  100. } else {
  101. $output->writeln(sprintf('<info>Database <comment>%s</comment> for connection named <comment>%s</comment> doesn\'t exist. Skipped.</info>', $name, $connectionName));
  102. }
  103. return 0;
  104. } catch (Exception $e) {
  105. $output->writeln(sprintf('<error>Could not drop database <comment>%s</comment> for connection named <comment>%s</comment></error>', $name, $connectionName));
  106. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  107. return self::RETURN_CODE_NOT_DROP;
  108. }
  109. }
  110. }