DatabaseToolDoctrineCommand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Symfony\Framework\DoctrineBundle\Command;
  3. use Symfony\Components\Console\Input\InputArgument;
  4. use Symfony\Components\Console\Input\InputOption;
  5. use Symfony\Components\Console\Input\InputInterface;
  6. use Symfony\Components\Console\Output\OutputInterface;
  7. use Symfony\Components\Console\Output\Output;
  8. use Symfony\Framework\WebBundle\Util\Filesystem;
  9. use Doctrine\Common\Cli\Configuration;
  10. use Doctrine\Common\Cli\CliController as DoctrineCliController;
  11. use Doctrine\DBAL\Connection;
  12. /*
  13. * This file is part of the symfony framework.
  14. *
  15. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  16. *
  17. * This source file is subject to the MIT license that is bundled
  18. * with this source code in the file LICENSE.
  19. */
  20. /**
  21. * Database tool allows you to easily drop and create your configured databases.
  22. *
  23. * @package symfony
  24. * @subpackage console
  25. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  26. * @author Jonathan H. Wage <jonwage@gmail.com>
  27. */
  28. class DatabaseToolDoctrineCommand extends DoctrineCommand
  29. {
  30. /**
  31. * @see Command
  32. */
  33. protected function configure()
  34. {
  35. $this
  36. ->setName('doctrine:database-tool')
  37. ->setDescription('Create and drop the configured databases.')
  38. ->addOption('re-create', null, null, 'Drop and re-create your databases.')
  39. ->addOption('drop', null, null, 'Drop your databases.')
  40. ->addOption('create', null, null, 'Create your databases.')
  41. ->addOption('connection', null, null, 'The connection name to work on.')
  42. ;
  43. }
  44. /**
  45. * @see Command
  46. */
  47. protected function execute(InputInterface $input, OutputInterface $output)
  48. {
  49. if ($input->getOption('re-create'))
  50. {
  51. $input->setOption('drop', true);
  52. $input->setOption('create', true);
  53. }
  54. if (!$input->getOption('drop') && !$input->getOption('create'))
  55. {
  56. throw new \InvalidArgumentException('You must specify one of the --drop and --create options or both.');
  57. }
  58. $found = false;
  59. $connections = $this->getDoctrineConnections();
  60. foreach ($connections as $name => $connection)
  61. {
  62. if ($input->getOption('connection') && $name != $input->getOption('connection'))
  63. {
  64. continue;
  65. }
  66. if ($input->getOption('drop'))
  67. {
  68. $this->dropDatabaseForConnection($connection, $output);
  69. }
  70. if ($input->getOption('create'))
  71. {
  72. $this->createDatabaseForConnection($connection, $output);
  73. }
  74. $found = true;
  75. }
  76. if ($found === false)
  77. {
  78. if ($input->getOption('connection'))
  79. {
  80. throw new \InvalidArgumentException(sprintf('<error>Could not find a connection named <comment>%s</comment></error>', $input->getOption('connection')));
  81. }
  82. else
  83. {
  84. throw new \InvalidArgumentException(sprintf('<error>Could not find any configured connections</error>', $input->getOption('connection')));
  85. }
  86. }
  87. }
  88. protected function dropDatabaseForConnection(Connection $connection, OutputInterface $output)
  89. {
  90. $params = $connection->getParams();
  91. $name = isset($params['path']) ? $params['path']:$params['dbname'];
  92. try {
  93. $connection->getSchemaManager()->dropDatabase($name);
  94. $output->writeln(sprintf('<info>Dropped database for connection named <comment>%s</comment></info>', $name));
  95. } catch (\Exception $e) {
  96. $output->writeln(sprintf('<error>Could not drop database for connection named <comment>%s</comment></error>', $name));
  97. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  98. }
  99. }
  100. protected function createDatabaseForConnection(Connection $connection, OutputInterface $output)
  101. {
  102. $params = $connection->getParams();
  103. $name = isset($params['path']) ? $params['path']:$params['dbname'];
  104. unset($params['dbname']);
  105. $tmpConnection = \Doctrine\DBAL\DriverManager::getConnection($params);
  106. try {
  107. $tmpConnection->getSchemaManager()->createDatabase($name);
  108. $output->writeln(sprintf('<info>Created database for connection named <comment>%s</comment></info>', $name));
  109. } catch (\Exception $e) {
  110. $output->writeln(sprintf('<error>Could not create database for connection named <comment>%s</comment></error>', $name));
  111. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  112. }
  113. $tmpConnection->close();
  114. }
  115. }