コード例 #1
0
 /**
  * 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();
 }
コード例 #2
0
 /**
  * Fetches a list of files and adds migrations to the database migrations table.
  *
  * @uses MpmListHelper::getListOfFiles()
  * @uses MpmListHelper::getTotalMigrations()
  * @uses MpmListHelper::getFullList()
  * @uses MpmListHelper::getTimestampArray()
  * @uses MpmDbHelper::getMethod()
  * @uses MpmDbHelper::getPdoObj()
  * @uses MpmDbHelper::getMysqliObj()
  * @uses MPM_METHOD_PDO
  *
  * @return void
  */
 static function mergeFilesWithDb()
 {
     $db_config = $GLOBALS['db_config'];
     $migrations_table = $db_config->migrations_table;
     $files = MpmListHelper::getListOfFiles();
     $total_migrations = MpmListHelper::getTotalMigrations();
     $db_list = MpmListHelper::getFullList(0, $total_migrations);
     $file_timestamps = MpmListHelper::getTimestampArray($files);
     if (MpmDbHelper::getMethod() == MPM_METHOD_PDO) {
         if (count($files) > 0) {
             $pdo = MpmDbHelper::getPdoObj();
             $pdo->beginTransaction();
             try {
                 foreach ($files as $file) {
                     $sql = "INSERT IGNORE INTO `{$migrations_table}` ( `timestamp`, `active`, `is_current` ) VALUES ( '{$file->timestamp}', 0, 0 )";
                     $pdo->exec($sql);
                 }
             } catch (Exception $e) {
                 $pdo->rollback();
                 echo "\n\nError: " . $e->getMessage();
                 echo "\n\n";
                 exit;
             }
             $pdo->commit();
         }
         if (count($db_list)) {
             $pdo->beginTransaction();
             try {
                 foreach ($db_list as $obj) {
                     if (!in_array($obj->timestamp, $file_timestamps) && $obj->active == 0) {
                         $sql = "DELETE FROM `{$migrations_table}` WHERE `id` = '{$obj->id}'";
                         $pdo->exec($sql);
                     }
                 }
             } catch (Exception $e) {
                 $pdo->rollback();
                 echo "\n\nError: " . $e->getMessage();
                 echo "\n\n";
                 exit;
             }
             $pdo->commit();
         }
     } else {
         $mysqli = MpmDbHelper::getMysqliObj();
         $mysqli->autocommit(false);
         if (count($files) > 0) {
             try {
                 $stmt = $mysqli->prepare('INSERT IGNORE INTO `' . $migrations_table . '` ( `timestamp`, `active`, `is_current` ) VALUES ( ?, 0, 0 )');
                 foreach ($files as $file) {
                     $stmt->bind_param('s', $file->timestamp);
                     $result = $stmt->execute();
                     if ($result === false) {
                         throw new Exception('Unable to execute query to update file list.');
                     }
                 }
             } catch (Exception $e) {
                 $mysqli->rollback();
                 $mysqli->close();
                 echo "\n\nError:", $e->getMessage(), "\n\n";
                 exit;
             }
             $mysqli->commit();
         }
         if (count($db_list)) {
             try {
                 $stmt = $mysqli->prepare('DELETE FROM `' . $migrations_table . '` WHERE `id` = ?');
                 foreach ($db_list as $obj) {
                     if (!in_array($obj->timestamp, $file_timestamps) && $obj->active == 0) {
                         $stmt->bind_param('i', $obj->id);
                         $result = $stmt->execute();
                         if ($result === false) {
                             throw new Exception('Unable to execute query to remove stale files from the list.');
                         }
                     }
                 }
             } catch (Exception $e) {
                 $mysqli->rollback();
                 $mysqli->close();
                 echo "\n\nError: " . $e->getMessage();
                 echo "\n\n";
                 exit;
             }
             $mysqli->commit();
         }
         $mysqli->close();
     }
 }
コード例 #3
0
 /**
  * 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();
 }