Example #1
0
    function main() {
    	try{
    		// get correct DbmsSyntax object
    		$dbms = substr($this->url, 0, strpos($this->url, ':'));
    		$dbmsSyntaxFactory = new DbmsSyntaxFactory($dbms);
    		$this->dbmsSyntax = $dbmsSyntaxFactory->getDbmsSyntax();

			// open file handles for output
    		$outputFileHandle = fopen($this->outputFile, "w+");
    		$undoOutputFileHandle = fopen($this->undoOutputFile, "w+");

    		// figure out which revisions are in the db already
			$this->appliedChangeNumbers = $this->getAppliedChangeNumbers();
			$this->log('Current db revision: ' . $this->getLastChangeAppliedInDb());

			// generate sql file needed to take db to "lastChangeToApply" version
			$doSql = $this->doDeploy();
			$undoSql = $this->undoDeploy();

			// write the do and undo SQL to their respective files
			fwrite($outputFileHandle, $doSql);
			fwrite($undoOutputFileHandle, $undoSql);

    	} catch (Exception $e){
    		throw new BuildException($e);
    	}
    }
Example #2
0
 /**
  * The main function for the task
  *
  * @throws BuildException
  * @return void
  */
 public function main()
 {
     try {
         // get correct DbmsSyntax object
         $dbms = substr($this->url, 0, strpos($this->url, ':'));
         $dbmsSyntaxFactory = new DbmsSyntaxFactory($dbms);
         $this->dbmsSyntax = $dbmsSyntaxFactory->getDbmsSyntax();
         // figure out which revisions are in the db already
         $this->appliedChangeNumbers = $this->getAppliedChangeNumbers();
         $this->log('Current db revision: ' . $this->getLastChangeAppliedInDb());
         $this->log('Checkall: ' . ($this->checkall ? 'On' : 'Off'));
         $this->deploy();
     } catch (Exception $e) {
         throw new BuildException($e);
     }
 }
    function main()
    {
        try {
            // Suffix deltaset name with _php so we don't conflict with SQL migrations
            $this->deltaSet .= '_php';
            // Get correct DbmsSyntax object
            $dbms = substr($this->url, 0, strpos($this->url, ':'));
            $dbmsSyntaxFactory = new DbmsSyntaxFactory($dbms);
            $this->dbmsSyntax = $dbmsSyntaxFactory->getDbmsSyntax();
            // Figure out which revisions are in the db already
            $this->appliedChangeNumbers = $this->getAppliedChangeNumbers();
            $lastChangeAppliedInDb = $this->getLastChangeAppliedInDb();
            $this->log('Current db revision: ' . $lastChangeAppliedInDb);
            // Generate sql file needed to take db to "lastChangeToApply" version
            $deploySql = $this->doDeploy();
            file_put_contents($this->outputFile . '.sql', $deploySql);
            $undoSql = $this->undoDeploy();
            file_put_contents($this->undoOutputFile . '.sql', $undoSql);
            $files = $this->getDeltasFilesArray();
            if (count($files)) {
                ksort($files);
                $calls = array('migrate' => array(), 'undo' => array());
                $output = array('#!/usr/bin/env php', '<?php', 'set_include_path("' . get_include_path() . '");');
                foreach ($files as $fileChangeNumber => $fileName) {
                    if (strpos($fileName, '.php') === strlen($fileName) - 4 && $fileChangeNumber > $lastChangeAppliedInDb && $fileChangeNumber <= $this->lastChangeToApply) {
                        // Load file content
                        $contents = file_get_contents($this->dir . '/' . $fileName);
                        // Replace "function migrate(" with "function migrate_$fileChangeNumber("
                        $contents = preg_replace('/function\\s+migrate\\s*\\(/', 'function migrate_' . $fileChangeNumber . '(', $contents);
                        // Replace "function undo(" with "function undo_$fileChangeNumber("
                        $contents = preg_replace('/function\\s+undo\\s*\\(/', 'function undo_' . $fileChangeNumber . '(', $contents);
                        // Replace any instance of '<?php' with ''
                        $contents = str_replace('<?php', '', $contents);
                        /* Replace any instance of '?>' with '' */
                        $contents = str_replace('?>', '', $contents);
                        // Concat modified contents
                        $output[] = $contents;
                        $calls['migrate'][] = 'migrate_' . $fileChangeNumber . '();';
                        $calls['undo'][] = 'undo_' . $fileChangeNumber . '();';
                    }
                }
                // Generate single migrate() and undo() calls that reference above calls
                $migrate = "\nfunction migrate() {\n";
                $migrate .= implode("\n", $calls['migrate']);
                $migrate .= "}\n";
                $output[] = $migrate;
                $undo = "\nfunction undo() {\n";
                $undo .= implode("\n", $calls['undo']);
                $undo .= "}\n";
                $output[] = $undo;
                // Generate command line handler for migrate() and undo()
                $handler = <<<EOT
switch (\$argv[1]) {
case 'migrate':
    migrate();
    exit(0);
case 'undo':
    undo();
    exit(0);
default:
    echo 'No command specified, must be migrate or undo';
    exit(1);
}
EOT;
                $output[] = $handler;
                // Write file out
                file_put_contents($this->outputFile, implode("\n", $output));
                $this->log('Output PHP migration script to: ' . $this->outputFile);
            }
        } catch (Exception $e) {
            throw new BuildException($e);
        }
    }