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; } } }
/** * 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; }
/** * testSetGetVersion method * * @return void */ public function testSetGetVersion() { $this->Version = $this->getMock('MigrationVersion', array('getMapping'), array(array('connection' => 'test'))); // Checking current $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping())); $result = $this->Version->getVersion('inexistent_plugin'); $expected = 0; $this->assertEqual($result, $expected); // Setting as 1 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping())); $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1))); $setResult = $this->Version->setVersion(1, 'inexistent_plugin'); $this->assertTrue(!empty($setResult)); $result = $this->Version->getVersion('inexistent_plugin'); $expected = 1; $this->assertEqual($result, $expected); // Setting as 2 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1))); $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 2))); $setResult = $this->Version->setVersion(2, 'inexistent_plugin'); $this->assertTrue(!empty($setResult)); $result = $this->Version->getVersion('inexistent_plugin'); $expected = 2; $this->assertEqual($result, $expected); // Setting as 1 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 2))); $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1))); $setResult = $this->Version->setVersion(2, 'inexistent_plugin', false); $this->assertTrue(!empty($setResult)); $result = $this->Version->getVersion('inexistent_plugin'); $expected = 1; $this->assertEqual($result, $expected); }
/** * Shows a list of available migrations * * @param array $mapping Migration mapping * @param string $type Can be 'app' or a plugin name * @return void */ protected function _showInfo($mapping, $type = null) { if ($type === null) { $type = $this->type; } $version = $this->Version->getVersion($type); if ($version != 0) { $info = $mapping[$version]; $this->out(__d('Migrations', 'Current migration version:')); $this->out(' #' . number_format($version / 100, 2, '', '') . ' ' . $info['name']); $this->hr(); } $this->out(__d('Migrations', 'Available migrations:')); foreach ($mapping as $version => $info) { $this->out(' [' . number_format($version / 100, 2, '', '') . '] ' . $info['name']); $this->out(' ', false); if ($info['migrated'] !== null) { $this->out(__d('Migrations', 'applied') . ' ' . date('r', strtotime($info['migrated']))); } else { $this->out(__d('Migrations', 'not applied')); } } }
/** * testRun method * * @return void */ public function testRun() { $back = $this->Version; $options = array('connection' => 'test'); $Version = $this->getMock('MigrationVersion', array('getMapping', 'getMigration'), array($options), 'TestMigrationVersionMockMigrationVersion', false); $this->Version = $Version; $this->Version->expects($this->any())->method('getMigration')->will($this->returnValue(new CakeMigration($options))); $this->Version->Version = ClassRegistry::init(array('class' => 'schema_migrations', 'ds' => 'test')); // direction => up $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping())); $this->assertEqual($Version->getVersion('mocks'), 0); $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array(1)); $this->assertEqual($Version->getVersion('mocks'), 1); // direction => down $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1))); $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array()); $this->assertEqual($Version->getVersion('mocks'), 0); // Set 1, 2, 3 versions applied $this->Version->setVersion(1, 'mocks'); $this->Version->setVersion(2, 'mocks'); $this->Version->setVersion(3, 'mocks'); // direction => up $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 3))); $this->assertEqual($Version->getVersion('mocks'), 3); $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 4)); $this->assertEqual($Version->getVersion('mocks'), 4); // direction => down $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 4))); $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); // version => 7 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 3))); $this->assertTrue($Version->run(array('version' => 7, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 7)); $this->assertEqual($Version->getVersion('mocks'), 7); // version => 3 $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 7))); $this->assertTrue($Version->run(array('version' => 3, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); // version => 10 (top version) $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 3))); $this->assertTrue($Version->run(array('version' => 10, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 10)); $this->assertEqual($Version->getVersion('mocks'), 10); // version => 0 (run down all migrations) //$Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 10)); $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 10))); $this->assertTrue($Version->run(array('version' => 0, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array()); $this->assertEqual($Version->getVersion('mocks'), 0); // Changing values back $this->Version = $back; unset($back); }
/** * Clear all caches present related to models * * Before the 'after' callback method be called is needed to clear all caches. * Without it any model operations will use cached data instead of real/modified * data. * * @return void */ protected function _clearCache() { // Clear the cache DboSource::$methodCache = array(); $keys = Cache::configured(); foreach ($keys as $key) { Cache::clear(false, $key); } ClassRegistry::flush(); // Refresh the model, in case something changed if ($this->Version instanceof MigrationVersion) { $this->Version->initVersion(); } }
/** * 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()); } } } }
/** * Shows a list of available migrations * * @param array $mapping Migration mapping * @param string $type Can be 'app' or a plugin name * @return void */ protected function _showInfo($mapping, $type = null) { if ($type === null) { $type = $this->type; } $version = $this->Version->getVersion($type); if ($version != 0) { $info = $mapping[$version]; $this->out(__d('migrations', 'Current migration version:')); $this->out(' #' . sprintf("%'.03d", $version) . ' ' . $info['name']); $this->hr(); } $this->out(__d('migrations', 'Available migrations:')); foreach ($mapping as $version => $info) { $this->out(' [' . sprintf("%'.03d", $version) . '] ' . $info['name']); $this->out(' ', false); if ($info['migrated'] !== null) { $this->out(__d('migrations', 'applied') . ' ' . CakeTime::nice(strtotime($info['migrated']))); } else { $this->out(__d('migrations', 'not applied')); } } }
/** * testRun method * * @return void */ function testRun() { $back = $this->Version; $options = array('connection' => 'test_suite'); $Version =& new TestMigrationVersionMockMigrationVersion($options); $this->Version = $Version; $this->Version->setReturnValue('getMigration', new CakeMigration($options)); $this->Version->Version =& ClassRegistry::init(array('class' => 'schema_migrations', 'ds' => 'test_suite')); // Variable used on setReturValueAt method $mappingCount = 0; // direction => up $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping()); $this->assertEqual($Version->getVersion('mocks'), 0); $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array(1)); $this->assertEqual($Version->getVersion('mocks'), 1); // direction => down $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 1)); $this->assertEqual($Version->getVersion('mocks'), 1); $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array()); $this->assertEqual($Version->getVersion('mocks'), 0); // Set 1, 2, 3 versions applied $this->Version->setVersion(1, 'mocks'); $this->Version->setVersion(2, 'mocks'); $this->Version->setVersion(3, 'mocks'); // direction => up $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 4)); $this->assertEqual($Version->getVersion('mocks'), 4); // direction => down $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 4)); $this->assertEqual($Version->getVersion('mocks'), 4); $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); // version => 7 $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); $this->assertTrue($Version->run(array('version' => 7, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 7)); $this->assertEqual($Version->getVersion('mocks'), 7); // version => 3 $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 7)); $this->assertEqual($Version->getVersion('mocks'), 7); $this->assertTrue($Version->run(array('version' => 3, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); // version => 10 (top version) $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3)); $this->assertEqual($Version->getVersion('mocks'), 3); $this->assertTrue($Version->run(array('version' => 10, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), range(1, 10)); $this->assertEqual($Version->getVersion('mocks'), 10); // version => 0 (run down all migrations) $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 10)); $this->assertEqual($Version->getVersion('mocks'), 10); $this->assertTrue($Version->run(array('version' => 0, 'type' => 'mocks'))); $this->assertEqual($this->__migrated(), array()); $this->assertEqual($Version->getVersion('mocks'), 0); // Changing values back $this->Version = $back; unset($back); }
/** * TestGetVersionByName method * * @return void */ public function testGetVersionByName() { $Version = new MigrationVersion(array('jumpTo' => '007_schema_dump')); $result = $Version->getVersionByName($this->_mapping()); $this->assertEquals(7, $result); $Version = new MigrationVersion(array('jumpTo' => '00_schema_dump')); $result = $Version->getVersionByName($this->_mapping()); $this->assertFalse($result); }
/** * 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)); } } }