/** * Returns a timestamp when given a migration ID number. * * @uses MpmDbHelper::getMethod() * @uses MpmDbHelper::getPdoObj() * @uses MpmDbHelper::getMysqliObj() * @uses MPM_METHOD_MYSQLI * @uses MPM_METHOD_PDO * * @param int $id the ID number of the migration * * @return string */ public static function getTimestampFromId($id) { try { switch (MpmDbHelper::getMethod()) { case MPM_METHOD_PDO: // Resolution to Issue #1 - PDO::rowCount is not reliable $pdo = MpmDbHelper::getPdoObj(); $sql = "SELECT COUNT(*) FROM `mpm_migrations` WHERE `id` = '{$id}'"; $stmt = $pdo->query($sql); if ($stmt->fetchColumn() == 1) { unset($stmt); $sql = "SELECT `timestamp` FROM `mpm_migrations` WHERE `id` = '{$id}'"; $stmt = $pdo->query($sql); $result = $stmt->fetch(PDO::FETCH_OBJ); $timestamp = $result->timestamp; } else { $timestamp = false; } break; case MPM_METHOD_MYSQLI: $mysqli = MpmDbHelper::getMysqliObj(); $sql = "SELECT COUNT(*) as total FROM `mpm_migrations` WHERE `id` = '{$id}'"; $stmt = $mysqli->query($sql); $row = $stmt->fetch_object(); if ($row->total == 1) { $stmt->close(); unset($stmt); $sql = "SELECT `timestamp` FROM `mpm_migrations` WHERE `id` = '{$id}'"; $stmt = $mysqli->query($sql); $result = $stmt->fetch_object(); $timestamp = $result->timestamp; $stmt->close(); $mysqli->close(); } else { $timestamp = false; } break; } } catch (Exception $e) { echo "\n\nERROR: " . $e->getMessage() . "\n\n"; exit; } return $timestamp; }
/** * 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(); } }
/** * Checks to make sure everything is in place to be able to use the migrations tool. * * @uses MpmDbHelper::getMethod() * @uses MpmDbHelper::getPdoObj() * @uses MpmDbHelper::getMysqliObj() * @uses MpmDbHelper::getMethod() * @uses MpmDbHelper::checkForDbTable() * @uses MpmCommandLineWriter::getInstance() * @uses MpmCommandLineWriter::addText() * @uses MpmCommandLineWriter::write() * @uses MPM_METHOD_PDO * @uses MPM_METHOD_MYSQLI * * @return void */ public static function test() { $problems = array(); if (!file_exists(MPM_PATH . '/config/db_config.php')) { $problems[] = 'You have not yet run the init command. You must run this command before you can use any other commands.'; } else { switch (MpmDbHelper::getMethod()) { case MPM_METHOD_PDO: if (!class_exists('PDO')) { $problems[] = 'It does not appear that the PDO extension is installed.'; } $drivers = PDO::getAvailableDrivers(); if (!in_array('mysql', $drivers)) { $problems[] = 'It appears that the mysql driver for PDO is not installed.'; } if (count($problems) == 0) { try { $pdo = MpmDbHelper::getPdoObj(); } catch (Exception $e) { $problems[] = 'Unable to connect to the database: ' . $e->getMessage(); } } break; case MPM_METHOD_MYSQLI: if (!class_exists('mysqli')) { $problems[] = "It does not appear that the mysqli extension is installed."; } if (count($problems) == 0) { try { $mysqli = MpmDbHelper::getMysqliObj(); } catch (Exception $e) { $problems[] = "Unable to connect to the database: " . $e->getMessage(); } } break; } if (!MpmDbHelper::checkForDbTable()) { $problems[] = 'Migrations table not found in your database. Re-run the init command.'; } if (count($problems) > 0) { $obj = MpmCommandLineWriter::getInstance(); $obj->addText("It appears there are some problems:"); $obj->addText("\n"); foreach ($problems as $problem) { $obj->addText($problem, 4); $obj->addText("\n"); } $obj->write(); exit; } } }