Permalink
Browse files

add a custom migration task, to execute migration one-by-one

  • Loading branch information...
1 parent 39f5b32 commit c92c3677b9eaa7e10d2a2b1684aaa653b6f3f81b @rande committed Jul 5, 2010
Showing with 150 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +147 −0 lib/task/swDoctrineMigrateTask.class.php
View
@@ -0,0 +1,3 @@
+.sw_git_migration
+.svn
+.DS_Store
@@ -0,0 +1,147 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ * (c) Jonathan H. Wage <jonwage@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Inserts SQL for current model.
+ *
+ * @package symfony
+ * @subpackage doctrine
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @author Jonathan H. Wage <jonwage@gmail.com>
+ * @version SVN: $Id: sfDoctrineMigrateTask.class.php 23922 2009-11-14 14:58:38Z fabien $
+ */
+class swDoctrineMigrateTask extends sfDoctrineBaseTask
+{
+ /**
+ * @see sfTask
+ */
+ protected function configure()
+ {
+ $this->addArguments(array(
+ new sfCommandArgument('version', sfCommandArgument::OPTIONAL, 'The version to migrate to'),
+ ));
+
+ $this->addOptions(array(
+ new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
+ new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
+ new sfCommandOption('up', null, sfCommandOption::PARAMETER_NONE, 'Migrate up one version'),
+ new sfCommandOption('down', null, sfCommandOption::PARAMETER_NONE, 'Migrate down one version'),
+ new sfCommandOption('dry-run', null, sfCommandOption::PARAMETER_NONE, 'Do not persist migrations'),
+ ));
+
+ $this->namespace = 'sw';
+ $this->name = 'doctrine-migrate';
+ $this->briefDescription = 'Migrates database to current/specified version !!! ONE BY ONE !!!';
+
+ $this->detailedDescription = <<<EOF
+The [doctrine:migrate|INFO] task migrates the database:
+
+ [./symfony doctrine:migrate|INFO]
+
+Provide a version argument to migrate to a specific version:
+
+ [./symfony doctrine:migrate 10|INFO]
+
+To migration up or down one migration, use the [--up|COMMENT] or [--down|COMMENT] options:
+
+ [./symfony doctrine:migrate --down|INFO]
+
+If your database supports rolling back DDL statements, you can run migrations
+in dry-run mode using the [--dry-run|COMMENT] option:
+
+ [./symfony doctrine:migrate --dry-run|INFO]
+EOF;
+ }
+
+ /**
+ * @see sfTask
+ */
+ protected function execute($arguments = array(), $options = array())
+ {
+ $databaseManager = new sfDatabaseManager($this->configuration);
+
+ $config = $this->getCliConfig();
+ $migration = new Doctrine_Migration($config['migrations_path']);
+ $from = $migration->getCurrentVersion();
+
+ if (is_numeric($arguments['version']))
+ {
+ $version = $arguments['version'];
+ }
+ else if ($options['up'])
+ {
+ $version = $from + 1;
+ }
+ else if ($options['down'])
+ {
+ $version = $from - 1;
+ }
+ else
+ {
+ $version = $migration->getLatestVersion();
+ }
+
+ if ($from == $version)
+ {
+ $this->logSection('doctrine', sprintf('Already at migration version %s', $version));
+ return;
+ }
+
+ $this->logSection('doctrine', sprintf('Migrating from version %s to %s%s', $from, $version, $options['dry-run'] ? ' (dry run)' : ''));
+ try
+ {
+ $migration_classes = $migration->getMigrationClasses();
+ if($version < $from)
+ {
+ for($i = (int)$from - 1; $i >= (int)$version; $i--)
+ {
+ $this->logSection('doctrine', 'executing migration : '.$i .', class: '.$migration_classes[$i]);
+ $migration->migrate($i, $options['dry-run']);
+ }
+ }
+ else
+ {
+ for($i = (int)$from + 1; $i <= (int)$version; $i++)
+ {
+ $this->logSection('doctrine', 'executing migration : '.$i.', class: '.$migration_classes[$i]);
+ $migration->migrate($i, $options['dry-run']);
+ }
+ }
+ }
+ catch (Exception $e)
+ {
+ }
+
+ // render errors
+ if ($migration->hasErrors())
+ {
+ if ($this->commandApplication && $this->commandApplication->withTrace())
+ {
+ $this->logSection('doctrine', 'The following errors occurred:');
+ foreach ($migration->getErrors() as $error)
+ {
+ $this->commandApplication->renderException($error);
+ }
+ }
+ else
+ {
+ $this->logBlock(array_merge(
+ array('The following errors occurred:', ''),
+ array_map(create_function('$e', 'return \' - \'.$e->getMessage();'), $migration->getErrors())
+ ), 'ERROR_LARGE');
+ }
+
+ return 1;
+ }
+
+ $this->logSection('doctrine', 'Migration complete');
+ }
+}

0 comments on commit c92c367

Please sign in to comment.