Development

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

You must first sign up to be able to contribute.

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

Revision 23810, 3.8 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • 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->aliases = array('doctrine-migrate');
43     $this->namespace = 'doctrine';
44     $this->name = 'migrate';
45     $this->briefDescription = 'Migrates database to current/specified version';
46
47     $this->detailedDescription = <<<EOF
48 The [doctrine:migrate|INFO] task migrates the database:
49
50   [./symfony doctrine:migrate|INFO]
51
52 Provide a version argument to migrate to a specific version:
53
54   [./symfony doctrine:migrate 10|INFO]
55
56 To migration up or down one migration, use the [--up|COMMENT] or [--down|COMMENT] options:
57
58   [./symfony doctrine:migrate --down|INFO]
59
60 If your database supports rolling back DDL statements, you can run migrations
61 in dry-run mode using the [--dry-run|COMMENT] option:
62
63   [./symfony doctrine:migrate --dry-run|INFO]
64 EOF;
65   }
66
67   /**
68    * @see sfTask
69    */
70   protected function execute($arguments = array(), $options = array())
71   {
72     $databaseManager = new sfDatabaseManager($this->configuration);
73
74     $config = $this->getCliConfig();
75     $migration = new Doctrine_Migration($config['migrations_path']);
76     $from = $migration->getCurrentVersion();
77
78     if (is_numeric($arguments['version']))
79     {
80       $version = $arguments['version'];
81     }
82     else if ($options['up'])
83     {
84       $version = $from + 1;
85     }
86     else if ($options['down'])
87     {
88       $version = $from - 1;
89     }
90     else
91     {
92       $version = $migration->getLatestVersion();
93     }
94
95     if ($from == $version)
96     {
97       $this->logSection('doctrine', sprintf('Already at migration version %s', $version));
98       return;
99     }
100
101     $this->logSection('doctrine', sprintf('Migrating from version %s to %s%s', $from, $version, $options['dry-run'] ? ' (dry run)' : ''));
102     try
103     {
104       $migration->migrate($version, $options['dry-run']);
105     }
106     catch (Exception $e)
107     {
108     }
109
110     // render errors
111     if ($migration->hasErrors())
112     {
113       if ($this->commandApplication && $this->commandApplication->withTrace())
114       {
115         $this->logSection('doctrine', 'The following errors occurred:');
116         foreach ($migration->getErrors() as $error)
117         {
118           $this->commandApplication->renderException($error);
119         }
120       }
121       else
122       {
123         $this->logBlock(array_merge(
124           array('The following errors occurred:', ''),
125           array_map(create_function('$e', 'return \' - \'.$e->getMessage();'), $migration->getErrors())
126         ), 'ERROR_LARGE');
127       }
128
129       return 1;
130     }
131
132     $this->logSection('doctrine', 'Migration complete');
133   }
134 }
135
Note: See TracBrowser for help on using the browser.