/** * testGetMapping method * * @return void */ public function testGetMapping() { CakePlugin::load('TestMigrationPlugin'); $result = $this->Version->getMapping('test_migration_plugin'); $expected = array(1 => array('version' => 1, 'name' => '001_schema_dump', 'class' => 'M4af6d40056b04408808500cb58157726', 'type' => 'TestMigrationPlugin', 'migrated' => null), 2 => array('version' => 2, 'name' => '002_another_migration_plugin_test_migration', 'class' => 'AnotherMigrationPluginTestMigration', 'type' => 'TestMigrationPlugin', 'migrated' => null)); $this->assertEqual($result, $expected); $result = $this->Version->getMapping('migrations'); $expected = array(1 => array('version' => 1, 'name' => '001_init_migrations', 'class' => 'InitMigrations', 'type' => 'Migrations', 'migrated' => '2009-11-10 00:55:34'), 2 => array('version' => 2, 'name' => '002_convert_version_to_class_names', 'class' => 'ConvertVersionToClassNames', 'type' => 'Migrations', 'migrated' => '2011-11-18 13:53:32'), 3 => array('version' => 3, 'name' => '003_increase_class_name_length', 'class' => 'IncreaseClassNameLength', 'type' => 'Migrations', 'migrated' => null)); $this->assertEqual($result, $expected); }
/** * testGetMapping method * * @return void */ function testGetMapping() { $result = $this->Version->getMapping('inexistent_plugin'); $this->assertFalse($result); $result = $this->Version->getMapping('test_migration_plugin'); $expected = array(1 => array('version' => 1, 'name' => '001_schema_dump', 'class' => 'M4af6d40056b04408808500cb58157726', 'type' => 'test_migration_plugin', 'migrated' => null)); $this->assertEqual($result, $expected); $result = $this->Version->getMapping('migrations'); $expected = array(1 => array('version' => 1, 'name' => '001_init_migrations', 'class' => 'M4af6e0f0a1284147a0b100ca58157726', 'type' => 'migrations', 'migrated' => '2009-11-10 00:55:34')); $this->assertEqual($result, $expected); }
/** * testGetMapping method on a plugin having an empty map.php file, or not * having this file at all * * @return void */ function testGetMappingEmptyMap() { try { $this->Version->getMapping('test_migration_plugin2'); $this->fail('No exception triggered'); } catch (MigrationVersionException $e) { $this->assertEqual('File `map.php` not found in the TestMigrationPlugin2 Plugin.', $e->getMessage()); } $result = $this->Version->getMapping('test_migration_plugin3'); $this->assertIdentical($result, false); }
/** * testGetMapping method * * @return void */ function testGetMapping() { try { $this->Version->getMapping('inexistent_plugin'); $this->fail('No exception triggered'); } catch (MigrationVersionException $e) { $this->assertEqual('File `map.php` not found in the InexistentPlugin Plugin.', $e->getMessage()); } $result = $this->Version->getMapping('test_migration_plugin'); $expected = array(1 => array('version' => 1, 'name' => '001_schema_dump', 'class' => 'M4af6d40056b04408808500cb58157726', 'type' => 'test_migration_plugin', 'migrated' => null)); $this->assertEqual($result, $expected); $result = $this->Version->getMapping('migrations'); $expected = array(1 => array('version' => 1, 'name' => '001_init_migrations', 'class' => 'M4af6e0f0a1284147a0b100ca58157726', 'type' => 'migrations', 'migrated' => '2009-11-10 00:55:34')); $this->assertEqual($result, $expected); }
/** * Displays a status of all plugin and app migrations * * @return void */ public function status() { $types = CakePlugin::loaded(); ksort($types); array_unshift($types, 'App'); $outdated = isset($this->args[0]) && $this->args[0] === 'outdated'; foreach ($types as $name) { try { $type = Inflector::underscore($name); $mapping = $this->Version->getMapping($type); if ($mapping === false) { continue; } $version = $this->Version->getVersion($type); $latest = end($mapping); if ($outdated && $latest['version'] == $version) { continue; } $this->out($type === 'app' ? 'Application' : $name . ' Plugin'); $this->out(''); $this->out(__d('migrations', 'Current version:')); if ($version != 0) { $info = $mapping[$version]; $this->out(' #' . number_format($info['version'] / 100, 2, '', '') . ' ' . $info['name']); } else { $this->out(' ' . __d('migrations', 'None applied.')); } $this->out(__d('migrations', 'Latest version:')); $this->out(' #' . number_format($latest['version'] / 100, 2, '', '') . ' ' . $latest['name']); $this->hr(); } catch (MigrationVersionException $e) { continue; } } }
/** * Displays a summary of all plugin and app migrations * * @access public * @return void */ public function summary() { $types = App::objects('plugin'); ksort($types); array_unshift($types, 'App'); foreach ($types as $name) { $type = Inflector::underscore($name); $mapping = $this->Version->getMapping($type); if ($mapping === false || count($mapping) === 0) { continue; } $version = $this->Version->getVersion($type); $this->out($name . ' Plugin'); $this->out(''); $this->out(__d('migrations', 'Current version:', true)); if ($version != 0) { $info = $mapping[$version]; $this->out(' #' . number_format($info['version'] / 100, 2, '', '') . ' ' . $info['name']); } else { $this->out(' ' . __d('migrations', 'None applied.', true)); } $info = array_pop($mapping); $this->out(__d('migrations', 'Latest version:', true)); $this->out(' #' . number_format($info['version'] / 100, 2, '', '') . ' ' . $info['name']); $this->hr(); } }
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; }
/** * beforeRender Callback * * @return array */ function beforeRender(&$controller) { $v = new MigrationVersion(); $map = array(); foreach (App::objects('plugin') as $plugin) { try { $map[$plugin] = array('map' => $v->getMapping($plugin), 'current' => $v->getVersion($plugin)); } catch (MigrationVersionException $e) { // Skip if we get an error. } } return $map; }
/** * BeforeRender Callback * * @param Controller $controller Current controller * @return array */ public function beforeRender(Controller $controller) { $v = new MigrationVersion(); $map = $migrations = array(); $migrations = Hash::merge(array('app'), CakePlugin::loaded()); foreach ($migrations as $plugin) { try { $map[$plugin] = array('map' => $v->getMapping($plugin), 'current' => $v->getVersion($plugin)); } catch (MigrationVersionException $e) { // Skip if we get an error. } } return $map; }
/** * Temp acl things protected function _getClassMethods($ctrlName = null) { App::import('Controller', $ctrlName); if (strlen(strstr($ctrlName, '.')) > 0) { // plugin's controller $num = strpos($ctrlName, '.'); $ctrlName = substr($ctrlName, $num+1); } $ctrlclass = $ctrlName . 'Controller'; $methods = get_class_methods($ctrlclass); // Add scaffold defaults if scaffolds are being used $properties = get_class_vars($ctrlclass); if (is_array($properties) && array_key_exists('scaffold', $properties)) { if($properties['scaffold'] == 'admin') { $methods = array_merge($methods, array('admin_add', 'admin_edit', 'admin_index', 'admin_view', 'admin_delete')); } } return $methods; } protected function _isPlugin($ctrlName = null) { $arr = String::tokenize($ctrlName, '/'); if (count($arr) > 1) { return true; } else { return false; } } protected function _getPluginControllerPath($ctrlName = null) { $arr = String::tokenize($ctrlName, '/'); if (count($arr) == 2) { return $arr[0] . '.' . $arr[1]; } else { return $arr[0]; } } protected function _getPluginName($ctrlName = null) { $arr = String::tokenize($ctrlName, '/'); if (count($arr) == 2) { return $arr[0]; } else { return false; } } protected function _getPluginControllerName($ctrlName = null) { $arr = String::tokenize($ctrlName, '/'); if (count($arr) == 2) { return $arr[1]; } else { return false; } } private function __getClassName() { if (isset($this->request->params['plugin'])) { return $this->request->plugin . '.' . $this->Controller->modelClass; } else { return $this->Controller->modelClass; } } protected function _getPlugins(){ $plugins = array( 'infinitas', 'extentions', 'plugins' ); $return = array(); foreach($plugins as $plugin ){ $return = array_merge($return, $this->_getPluginControllerNames($plugin)); } return $return; } protected function _getPluginControllerNames($plugin) { App::import('Core', 'File', 'Folder'); $paths = Configure::getInstance(); $folder = new Folder(); $folder->cd(APP . $plugin); $Plugins = $folder->read(); $Plugins = $Plugins[0]; $arr = array(); // Loop through the plugins foreach($Plugins as $pluginName) { // Change directory to the plugin $didCD = $folder->cd(APP . $plugin. DS . $pluginName . DS . 'controllers'); // Get a list of the files that have a file name that ends // with controller.php $files = $folder->findRecursive('.*_controller\.php'); // Loop through the controllers we found in the plugins directory foreach($files as $fileName) { // Get the base file name $file = basename($fileName); // Get the controller name $file = Inflector::camelize(substr($file, 0, strlen($file)-strlen('_controller.php'))); if (!preg_match('/^'. Inflector::humanize($pluginName). 'App/', $file)) { if (!App::import('Controller', $pluginName.'.'.$file)) { debug('Error importing '.$file.' for plugin '.$pluginName); } else { /// Now prepend the Plugin name ... // This is required to allow us to fetch the method names. $arr[] = Inflector::humanize($pluginName) . "/" . $file; } } } } return $arr; } */ public function checkDbVersion() { App::import('Lib', 'Migrations.MigrationVersion'); $Version = new MigrationVersion(); $currentVersion = $Version->getVersion('app'); $latestVersion = end($Version->getMapping('app')); if ($currentVersion < $latestVersion['version']) { $this->Controller->redirect(array('plugin' => 'installer', 'controller' => 'upgrade', 'action' => 'index', 'admin' => 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)); } } }