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

Changeset 12257


Ignore:
Timestamp:
Jul 14, 2009, 6:18:54 PM (11 years ago)
Author:
ebihara
Message:

#4101:fixed migration method to work changing columns and old OpenPNE migration scripts

Location:
OpenPNE3/trunk/lib
Files:
3 edited

Legend:

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

    r11526 r12257  
    1919{
    2020  protected
     21    $migrationBase = null,
     22
    2123    $dispatcher = null,
    2224    $dbManager = null,
     
    3436  public function __construct($dispatcher, $dbManager, $targetName = '', $connectionName = '', $params = array())
    3537  {
     38    $this->migrationBase = new opMigrationBase();
     39
    3640    $this->dispatcher = $dispatcher;
    3741    $this->dbManager = $dbManager;
     
    139143  protected function doMigrate($direction)
    140144  {
    141     $method = 'pre'.$direction;
     145    $method = 'pre'.ucfirst($direction);
    142146    $this->$method();
    143147
     
    158162    }
    159163
    160     $method = 'post'.$direction;
     164    $method = 'post'.ucfirst($direction);
    161165    $this->$method();
    162166  }
     
    309313
    310314    return $revision;
     315  }
     316
     317
     318  public function __call($name, $arguments)
     319  {
     320    if (method_exists($this->migrationBase, $name))
     321    {
     322      return call_user_func_array(array($this->migrationBase, $name), $arguments);
     323    }
     324
     325    throw new BadMethodCallException('Undefined method '.get_class($this).'::'.$name.'()');
     326  }
     327
     328  public function __get($name)
     329  {
     330    return $this->migrationBase->getProperty($name);
     331  }
     332
     333  public function __set($name, $value)
     334  {
     335    $this->migrationBase->setProperty($name, $value);
     336  }
     337}
     338
     339class opMigrationBase extends Doctrine_Migration_Base
     340{
     341  public function getProperty($name)
     342  {
     343    return $this->$name;
     344  }
     345
     346  public function setProperty($name, $value)
     347  {
     348    $this->$name = $value;
    311349  }
    312350}
  • OpenPNE3/trunk/lib/migration/opMigrationDiff.class.php

    r12235 r12257  
    4343    return $info;
    4444  }
     45
     46  protected function _diff($from, $to)
     47  {
     48    $fromModels = Doctrine::initializeModels(Doctrine::loadModels($from, Doctrine::MODEL_LOADING_AGGRESSIVE));
     49    $toModels = Doctrine::initializeModels(Doctrine::loadModels($to, Doctrine::MODEL_LOADING_AGGRESSIVE));
     50
     51    // Build schema information for the models
     52    $fromInfo = $this->_buildModelInformation($fromModels);
     53    $toInfo = $this->_buildModelInformation($toModels);
     54
     55    // Build array of changes between the from and to information
     56    $changes = $this->_buildChanges($fromInfo, $toInfo);
     57
     58    $this->_cleanup();
     59
     60    return $changes;
     61  }
    4562}
  • OpenPNE3/trunk/lib/task/openpneGenerateMigrationsTask.class.php

    r12235 r12257  
    3030
    3131    $tmpdir = sfConfig::get('sf_cache_dir').'/models_tmp';
     32    $ymlTmp = $tmpdir.'/yaml';
     33    $modelTmp = $tmpdir.'/model';
     34    $this->getFileSystem()->mkdirs($ymlTmp);
     35    $this->getFileSystem()->mkdirs($modelTmp);
    3236    $migrationsPath = sfConfig::get('sf_data_dir').'/migrations/generated';
    3337
    3438    $config = $this->getCliConfig();
    3539
    36     $this->callDoctrineCli('generate-models-db', array('models_path' => $tmpdir));
     40    $manager = Doctrine_Manager::getInstance();
     41    $oldAttr = $manager->getAttribute(Doctrine::ATTR_MODEL_LOADING);
     42    $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_AGGRESSIVE);
     43
     44    sfSimpleAutoload::unregister();
     45    Doctrine::generateYamlFromDb($ymlTmp.'/from.yml', array(), array('generateBaseClasses' => false));
     46    sfSimpleAutoload::register();
     47    $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, $oldAttr);
     48
     49    $models = sfFinder::type('file')->name('*.php')->in($config['models_path']);
     50    foreach ($models as $model)
     51    {
     52      $dirname = basename(dirname($model));
     53      $filename = basename($model);
     54      if ('base' !== $dirname)
     55      {
     56        continue;
     57      }
     58
     59      $content = file_get_contents($model);
     60      $content = str_replace('abstract class Base', 'class ToPrfx', $content);
     61      $content = str_replace('extends opDoctrineRecord', 'extends Doctrine_Record', $content);
     62
     63      file_put_contents($modelTmp.'/'.str_replace('Base', 'ToPrfx', $filename), $content);
     64    }
    3765
    3866    $migration = new Doctrine_Migration($migrationsPath);
    39     $diff = new opMigrationDiff($tmpdir, $config['models_path'], $migration);
     67    $diff = new opMigrationDiff($ymlTmp.'/from.yml', $modelTmp, $migration);
    4068    $changes = $diff->generateMigrationClasses();
     69
     70    $this->getFileSystem()->remove($ymlTmp);
     71    $this->getFileSystem()->remove($modelTmp);
    4172
    4273    $numChanges = count($changes, true) - count($changes);
Note: See TracChangeset for help on using the changeset viewer.