Author: david
Date: Fri Jun 24 16:18:25 2011
New Revision: 9171

Log:
Fix migration task to allow true fine grained migrations (within release).  
Refactor factory method for returning QubitMigrate objects.

Added:
   trunk/lib/task/migrate/QubitMigrateFactory.class.php   (contents, props 
changed)
      - copied, changed from r9169, 
trunk/lib/task/migrate/QubitMigrator.class.php
Deleted:
   trunk/lib/task/migrate/QubitMigrator.class.php
Modified:
   trunk/lib/task/migrate/MigrateTask.class.php
   trunk/lib/task/migrate/QubitMigrate.class.php
   trunk/lib/task/migrate/QubitMigrate108.class.php
   trunk/lib/task/migrate/QubitMigrate109.class.php

Modified: trunk/lib/task/migrate/MigrateTask.class.php
==============================================================================
--- trunk/lib/task/migrate/MigrateTask.class.php        Fri Jun 24 08:55:41 
2011        (r9170)
+++ trunk/lib/task/migrate/MigrateTask.class.php        Fri Jun 24 16:18:25 
2011        (r9171)
@@ -25,20 +25,21 @@
  * @version    svn: $Id$
  * @author     David Juhasz <[email protected]>
  */
-class UpgradeTask extends sfBaseTask
+class MigrateTask extends sfBaseTask
 {
-  // list of migratable versions
-  protected $validVersions = array(
-    '1.0.3',
-    '1.0.4',
-    '1.0.5',
-    '1.0.6',
-    '1.0.7',
-    '1.0.8',
-    '1.0.9',
-    '1.1',
-    '1.2'
-  );
+  protected
+    $data,
+    $dataModified = false,
+    $initialVersion,
+    $targetVersion,
+    // list of migratable versions
+    $validVersions = array(
+      '1.0.3',
+      '1.0.4',
+      '1.0.5',
+      '1.0.6',
+      '1.0.7'
+    );
 
   /**
    * @see sfBaseTask
@@ -76,30 +77,60 @@
     // Get target application version for data migration
     if (isset($options['target-version']))
     {
-      if (!in_array($options['target-version'], $this->validVersions))
+      if (!preg_match('/^\d+$/', $options['target-version']) || 
!in_array($options['target-version'], $this->validVersions))
       {
         throw new Exception('Invalid target version 
"'.$options['target-version'].'".');
       }
 
       $this->targetVersion = $options['target-version'];
     }
-    else
-    {
-      // Set final version to last value in migration version array
-      $this->targetVersion = end($this->validVersions);
-    }
 
-    // load yml dumpfile into an array ($data)
+    // load yml dumpfile into an array ($this->data)
     $yamlParser = new sfYamlParser;
-    $data = $yamlParser->parse(file_get_contents($arguments['datafile']));
+    $this->data = 
$yamlParser->parse(file_get_contents($arguments['datafile']));
 
     // Determine current version of the application (according to settings)
-    if (null !== $this->initialVersion = $this->getDataVersion($data))
+    if (null !== $this->initialVersion = $this->getDataVersion())
     {
       $this->logSection('migrate', 'Initial data version 
'.$this->initialVersion);
     }
 
-    $migrator = new QubitMigrator($data);
+    // At version 1.0.8 we switched from versioning by release to fine-grained
+    // versions (integer)
+    var_dump($this->initialVersion, $this->targetVersion);
+    if (preg_match('/^\d+$/', $this->initialVersion))
+    {
+      $this->migrateFineGrained();
+    }
+    else
+    {
+      $this->migratePre108();
+    
+      if (null == $this->targetVerion || preg_match('/^\d+$/', 
$this->targetVersion))
+      {
+        $this->migrateFineGrained();
+      }
+    }
+
+    // write new data.yml file (if data was modified)
+    if ($this->dataModified)
+    {
+      $this->writeMigratedData($arguments['datafile']);
+    }
+    else
+    {
+      $this->logSection('migrate', 'The specified data file is up-to-date, no 
migration done.');
+    }
+  }
+
+  protected function migratePre108()
+  {
+    // If target version is not set, then use last valid version (1.0.7) 
+    $targetVersion = $this->targetVersion;
+    if (null == $targetVersion)
+    {
+      $targetVersion = '1.0.7';
+    }
 
     // Incrementally call the upgrade task for each intervening version from
     // initial version to the target version
@@ -109,69 +140,74 @@
       $initialIndex = count($this->validVersions) - 2;
     }
 
-    $finalIndex = array_search($this->targetVersion, $this->validVersions);
+    $finalIndex = array_search($targetVersion, $this->validVersions);
 
     if ($initialIndex !== false && $finalIndex !== false && $initialIndex < 
$finalIndex)
     {
       for ($i = $initialIndex; $i < $finalIndex; $i++)
       {
-        switch ($this->validVersions[$i])
+        $this->migrator = QubitMigrateFactory::getMigrator($this->data, 
$this->validVersions[$i]);
+        $this->data = $this->migrator->execute();
+        $this->dataModified = true;
+        
+        // Set version
+        if ('1.0.7' == $this->validVersion[$i])
+        {
+          $this->version = 0;
+        }
+        else
         {
-          case '1.0.3':
-            $migrator->migrate103();
-            $this->logSection('migrate', 'Data migrated to version 1.0.4');
-            break;
-          case '1.0.4':
-            $migrator->migrate104();
-            $this->logSection('migrate', 'Data migrated to version 1.0.5');
-            break;
-          case '1.0.5':
-            $migrator->migrate105();
-            $this->logSection('migrate', 'Data migrated to version 1.0.6');
-            break;
-          case '1.0.6':
-            $migrator->migrate106();
-            $this->logSection('migrate', 'Data migrated to version 1.0.7');
-            break;
-          case '1.0.7':
-            $migrator->migrate107();
-            $this->logSection('migrate', 'Data migrated to version 1.0.8');
-            break;
-          case '1.0.8':
-            $migrator->migrate108();
-            $this->logSection('migrate', 'Data migrated to version 1.0.9');
-            break;
-          case '1.0.9':
-            $migrator->migrate109();
-            $this->logSection('migrate', 'Data migrated to version 1.1');
-            break;
-          case '1.1.0':
-            $migrator->migrate110();
-            $this->logSection('migrate', 'Data migrated to version 1.2');
-            break;
+          $this->version = $this->thisValidVersions[i+1];
         }
+
+        $this->logSection('migrate', 'Data migrated to version 
'.$this->validVersions[$i]);
       }
     }
+  }
 
-    // write new data.yml file (if data was modified)
-    if ($migrator->isDataModified())
+  protected function migrateFineGrained()
+  {
+    if (preg_match('/^\d+$/', $this->initialVersion))
     {
-      $this->writeMigratedData($arguments['datafile'], $migrator->getData());
+      $this->version = $this->initialVersion;
     }
     else
     {
-      $this->logSection('migrate', 'The specified data file is up-to-date, no 
migration done.');
+      $this->version = 0;
     }
+
+    while (null !== $this->version && (null === $this->targetVersion || 
$this->targetVersion < $this->version))
+    {
+      $migrator = QubitMigrateFactory::getMigrator($this->data, 
$this->version);
+      var_dump(get_class($migrator));
+      $this->data = $migrator->execute();
+      $this->dataModified = true;
+
+      $this->version = $migrator::FINAL_VERSION;
+    }
+
+    if (null == $this->version)
+    {
+      // Set version to value in data/fixtures/settings.yml
+      $parser = new sfYamlParser;
+      $data = 
$parser->parse(file_get_contents(sfConfig::get('sf_data_dir').'/fixtures/settings.yml'));
+      $this->version = $data['QubitSetting']['version']['value'];
+    }
+
+    $this->logSection('migrate', 'Data migrated to Release 
'.$migrator::MILESTONE.' v'.$this->version);
   }
 
-  protected function writeMigratedData($originalFileName, $data)
+  protected function writeMigratedData($originalFileName)
   {
+    // Update version number
+    
$this->data['QubitSetting'][$key]['value'][$this->data['QubitSetting'][$key]['source_culture']]
 = $this->version;
+
     $migratedFileName = 'migrated_data_'.date('YmdHis').'.yml';
     $dir = dirname($originalFileName);
     $migratedFileName = $dir.DIRECTORY_SEPARATOR.$migratedFileName;
 
     $yamlDumper = new sfYamlDumper();
-    $yamlData = sfYaml::dump($data, 3);
+    $yamlData = sfYaml::dump($this->data, 3);
 
     // Remove single quotes from <?php statements to prevent errors on load
     $yamlData = preg_replace("/'(\<\?php echo .+ \?\>)'/", '$1', $yamlData);
@@ -180,14 +216,18 @@
     $this->logSection('migrate', 'Migrated data written to 
"'.$migratedFileName.'"');
   }
 
-  protected function getDataVersion($data)
+  protected function getDataVersion()
   {
     $currentVersion = null;
-    foreach ($data['QubitSetting'] as $setting)
+    foreach ($this->data['QubitSetting'] as $setting)
     {
       if ($setting['name'] == 'version')
       {
-        if (preg_match('/\d\.\d(\.\d)?/', $setting['value']['en'], $matches))
+        if (preg_match('/^\d+$/', $setting['value']['en'], $matches))
+        {
+          $currentVersion = $matches[0];
+        }
+        else if (preg_match('/\d\.\d(\.\d)?/', $setting['value']['en'], 
$matches))
         {
           $currentVersion = $matches[0];
         }

Modified: trunk/lib/task/migrate/QubitMigrate.class.php
==============================================================================
--- trunk/lib/task/migrate/QubitMigrate.class.php       Fri Jun 24 08:55:41 
2011        (r9170)
+++ trunk/lib/task/migrate/QubitMigrate.class.php       Fri Jun 24 16:18:25 
2011        (r9171)
@@ -27,11 +27,14 @@
  */
 class QubitMigrate
 {
-  protected $data;
+  public
+    $data,
+    $version;
 
-  public function __construct($data)
+  public function __construct($data, $version)
   {
     $this->data = $data;
+    $this->version = $version;
   }
 
   /**

Modified: trunk/lib/task/migrate/QubitMigrate108.class.php
==============================================================================
--- trunk/lib/task/migrate/QubitMigrate108.class.php    Fri Jun 24 08:55:41 
2011        (r9170)
+++ trunk/lib/task/migrate/QubitMigrate108.class.php    Fri Jun 24 16:18:25 
2011        (r9171)
@@ -27,14 +27,19 @@
  */
 class QubitMigrate108 extends QubitMigrate
 {
+  const 
+    MILESTONE = '1.0.8',
+    INIT_VERSION = 0,
+    FINAL_VERSION = 39;
+
   /**
    * Controller for calling methods to alter data
    *
    * @return QubitMigrate108 this object
    */
-  protected function alterData($version)
+  protected function alterData()
   {
-    switch ($version)
+    switch ($this->version)
     {
       default:
         $this->updateStaticPageVersionNumber();
@@ -208,7 +213,7 @@
         $this->updateEnMenuLabels();
     }
 
-    // Delete "stub" objects
+    // Delete "stub" object
     $this->deleteStubObjects();
 
     return $this;
@@ -231,31 +236,6 @@
     return $this;
   }
 
-  public function execute()
-  {
-    // Find version
-    foreach ($this->data['QubitSetting'] as $key => $value)
-    {
-      if ('version' == $value['name'])
-      {
-        $version = $value['value'][$value['source_culture']];
-        break;
-      }
-    }
-
-    $this->alterData($version);
-
-    $parser = new sfYamlParser;
-    $data = 
$parser->parse(file_get_contents(sfConfig::get('sf_data_dir').'/fixtures/settings.yml'));
-
-    // Update version to 39 (last version for 1.0.8)
-    
$this->data['QubitSetting'][$key]['value'][$this->data['QubitSetting'][$key]['source_culture']]
 = 39;
-
-    $this->sortData();
-
-    return $this->getData();
-  }
-
   /**
    * Ver 3: Change table name from QubitActorName to QubitOtherName and column
    * actor_id -> object_id

Modified: trunk/lib/task/migrate/QubitMigrate109.class.php
==============================================================================
--- trunk/lib/task/migrate/QubitMigrate109.class.php    Fri Jun 24 08:55:41 
2011        (r9170)
+++ trunk/lib/task/migrate/QubitMigrate109.class.php    Fri Jun 24 16:18:25 
2011        (r9171)
@@ -27,14 +27,28 @@
  */
 class QubitMigrate109 extends QubitMigrate
 {
+  const 
+    MILESTONE = '1.0.9',
+    INIT_VERSION = 39,
+    FINAL_VERSION = 62;
+
+  public function execute()
+  {
+    $this->slugData();
+    $this->alterData();
+    $this->sortData();
+
+    return $this->getData();
+  }
+
   /**
    * Controller for calling methods to alter data
    *
    * @return QubitMigrate109 this object
    */
-  protected function alterData($version)
+  protected function alterData()
   {
-    switch ($version)
+    switch ($this->version)
     {
       case 39:
         $this->updateStaticPageVersionNumber();
@@ -280,33 +294,6 @@
     return $this;
   }
 
-  public function execute()
-  {
-    $this->slugData();
-
-    // Find version
-    foreach ($this->data['QubitSetting'] as $key => $value)
-    {
-      if ('version' == $value['name'])
-      {
-        $version = $value['value'][$value['source_culture']];
-        break;
-      }
-    }
-
-    $this->alterData($version);
-
-    $parser = new sfYamlParser;
-    $data = 
$parser->parse(file_get_contents(sfConfig::get('sf_data_dir').'/fixtures/settings.yml'));
-
-    // Update version
-    
$this->data['QubitSetting'][$key]['value'][$this->data['QubitSetting'][$key]['source_culture']]
 = $data['QubitSetting']['version']['value'];
-
-    $this->sortData();
-
-    return $this->getData();
-  }
-
   /**
    * Ver 40: Update static page release number to 1.1
    *

Copied and modified: trunk/lib/task/migrate/QubitMigrateFactory.class.php (from 
r9169, trunk/lib/task/migrate/QubitMigrator.class.php)
==============================================================================
--- trunk/lib/task/migrate/QubitMigrator.class.php      Thu Jun 23 00:02:20 
2011        (r9169, copy source)
+++ trunk/lib/task/migrate/QubitMigrateFactory.class.php        Fri Jun 24 
16:18:25 2011        (r9171)
@@ -25,103 +25,30 @@
  * @version    svn: $Id$
  * @author     David Juhasz <[email protected]>
  */
-class QubitMigrator
+class QubitMigrateFactory
 {
-  protected
-    $data = null,
-    $dataModified = false;
-
-  public function __construct($data)
-  {
-    $this->setData($data);
-  }
-
-  public function setData($data)
-  {
-    $this->data = $data;
-
-    return $this;
-  }
-
-  public function getData()
-  {
-    return $this->data;
-  }
-
-  public function isDataModified()
-  {
-    return $this->dataModified;
-  }
-
-  public function migrate103()
-  {
-    $migrator = new QubitMigrate103($this->data);
-    $this->data = $migrator->execute();
-    $this->dataModified = true;
-
-    return $this;
-  }
-
-  public function migrate104()
-  {
-    $migrator = new QubitMigrate104($this->data);
-    $this->data = $migrator->execute();
-    $this->dataModified = true;
-
-    return $this;
-  }
-
-  public function migrate105()
-  {
-    $migrator = new QubitMigrate105($this->data);
-    $this->data = $migrator->execute();
-    $this->dataModified = true;
-
-    return $this;
-  }
-
-  public function migrate106()
-  {
-    $migrator = new QubitMigrate106($this->data);
-    $this->data = $migrator->execute();
-    $this->dataModified = true;
-
-    return $this;
-  }
-
-  public function migrate107()
-  {
-    $migrator = new QubitMigrate107($this->data);
-    $this->data = $migrator->execute();
-    $this->dataModified = true;
-
-    return $this;
-  }
-
-  public function migrate108()
-  {
-    $migrator = new QubitMigrate108($this->data);
-    $this->data = $migrator->execute();
-    $this->dataModified = true;
-
-    return $this;
-  }
-
-  public function migrate109()
-  {
-    $migrator = new QubitMigrate109($this->data);
-    $this->data = $migrator->execute();
-    $this->dataModified = true;
-
-    return $this;
-  }
-
-  public function migrate110()
+  public function getMigrator($data, $version)
   {
-    $migrator = new QubitMigrate110($this->data);
-    $this->data = $migrator->execute();
-    $this->dataModified = true;
+    if (preg_match('/^\d+$/', $version))
+    {
+       if (QubitMigrate108::FINAL_VERSION > intval($version))
+       {
+         return new QubitMigrate108($data, $version);
+       }
+       else if (QubitMigrate109::FINAL_VERSION > intval($version))
+       {
+         return new QubitMigrate109($data, $version);
+       }
+       else 
+       {
+         return new QubitMigrate110($data, $version);
+       }
+    }
+    else
+    {
+      $migrateClass = 'QubitMigrate'.str_replace('.', '', $version);
 
-    return $this;
+      return new $migrateClass($data, $version);
+    }
   }
 }

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to