/** * 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; } }
/** * 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(); } }
/** * Determines what action should be performed and takes that action. * * @uses MpmDbHelper::test() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::addText() * @uses MpmCommandLineWriter::write() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmCommandLineWriter::writeFooter() * @uses MpmBuildController::build() * @uses MPM_DB_PATH * * @return void */ public function doAction() { // make sure system is init'ed MpmDbHelper::test(); $this->clw = MpmCommandLineWriter::getInstance(); $with_data = $forced = $dryrun = false; // are we adding a schema file? if (isset($this->arguments[0])) { if ($this->arguments[0] == 'add') { $this->add(); // stop here exit; } else { if ($this->arguments[0] == 'with_data') { $with_data = true; // remove the first 'with_data' array_shift($this->arguments); } } } // parse other optional arguments list($forced, $dryrun) = $this->parse_options($this->arguments); // make sure the schema file exists if (!file_exists(MPM_DB_PATH . 'schema.php')) { $this->clw->addText('The schema file does not exist. Run this command with the "add" argument to create one (only a stub).'); $this->clw->write(); exit; } // make sure the test data file exists if ($with_data == true && !file_exists(MPM_DB_PATH . 'test_data.php')) { $this->clw->addText('The test data file does not exist. Run this command with the "add" argument to create one (only a stub).'); $this->clw->write(); exit; } $this->clw->writeHeader(); if (!$forced && !$dryrun) { echo "\nWARNING: IF YOU CONTINUE, ALL TABLES IN YOUR DATABASE WILL BE ERASED!"; echo "\nDO YOU WANT TO CONTINUE? [y/N] "; $answer = fgets(STDIN); $answer = trim($answer); $answer = strtolower($answer); if (empty($answer) || substr($answer, 0, 1) == 'n') { echo "\nABORTED!\n\n"; $this->clw->writeFooter(); exit; } } echo "\n"; $this->build($with_data); $this->clw->writeFooter(); exit; }
/** * 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 MPM_DB_PATH * @uses MpmDbHelper::test() * @uses MpmListHelper::getFiles() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::addText() * @uses MpmCommandLineWriter::write() * @uses MpmDbHelper::getMethod() * @uses MpmUpController::displayHelp() * * @return void */ public function doAction() { // make sure system is init'ed MpmDbHelper::test(); // get date stamp for use in generating filename $date_stamp = date('Y_m_d_H_i_s'); $filename = $date_stamp . '.php'; $vars = array('timestamp' => $date_stamp); //$classname = 'Migration_' . $date_stamp; // get list of files $files = MpmListHelper::getFiles(); // if filename is taken, throw error if (in_array($filename, $files)) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to obtain a unique filename for your migration. Please try again in a few seconds.'); $obj->write(); } // create file if (MpmDbHelper::getMethod() == MPM_METHOD_PDO) { $file = MpmTemplateHelper::getTemplate('pdo_migration.txt', $vars); } else { $file = MpmTemplateHelper::getTemplate('mysqli_migration.txt', $vars); } // write the file $fp = fopen(MPM_DB_PATH . $filename, "w"); if ($fp == false) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to write new migration file.'); $obj->write(); } $success = fwrite($fp, $file); if ($success == false) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to write new migration file.'); $obj->write(); } fclose($fp); // display success message $obj = MpmCommandLineWriter::getInstance(); $obj->addText('New migration created: file /db/' . $filename); $obj->write(); }
/** * 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 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(); }
/** * Determines what action should be performed and takes that action. * * @uses MpmDbHelper::test() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::addText() * @uses MpmCommandLineWriter::write() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmCommandLineWriter::writeFooter() * @uses MpmBuildController::build() * @uses MPM_DB_PATH * * @return void */ public function doAction() { // make sure system is init'ed MpmDbHelper::test(); $clw = MpmCommandLineWriter::getInstance(); $forced = false; $with_data = false; // are we adding a schema file? if (isset($this->arguments[0]) && $this->arguments[0] == 'add') { // make sure the schema file doesn't exist if (file_exists(MPM_DB_PATH . 'schema.php') || file_exists(MPM_DB_PATH . 'test_data.php')) { $clw->addText('The schema and/or test data files already exist. Delete them first if you want to use this option.'); $clw->write(); exit; } $file = MpmTemplateHelper::getTemplate('schema.txt'); $test_data_file = MpmTemplateHelper::getTemplate('test_data.txt'); $fp = fopen(MPM_DB_PATH . 'schema.php', "w"); if ($fp == false) { echo "\nUnable to write to file. Initialization failed!\n\n"; exit; } $success = fwrite($fp, $file); if ($success == false) { echo "\nUnable to write to file. Initialization failed!\n\n"; exit; } fclose($fp); $fp = fopen(MPM_DB_PATH . 'test_data.php', "w"); if ($fp == false) { echo "\nUnable to write to file. Initialization failed!\n\n"; exit; } $success = fwrite($fp, $test_data_file); if ($success == false) { echo "\nUnable to write to file. Initialization failed!\n\n"; exit; } fclose($fp); $clw->addText('File ' . MPM_DB_PATH . 'schema.php has been created.'); $clw->addText('File ' . MPM_DB_PATH . 'test_data.php has been created.'); $clw->write(); exit; } else { if (isset($this->arguments[0]) && $this->arguments[0] == 'with_data') { $with_data = true; } else { if (isset($this->arguments[0]) && $this->arguments[0] == '--force') { $forced = true; } } } // make sure the schema file exists if (!file_exists(MPM_DB_PATH . 'schema.php')) { $clw->addText('The schema file does not exist. Run this command with the "add" argument to create one (only a stub).'); $clw->write(); exit; } // make sure the test data file exists if ($with_data == true && !file_exists(MPM_DB_PATH . 'test_data.php')) { $clw->addText('The test data file does not exist. Run this command with the "add" argument to create one (only a stub).'); $clw->write(); exit; } $clw->writeHeader(); if (!$forced) { echo "\nWARNING: IF YOU CONTINUE, ALL TABLES IN YOUR DATABASE WILL BE ERASED!"; echo "\nDO YOU WANT TO CONTINUE? [y/N] "; $answer = fgets(STDIN); $answer = trim($answer); $answer = strtolower($answer); if (empty($answer) || substr($answer, 0, 1) == 'n') { echo "\nABORTED!\n\n"; $clw->writeFooter(); exit; } } echo "\n"; $this->build($with_data); $clw->writeFooter(); exit; }
/** * Determines what action should be performed and takes that action. * * @uses MPM_DB_PATH * @uses MpmDbHelper::test() * @uses MpmListHelper::getFiles() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::addText() * @uses MpmCommandLineWriter::write() * @uses MpmDbHelper::getMethod() * @uses MpmUpController::displayHelp() * * @return void */ public function doAction() { // make sure system is init'ed MpmDbHelper::test(); $filename_note = ''; if (!empty($this->arguments)) { $argument = preg_replace('|[^a-zA-Z0-9_-]|', '', $this->arguments[0]); if (!empty($argument)) { $filename_note = '_' . $argument; } } // get date stamp for use in generating filename $date_stamp = date('Y_m_d_H_i_s'); $filename = $date_stamp . $filename_note . '.php'; $classname = 'Migration_' . $date_stamp; // get list of files $files = glob(MPM_DB_PATH . $date_stamp . '*.php'); // if filename is taken, throw error if (!empty($files)) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to obtain a unique filename for your migration. Please try again in a few seconds.'); $obj->write(); exit; } // create file if (MpmDbHelper::getMethod() == MPM_METHOD_PDO) { $file = "<?php\n\n"; $file .= 'class ' . $classname . ' extends MpmMigration' . "\n"; $file .= "{\n\n"; $file .= "\t" . 'public function up(PDO &$pdo)' . "\n"; $file .= "\t{\n"; $file .= "\t\t" . '$pdo->exec("DO 0");' . "\n"; $file .= "\t}\n\n"; $file .= "\t" . 'public function down(PDO &$pdo)' . "\n"; $file .= "\t{\n"; $file .= "\t\t" . '$pdo->exec("DO 0");' . "\n"; $file .= "\t}\n\n"; $file .= "}\n\n"; $file .= "?>"; } else { $file = "<?php\n\n"; $file .= 'class ' . $classname . ' extends MpmMysqliMigration' . "\n"; $file .= "{\n\n"; $file .= "\t" . 'public function up(ExceptionalMysqli &$mysqli)' . "\n"; $file .= "\t{\n"; $file .= "\t\t" . '$mysqli->exec("DO 0");' . "\n"; $file .= "\t}\n\n"; $file .= "\t" . 'public function down(ExceptionalMysqli &$mysqli)' . "\n"; $file .= "\t{\n"; $file .= "\t\t" . '$mysqli->exec("DO 0");' . "\n"; $file .= "\t}\n\n"; $file .= "}\n\n"; $file .= "?>"; } // write the file $fp = fopen(MPM_DB_PATH . $filename, "w"); if ($fp == false) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to write new migration file.'); $obj->write(); } $success = fwrite($fp, $file); if ($success == false) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to write new migration file.'); $obj->write(); } fclose($fp); // display success message $obj = MpmCommandLineWriter::getInstance(); $obj->addText('New migration created: file ' . MPM_DB_PATH . $filename); $obj->write(); }
/** * 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(); }
/** * Determines what action should be performed and takes that action. * * @uses MPM_DB_PATH * @uses MpmDbHelper::test() * @uses MpmListHelper::getFiles() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::addText() * @uses MpmCommandLineWriter::write() * @uses MpmDbHelper::getMethod() * @uses MpmUpController::displayHelp() * * @return void */ public function doAction() { // make sure system is init'ed MpmDbHelper::test(); // get date stamp for use in generating filename $date_stamp = date('Y_m_d_H_i_s'); $filename = $date_stamp . '.php'; $classname = 'Migration_' . $date_stamp; // get list of files $files = MpmListHelper::getFiles(); // if filename is taken, throw error if (in_array($filename, $files)) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to obtain a unique filename for your migration. Please try again in a few seconds.'); $obj->write(); } // create file if (MpmDbHelper::getMethod() == MPM_METHOD_PDO) { $file = "<?php\n\n"; $file .= 'class ' . $classname . ' extends MpmMigration' . "\n"; $file .= "{\n\n"; $file .= "\t" . 'public function up(PDO &$pdo)' . "\n"; $file .= "\t{\n\t\t\n"; $file .= "\t}\n\n"; $file .= "\t" . 'public function down(PDO &$pdo)' . "\n"; $file .= "\t{\n\t\t\n"; $file .= "\t}\n\n"; $file .= "}\n\n"; $file .= "?>"; } else { $file = "<?php\n\n"; $file .= 'class ' . $classname . ' extends MpmMysqliMigration' . "\n"; $file .= "{\n\n"; $file .= "\t" . 'public function up(ExceptionalMysqli &$mysqli)' . "\n"; $file .= "\t{\n\t\t\n"; $file .= "\t}\n\n"; $file .= "\t" . 'public function down(ExceptionalMysqli &$mysqli)' . "\n"; $file .= "\t{\n\t\t\n"; $file .= "\t}\n\n"; $file .= "}\n\n"; $file .= "?>"; } // write the file $fp = fopen(MPM_DB_PATH . $filename, "w"); if ($fp == false) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to write new migration file.'); $obj->write(); } $success = fwrite($fp, $file); if ($success == false) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText('Unable to write new migration file.'); $obj->write(); } fclose($fp); // display success message $obj = MpmCommandLineWriter::getInstance(); $obj->addText('New migration created: file /db/' . $filename); $obj->write(); }
/** * Determines what action should be performed and takes that action. * * @uses MpmDbHelper::test() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::addText() * @uses MpmCommandLineWriter::write() * @uses MpmCommandLineWriter::writeHeader() * @uses MpmCommandLineWriter::writeFooter() * @uses MpmBuildController::build() * @uses MPM_DB_PATH * * @return void */ public function doAction() { // make sure system is init'ed MpmDbHelper::test(); $clw = MpmCommandLineWriter::getInstance(); $forced = false; // are we adding a schema file? if (isset($this->arguments[0]) && $this->arguments[0] == 'add') { // make sure the schema file doesn't exist if (file_exists(MPM_DB_PATH . 'schema.php')) { $clw->addText('The schema file already exists. Delete it first if you want to use this option.'); $clw->write(); exit; } $db = MpmDbHelper::getDbObj(); $result = $db->exec("show tables"); $schema_queries = array(); while ($row = $result->fetch_array(MYSQLI_NUM)) { if ($row[0] === 'mpm_migrations') { continue; } $tabres = $db->exec("show create table {$row[0]}"); $tabrow = $tabres->fetch_array(MYSQLI_NUM); $schema_queries[] = $tabrow[1]; } $file = "<?php\n"; $file .= "/**\n"; $file .= " * This file houses the MpmInitialSchema class.\n"; $file .= " *\n"; $file .= " * This file may be deleted if you do not wish to use the build command or build on init features.\n"; $file .= " *\n"; $file .= " * @package mysql_php_migrations\n"; $file .= " * @subpackage Classes\n"; $file .= " * @license http://www.opensource.org/licenses/bsd-license.php The New BSD License\n"; $file .= " * @link http://code.google.com/p/mysql-php-migrations/\n"; $file .= " */\n"; $file .= "\n"; $file .= "/**\n"; $file .= " * The MpmInitialSchema class is used to build an initial database structure.\n"; $file .= " *\n"; $file .= " * @package mysql_php_migrations\n"; $file .= " * @subpackage Classes\n"; $file .= " */\n"; $file .= "class MpmInitialSchema extends MpmSchema\n"; $file .= "{\n"; $file .= "\n"; $file .= "\t" . 'protected $tables = array(' . "\n"; $file .= "\t\"" . implode("\",\n\t\"", $schema_queries); $file .= "\"\n\t);\n\n"; $file .= "\tpublic function __construct()\n"; $file .= "\t{\n"; $file .= "\t\tparent::__construct();\n"; $file .= "\n"; $file .= "\t\t/* If you build your initial schema having already executed a number of migrations,\n"; $file .= "\t\t* you should set the initial migration timestamp.\n"; $file .= "\t\t*\n"; $file .= "\t\t* The initial migration timestamp will be set to active and this migration and all\n"; $file .= "\t\t* previous will be ignored when the build command is used.\n"; $file .= "\t\t*\n"; $file .= "\t\t* EX:\n"; $file .= "\t\t*\n"; $file .= "\t\t* " . '$' . "this->initialMigrationTimestamp = '2009-08-01 15:23:44';\n"; $file .= "\t\t*/\n"; $file .= "\t\t" . '$' . "this->initialMigrationTimestamp = date('Y-m-d H:i:s');\n"; $file .= "\t}\n"; $file .= "\n"; $file .= "\tpublic function build()\n"; $file .= " \t{\n"; $file .= "\t\t/* Add the queries needed to build the initial structure of your database.\n"; $file .= "\t\t*\n"; $file .= "\t\t* EX:\n"; $file .= "\t\t*\n"; $file .= "\t\t* " . '$' . "this->dbObj->exec('CREATE TABLE `testing` ( `id` INT(11) AUTO_INCREMENT NOT NULL, `vals` INT(11) NOT NULL, PRIMARY KEY ( `id` ))');\n"; $file .= "\t\t*/\n"; $file .= "\t\tforeach(\$this->tables as \$table)\n"; $file .= "\t\t{\n"; $file .= "\t\t\t\$this->dbObj->exec(\$table);\n"; $file .= "\t\t}\n"; $file .= "\t}\n"; $file .= "\n"; $file .= "}\n"; $file .= "\n"; $file .= "?>"; $fp = fopen(MPM_DB_PATH . 'schema.php', "w"); if ($fp == false) { echo "\nUnable to write to file. Initialization failed!\n\n"; exit; } $success = fwrite($fp, $file); if ($success == false) { echo "\nUnable to write to file. Initialization failed!\n\n"; exit; } fclose($fp); $clw->addText('File ' . MPM_DB_PATH . 'schema.php has been created.'); $clw->write(); exit; } else { if (isset($this->arguments[0]) && $this->arguments[0] == '--force') { $forced = true; } } // make sure the schema file exists if (!file_exists(MPM_DB_PATH . 'schema.php')) { $clw->addText('The schema file does not exist. Run this command with the "add" argument to create one (only a stub).'); $clw->write(); exit; } $clw->writeHeader(); if (!$forced) { echo "\nWARNING: IF YOU CONTINUE, ALL TABLES IN YOUR DATABASE WILL BE ERASED!"; echo "\nDO YOU WANT TO CONTINUE? [y/N] "; $answer = fgets(STDIN); $answer = trim($answer); $answer = strtolower($answer); if (empty($answer) || substr($answer, 0, 1) == 'n') { echo "\nABORTED!\n\n"; $clw->writeFooter(); exit; } } echo "\n"; $this->build(); $clw->writeFooter(); exit; }