/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); $database_dir = sfConfig::get('sf_data_dir'); $default_build_sequence_file = $database_dir . '/build-sequence.xml'; $default_update_log_file = $database_dir . '/update-log.xml'; $default_updater_configuration_file = $database_dir . '/updater-configuration.xml'; $command = $arguments['command']; //initialize updater, if there is meaningful work to do if (in_array($command, array('update', 'drop'))) { $xml_build_sequence = new \sfAltumoPlugin\Build\DatabaseBuildSequenceFile($default_build_sequence_file); $xml_sf_altumo_build_sequence = new \sfAltumoPlugin\Build\DatabaseBuildSequenceFile($database_dir . '/../plugins/sfAltumoPlugin/data/build-sequence.xml'); $xml_update_log = new \sfAltumoPlugin\Deployment\DatabaseUpdateLogFile($default_update_log_file, false); $xml_updater_configuration = new \sfAltumoPlugin\Deployment\DatabaseUpdaterConfigurationFile($default_updater_configuration_file); $xml_updater_configuration->setDatabaseDirectory($database_dir); $database_updater = new \sfAltumoPlugin\Deployment\DatabaseUpdater($xml_updater_configuration, $xml_build_sequence, $xml_update_log, $xml_sf_altumo_build_sequence); } switch ($command) { case 'update': $ignore_working_tree_changes = \Altumo\Validation\Booleans::assertLooseBoolean($options['ignore-working-tree-changes']); $has_upcoming_data_updates = $database_updater->hasUpcomingDataUpdates(); if ($has_upcoming_data_updates && \Altumo\Git\Status::hasChanges() && !$ignore_working_tree_changes) { throw new \Exception('Your working tree currently has changes. You must commit or stage these before performing an update or pass the ignore-working-tree-changes flag.'); } $number_of_scripts_executed = $database_updater->update($arguments); break; case 'drop': $number_of_scripts_executed = $database_updater->drop($arguments); break; case 'init': $xml_updater_configuration = new \sfAltumoPlugin\Deployment\DatabaseUpdaterConfigurationFile($default_updater_configuration_file, false); $xml_update_log = new \sfAltumoPlugin\Deployment\DatabaseUpdateLogFile($default_update_log_file, false); $number_of_scripts_executed = 0; break; default: throw new sfCommandException(sprintf('Command "%s" does not exist.', $arguments['command'])); } $this->log($number_of_scripts_executed . ' scripts executed successfully.' . "\n"); }
/** * Applies a data update * * A data update can perform operations at the application level. They have * access to the same functionallity the application does. Data updates can * be used to modify data at the ORM level, or perform other operations * conveniently from code. * * @param string $hash //the commit hash of the delta * * @throws \Exception * // if the hash for the commit where the data update file was added cannot be found. */ protected function applyDataUpdate($hash) { // Remember the current position of the working tree. $current_position = \Altumo\Git\Status::getCurrentBranch(); // Get the hash where the file was auto-moved to its folder by the build system $autocommit_hash = \Altumo\Git\History::getCommitsAfterMatching("^Autocommit\\:\\smoving.*" . $hash, $hash); if (count($autocommit_hash) != 1) { throw new \Exception('Unable to find an Autocommit for hash \\"' . $hash . '\\"'); } $autocommit_hash = reset(array_keys($autocommit_hash)); // remember current pwd $old_pwd = getcwd(); // Get working tree root path $root_path = realpath(\sfConfig::get('sf_root_dir') . '/../../'); // Go to the commit where the data_update script was auto-committed \Altumo\Git\WorkingTree::checkout($autocommit_hash); // Update submodules chdir($root_path); \Altumo\Git\WorkingTree::updateSubmodulesRecursively(); //determine the php script filename and ensure the file exists $php_filename = $this->getDatabaseUpdaterConfigurationFile()->getDatabaseDirectory() . '/data_updates/' . 'data_update_' . $hash . '.php'; if (!file_exists($php_filename)) { throw new \Exception('Script File "' . $php_filename . '" does not exist.'); } // Call the altumo:apply-data-update in a separate process $project_path = realpath(\sfConfig::get('sf_root_dir')); chdir($project_path); $command = "./symfony altumo:apply-data-update {$hash}"; // execute command \Altumo\Utils\Shell::runWithPipedOutput($command); // return the tree to where it was before. \Altumo\Git\WorkingTree::checkout($current_position); // Update submodules chdir($root_path); \Altumo\Git\WorkingTree::updateSubmodulesRecursively(); chdir($old_pwd); $this->getDatabaseUpdateLogFile()->addDataUpdate($hash); }