| 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 sfDoctrineMigrateTask extends sfDoctrineBaseTask |
|---|
| 24 |
{ |
|---|
| 25 |
|
|---|
| 26 |
* @see sfTask |
|---|
| 27 |
*/ |
|---|
| 28 |
protected function configure() |
|---|
| 29 |
{ |
|---|
| 30 |
$this->addArguments(array( |
|---|
| 31 |
new sfCommandArgument('version', sfCommandArgument::OPTIONAL, 'The version to migrate to'), |
|---|
| 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('up', null, sfCommandOption::PARAMETER_NONE, 'Migrate up one version'), |
|---|
| 38 |
new sfCommandOption('down', null, sfCommandOption::PARAMETER_NONE, 'Migrate down one version'), |
|---|
| 39 |
new sfCommandOption('dry-run', null, sfCommandOption::PARAMETER_NONE, 'Do not persist migrations'), |
|---|
| 40 |
)); |
|---|
| 41 |
|
|---|
| 42 |
$this->namespace = 'doctrine'; |
|---|
| 43 |
$this->name = 'migrate'; |
|---|
| 44 |
$this->briefDescription = 'Migrates database to current/specified version'; |
|---|
| 45 |
|
|---|
| 46 |
$this->detailedDescription = <<<EOF |
|---|
| 47 |
The [doctrine:migrate|INFO] task migrates the database: |
|---|
| 48 |
|
|---|
| 49 |
[./symfony doctrine:migrate|INFO] |
|---|
| 50 |
|
|---|
| 51 |
Provide a version argument to migrate to a specific version: |
|---|
| 52 |
|
|---|
| 53 |
[./symfony doctrine:migrate 10|INFO] |
|---|
| 54 |
|
|---|
| 55 |
To migration up or down one migration, use the [--up|COMMENT] or [--down|COMMENT] options: |
|---|
| 56 |
|
|---|
| 57 |
[./symfony doctrine:migrate --down|INFO] |
|---|
| 58 |
|
|---|
| 59 |
If your database supports rolling back DDL statements, you can run migrations |
|---|
| 60 |
in dry-run mode using the [--dry-run|COMMENT] option: |
|---|
| 61 |
|
|---|
| 62 |
[./symfony doctrine:migrate --dry-run|INFO] |
|---|
| 63 |
EOF; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* @see sfTask |
|---|
| 68 |
*/ |
|---|
| 69 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 70 |
{ |
|---|
| 71 |
$databaseManager = new sfDatabaseManager($this->configuration); |
|---|
| 72 |
|
|---|
| 73 |
$config = $this->getCliConfig(); |
|---|
| 74 |
$migration = new Doctrine_Migration($config['migrations_path']); |
|---|
| 75 |
$from = $migration->getCurrentVersion(); |
|---|
| 76 |
|
|---|
| 77 |
if (is_numeric($arguments['version'])) |
|---|
| 78 |
{ |
|---|
| 79 |
$version = $arguments['version']; |
|---|
| 80 |
} |
|---|
| 81 |
else if ($options['up']) |
|---|
| 82 |
{ |
|---|
| 83 |
$version = $from + 1; |
|---|
| 84 |
} |
|---|
| 85 |
else if ($options['down']) |
|---|
| 86 |
{ |
|---|
| 87 |
$version = $from - 1; |
|---|
| 88 |
} |
|---|
| 89 |
else |
|---|
| 90 |
{ |
|---|
| 91 |
$version = $migration->getLatestVersion(); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
if ($from == $version) |
|---|
| 95 |
{ |
|---|
| 96 |
$this->logSection('doctrine', sprintf('Already at migration version %s', $version)); |
|---|
| 97 |
return; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
$this->logSection('doctrine', sprintf('Migrating from version %s to %s%s', $from, $version, $options['dry-run'] ? ' (dry run)' : '')); |
|---|
| 101 |
try |
|---|
| 102 |
{ |
|---|
| 103 |
$migration_classes = $migration->getMigrationClasses(); |
|---|
| 104 |
if($version < $from) |
|---|
| 105 |
{ |
|---|
| 106 |
for($i = (int)$from - 1; $i >= (int)$version; $i--) |
|---|
| 107 |
{ |
|---|
| 108 |
$this->logSection('doctrine', 'executing migration : '.$i .', class: '.$migration_classes[$i]); |
|---|
| 109 |
$migration->migrate($i, $options['dry-run']); |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
else |
|---|
| 113 |
{ |
|---|
| 114 |
for($i = (int)$from + 1; $i <= (int)$version; $i++) |
|---|
| 115 |
{ |
|---|
| 116 |
$this->logSection('doctrine', 'executing migration : '.$i.', class: '.$migration_classes[$i]); |
|---|
| 117 |
$migration->migrate($i, $options['dry-run']); |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
catch (Exception $e) |
|---|
| 122 |
{ |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
if ($migration->hasErrors()) |
|---|
| 127 |
{ |
|---|
| 128 |
if ($this->commandApplication && $this->commandApplication->withTrace()) |
|---|
| 129 |
{ |
|---|
| 130 |
$this->logSection('doctrine', 'The following errors occurred:'); |
|---|
| 131 |
foreach ($migration->getErrors() as $error) |
|---|
| 132 |
{ |
|---|
| 133 |
$this->commandApplication->renderException($error); |
|---|
| 134 |
} |
|---|
| 135 |
} |
|---|
| 136 |
else |
|---|
| 137 |
{ |
|---|
| 138 |
$this->logBlock(array_merge( |
|---|
| 139 |
array('The following errors occurred:', ''), |
|---|
| 140 |
array_map(create_function('$e', 'return \' - \'.$e->getMessage();'), $migration->getErrors()) |
|---|
| 141 |
), 'ERROR_LARGE'); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
return 1; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
$this->logSection('doctrine', 'Migration complete'); |
|---|
| 148 |
} |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|