/** * Execute every sql query in the file * * @param string $sqlFilepath path to file with SQL queries * @return bool */ public static function executeSQLFile($sqlFilepath) { if (is_file($sqlFilepath) == false) { return true; } $sqlString = file_get_contents($sqlFilepath); if ($sqlString === false || $sqlString == "") { return true; } $sqlString = trim($sqlString); $system = new RM_System(); $result = $system->parseSQL($sqlString); //$result = self::executeSQLCode($sqlString); return $result; }
/** This performs the DB Upgrade * step 3 * This process will apply the upgrade sql files in sequence. So that all * updates are applied from one version to the next. * * @return json */ public function upgradedatabaseJsonAction() { // get the current db version $system = new RM_System(); $dbVersion = $system->getDBVersion()->current()->db_version; // get the latest version $version = RM_Environment::getInstance()->getLatestVersion(); if ($dbVersion == $version) { RM_Log::toLog('Upgrade - DB upgrade not required as DB is at latest revision: ' . $dbVersion, RM_Log::INFO); return array('data' => array('success' => 1, 'error' => "No DB Upgrade Action Required as DB Version is at Latest Revision")); } // Remove all dots from version so we will get the integer of the version. ie: 1211630 $dbCurrentVersionNumber = (int) str_replace(".", "", $dbVersion); $sqlPath = implode(DIRECTORY_SEPARATOR, array(RM_Environment::getConnector()->getRootPath(), 'RM', 'userdata', 'temp', 'upgrade', 'system', 'sql')); // if this is the stable release switch to the upgrades folder... if (version_compare($dbVersion, "1.0.0", '>=')) { $sqlPath = $sqlPath . DIRECTORY_SEPARATOR . "upgrades"; } $filesContent = array(); $files = RM_Filesystem::getFiles($sqlPath, array('sql')); $numbers = array(); foreach ($files as $file) { $chunks = explode('.', $file); if (count($chunks) == 3 && $chunks[1] == 'upgrade') { $numbers[] = $chunks[0]; } } sort($numbers); foreach ($numbers as $number) { if ($number > $dbCurrentVersionNumber) { $sqlFile = $sqlPath . DIRECTORY_SEPARATOR . $number . ".upgrade.sql"; RM_Log::toLog('Upgrade - Applying DB upgrade file: ' . $sqlFile, RM_Log::INFO); if (file_exists($sqlFile)) { $filesContent[] = file_get_contents($sqlFile); } } } foreach ($filesContent as $fileContent) { try { $result = $system->parseSQL($fileContent); } catch (Exception $e) { $result = false; } if (!$result) { RM_Log::toLog('Upgrade - DB Upgrade has failed!', RM_Log::ERR); $msg = 'The database query for installation/upgrade has failed! More Information about the exception thrown will be in the systemlog'; return array('data' => array('success' => false, 'msg' => $msg)); } } return array('data' => array('success' => true)); }