vendor/symfony/src/Symfony/Bundle/DoctrineBundle/Command/DropDatabaseDoctrineCommand.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\DoctrineBundle\Command;
       
    13 
       
    14 use Symfony\Component\Console\Input\InputOption;
       
    15 use Symfony\Component\Console\Input\InputInterface;
       
    16 use Symfony\Component\Console\Output\OutputInterface;
       
    17 
       
    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 DropDatabaseDoctrineCommand extends DoctrineCommand
       
    25 {
       
    26     protected function configure()
       
    27     {
       
    28         $this
       
    29             ->setName('doctrine:database:drop')
       
    30             ->setDescription('Drop the configured databases')
       
    31             ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
       
    32             ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action')
       
    33             ->setHelp(<<<EOT
       
    34 The <info>doctrine:database:drop</info> command drops the default connections
       
    35 database:
       
    36 
       
    37 <info>php app/console doctrine:database:drop</info>
       
    38 
       
    39 The --force parameter has to be used to actually drop the database.
       
    40 
       
    41 You can also optionally specify the name of a connection to drop the database
       
    42 for:
       
    43 
       
    44 <info>php app/console doctrine:database:drop --connection=default</info>
       
    45 
       
    46 <error>Be careful: All data in a given database will be lost when executing
       
    47 this command.</error>
       
    48 EOT
       
    49         );
       
    50     }
       
    51 
       
    52     protected function execute(InputInterface $input, OutputInterface $output)
       
    53     {
       
    54         $connection = $this->getDoctrineConnection($input->getOption('connection'));
       
    55 
       
    56         $params = $connection->getParams();
       
    57 
       
    58         $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
       
    59 
       
    60         if (!$name) {
       
    61             throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
       
    62         }
       
    63 
       
    64         if ($input->getOption('force')) {
       
    65             try {
       
    66                 $connection->getSchemaManager()->dropDatabase($name);
       
    67                 $output->writeln(sprintf('<info>Dropped database for connection named <comment>%s</comment></info>', $name));
       
    68             } catch (\Exception $e) {
       
    69                 $output->writeln(sprintf('<error>Could not drop database for connection named <comment>%s</comment></error>', $name));
       
    70                 $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
       
    71             }
       
    72         } else {
       
    73             $output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.');
       
    74             $output->writeln('');
       
    75             $output->writeln(sprintf('<info>Would drop the database named <comment>%s</comment>.</info>', $name));
       
    76             $output->writeln('Please run the operation with --force to execute');
       
    77             $output->writeln('<error>All data will be lost!</error>');
       
    78         }
       
    79     }
       
    80 }