public function startMigration($versionName, $action = 'up', $params = array(), $force = false) { /* @global $APPLICATION \CMain */ global $APPLICATION; if (isset($this->restarts[$versionName])) { unset($this->restarts[$versionName]); } try { $action = $action == 'up' ? 'up' : 'down'; $meta = $this->getVersionMeta($versionName); if (!$meta || empty($meta['class'])) { throw new MigrationException('failed to initialize migration'); } if (!$force) { if ($action == 'up' && $meta['status'] != 'new') { throw new MigrationException('migration already up'); } if ($action == 'down' && $meta['status'] != 'installed') { throw new MigrationException('migration already down'); } } /** @var $versionInstance Version */ $versionInstance = new $meta['class'](); $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->addRecord($versionName); } else { $ok = $this->removeRecord($versionName); } if ($ok === false) { throw new MigrationException('unable to write migration to the database'); } Out::out('%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 createMigrationFile($description = '') { $description = preg_replace("/\r\n|\r|\n/", '<br/>', $description); $description = strip_tags($description); $description = addslashes($description); $originTz = date_default_timezone_get(); date_default_timezone_set('Europe/Moscow'); $version = 'Version' . date('YmdHis'); date_default_timezone_set($originTz); $str = $this->renderVersionFile(array('version' => $version, 'description' => $description)); $file = $this->getFileName($version); file_put_contents($file, $str); if (is_file($file)) { Out::out('%s created', $version); return $version; } else { Out::out('%s, error: can\'t create a file "%s"', $version, $file); return false; } }