protected function _execute($options, $once) { $result = true; try { $result = $this->Version->run($options); } catch (MigrationException $e) { $this->out(__d('migrations', 'An error occurred when processing the migration:')); $this->out(' ' . sprintf(__d('migrations', 'Migration: %s'), $e->Migration->info['name'])); $this->out(' ' . sprintf(__d('migrations', 'Error: %s'), $e->getMessage())); $this->hr(); $response = $this->in(__d('migrations', 'Do you want to mark the migration as successful?. [y]es or [a]bort.'), array('y', 'a')); if (strtolower($response) === 'y') { $this->Version->setVersion($e->Migration->info['version'], $this->type, $options['direction'] === 'up'); if (!$once) { return $this->run(); } } else { if (strtolower($response) === 'a') { return $this->_stop(); } } $this->hr(); } return $result; }
private function __dbStructure($options = array()) { if (is_string($options) || isset($options['useSchema'])) { $version = new MigrationVersion(); $versions = $version->getMapping('rcms'); if (!isset($options['targetVersion'])) { $options['targetVersion'] = array_pop($versions); } if (!isset($options['initVersion'])) { $options['initVersion'] = array_pop($versions); } $version->run(array('version' => array($options['initVersion'], $options['targetVersion']), 'type' => 'rcms', 'direction' => 'up')); } else { if (isset($options['fileName'])) { $db = ConnectionManager::getDataSource('default'); $statements = file_get_contents(CONFIGS . 'sql/' . $options['fileName']); /* Replacing the block comments */ $statements = preg_replace('/\\/\\*[^\\*]*\\*\\//', '', $statements); /* Replacing the line comments */ $statements = preg_replace('/.*\\-\\-.*\\n/', '', $statements); $statements = explode(';', $statements); foreach ($statements as $statement) { if (trim($statement) != '') { $db->query($statement); } } return true; } } }
/** * Run the migrations * * @return void * @access public */ public function run() { $mapping = $this->Version->getMapping($this->type); if ($mapping === false) { $this->out(__d('migrations', 'No migrations available.')); return $this->_stop(); } $latestVersion = $this->Version->getVersion($this->type); $options = array('type' => $this->type, 'callback' => &$this); if (isset($this->args[0]) && in_array($this->args[0], array('up', 'down'))) { $options['direction'] = $this->args[0]; if ($options['direction'] == 'up') { $latestVersion++; } if (!isset($mapping[$latestVersion])) { $this->out(__d('migrations', 'Not a valid migration version.')); return $this->_stop(); } } elseif (isset($this->args[0]) && $this->args[0] == 'all') { end($mapping); $options['version'] = key($mapping); } elseif (isset($this->args[0]) && $this->args[0] == 'reset') { $options['version'] = 0; } else { if (isset($this->args[0]) && is_numeric($this->args[0])) { $options['version'] = (int) $this->args[0]; $valid = isset($mapping[$options['version']]) || $options['version'] === 0 && $latestVersion > 0; if (!$valid) { $this->out(__d('migrations', 'Not a valid migration version.')); return $this->_stop(); } } else { $this->_showInfo($mapping, $this->type); $this->hr(); while (true) { $response = $this->in(__d('migrations', 'Please, choose what version you want to migrate to. [q]uit or [c]lean.')); if (strtolower($response) === 'q') { return $this->_stop(); } elseif (strtolower($response) === 'c') { $this->Dispatch->clear(); continue; } $valid = is_numeric($response) && (isset($mapping[(int) $response]) || (int) $response === 0 && $latestVersion > 0); if ($valid) { $options['version'] = (int) $response; break; } else { $this->out(__d('migrations', 'Not a valid migration version.')); } } $this->hr(); } } $this->out(__d('migrations', 'Running migrations:')); $this->Version->run($options); $this->out(__d('migrations', 'All migrations have completed.')); $this->out(); return true; }
/** * Migrate a plugin * * @param string $plugin Plugin name * @return boolean Success of the migration */ public function migrate($plugin) { $success = false; $mapping = $this->_getMigrationVersion()->getMapping($plugin); if ($mapping) { $lastVersion = max(array_keys($mapping)); $executionResult = $this->_MigrationVersion->run(array('version' => $lastVersion, 'type' => $plugin)); $success = $executionResult === true; if (!$success) { array_push($this->migrationErrors, $executionResult); } } return $success; }
/** * Step 2: insert required data * * @return void */ public function data() { $this->pageTitle = __('Step 2: Run SQL', true); if ($this->RequestHandler->isPost()) { App::import('Model', 'ConnectionManager'); $db = ConnectionManager::getDataSource('default'); if (!$db->isConnected()) { $this->Session->setFlash(__('Could not connect to database.', true)); } else { try { if (App::import('Lib', 'Migrations.MigrationVersion')) { $migration = new MigrationVersion(array('connection' => 'default')); $migration->run(array('type' => 'app', 'direction' => 'up')); } $this->redirect(array('action' => 'finish')); } catch (MigrationVersionException $e) { $this->Session->setFlash($e->getMessage()); } catch (MigrationException $e) { $this->Session->setFlash($e->getMessage()); } } } }
/** * Run the migrations * * @return void * @access public */ public function run() { try { $mapping = $this->Version->getMapping($this->type); } catch (MigrationVersionException $e) { $this->err($e->getMessage()); return false; } if ($mapping === false) { $this->out(__d('migrations', 'No migrations available.', true)); return $this->_stop(); } $latestVersion = $this->Version->getVersion($this->type); $options = array('type' => $this->type, 'callback' => &$this); if (isset($this->args[0]) && in_array($this->args[0], array('up', 'down'))) { $options['direction'] = $this->args[0]; if ($options['direction'] == 'up') { $latestVersion++; } if (!isset($mapping[$latestVersion])) { $this->out(__d('migrations', 'Not a valid migration version.', true)); return $this->_stop(); } } else { if (isset($this->args[0]) && $this->args[0] == 'all') { end($mapping); $options['version'] = key($mapping); } else { if (isset($this->args[0]) && $this->args[0] == 'reset') { $options['version'] = 0; } else { if (isset($this->args[0]) && is_numeric($this->args[0])) { $options['version'] = (int) $this->args[0]; $valid = isset($mapping[$options['version']]) || $options['version'] === 0 && $latestVersion > 0; if (!$valid) { $this->out(__d('migrations', 'Not a valid migration version.', true)); return $this->_stop(); } } else { $this->_showInfo($mapping, $this->type); $this->hr(); while (true) { $response = $this->in(__d('migrations', 'Please, choose what version you want to migrate to. [q]uit or [c]lean.', true)); if (strtolower($response) === 'q') { return $this->_stop(); } else { if (strtolower($response) === 'c') { $this->_clear(); continue; } } $valid = is_numeric($response) && (isset($mapping[(int) $response]) || (int) $response === 0 && $latestVersion > 0); if ($valid) { $options['version'] = (int) $response; break; } else { $this->out(__d('migrations', 'Not a valid migration version.', true)); } } $this->hr(); } } } } $this->out(__d('migrations', 'Running migrations:', true)); try { $this->Version->run($options); } catch (MigrationException $e) { $this->out(__d('migrations', 'An error occurred when processing the migration:', true)); $this->out(' ' . sprintf(__d('migrations', 'Migration: %s', true), $e->Migration->info['name'])); $this->out(' ' . sprintf(__d('migrations', 'Error: %s', true), $e->getMessage())); $this->out(''); return false; } $this->out(__d('migrations', 'All migrations have completed.', true)); $this->out(''); return true; }
/** * configuration * * @return void */ function install() { $this->set('title_for_layout', __('Install Database', true)); App::import('Core', 'File'); App::import('Model', 'ConnectionManager'); $db = ConnectionManager::getDataSource('default'); if (!$db->isConnected()) { pr('Could not connect'); //SessionComponent::setFlash(__('Could not connect to database.', true)); } else { // Can be 'app' or a plugin name $type = 'app'; App::import('Lib', 'Migrations.MigrationVersion'); // All the job is done by MigrationVersion $version = new MigrationVersion(); // Get the mapping and the latest version avaiable $mapping = $version->getMapping($type); $latest = array_pop($mapping); // Run it to latest version if ($version->run(array('type' => $type, 'version' => $latest['version']))) { ClassRegistry::init('Installer.Release')->installData($this->data['Install']['sample_data']); $this->redirect(array('action' => 'siteConfig')); } else { //SessionComponent::setFlash(__('There was an error installing database data.', true)); } } }