/**
  * Parse command line options
  *
  * @usage list($forced, $dryrun) = $this->parse_options($this->arguments);
  *
  * @param array $options
  * @return array
  */
 public function parse_options($options = array())
 {
     $forced = $dryrun = false;
     foreach ($options as $option) {
         switch ($option) {
             case '--force':
             case '-f':
                 $forced = true;
                 break;
             case '--dry-run':
             case '--dryrun':
             case '-p':
                 $dryrun = true;
                 break;
                 // dump SQL log file
             // dump SQL log file
             case '--dump-sql':
             case '--dumpsql':
             case '-d':
                 MpmSqlLogger::set_enable(true);
                 break;
         }
     }
     return array($forced, $dryrun);
 }
 /**
  * Wrapper for the mysqli::query method.
  *
  * @throws MpmMalformedQueryException
  *
  * @param string $query      the SQL query to send to MySQL
  * @param int    $resultMode Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior
  * @param bool   $is_internal set to true for internal SQL calls which won't each SQL back when in dry run mode
  *
  * @return mysqli_result
  */
 public function query($query, $resultMode = MYSQLI_STORE_RESULT, $is_internal = false)
 {
     if (!$is_internal) {
         // Log to sql log file
         MpmSqlLogger::log_to_file($query);
     }
     if ($this->dryrun) {
         if (!$is_internal) {
             echo "\nSQL: " . $query . "\n";
         }
         return true;
     } else {
         // not dry-run
         $result = parent::query($query, $resultMode);
         if ($this->errno) {
             throw new MpmMalformedQueryException($this->error);
         }
         return $result;
     }
 }
Example #3
0
     * Flag to use MySQLi to talk to the database.
     */
    define('MPM_METHOD_MYSQLI', 2);
}
if (!defined('STDIN')) {
    /**
     * In some cases STDIN built-in can be undefined
     */
    define('STDIN', fopen("php://stdin", "r"));
}
/**
 * Include the MpmClassUndefinedException class.
 */
require_once MPM_PATH . '/lib/exceptions/class_undefined_exception.php';
/** 
 * Include the MpmStringHelper class.
 */
require_once MPM_PATH . '/lib/helpers/string_helper.php';
/** 
 * Include the MpmAutoloadHelper class.
 */
require_once MPM_PATH . '/lib/helpers/autoload_helper.php';
// add default autoloader function to the autoload stack
if (function_exists('__autoload')) {
    spl_autoload_register('__autoload');
}
// add custom library autoloader to the stack
spl_autoload_register('MpmAutoloadHelper::load');
// Remove SQL log file if any
MpmSqlLogger::remove_file();
 /**
  * @static
  * @param object $obj		a simple object with migration information (from a migration list)
  * @param string $method
  * @param array $options
  * @return mixed
  */
 public static function runMigration(&$obj, $method = 'up', array $options = array())
 {
     // if true, exceptions will not cause the script to exit
     $forced = isset($options['forced']) ? $options['forced'] : false;
     // if true, only echo back the SQL to be run
     $dryrun = isset($options['dryrun']) ? $options['dryrun'] : false;
     $db_config = MpmDbHelper::get_db_config();
     $migrations_table = $db_config->migrations_table;
     $filename = MpmStringHelper::getFilenameFromTimestamp($obj->timestamp);
     $classname = 'Migration_' . str_replace('.php', '', $filename);
     // make sure the file exists; if it doesn't, skip it but display a message
     $migration_file = MpmListHelper::get_migration_file(MPM_DB_PATH . $filename);
     if (!$migration_file) {
         echo "\n\tMigration " . $obj->timestamp . ' (ID ' . $obj->id . ') skipped - file missing.';
         return;
     }
     // file exists -- run the migration
     require_once $migration_file;
     $migration = new $classname();
     $msg = "# Performing " . strtoupper($method) . " migration " . $obj->timestamp . ' (ID ' . $obj->id . ')';
     if (!empty($migration->info)) {
         $msg .= "\n# {$migration->info}";
     }
     echo "\n" . $msg . " ... ";
     MpmSqlLogger::log_to_file("\n" . $msg . "\n");
     if ($migration instanceof MpmMigration) {
         $dbObj = MpmDbHelper::getPdoObj();
     } else {
         $dbObj = MpmDbHelper::getMysqliObj();
     }
     if ($dryrun) {
         $dbObj->dryrun = true;
     }
     $dbObj->beginTransaction();
     if ($method == 'down') {
         $active = 0;
     } else {
         $active = 1;
     }
     try {
         $migration->{$method}($dbObj);
         $sql = "UPDATE `{$migrations_table}` SET `active` = '{$active}' WHERE `id` = {$obj->id}";
         $dbObj->internal_exec($sql);
     } catch (Exception $e) {
         $dbObj->rollback();
         echo "failed!";
         echo "\n";
         $clw = MpmCommandLineWriter::getInstance();
         $clw->writeLine($e->getMessage(), 12);
         if (!$forced) {
             echo "\n\n";
             // Return with non-0 error code to notify external caller to stop the deployment
             exit(1);
         } else {
             return;
         }
     }
     $dbObj->commit();
     echo "done.";
 }
 public static function set_enable($enabled)
 {
     self::$enabled = $enabled;
 }