CreateDatabaseDoctrineCommand.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. use Doctrine\DBAL\DriverManager;
  18. /**
  19. * Database tool allows you to easily drop and create your configured databases.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Jonathan H. Wage <jonwage@gmail.com>
  23. */
  24. class CreateDatabaseDoctrineCommand extends DoctrineCommand
  25. {
  26. /**
  27. * {@inheritDoc}
  28. */
  29. protected function configure()
  30. {
  31. $this
  32. ->setName('doctrine:database:create')
  33. ->setDescription('Creates the configured databases')
  34. ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
  35. ->setHelp(<<<EOT
  36. The <info>doctrine:database:create</info> command creates the default
  37. connections database:
  38. <info>php app/console doctrine:database:create</info>
  39. You can also optionally specify the name of a connection to create the
  40. database for:
  41. <info>php app/console doctrine:database:create --connection=default</info>
  42. EOT
  43. );
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. protected function execute(InputInterface $input, OutputInterface $output)
  49. {
  50. $connection = $this->getDoctrineConnection($input->getOption('connection'));
  51. $params = $connection->getParams();
  52. $name = isset($params['path']) ? $params['path'] : $params['dbname'];
  53. unset($params['dbname']);
  54. $tmpConnection = DriverManager::getConnection($params);
  55. // Only quote if we don't have a path
  56. if (!isset($params['path'])) {
  57. $name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name);
  58. }
  59. $error = false;
  60. try {
  61. $tmpConnection->getSchemaManager()->createDatabase($name);
  62. $output->writeln(sprintf('<info>Created database for connection named <comment>%s</comment></info>', $name));
  63. } catch (\Exception $e) {
  64. $output->writeln(sprintf('<error>Could not create database for connection named <comment>%s</comment></error>', $name));
  65. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  66. $error = true;
  67. }
  68. $tmpConnection->close();
  69. return $error ? 1 : 0;
  70. }
  71. }