/** * 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 MpmDownController::displayHelp() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmMigrationHelper::getListOfMigrations() * @uses MpmMigrationHelper::getCurrentMigrationNumber() * @uses MpmMigrationHelper::getTimestampFromId() * @uses MpmMigrationHelper::runMigration() * @uses MpmMigrationHelper::setCurrentMigration() * * @return void */ public function doAction($quiet = false) { // write the header $clw = MpmCommandLineWriter::getInstance(); $skip_headers = false; if (!empty($this->arguments[2])) { $skip_headers = true; } if (!$quiet && !$skip_headers) { $clw->writeHeader(); } // correct number of command line arguments? if (count($this->arguments) == 0) { return $this->displayHelp(); } // ID of the migration we are going down to $down_to = $this->arguments[0]; if (!is_numeric($down_to)) { return $this->displayHelp(); } if ($down_to == 0) { $down_to = -1; } // are we forcing this? $forced = false; if (isset($this->arguments[1]) && strcasecmp($this->arguments[1], '--force') == 0) { $forced = true; } // get list of migrations and the current migration number $list = MpmMigrationHelper::getListOfMigrations($down_to, 'down'); $total = count($list); $current = MpmMigrationHelper::getCurrentMigrationNumber(); if ($down_to == '-1') { if (!$quiet) { echo "Removing all migrations... "; } $down_to = 0; } else { if (!$quiet) { echo "Migrating to " . MpmMigrationHelper::getTimestampFromId($down_to) . ' (ID ' . $down_to . ')... '; } } foreach ($list as $id => $obj) { MpmMigrationHelper::runMigration($obj, 'down', $forced); } MpmMigrationHelper::setCurrentMigration($down_to); if (!$skip_headers && !$quiet) { echo "\n"; $clw->writeFooter(); } }
/** * Determines what action should be performed and takes that action. * * @uses MpmUpController::displayHelp() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmCommandLineWriter::writeFooter() * @uses MpmMigrationHelper::getListOfMigrations() * @uses MpmMigrationHelper::getTimestampFromId() * @uses MpmMigrationHelper::runMigration() * @uses MpmMigrationHelper::setCurrentMigration * * @param bool $quiet suppresses certain text when true * * @return void */ public function doAction($quiet = false) { $clw = MpmCommandLineWriter::getInstance(); $skip_headers = false; if (!empty($this->arguments[2])) { $skip_headers = true; } if (!$quiet && !$skip_headers) { $clw->writeHeader(); } if (count($this->arguments) == 0) { return $this->displayHelp(); } $up_to = $this->arguments[0]; if (!is_numeric($up_to)) { return $this->displayHelp(); } // are we forcing this? $forced = false; if (isset($this->arguments[1]) && strcasecmp($this->arguments[1], '--force') == 0) { $forced = true; } // what migrations need to be done? $list = MpmMigrationHelper::getListOfMigrations($up_to); if (count($list) == 0) { if (!$quiet) { echo "\nAll needed migrations have already been run or no migrations exist."; if (!$skip_headers) { $clw->writeFooter(); } return; } else { return; } } $to = MpmMigrationHelper::getTimestampFromId($up_to); if (!$quiet) { echo "\nMigrating to " . $to . ' (ID ' . $up_to . ')... '; } foreach ($list as $id => $obj) { MpmMigrationHelper::runMigration($obj, 'up', $forced); } MpmMigrationHelper::setCurrentMigration($up_to); if (!$quiet && !$skip_headers) { $clw->writeFooter(); } }
/** * Determines what action should be performed and takes that action. * * @uses MpmRunController::displayHelp() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::addText() * @uses MpmCommandLineWriter::write() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmCommandLineWriter::writeFooter() * @uses MpmMigrationHelper::doesMigrationExist() * @uses MpmMigrationHelper::getMigrationObject() * @uses MpmMigrationHelper::runMigration() * * @return void */ public function doAction() { // make sure system is init'ed MpmDbHelper::test(); if (count($this->arguments) != 2) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('ERROR: You must provide two arguments with this command.'); $obj->addText(' '); $this->displayHelp(); return; } // are we running the up or the down? $type = strtolower($this->arguments[0]); // what number do we want to run? $num = $this->arguments[1]; if (!is_numeric($num)) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('ERROR: Migration number must be numeric.'); $obj->addText(' '); $this->displayHelp(); return; } if ($type != 'up' && $type != 'down') { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('ERROR: Method must be either up or down.'); $obj->addText(' '); $this->displayHelp(); return; } // does this migration number exist? if (!MpmMigrationHelper::doesMigrationExist($num)) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('ERROR: Migration ' . $num . ' does not exist.'); $obj->write(); return; } $row = MpmMigrationHelper::getMigrationObject($num); $obj = MpmCommandLineWriter::getInstance(); $obj->writeHeader(); MpmMigrationHelper::runMigration($row, $type); echo "\n"; $obj->writeFooter(); }
/** * 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; } }
/** * Determines what action should be performed and takes that action. * * @uses MpmDbHelper::test() * @uses MpmMigrationHelper::getCurrentMigrationTimestamp() * @uses MpmMigrationHelper::getCurrentMigrationNumber() * @uses MpmListHelper::getFullList() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmCommandLineWriter::writeFooter() * * @return void */ public function doAction() { // make sure we're init'd MpmDbHelper::test(); // get latest timestamp $latest = MpmMigrationHelper::getCurrentMigrationTimestamp(); // get latest number $num = MpmMigrationHelper::getCurrentMigrationNumber(); // get list of migrations $list = MpmListHelper::getFullList(); // get command line writer $clw = MpmCommandLineWriter::getInstance(); $clw->writeHeader(); if (empty($latest)) { echo "You have not performed any migrations yet."; } else { echo "You are currently on migration {$num} -- " . $latest . '.'; } echo "\n"; $clw->writeFooter(); }
/** * Determines what action should be performed and takes that action. * * @uses MpmUpController::displayHelp() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmCommandLineWriter::writeFooter() * @uses MpmMigrationHelper::getListOfMigrations() * @uses MpmMigrationHelper::getTimestampFromId() * @uses MpmMigrationHelper::runMigration() * @uses MpmMigrationHelper::setCurrentMigration * * @param bool $quiet supresses certain text when true * * @return void */ public function doAction($quiet = false) { $clw = MpmCommandLineWriter::getInstance(); if (!$quiet) { $clw->writeHeader(); } if (count($this->arguments) == 0) { return $this->displayHelp(); } $up_to = $this->arguments[0]; if (!is_numeric($up_to)) { return $this->displayHelp(); } // parse other optional arguments list($forced, $dryrun) = $this->parse_options($this->arguments); // what migrations need to be done? $list = MpmMigrationHelper::getListOfMigrations($up_to); if (count($list) == 0) { if (!$quiet) { echo 'All needed migrations have already been run or no migrations exist.'; $clw->writeFooter(); exit; } else { return; } } $to = MpmMigrationHelper::getTimestampFromId($up_to); if (!$quiet) { echo "Migrating to " . $to . ' (ID ' . $up_to . ')... '; } foreach ($list as $id => $obj) { MpmMigrationHelper::runMigration($obj, 'up', array('forced' => $forced, 'dryrun' => $dryrun)); } MpmMigrationHelper::setCurrentMigration($up_to, $dryrun); if (!$quiet) { echo "\n"; $clw->writeFooter(); } }
/** * Determines what action should be performed and takes that action. * * @uses MpmDownController::displayHelp() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmMigrationHelper::getListOfMigrations() * @uses MpmMigrationHelper::getCurrentMigrationNumber() * @uses MpmMigrationHelper::getTimestampFromId() * @uses MpmMigrationHelper::runMigration() * @uses MpmMigrationHelper::setCurrentMigration() * * @return void */ public function doAction() { // write the header $clw = MpmCommandLineWriter::getInstance(); $clw->writeHeader(); // correct number of command line arguments? if (count($this->arguments) == 0) { return $this->displayHelp(); } // ID of the migration we are going down to $down_to = $this->arguments[0]; if (!is_numeric($down_to)) { return $this->displayHelp(); } if ($down_to == 0) { $down_to = -1; } // parse other optional arguments list($forced, $dryrun) = $this->parse_options($this->arguments); // get list of migrations and the current migration number $list = MpmMigrationHelper::getListOfMigrations($down_to, 'down'); $total = count($list); $current = MpmMigrationHelper::getCurrentMigrationNumber(); if ($down_to == '-1') { echo "Removing all migrations... "; $down_to = 0; } else { echo "Migrating to " . MpmMigrationHelper::getTimestampFromId($down_to) . ' (ID ' . $down_to . ')... '; } foreach ($list as $id => $obj) { MpmMigrationHelper::runMigration($obj, 'down', array('forced' => $forced, 'dryrun' => $dryrun)); } MpmMigrationHelper::setCurrentMigration($down_to, $dryrun); echo "\n"; $clw->writeFooter(); }
/** * 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"; }
/** * Returns an array of migrations which need to be run (in order). * * @uses MpmMigrationHelper::getTimestampFromId() * @uses MpmDbHelper::getMethod() * @uses MpmDbHelper::getPdoObj() * @uses MpmDbHelper::getMysqliObj() * @uses MPM_METHOD_MYSQLI * @uses MPM_METHOD_PDO * * @param int $toId the ID of the migration to stop on * @param string $direction the direction of the migration; should be 'up' or 'down' * * @return array */ public static function getListOfMigrations($toId, $direction = 'up') { $list = array(); $timestamp = MpmMigrationHelper::getTimestampFromId($toId); if ($direction == 'up') { $sql = "SELECT `id`, `timestamp` FROM `mpm_migrations` WHERE `active` = 0 AND `timestamp` <= '{$timestamp}' ORDER BY `timestamp`"; } else { $sql = "SELECT `id`, `timestamp`, `objectstore` FROM `mpm_migrations` WHERE `active` = 1 AND `timestamp` > '{$timestamp}' ORDER BY `timestamp` DESC"; } switch (MpmDbHelper::getMethod()) { case MPM_METHOD_PDO: try { $pdo = MpmDbHelper::getPdoObj(); $stmt = $pdo->query($sql); while ($obj = $stmt->fetch(PDO::FETCH_OBJ)) { $list[$obj->id] = $obj; } } catch (Exception $e) { echo "\n\nError: " . $e->getMessage() . "\n\n"; exit; } break; case MPM_METHOD_MYSQLI: try { $mysqli = MpmDbHelper::getMysqliObj(); $results = $mysqli->query($sql); while ($row = $results->fetch_object()) { $list[$row->id] = $row; } } catch (Exception $e) { echo "\n\nError: " . $e->getMessage() . "\n\n"; exit; } break; } return $list; }
/** * 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(); //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($all_file_timestamps); $latest_timestamp = current($all_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); $clw = MpmCommandLineWriter::getInstance(); $clw->writeHeader(); if (!$current_num) { echo 'You have not run any migrations'; } else { echo "You are currently on migration {$current_num} -- " . $current_timestamp . '.'; } if (!empty($missing_files)) { echo "\n\nCompleted migrations that are no longer in migrations directory\n----------\n"; foreach ($missing_files as $file) { echo " {$file}\n"; } } if (!empty($missing_database)) { echo "\n\nOld migrations that have not been run\n----------"; foreach ($missing_database as $db) { echo "\n {$db}"; } } if ($current_timestamp < $latest_timestamp) { echo "\nLatest migration is: {$latest_timestamp}."; } if ($oldest_missing && $oldest_missing <= $current_timestamp || $current_timestamp < $latest_timestamp) { echo "\n\n--- Migration Path --------------------------\n"; $post_down_timestamp = $current_timestamp; if ($oldest_missing && $oldest_missing <= $current_timestamp) { //find target down timestamp $previous_migration = MpmMigrationHelper::getNextTimestamp($oldest_missing, 'down'); if ($previous_migration) { $post_down_timestamp = $previous_migration->timestamp; echo "\n Migrate down to: {$previous_migration->id} -- {$previous_migration->timestamp}"; } else { $post_down_timestamp = 0; echo "\n Remove all migrations"; } } if ($post_down_timestamp < $latest_timestamp) { echo "\n Migrate up to latest: {$latest_timestamp}"; } } else { echo "\n\n You are up to date"; } /*$total_migrations = MpmMigrationHelper::getMigrationCount(); if ($total_migrations == 0) { $clw = MpmCommandLineWriter::getInstance(); $clw->addText('No migrations exist.'); $clw->write(); exit; } $to_id = MpmMigrationHelper::getLatestMigration(); $obj = new MpmUpController('up', array ( $to_id, $forced )); $obj->doAction($quiet); */ $clw->writeFooter(); }
/** * 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(); }