/** * Object constructor. * * @uses MpmDbHelper::test() * @uses MpmListHelper::mergeFilesWithDb() * * @param array $arguments an array of command line arguments (minus the first two elements which should already be shifted off from the MpmControllerFactory) * * @return MpmController */ public function __construct($command = 'help', $arguments = array()) { $this->arguments = $arguments; $this->command = $command; if ($command != 'help' && $command != 'init') { MpmDbHelper::test(); MpmListHelper::mergeFilesWithDb(); } }
/** * Clears the migrations table and then rebuilds it. * * @uses MpmListHelper::mergeFilesWithDb() * @uses MpmDbHelper::doSingleRowSelect() * * @return void */ public function reloadMigrations() { echo 'Clearing out existing migration data... '; $this->dbObj->exec('TRUNCATE TABLE `mpm_migrations`'); echo 'done.', "\n\n", 'Rebuilding migration data... '; MpmListHelper::mergeFilesWithDb(); echo 'done.', "\n"; if ($this->initialMigrationTimestamp != null) { echo "\n", 'Updating initial migration timestamp to ', $this->initialMigrationTimestamp, '... '; $result = MpmDbHelper::doSingleRowSelect('SELECT COUNT(*) AS total FROM `mpm_migrations` WHERE `timestamp` = "' . $this->initialMigrationTimestamp . '"', $this->dbObj); if ($result->total == 1) { $this->dbObj->exec('UPDATE `mpm_migrations` SET `is_current` = 0'); $this->dbObj->exec('UPDATE `mpm_migrations` SET `is_current` = 1 WHERE `timestamp` = "' . $this->initialMigrationTimestamp . '"'); $this->dbObj->exec('UPDATE `mpm_migrations` SET `active` = 1 WHERE `timestamp` <= "' . $this->initialMigrationTimestamp . '"'); } echo 'done.', "\n"; } }
/** * Clears the migrations table and then rebuilds it. * * @uses MpmListHelper::mergeFilesWithDb() * @uses MpmDbHelper::doSingleRowSelect() * * @return void */ public function reloadMigrations() { if ($this->dryrun) { echo "No clear out existing migration data for dry-run.\n"; } else { $db_config = MpmDbHelper::get_db_config(); $migrations_table = $db_config->migrations_table; echo 'Clearing out existing migration data... '; $this->dbObj->internal_exec('TRUNCATE TABLE `' . $migrations_table . '`'); echo 'done.', "\n\n", 'Rebuilding migration data... '; MpmListHelper::mergeFilesWithDb(); echo 'done.', "\n"; if ($this->initialMigrationTimestamp != null) { echo "\n", 'Updating initial migration timestamp to ', $this->initialMigrationTimestamp, '... '; $result = MpmDbHelper::doSingleRowSelect('SELECT COUNT(*) AS total FROM `' . $migrations_table . '` WHERE `timestamp` = "' . $this->initialMigrationTimestamp . '"', $this->dbObj); if ($result->total == 1) { $this->dbObj->internal_exec('UPDATE `' . $migrations_table . '` SET `is_current` = 0'); $this->dbObj->internal_exec('UPDATE `' . $migrations_table . '` SET `is_current` = 1 WHERE `timestamp` = "' . $this->initialMigrationTimestamp . '"'); $this->dbObj->internal_exec('UPDATE `' . $migrations_table . '` SET `active` = 1 WHERE `timestamp` <= "' . $this->initialMigrationTimestamp . '"'); } echo 'done.', "\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(); }