/** * @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."; }
/** * Performs a single migration. * * @uses MpmStringHelper::getFilenameFromTimestamp() * @uses MpmDbHelper::getPdoObj() * @uses MpmDbHelper::getMysqliObj() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::writeLine() * @uses MPM_DB_PATH * * @param object $obj a simple object with migration information (from a migration list) * @param int &$total_migrations_run a running total of migrations run * @param bool $forced if true, exceptions will not cause the script to exit * * @return void */ public static function runMigration(&$obj, $method = 'up', $forced = false) { $db_config = $GLOBALS['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 if (!file_exists(MPM_DB_PATH . $filename)) { echo "\n\tMigration " . $obj->timestamp . ' (ID ' . $obj->id . ') skipped - file missing.'; return; } // file exists -- run the migration echo "\n\tPerforming " . strtoupper($method) . " migration " . $obj->timestamp . ' (ID ' . $obj->id . ')... '; require_once MPM_DB_PATH . $filename; $migration = new $classname(); if ($migration instanceof MpmMigration) { $dbObj = MpmDbHelper::getPdoObj(); } else { $dbObj = MpmDbHelper::getMysqliObj(); } $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->exec($sql); } catch (Exception $e) { $dbObj->rollback(); echo "failed!"; echo "\n"; $clw = MpmCommandLineWriter::getInstance(); $clw->writeLine($e->getMessage(), 12); if (!$forced) { echo "\n\n"; exit; } else { return; } } $dbObj->commit(); echo "done."; }
/** * Performs a single migration. * * @uses MpmStringHelper::getFilenameFromTimestamp() * @uses MpmDbHelper::getPdoObj() * @uses MpmDbHelper::getMysqliObj() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::writeLine() * @uses MPM_DB_PATH * * @param object $obj a simple object with migration information (from a migration list) * @param int &$total_migrations_run a running total of migrations run * @param bool $forced if true, exceptions will not cause the script to exit * * @return void */ public static function runMigration(&$obj, $method = 'up', $forced = false) { $file_timestamp = MpmStringHelper::getFilenameFromTimestamp($obj->timestamp); if ($method == 'up') { $classname = 'Migration_' . $file_timestamp; // make sure the file exists; if it doesn't, skip it but display a message $files = glob(MPM_DB_PATH . $file_timestamp . '*.php'); if (empty($files)) { echo "\n\tMigration " . $obj->timestamp . ' (ID ' . $obj->id . ') skipped - file missing.'; return; } if (count($files) > 1) { echo "\n\tError: Duplicate migration timestamp found! " . $obj->timestamp . ' (ID ' . $obj->id . ')'; exit; } $filename = $files[0]; // file exists -- run the migration echo "\n\tPerforming " . strtoupper($method) . " migration " . $obj->timestamp . ' (ID ' . $obj->id . ')... '; require_once $filename; $migration = new $classname(); } else { //migrate down via stored database objects //fetch object from database //unserialize eval('?>' . $obj->objectstore . '<?'); $down_migration = 'Migration_objectstore_' . $file_timestamp; $migration = new $down_migration(); if (!$migration) { echo "failed!"; $clw = MpmCommandLineWriter::getInstance(); $clw->writeLine("Migration " . $obj->timestamp . ' (ID ' . $obj->id . ') - Object missing or broken', 12); if (!$forced) { echo "\n\n"; exit; } else { return; } } echo "\n\tPerforming " . strtoupper($method) . " migration " . $obj->timestamp . ' (ID ' . $obj->id . ')... '; } if ($migration instanceof MpmMigration) { $dbObj = MpmDbHelper::getPdoObj(); } else { $dbObj = MpmDbHelper::getMysqliObj(); } $dbObj->beginTransaction(); if ($method == 'down') { $active = 0; } else { $active = 1; } try { $migration->{$method}($dbObj); if ($method == 'up') { //fetch object, store in database $string = file_get_contents($filename); $string = preg_replace('/(class Migration_)(\\d{4}(?:_\\d{2}){5})/', '$1objectstore_$2', $string, 1); $query_serial = sprintf(', objectstore="%s" ', $dbObj->real_escape_string($string)); $sql = "UPDATE `mpm_migrations` SET `active` = '{$active}' {$query_serial} WHERE `id` = {$obj->id}"; } else { //delete from database. //Old and new items may need to take same ID space in DB $sql = "DELETE FROM `mpm_migrations` WHERE `id` = {$obj->id}"; } $dbObj->exec($sql); } catch (Exception $e) { $dbObj->rollback(); echo "failed!"; echo "\n"; $clw = MpmCommandLineWriter::getInstance(); $clw->writeLine($e->getMessage(), 12); if (!$forced) { echo "\n\n"; exit; } else { return; } } $dbObj->commit(); echo "done."; }