ここの情報は古いです。ご理解頂いた上でお取り扱いください。

Changeset 10737


Ignore:
Timestamp:
Feb 10, 2009, 6:37:46 PM (13 years ago)
Author:
ebihara
Message:

#3502:added the openpne:migrate task

Location:
OpenPNE3/trunk
Files:
1 added
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • OpenPNE3/trunk/lib/migration/opMigration.class.php

    r10732 r10737  
    1616 * @author     Kousuke Ebihara <ebihara@tejimaya.com>
    1717 */
    18 abstract class opMigration extends Doctrine_Migration
     18class opMigration extends Doctrine_Migration
    1919{
    2020  protected $targetName = '';
     
    3030    {
    3131      $names = $this->dbManager->getNames();
    32       $connectionName = array_shift($connectionName);
     32      $connectionName = array_shift($names);
    3333    }
    3434    $this->database = $this->dbManager->getDatabase($connectionName);
     
    9090    $this->$method();
    9191  }
     92
     93 /**
     94  * @see Doctrine_Migration
     95  */
     96  protected function createMigrationTable()
     97  {
     98    // do nothing
     99  }
     100
     101 /**
     102  * @see Doctrine_Migration
     103  */
     104  protected function setCurrentVersion($number)
     105  {
     106    SnsConfigPeer::set($this->targetName.'_revision', $number);
     107  }
     108
     109 /**
     110  * @see Doctrine_Migration
     111  */
     112  public function getCurrentVersion()
     113  {
     114    return SnsConfigPeer::get($this->targetName.'_revision', 0);
     115  }
     116
     117 /**
     118  * @see Doctrine_Migration
     119  */
     120  public function hasMigrated()
     121  {
     122    return is_null(SnsConfigPeer::get($this->targetName.'_revision'));
     123  }
     124
     125 /**
     126  * @see Doctrine_Migration
     127  */
     128  public function loadMigrationClassesFromDirectory()
     129  {
     130    $classes = get_declared_classes();
     131
     132    foreach ((array)$this->_migrationClassesDirectory as $dir)
     133    {
     134      $files = sfFinder::type('file')->name('*.php')->in($dir);
     135      $iterator = new CompareMigrateDirectoryVersionFilterIterator($files, str_replace('-dev', '', OPENPNE_VERSION));
     136      foreach ($iterator as $file)
     137      {
     138        if (!in_array(basename($file), $this->_loadedMigrations))
     139        {
     140          require_once $file;
     141
     142          $requiredClass = array_diff(get_declared_classes(), $classes);
     143          $requiredClass = end($requiredClass);
     144          if ($requiredClass)
     145          {
     146            $this->_loadedMigrations[$requiredClass] = basename($file);
     147          }
     148        }
     149      }
     150    }
     151  }
    92152}
     153
     154class CompareMigrateDirectoryVersionFilterIterator extends FilterIterator
     155{
     156  protected $limitVersion;
     157
     158  public function __construct(array $fileList, $limitVersion)
     159  {
     160    $this->limitVersion = $limitVersion;
     161
     162    return parent::__construct(new ArrayIterator($fileList));
     163  }
     164
     165  public function accept()
     166  {
     167    $version = basename(dirname($this->current()));
     168    return version_compare($version, $this->limitVersion, '<=');
     169  }
     170}
  • OpenPNE3/trunk/lib/task/openpneMigrateTask.class.php

    r10734 r10737  
    99 */
    1010
    11 class openpneUpdateTask extends sfPropelBaseTask
     11class openpneMigrateTask extends sfPropelBaseTask
    1212{
    1313  protected function configure()
    1414  {
    1515    $this->namespace        = 'openpne';
    16     $this->name             = 'update';
     16    $this->name             = 'migrate';
    1717
    1818    require sfConfig::get('sf_data_dir').'/version.php';
     
    2020    $this->addArguments(array(
    2121      new sfCommandArgument('name', sfCommandArgument::OPTIONAL, 'The plugin name or "OpenPNE"', 'OpenPNE'),
    22       new sfCommandArgument('before-version', sfCommandArgument::OPTIONAL, '', '3.0.0'),
    23       new sfCommandArgument('after-version', sfCommandArgument::OPTIONAL, '', OPENPNE_VERSION),
    2422    ));
    2523
    2624    $this->addOptions(array(
     25      new sfCommandOption('openpne-version', 'v', sfCommandOption::PARAMETER_OPTIONAL, 'To version', OPENPNE_VERSION),
     26      new sfCommandOption('revision', 'r', sfCommandOption::PARAMETER_OPTIONAL, 'To revision'),
    2727      new sfCommandOption('no-build-model', null, sfCommandOption::PARAMETER_NONE, 'Do not build model classes'),
    2828    ));
     
    3030    $this->briefDescription = 'update OpenPNE';
    3131    $this->detailedDescription = <<<EOF
    32 The [openpne:update|INFO] task updates OpenPNE and/or plugin.
     32The [openpne:migrade|INFO] task upgrades or downgrades OpenPNE and/or plugin.
    3333Call it with:
    3434
    35   [./symfony openpne:update|INFO]
     35  [./symfony openpne:migrade|INFO]
    3636EOF;
    3737  }
     
    4040  {
    4141    $databaseManager = new sfDatabaseManager($this->configuration);
    42 
    43     $dir = sfConfig::get('sf_lib_dir').'/update';
    44     $prefix = 'op';
    45     if ('OpenPNE' !== $arguments['name'])
    46     {
    47       $dir = sfConfig::get('sf_plugins_dir').'/'.$arguments['name'].'/lib/update';
    48       $prefix = $arguments['name'];
    49     }
    50 
    51     $beforeVersion = self::formatVersion($arguments['before-version']);
    52     $afterVersion = self::formatVersion($arguments['after-version']);
    53 
    54     $files = sfFinder::type('file')->not_name('opUpdate.class.php')->not_name('opUpdateDoctrineMigrationProcess.class.php')->in($dir);
    55     $files = array_map(create_function('$path', 'return basename($path);'), $files);
    56     usort($files, array(get_class($this), 'sortBeforeVersions'));
    57     foreach ($files as $file)
    58     {
    59       $versions = explode('_to_', str_replace('.php', '', $file), 2);
    60       $_before = self::formatVersion($versions[0]);
    61       $_after = self::formatVersion($versions[1]);
    62 
    63       if (version_compare($beforeVersion, $_after, '<=') && version_compare($afterVersion, $_after, '>='))
    64       {
    65         $className = $prefix.'Update_'.str_replace('.php', '', $file);
    66         $this->logSection('execute', $className);
    67         require_once $dir.'/'.$file;
    68         $obj = new $className($this->dispatcher, $databaseManager);
    69         $obj->doUpdate();
    70       }
    71     }
     42    $migration = new opMigration($this->dispatcher, $databaseManager);
     43    $migration->migrate();
    7244
    7345    if (!$options['no-build-model'])
     
    7547      $this->buildModel();
    7648    }
    77   }
    78 
    79   static public function sortBeforeVersions($name1, $name2)
    80   {
    81     $_versions = explode('_to_', str_replace('.php', '', $name1), 2);
    82     $version1 = array_shift($_versions);
    83     $_versions = explode('_to_', str_replace('.php', '', $name2), 2);
    84     $version2 = array_shift($_versions);
    85 
    86     return version_compare(self::formatVersion($version1), self::formatVersion($version2));
    87   }
    88 
    89   static public function formatVersion($version)
    90   {
    91     $_searches = array('-', '_', '+');
    92     $version = str_replace($_searches, '.', $version);
    93 
    94     $pos = strpos($version, '.dev.');
    95     if (false !== $pos)
    96     {
    97       $time = substr($version, $pos + strlen('.dev.'));
    98       $datetime = new DateTime($time);
    99       $version = substr($version, 0, $pos + strlen('.dev.')).$datetime->format('U');
    100     }
    101 
    102     return $version;
    10349  }
    10450
Note: See TracChangeset for help on using the changeset viewer.