function test_executeTasksTablesAlter() { $this->assertTrue($this->initDatabase(325, array('campaigns')) . 'failed to created version 325 of campaigns table'); $tblCampaigns = $this->oDbh->quoteIdentifier($this->prefix . 'campaigns', true); // Insert some data to test the upgrade from... we know the schema being used so we can directly insert $this->oDbh->exec("INSERT INTO {$tblCampaigns} VALUES (1,'campaign one', 1, 100, 10, 1, '2000-01-01', '2000-01-01', 't', 'h', 1, 1, 'f', 'f')"); $this->oDbh->exec("INSERT INTO {$tblCampaigns} VALUES (2,'campaign two', 1, 100, 10, 1, '2000-01-01', '2000-01-01', 't', 'm', 1, 1, 'f', 'f')"); $this->oDbh->exec("INSERT INTO {$tblCampaigns} VALUES (3,'campaign three', 1, -1, -1, -1, '2000-01-01', '2000-01-01', 't', 'l', 1, 1, 'f', 'f')"); $this->oDbh->exec("INSERT INTO {$tblCampaigns} VALUES (4,'campaign four', 1, 100, 10, 1, '2000-01-01', '2000-01-01', 't', 'h', 1, 1, 'f', 'f')"); $this->oDbh->exec("INSERT INTO {$tblCampaigns} VALUES (5,'campaign five', 1, 100, 10, 1, '2000-01-01', '2000-01-01', 't', 'm', 1, 1, 'f', 'f')"); $this->oDbh->exec("INSERT INTO {$tblCampaigns} VALUES (6,'campaign six', 1, -1, -1, -1, '2000-01-01', '2000-01-01', 't', 'l', 1, 1, 'f', 'f')"); Mock::generatePartial('OA_DB_UpgradeAuditor', $mockAuditor = 'OA_DB_UpgradeAuditor' . rand(), array('logAuditAction', 'setKeyParams')); $oLogger = new OA_UpgradeLogger(); $oLogger->setLogFile('DB_Upgrade.test.log'); $oDB_Upgrade = new OA_DB_Upgrade($oLogger); $oDB_Upgrade->oAuditor = new $mockAuditor($this); $oDB_Upgrade->oAuditor->setReturnValue('logAuditAction', true); $oDB_Upgrade->oAuditor->setReturnValue('setKeyParams', true); $oDB_Upgrade->init('constructive', 'tables_core', 326); $aDef325 = $this->oaTable->aDefinition; $oDB_Upgrade->aDBTables = $oDB_Upgrade->_listTables(); $this->assertTrue($oDB_Upgrade->_verifyTasksTablesAlter(), 'failed _verifyTasksTablesAlter: change field'); $this->assertTrue($oDB_Upgrade->_executeTasksTablesAlter(), 'failed _executeTasksTablesAlter: change field'); $aDefDB = $oDB_Upgrade->oSchema->getDefinitionFromDatabase(array($this->prefix . 'campaigns')); $aDiff = $oDB_Upgrade->oSchema->compareDefinitions($this->aDefNew, $aDefDb); $this->assertEqual(count($aDiff), 0, 'comparison failed'); $aResults = $this->oDbh->queryAll("SELECT * FROM " . $tblCampaigns); $this->assertIsa($aResults, 'array'); $expected = array(1 => '5', 2 => '3', 3 => '0', 4 => '5', 5 => '3', 6 => '0'); foreach ($aResults as $idx => $aRow) { $this->assertEqual($aRow['priority'], $expected[$aRow['campaignid']], ' unexpected campaign priority value detected after upgrade'); } }
/** * Log errors to error log * * @param string $result result array (should contains name, type, status and errors fields) * @param string $message error message */ public static function logError(&$result, $message) { if (!isset(self::$oLogger)) { self::$oLogger = new OA_UpgradeLogger(); } $result['errors'][] = $message; self::$oLogger->logError($result['name'] . '(' . $result['type'] . '): ' . $message); }
/** * retrieves a list of files to be included at the very end of the upgrade * include each file * * @return array | false */ function executePostUpgradeTasks() { $oldAudit = $GLOBALS['_MAX']['CONF']['audit']['enabled']; $GLOBALS['_MAX']['CONF']['audit']['enabled'] = 0; if (file_exists($this->postTaskFile)) { $aContent = array_unique(explode(';', trim(file_get_contents($this->postTaskFile)))); foreach ($aContent as $k => &$v) { if (trim($v)) { $file = $this->upgradePath . "tasks/openads_upgrade_task_" . trim($v) . ".php"; if (file_exists($file)) { $this->oLogger->logOnly('attempting to include file ' . $file); include $file; $this->oLogger->logOnly('executed file ' . $file); } else { $this->oLogger->logOnly('file not found ' . $file); } $aResult[$k]['task'] = trim($v); $aResult[$k]['file'] = $file; $aResult[$k]['result'] = $upgradeTaskResult; $aResult[$k]['message'] = $upgradeTaskMessage; $aResult[$k]['error'] = $upgradeTaskError; } } $this->_pickupPostUpgradeTasksFile(); return $aResult; } $GLOBALS['_MAX']['CONF']['audit']['enabled'] = $oldAudit; return true; }
/** * Run single post upgrade task * * @param string $task task name * @return array contains 'task' - task name, 'file' - executed file, 'result' - task result 'errors' - array of errors if any */ public function runPostUpgradeTask($task) { require_once MAX_PATH . '/lib/OX/Upgrade/PostUpgradeTask/MessagesCollector.php'; $oldAudit = $GLOBALS['_MAX']['CONF']['audit']['enabled']; $GLOBALS['_MAX']['CONF']['audit']['enabled'] = 0; $file = $this->upgradePath . "tasks/openads_upgrade_task_" . $task . ".php"; $upgradeTaskResult = null; $oMessages = new OX_Upgrade_PostUpgradeTask_MessagesCollector($this->oLogger); if (file_exists($file)) { $this->oLogger->logOnly('attempting to include file ' . $file); include $file; $this->oLogger->logOnly('executed file ' . $file); } else { $oMessages->logError('file not found ' . $file); } $aResult['task'] = $task; $aResult['file'] = $file; $aResult['result'] = $upgradeTaskResult; $aResult['errors'] = $oMessages->getErrors(); $GLOBALS['_MAX']['CONF']['audit']['enabled'] = $oldAudit; return $aResult; }
/** * Log error message * * @param string $message */ public function logError($message) { $this->aErrors[] = $message; $this->oUpgradeLogger->logError($message); }