Skip to content

Instantly share code, notes, and snippets.

@kriswallsmith
Created October 7, 2009 18:08
Show Gist options
  • Save kriswallsmith/204275 to your computer and use it in GitHub Desktop.
Save kriswallsmith/204275 to your computer and use it in GitHub Desktop.
<?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.
*/
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
/**
* 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 21089 2009-08-12 08:09:57Z Kris.Wallsmith $
*/
class sfDoctrineMigrateTask extends sfDoctrineBaseTask
{
/**
* @see sfTask
*/
protected function configure()
{
$this->addArguments(array(
new sfCommandArgument('version', sfCommandArgument::OPTIONAL, 'The version to migrate to', null),
));
$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->aliases = array('doctrine-migrate');
$this->namespace = 'doctrine';
$this->name = 'migrate';
$this->briefDescription = 'Migrates database to current/specified version';
$this->detailedDescription = <<<EOF
The [doctrine:migrate|INFO] task migrates database to current/specified version
[./symfony doctrine:migrate|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 (ctype_digit($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->migrate($version, $options['dry-run']);
}
catch (Exception $e)
{
}
if (!$migration->hasErrors())
{
$this->logSection('doctrine', 'Migration complete.');
return;
}
$this->logBlock('Doctrine encountered the following exceptions:', 'ERROR_LARGE');
foreach ($migration->getErrors() as $e)
{
if ($this->commandApplication)
{
$this->commandApplication->renderException($e);
}
else
{
$this->logBlock($e->getMessage(), 'ERROR_LARGE');
}
}
return 1;
}
protected function setupTransactions($method = 'beginTransaction')
{
$doctrineManager = Doctrine_Manager::getInstance();
foreach ($doctrineManager->getConnections() as $conn)
{
$conn->$method();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment