| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php'); |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 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->aliases = array('doctrine-drop-db'); |
|---|
| 41 |
$this->namespace = 'doctrine'; |
|---|
| 42 |
$this->name = 'drop-db'; |
|---|
| 43 |
$this->briefDescription = 'Drops database for current model'; |
|---|
| 44 |
|
|---|
| 45 |
$this->detailedDescription = <<<EOF |
|---|
| 46 |
The [doctrine:drop-db|INFO] task drops one or more databases based on |
|---|
| 47 |
configuration in [config/databases.yml|COMMENT]: |
|---|
| 48 |
|
|---|
| 49 |
[./symfony doctrine:drop-db|INFO] |
|---|
| 50 |
|
|---|
| 51 |
You will be prompted for confirmation before any databases are dropped unless |
|---|
| 52 |
you provide the [--no-confirmation|COMMENT] option: |
|---|
| 53 |
|
|---|
| 54 |
[./symfony doctrine:drop-db --no-confirmation|INFO] |
|---|
| 55 |
|
|---|
| 56 |
You can specify what databases to drop by providing their names: |
|---|
| 57 |
|
|---|
| 58 |
[./symfony doctrine:drop-db slave1 slave2|INFO] |
|---|
| 59 |
EOF; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
* @see sfTask |
|---|
| 64 |
*/ |
|---|
| 65 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 66 |
{ |
|---|
| 67 |
$databaseManager = new sfDatabaseManager($this->configuration); |
|---|
| 68 |
$databases = $this->getDoctrineDatabases($databaseManager, count($arguments['database']) ? $arguments['database'] : null); |
|---|
| 69 |
|
|---|
| 70 |
$environment = $this->configuration instanceof sfApplicationConfiguration ? $this->configuration->getEnvironment() : 'all'; |
|---|
| 71 |
|
|---|
| 72 |
if ( |
|---|
| 73 |
!$options['no-confirmation'] |
|---|
| 74 |
&& |
|---|
| 75 |
!$this->askConfirmation(array_merge( |
|---|
| 76 |
array(sprintf('This command will remove all data in the following "%s" connection(s):', $environment), ''), |
|---|
| 77 |
array_map(create_function('$v', 'return \' - \'.$v;'), array_keys($databases)), |
|---|
| 78 |
array('', 'Are you sure you want to proceed? (y/N)') |
|---|
| 79 |
), 'QUESTION_LARGE', false) |
|---|
| 80 |
) |
|---|
| 81 |
{ |
|---|
| 82 |
$this->logSection('doctrine', 'task aborted'); |
|---|
| 83 |
|
|---|
| 84 |
return 1; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
foreach ($databases as $name => $database) |
|---|
| 88 |
{ |
|---|
| 89 |
$this->logSection('doctrine', sprintf('Dropping "%s" database', $name)); |
|---|
| 90 |
try |
|---|
| 91 |
{ |
|---|
| 92 |
$database->getDoctrineConnection()->dropDatabase(); |
|---|
| 93 |
} |
|---|
| 94 |
catch (Exception $e) |
|---|
| 95 |
{ |
|---|
| 96 |
$this->logSection('doctrine', $e->getMessage(), null, 'ERROR'); |
|---|
| 97 |
} |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|