Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 * (c) Jonathan H. Wage <jonwage@gmail.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
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
13
 
14
/**
15
 * Drops database for current model.
16
 *
17
 * @package    symfony
18
 * @subpackage doctrine
19
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20
 * @author     Jonathan H. Wage <jonwage@gmail.com>
21
 * @version    SVN: $Id: sfDoctrineDropDbTask.class.php 24341 2009-11-24 15:01:58Z Kris.Wallsmith $
22
 */
23
class sfDoctrineDropDbTask extends sfDoctrineBaseTask
24
{
25
  /**
26
   * @see sfTask
27
   */
28
  protected function configure()
29
  {
30
    $this->addArguments(array(
31
      new sfCommandArgument('database', sfCommandArgument::OPTIONAL | sfCommandArgument::IS_ARRAY, 'A specific database'),
32
    ));
33
 
34
    $this->addOptions(array(
35
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
36
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
37
      new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Whether to force dropping of the database')
38
    ));
39
 
40
    $this->namespace = 'doctrine';
41
    $this->name = 'drop-db';
42
    $this->briefDescription = 'Drops database for current model';
43
 
44
    $this->detailedDescription = <<<EOF
45
The [doctrine:drop-db|INFO] task drops one or more databases based on
46
configuration in [config/databases.yml|COMMENT]:
47
 
48
  [./symfony doctrine:drop-db|INFO]
49
 
50
You will be prompted for confirmation before any databases are dropped unless
51
you provide the [--no-confirmation|COMMENT] option:
52
 
53
  [./symfony doctrine:drop-db --no-confirmation|INFO]
54
 
55
You can specify what databases to drop by providing their names:
56
 
57
  [./symfony doctrine:drop-db slave1 slave2|INFO]
58
EOF;
59
  }
60
 
61
  /**
62
   * @see sfTask
63
   */
64
  protected function execute($arguments = array(), $options = array())
65
  {
66
    $databaseManager = new sfDatabaseManager($this->configuration);
67
    $databases = $this->getDoctrineDatabases($databaseManager, count($arguments['database']) ? $arguments['database'] : null);
68
 
69
    $environment = $this->configuration instanceof sfApplicationConfiguration ? $this->configuration->getEnvironment() : 'all';
70
 
71
    if (
72
      !$options['no-confirmation']
73
      &&
74
      !$this->askConfirmation(array_merge(
75
        array(sprintf('This command will remove all data in the following "%s" connection(s):', $environment), ''),
76
        array_map(create_function('$v', 'return \' - \'.$v;'), array_keys($databases)),
77
        array('', 'Are you sure you want to proceed? (y/N)')
78
      ), 'QUESTION_LARGE', false)
79
    )
80
    {
81
      $this->logSection('doctrine', 'task aborted');
82
 
83
      return 1;
84
    }
85
 
86
    foreach ($databases as $name => $database)
87
    {
88
      $this->logSection('doctrine', sprintf('Dropping "%s" database', $name));
89
      try
90
      {
91
        $database->getDoctrineConnection()->dropDatabase();
92
      }
93
      catch (Exception $e)
94
      {
95
        $this->logSection('doctrine', $e->getMessage(), null, 'ERROR');
96
      }
97
    }
98
  }
99
}