Development

/branches/1.4/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineMigrateTask.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineMigrateTask.class.php

Revision 33338, 4.3 kB (checked in by fabien, 3 years ago)

[1.4] added a way to understand why a Doctrine migration fails (closes #8728, patch from rande)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
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     // render errors
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
Note: See TracBrowser for help on using the browser.