| 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 |
* Inserts SQL 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: sfDoctrineMigrateTask.class.php 23922 2009-11-14 14:58:38Z fabien $
|
|
|
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->migrate($version, $options['dry-run']);
|
|
|
104 |
}
|
|
|
105 |
catch (Exception $e)
|
|
|
106 |
{
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
// render errors
|
|
|
110 |
if ($migration->hasErrors())
|
|
|
111 |
{
|
|
|
112 |
if ($this->commandApplication && $this->commandApplication->withTrace())
|
|
|
113 |
{
|
|
|
114 |
$this->logSection('doctrine', 'The following errors occurred:');
|
|
|
115 |
foreach ($migration->getErrors() as $error)
|
|
|
116 |
{
|
|
|
117 |
$this->commandApplication->renderException($error);
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
else
|
|
|
121 |
{
|
|
|
122 |
$this->logBlock(array_merge(
|
|
|
123 |
array('The following errors occurred:', ''),
|
|
|
124 |
array_map(create_function('$e', 'return \' - \'.$e->getMessage();'), $migration->getErrors())
|
|
|
125 |
), 'ERROR_LARGE');
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
return 1;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
$this->logSection('doctrine', 'Migration complete');
|
|
|
132 |
}
|
|
|
133 |
}
|