/**
  * Determines what action should be performed and takes that action.
  *
  * @uses MpmLatestController::displayHelp()
  * @uses MpmDbHelper::test()
  * @uses MpmMigrationHelper::getMigrationCount()
  * @uses MpmCommandLineWriter::getInstance()
  * @uses MpmMigrationHelper::getLatestMigration()
  * @uses MpmUpController::doAction()
  *
  * @param bool $quiet supresses certain text when true
  *
  * @return void
  */
 public function doAction($quiet = false)
 {
     // make sure we're init'd
     MpmDbHelper::test();
     // are we forcing this?
     $forced = '';
     if (isset($this->arguments[0]) && strcasecmp($this->arguments[0], '--force') == 0) {
         $forced = '--force';
     }
     try {
         $total_migrations = MpmMigrationHelper::getMigrationCount();
         if ($total_migrations == 0) {
             $clw = MpmCommandLineWriter::getInstance();
             $clw->addText('No migrations exist.');
             $clw->write();
         } else {
             $to_id = MpmMigrationHelper::getLatestMigration();
             $obj = new MpmUpController('up', array($to_id, $forced));
             $obj->doAction($quiet);
         }
     } catch (Exception $e) {
         echo "\n\nERROR: " . $e->getMessage() . "\n\n";
         exit;
     }
 }
 /**
  * Determines what action should be performed and takes that action.
  *
  * @uses MpmLatestController::displayHelp()
  * @uses MpmDbHelper::test()
  * @uses MpmMigrationHelper::getMigrationCount()
  * @uses MpmCommandLineWriter::getInstance()
  * @uses MpmMigrationHelper::getLatestMigration()
  * @uses MpmUpController::doAction()
  *
  * @param bool $quiet supresses certain text when true
  *
  * @return void
  */
 public function doAction($quiet = false)
 {
     // make sure we're init'd
     MpmDbHelper::test();
     try {
         $total_migrations = MpmMigrationHelper::getMigrationCount();
         if ($total_migrations == 0) {
             $clw = MpmCommandLineWriter::getInstance();
             $clw->addText('No migrations exist.');
             $clw->write();
         } else {
             $to_id = MpmMigrationHelper::getLatestMigration();
             $obj = new MpmUpController('up', array_merge(array($to_id), $this->arguments));
             $obj->doAction($quiet);
         }
     } catch (Exception $e) {
         echo "\n\nERROR: " . $e->getMessage() . "\n\n";
         exit;
     }
 }
 /**
  * Does the actual task of destroying and rebuilding the database from the ground up.
  *
  * @uses MpmSchema::destroy()
  * @uses MpmSchema::reloadMigrations()
  * @uses MpmSchema::build()
  * @uses MpmLatestController::doAction()
  * @uses MPM_DB_PATH
  * @param bool $with_data	whether or not to run the test_data.php file after build
  */
 public function build($with_data = false)
 {
     // parse other optional arguments
     list($forced, $dryrun) = $this->parse_options($this->arguments);
     require_once MPM_DB_PATH . 'schema.php';
     $obj = new MpmInitialSchema($dryrun);
     $obj->destroy();
     echo "\n";
     $obj->reloadMigrations();
     if (!$dryrun) {
         echo "\n", 'Building initial database schema... ';
         $obj->build();
         echo 'done.', "\n\n", 'Applying migrations... ';
     }
     try {
         $total_migrations = MpmMigrationHelper::getMigrationCount();
         if ($total_migrations == 0) {
             echo "no migrations exist.";
         } else {
             $to_id = MpmMigrationHelper::getLatestMigration();
             $obj = new MpmUpController('up', array_merge(array($to_id), $this->arguments));
             $obj->doAction();
         }
     } catch (Exception $e) {
         echo "\n\nERROR: " . $e->getMessage() . "\n\n";
         exit;
     }
     if (!$dryrun && $with_data) {
         require_once MPM_DB_PATH . 'test_data.php';
         echo "\n\nInserting test data... ";
         $test_data_obj = new MpmTestData();
         $test_data_obj->build();
         echo 'done.';
     }
     echo "\n\n", 'Database build complete.', "\n";
 }
 /**
  * Does the actual task of destroying and rebuilding the database from the ground up.
  *
  * @uses MpmSchema::destroy()
  * @uses MpmSchema::reloadMigrations()
  * @uses MpmSchema::build()
  * @uses MpmLatestController::doAction()
  * @uses MPM_DB_PATH
  *
  * @param bool $with_data whether or not to run the test_data.php file after build
  *
  * @return void
  */
 public function build($with_data)
 {
     require_once MPM_DB_PATH . 'schema.php';
     $obj = new MpmInitialSchema();
     $obj->destroy();
     echo "\n";
     $obj->reloadMigrations();
     echo "\n", 'Building initial database schema... ';
     $obj->build();
     echo 'done.', "\n\n", 'Applying migrations... ';
     try {
         $total_migrations = MpmMigrationHelper::getMigrationCount();
         if ($total_migrations == 0) {
             echo "no migrations exist.";
         } else {
             $to_id = MpmMigrationHelper::getLatestMigration();
             $obj = new MpmUpController('up', array($to_id, $forced));
             $obj->doAction($quiet);
         }
     } catch (Exception $e) {
         echo "\n\nERROR: " . $e->getMessage() . "\n\n";
         exit;
     }
     if ($with_data) {
         require_once MPM_DB_PATH . 'test_data.php';
         echo "\n\nInserting test data... ";
         $test_data_obj = new MpmTestData();
         $test_data_obj->build();
         echo 'done.';
     }
     echo "\n\n", 'Database build complete.', "\n";
 }
 /**
  * Determines what action should be performed and takes that action.
  *
  * @uses MpmLatestController::displayHelp()
  * @uses MpmDbHelper::test()
  * @uses MpmMigrationHelper::getMigrationCount()
  * @uses MpmCommandLineWriter::getInstance()
  * @uses MpmMigrationHelper::getLatestMigration()
  * @uses MpmUpController::doAction()
  * 
  * @param bool $quiet supresses certain text when true
  *
  * @return void
  */
 public function doAction($quiet = false)
 {
     // make sure we're init'd
     MpmDbHelper::test();
     $clw = MpmCommandLineWriter::getInstance();
     $clw->writeHeader();
     // are we forcing this?
     $forced = '';
     if (isset($this->arguments[0]) && strcasecmp($this->arguments[0], '--force') == 0) {
         $forced = '--force';
     }
     //get completed migrations from database
     //get migrations from file
     $oldest_migration_id = MpmMigrationHelper::getOldestMigration();
     $current_timestamp = MpmMigrationHelper::getCurrentMigrationTimestamp();
     $current_num = MpmMigrationHelper::getCurrentMigrationNumber();
     //$latest_num =  MpmMigrationHelper::getLatestMigration();
     //$latest_timestamp = MpmMigrationHelper::getTimestampFromId($latest_num);
     $db_migrations = MpmListHelper::getListFromDb($current_timestamp, 'down');
     $files = MpmListHelper::getListOfFiles();
     $all_file_timestamps = MpmListHelper::getTimestampArray($files);
     $file_timestamps = array();
     foreach ($all_file_timestamps as $timestamp) {
         if ($timestamp <= $current_timestamp) {
             $file_timestamps[] = $timestamp;
         }
     }
     end($file_timestamps);
     $latest_timestamp = current($file_timestamps);
     //compare timestamps that are in either array to timestamps that are in both arrays to find missing timestamps in either
     //$missing_merges = array_diff(array_unique( array_merge($file_timestamps,$db_migrations) ), array_intersect($file_timestamps,$db_migrations) );
     $missing_database = array_diff($file_timestamps, $db_migrations);
     $missing_files = array_diff($db_migrations, $file_timestamps);
     $missing_merges = array_merge($missing_files, $missing_database);
     sort($missing_merges);
     reset($missing_merges);
     $oldest_missing = current($missing_merges);
     try {
         if ($oldest_missing && $oldest_missing <= $current_timestamp) {
             $previous_migration = MpmMigrationHelper::getNextTimestamp($oldest_missing, 'down');
             if ($previous_migration) {
                 $target_down = $previous_migration->id;
             } else {
                 $target_down = -1;
             }
             $down = new MpmDownController('down', array($target_down, $forced, true));
             $down->doAction($quiet);
         }
         //merge files with database
         MpmListHelper::mergeFilesWithDb();
         $newest_id = MpmMigrationHelper::getLatestMigration();
         if ($newest_id) {
             $newest_timestamp = MpmMigrationHelper::getTimestampFromId($newest_id);
             $current_timestamp = MpmMigrationHelper::getCurrentMigrationTimestamp();
             if ($newest_timestamp > $current_timestamp) {
                 $obj = new MpmUpController('up', array($newest_id, $forced, true));
                 $obj->doAction($quiet);
             } else {
                 echo "\nUp to Date";
             }
         } else {
             echo "\nUp to Date";
         }
     } catch (Exception $e) {
         echo "\n\nERROR: " . $e->getMessage() . "\n\n";
         exit;
     }
     $clw->writeFooter();
 }