public function startMigration($versionName, $action = 'up', $params = array()) { /* @global $APPLICATION \CMain */ global $APPLICATION; try { $action = $action == 'up' ? 'up' : 'down'; $versionInstance = $this->getVersionInstance($versionName); if (!$versionInstance) { throw new MigrationException('failed to initialize migration'); } if ($this->checkPerms) { $versionType = $this->getVersionType($versionName); if (!$versionType || $versionType == 'is_unknown') { throw new MigrationException('migration not found'); } if ($action == 'up' && $versionType != 'is_new') { throw new MigrationException('migration already up'); } if ($action == 'down' && $versionType != 'is_installed') { throw new MigrationException('migration already down'); } } if (isset($this->restarts[$versionName])) { unset($this->restarts[$versionName]); } else { Out::outToConsoleOnly('%s (%s) start', $versionName, $action); } $versionInstance->setParams($params); if ($action == 'up') { $ok = $versionInstance->up(); } else { $ok = $versionInstance->down(); } if ($APPLICATION->GetException()) { throw new MigrationException($APPLICATION->GetException()->GetString()); } if ($ok === false) { throw new MigrationException('migration returns false'); } if ($action == 'up') { $ok = $this->versionTable->addRecord($versionName); } else { $ok = $this->versionTable->removeRecord($versionName); } if ($ok === false) { throw new MigrationException('unable to write migration to the database'); } Out::outToConsoleOnly('%s (%s) success', $versionName, $action); return true; } catch (RestartException $e) { $this->restarts[$versionName] = isset($versionInstance) ? $versionInstance->getParams() : array(); } catch (MigrationException $e) { Out::outError('%s (%s) error: %s', $versionName, $action, $e->getMessage()); } catch (\Exception $e) { Out::outError('%s (%s) error: %s', $versionName, $action, $e->getMessage()); } return false; }
public function startMigration($version, $action = 'up', $params = array()) { try { $action = $action == 'up' ? 'up' : 'down'; if (isset($this->restarts[$version])) { unset($this->restarts[$version]); } if (!$this->force) { $aItem = $this->getVersionByName($version); if (!$aItem || $aItem['type'] == 'is_unknown') { throw new MigrationException('migration not found'); } if ($action == 'up' && $aItem['type'] != 'is_new') { throw new MigrationException('migration already up'); } if ($action == 'down' && $aItem['type'] != 'is_success') { throw new MigrationException('migration already down'); } } $oVersion = $this->getVersionInstance($version); if (!$oVersion) { throw new MigrationException('failed to initialize migration'); } $oVersion->setParams($params); if ($action == 'up') { $ok = $oVersion->up(); } else { $ok = $oVersion->down(); } /** @global $APPLICATION \CMain */ global $APPLICATION; if ($APPLICATION->GetException()) { throw new MigrationException($APPLICATION->GetException()->GetString()); } if ($ok === false) { throw new MigrationException('migration returns false'); } if ($action == 'up') { $this->addRecord($version); } else { $this->removeRecord($version); } Out::outToConsoleOnly('%s (%s) success', $version, $action); return true; } catch (RestartException $e) { $this->restarts[$version] = isset($oVersion) ? $oVersion->getParams() : array(); } catch (MigrationException $e) { Out::outError('%s (%s) error: %s', $version, $action, $e->getMessage()); } catch (\Exception $e) { Out::outError('%s (%s) error: %s', $version, $action, $e->getMessage()); } return false; }