DropDatabaseDoctrineCommand.php 5.5 KB

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