public function testGetMaxVersion() { $versions = array(); $this->_adapter->versions = $versions; $this->assertNull($this->object->getMaxVersion()); $versions = array(array('version' => 1, 'class' => 'CreateUsers', 'file' => '001_CreateUsers.php')); $this->_adapter->versions = $versions; $actual = $this->object->getMaxVersion(); $this->assertNotNull($actual); $this->assertEquals('1', $actual); $versions = array(array('version' => 1, 'class' => 'CreateUsers', 'file' => '001_CreateUsers.php'), array('version' => 2, 'class' => 'AddNewTable', 'file' => '002_AddNewTable.php'), array('version' => 3, 'class' => 'AddIndexToBlogs', 'file' => '003_AddIndexToBlogs.php')); $this->_adapter->versions = $versions; $actual = $this->object->getMaxVersion(); $this->assertNotNull($actual); $this->assertEquals('3', $actual); }
/** * Primary task entry point * * @param array $args Arguments of task * * @return void */ protected function _execute($args) { $this->_logger->debug(__METHOD__ . ' Start'); $this->_taskArgs = $args; $this->_logger->debug('Args of task: ' . var_export($args, true)); // Flag d'échec de migration $failed = false; try { // Check that the schema_version table exists, // and if not, automatically create it $this->_verifyEnvironment(); $this->_migratorUtil = new Phigrate_Util_Migrator($this->_adapter); $targetVersion = null; $style = self::STYLE_REGULAR; //did the user specify an explicit version? if (array_key_exists('VERSION', $this->_taskArgs)) { $targetVersion = trim($this->_taskArgs['VERSION']); $this->_logger->info('Version specified: ' . $targetVersion); } // did the user specify a relative offset, e.g. "-2" or "+3" ? $matches = array(); if ($targetVersion !== null && preg_match('/^([\\+-])(\\d+)$/', $targetVersion, $matches)) { if (count($matches) == 3) { $direction = $matches[1] === '-' ? self::DIRECTION_DOWN : self::DIRECTION_UP; $offset = intval($matches[2]); $style = self::STYLE_OFFSET; $this->_logger->debug('[OFFSET] direction: ' . $direction . ' - offset: ' . $offset); } } //determine our direction and target version $currentVersion = $this->_migratorUtil->getMaxVersion(); if ($style == self::STYLE_REGULAR) { $this->_logger->debug('STYLE REGULAR'); $direction = $this->_fetchDirection($targetVersion, $currentVersion); // Up or down to version specified by user or up to max if version not specified $this->_prepareToMigrate($targetVersion, $direction); } elseif ($style == self::STYLE_OFFSET) { $this->_logger->debug('STYLE OFFSET'); $this->_migrateFromOffset($offset, $currentVersion, $direction); } } catch (Phigrate_Exception_MissingSchemaInfoTable $ex) { $this->_return .= $ex->getMessage(); } catch (Phigrate_Exception_MissingMigrationMethod $ex) { $failed = true; $this->_return .= $ex->getMessage(); } catch (Phigrate_Exception $ex) { $failed = true; $this->_logger->err('Exception: ' . $ex->getMessage()); $this->_return .= "\n" . $ex->getMessage() . "\n"; } // Si une erreur de migration est détecté et que la version courante est renseignée if ($failed && !is_null($currentVersion) && $this instanceof Task_Db_Migrate) { $targetVersion = $currentVersion; $currentVersion = $this->_migratorUtil->getMaxVersion(); $this->_logger->debug('Retour en arriere: ' . var_export($targetVersion . ' ' . $currentVersion, true)); if ($currentVersion != $targetVersion) { $this->_return .= "\n\n" . $this->_prefixText . 'Back to original version: ' . $targetVersion . "\n\n"; $direction = $this->_fetchDirection($targetVersion, $currentVersion); // On retourne en arrière pour annuler les migrations $this->_prepareToMigrate($targetVersion, $direction); } } $this->_logger->debug(__METHOD__ . ' End'); return $this->_return; }