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; }
public function createVersionFile($description = '', $prefix = '') { $description = $this->purifyDescriptionForFile($description); $prefix = $this->preparePrefix($prefix); $originTz = date_default_timezone_get(); date_default_timezone_set('Europe/Moscow'); $ts = date('YmdHis'); date_default_timezone_set($originTz); $versionName = $prefix . $ts; list($extendUse, $extendClass) = explode(' as ', $this->getConfigVal('migration_extend_class')); $extendUse = trim($extendUse); $extendClass = trim($extendClass); if (!empty($extendClass)) { $extendUse = 'use ' . $extendUse . ' as ' . $extendClass . ';' . PHP_EOL; } else { $extendClass = $extendUse; $extendUse = ''; } $str = $this->renderFile($this->getConfigVal('migration_template'), array('version' => $versionName, 'description' => $description, 'extendUse' => $extendUse, 'extendClass' => $extendClass)); $file = $this->getVersionFile($versionName); file_put_contents($file, $str); if (!is_file($file)) { Out::outError('%s, error: can\'t create a file "%s"', $versionName, $file); return false; } return $this->getVersionMeta($versionName); }
public function createVersionFile($description = '') { $description = $this->prepareDescription($description); $originTz = date_default_timezone_get(); date_default_timezone_set('Europe/Moscow'); $versionName = 'Version' . date('YmdHis'); date_default_timezone_set($originTz); list($extendUse, $extendClass) = explode(' as ', Module::getMigrationExtendClass()); $extendUse = trim($extendUse); $extendClass = trim($extendClass); if (!empty($extendClass)) { $extendUse = 'use ' . $extendUse . ' as ' . $extendClass . ';' . PHP_EOL; } else { $extendClass = $extendUse; $extendUse = ''; } $str = $this->renderFile(Module::getMigrationTemplate(), array('version' => $versionName, 'description' => $description, 'extendUse' => $extendUse, 'extendClass' => $extendClass)); $file = $this->getVersionFile($versionName); file_put_contents($file, $str); if (!is_file($file)) { Out::outError('%s, error: can\'t create a file "%s"', $versionName, $file); return false; } return array('version' => $versionName, 'location' => $file, 'description' => $description); }