public function testGetDbVersionErrors() { $this->moduleResource->expects($this->any())->method('getDataVersion')->will($this->returnValue(2)); $this->moduleResource->expects($this->any())->method('getDbVersion')->will($this->returnValue(2)); $expectedErrors = [[DbVersionInfo::KEY_MODULE => 'Module_One', DbVersionInfo::KEY_CURRENT => '2', DbVersionInfo::KEY_REQUIRED => '1', DbVersionInfo::KEY_TYPE => 'schema'], [DbVersionInfo::KEY_MODULE => 'Module_One', DbVersionInfo::KEY_CURRENT => '2', DbVersionInfo::KEY_REQUIRED => '1', DbVersionInfo::KEY_TYPE => 'data']]; $this->assertEquals($expectedErrors, $this->dbVersionInfo->getDbVersionErrors()); }
/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return int|null|void */ protected function execute(InputInterface $input, OutputInterface $output) { $time = microtime(true); $ignoreDataUpdate = $input->getOption('ignore-data'); $headers = array('Setup', 'Module', 'DB', 'Data', 'Status'); if ($ignoreDataUpdate) { unset($headers[array_search('Data', $headers)]); } $errorCounter = 0; $table = array(); foreach ($this->moduleList->getAll() as $moduleName => $moduleInfo) { $moduleVersion = $moduleInfo['setup_version']; $dbVersion = $this->resource->getDbVersion($moduleName); if (!$ignoreDataUpdate) { $dataVersion = $this->resource->getDataVersion($moduleName); } $ok = $dbVersion == $moduleVersion; if ($ok && !$ignoreDataUpdate) { $ok = $dataVersion == $moduleVersion; } if (!$ok) { $errorCounter++; } $row = array('Module' => $moduleName, 'DB' => $dbVersion, 'Data' => $dataVersion); if (!$ignoreDataUpdate) { $row['Data-Version'] = $dataVersion; } $row['Status'] = $ok ? 'OK' : 'Error'; $table[] = $row; } //if there is no output format //highlight the status //and show error'd rows at bottom if (!$input->getOption('format')) { usort($table, function ($a, $b) { return $a['Status'] !== 'OK'; }); array_walk($table, function (&$row) { $status = $row['Status']; $availableStatus = array('OK' => 'info', 'Error' => 'error'); $statusString = sprintf('<%s>%s</%s>', $availableStatus[$status], $status, $availableStatus[$status]); $row['Status'] = $statusString; }); } if ($input->getOption('log-junit')) { $this->logJUnit($table, $input->getOption('log-junit'), microtime($time) - $time); } else { $this->getHelper('table')->setHeaders($headers)->renderByFormat($output, $table, $input->getOption('format')); //if no output format specified - output summary line if (!$input->getOption('format')) { if ($errorCounter > 0) { $this->writeSection($output, sprintf('%s error%s %s found!', $errorCounter, $errorCounter === 1 ? '' : 's', $errorCounter === 1 ? 'was' : 'were'), 'error'); } else { $this->writeSection($output, 'No setup problems were found.', 'info'); } } } }
/** * Get error data for an out-of-date schema or data. * * @param string $moduleName * @return string[] */ private function getDataInfo($moduleName) { $dataVer = $this->moduleResource->getDataVersion($moduleName); $module = $this->moduleList->getOne($moduleName); $configVer = $module['setup_version']; $dataVer = $dataVer ?: 'none'; return [self::KEY_CURRENT => $dataVer, self::KEY_REQUIRED => $configVer, self::KEY_MODULE => $moduleName, self::KEY_TYPE => 'data']; }
/** * Run module modification files. Return version of last applied upgrade (false if no upgrades applied) * @param string $actionType * @param string $fromVersion * @param string $toVersion * @return false|string * @throws \Magento\Framework\Exception */ protected function _modifyResourceDb($actionType, $fromVersion, $toVersion) { $files = $this->_getAvailableDataFiles($actionType, $fromVersion, $toVersion); if (empty($files) || !$this->getConnection()) { return false; } $version = false; foreach ($files as $file) { $fileName = $file['fileName']; $fileType = pathinfo($fileName, PATHINFO_EXTENSION); $this->getConnection()->disallowDdlCache(); try { switch ($fileType) { case 'php': $result = $this->_includeFile($fileName); break; case 'sql': $sql = $this->modulesDir->readFile($this->modulesDir->getRelativePath($fileName)); if (!empty($sql)) { $result = $this->run($sql); } else { $result = true; } break; default: $result = false; break; } if ($result) { $this->_resource->setDataVersion($this->_resourceName, $file['toVersion']); $this->_logger->info($fileName); } else { $this->_logger->info("Failed resource setup: {$fileName}"); } } catch (\Exception $e) { throw new \Magento\Framework\Exception(sprintf('Error in file: "%s" - %s', $fileName, $e->getMessage()), 0, $e); } $version = $file['toVersion']; $this->getConnection()->allowDdlCache(); } return $version; }
/** * @param string $moduleName * @param string $resourceName * @return bool */ public function isDbDataUpToDate($moduleName, $resourceName) { $dataVer = $this->_moduleResource->getDataVersion($resourceName); return $this->isModuleVersionEqual($moduleName, $dataVer); }
/** * Save resource version * * @param string $actionType * @param string $version * @return $this */ protected function _setResourceVersion($actionType, $version) { switch ($actionType) { case self::TYPE_DB_INSTALL: case self::TYPE_DB_UPGRADE: $this->_resourceResource->setDbVersion($this->_resourceName, $version); break; case self::TYPE_DATA_INSTALL: case self::TYPE_DATA_UPGRADE: $this->_resourceResource->setDataVersion($this->_resourceName, $version); break; default: break; } return $this; }