Example #1
0
 /**
  * Checks the "data/new" folder for new SQL files. If it finds some (and 
  * they're being committed), it moves them to the appropriate folder and 
  * makes a git commit automatically.
  * 
  */
 protected function createDatabaseDelta()
 {
     $database_dir = $this->getDatabaseDirectory();
     //open the application build sequence for writing
     if (!$this->calledFromPlugin()) {
         $xml_build_sequence = $this->getApplicationBuildSequence();
     } else {
         $xml_build_sequence = $this->getAltumoBuildSequence();
     }
     //creates a new application "database build" if there is a delta
     //get the last commit hash
     $last_commit_hash = \Altumo\Git\History::getLastCommitHash();
     //check to see if there are any new scripts in the "new" folder
     $sql_files = $this->getFilesToMove($last_commit_hash);
     if (empty($sql_files)) {
         //no sql files to move
         return;
     }
     //move the files to the appropriate place and auto-commit
     $move_commands = array();
     $has_snapshot = false;
     $has_drop = false;
     $has_upgrade = false;
     $has_data_update = false;
     foreach ($sql_files as $sql_file) {
         //detach the extension */
         if (preg_match('%^(.*/)(.*?)(\\.(sql|php))?$%im', $sql_file, $regs)) {
             $file_path = $regs[1];
             $file_stub = $regs[2];
             $file_extension = $regs[3];
             $file_base_name = $file_stub . $file_extension;
         } else {
             throw new \sfCommandException(sprintf('Cannot find file with extension for "%s".', $sql_file));
         }
         //assemble the new filename
         switch ($file_base_name) {
             case 'drop.sql':
                 $new_filename = $database_dir . '/drops/' . $file_stub . '_' . $last_commit_hash . $file_extension;
                 $has_drop = true;
                 break;
             case 'upgrade_script.sql':
                 $new_filename = $database_dir . '/upgrade_scripts/' . $file_stub . '_' . $last_commit_hash . $file_extension;
                 $has_upgrade = true;
                 break;
             case 'data_update.php':
                 $new_filename = $database_dir . '/data_updates/' . $file_stub . '_' . $last_commit_hash . $file_extension;
                 $has_data_update = true;
                 break;
             case 'snapshot.sql':
                 $new_filename = $database_dir . '/snapshots/' . $file_stub . '_' . $last_commit_hash . $file_extension;
                 $has_snapshot = true;
                 break;
             default:
                 throw new \sfCommandException(sprintf('Error: unknown filetype (%s).', $file_base_name));
         }
         //move the file and add it to the git repository
         $move_commands[] = 'git mv ' . $sql_file . ' ' . $new_filename;
     }
     //this is in a separate loop so there are no moves executed if one
     //of the files fails to validate
     foreach ($move_commands as $move_command) {
         `{$move_command}`;
     }
     //update the build sequence log
     $xml_build_sequence->addChange($last_commit_hash, $has_upgrade, $has_data_update, $has_drop, $has_snapshot);
     $xml_build_sequence->writeToFile();
     $build_sequence_filename = $xml_build_sequence->getFilename();
     $shell_command = "git add {$build_sequence_filename}";
     `{$shell_command}`;
     //commit the files
     $shell_command = 'git commit -m "Autocommit: moving sql files to appropriate locations for commit ' . $last_commit_hash . '"';
     `{$shell_command}`;
 }
 /**
  * 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);
 }