Skip to content

Commit 008bb5a

Browse files
committed
Show different message if DB module version is higher than code module version
1 parent 0c0393d commit 008bb5a

File tree

2 files changed

+132
-27
lines changed

2 files changed

+132
-27
lines changed

lib/internal/Magento/Framework/Module/Plugin/DbStatusValidator.php

+58-5
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,21 @@ public function __construct(FrontendCacheInterface $cache, DbVersionInfo $dbVers
5050
public function beforeDispatch(FrontController $subject, RequestInterface $request)
5151
{
5252
if (!$this->cache->load('db_is_up_to_date')) {
53-
$errors = $this->dbVersionInfo->getDbVersionErrors();
54-
55-
if ($errors) {
53+
list($versionTooLowErrors, $versionTooHighErrors) = array_values($this->getGroupedDbVersionErrors());
54+
if ($versionTooHighErrors) {
55+
$message = 'Please update your modules: ' . "Run \"composer install\" from the Magento root directory.\n"
56+
. "The following modules are outdated:\n%1";
57+
throw new LocalizedException(
58+
new Phrase($message, [implode("\n", $this->formatVersionTooHighErrors($versionTooHighErrors))])
59+
);
60+
} elseif ($versionTooLowErrors) {
5661
$message = 'Please upgrade your database: '
5762
. "Run \"bin/magento setup:upgrade\" from the Magento root directory.\n"
5863
. "The following modules are outdated:\n%1";
5964

60-
throw new LocalizedException(new Phrase($message, [implode("\n", $this->formatErrors($errors))]));
65+
throw new LocalizedException(
66+
new Phrase($message, [implode("\n", $this->formatVersionTooLowErrors($versionTooLowErrors))])
67+
);
6168
} else {
6269
$this->cache->save('true', 'db_is_up_to_date');
6370
}
@@ -70,7 +77,7 @@ public function beforeDispatch(FrontController $subject, RequestInterface $reque
7077
* @param array $errorsData array of error data from getOutOfDateDbErrors
7178
* @return array Messages that can be used to log the error
7279
*/
73-
private function formatErrors($errorsData)
80+
private function formatVersionTooLowErrors($errorsData)
7481
{
7582
$formattedErrors = [];
7683

@@ -82,4 +89,50 @@ private function formatErrors($errorsData)
8289

8390
return $formattedErrors;
8491
}
92+
93+
/**
94+
* Format each error in the error data from getOutOfDataDbErrors into a single message
95+
*
96+
* @param array $errorsData array of error data from getOutOfDateDbErrors
97+
* @return array Messages that can be used to log the error
98+
*/
99+
private function formatVersionTooHighErrors($errorsData)
100+
{
101+
$formattedErrors = [];
102+
103+
foreach ($errorsData as $error) {
104+
$formattedErrors[] = $error[DbVersionInfo::KEY_MODULE] . ' ' . $error[DbVersionInfo::KEY_TYPE]
105+
. ': code version - ' . $error[DbVersionInfo::KEY_REQUIRED]
106+
. ', database version - ' . $error[DbVersionInfo::KEY_CURRENT];
107+
}
108+
109+
return $formattedErrors;
110+
}
111+
112+
/**
113+
* Return DB version errors grouped by 'version_too_low' and 'version_too_high'
114+
*
115+
* @return mixed
116+
*/
117+
private function getGroupedDbVersionErrors()
118+
{
119+
$allDbVersionErrors = $this->dbVersionInfo->getDbVersionErrors();
120+
return array_reduce(
121+
(array)$allDbVersionErrors,
122+
function ($carry, $item) {
123+
if ($item[DbVersionInfo::KEY_CURRENT] === 'none'
124+
|| $item[DbVersionInfo::KEY_CURRENT] < $item[DbVersionInfo::KEY_REQUIRED]
125+
) {
126+
$carry['version_too_low'][] = $item;
127+
} else {
128+
$carry['version_too_high'][] = $item;
129+
}
130+
return $carry;
131+
},
132+
[
133+
'version_too_low' => [],
134+
'version_too_high' => [],
135+
]
136+
);
137+
}
85138
}

lib/internal/Magento/Framework/Test/Unit/Module/Plugin/DbStatusValidatorTest.php

+74-22
Original file line numberDiff line numberDiff line change
@@ -99,28 +99,11 @@ public function testBeforeDispatchOutOfDateNoErrors()
9999
$this->plugin->beforeDispatch($this->frontControllerMock, $this->requestMock);
100100
}
101101

102-
public function testBeforeDispatchOutOfDateWithErrors()
102+
/**
103+
* @dataProvider beforeDispatchOutOfDateWithErrorsDataProvider
104+
*/
105+
public function testBeforeDispatchOutOfDateWithErrors(array $errors, string $expectedMessage)
103106
{
104-
$errors = [
105-
[
106-
DbVersionInfo::KEY_MODULE => 'Magento_Module1',
107-
DbVersionInfo::KEY_TYPE => 'schema',
108-
DbVersionInfo::KEY_CURRENT => '3.3.3',
109-
DbVersionInfo::KEY_REQUIRED => '4.4.4'
110-
],
111-
[
112-
DbVersionInfo::KEY_MODULE => 'Magento_Module2',
113-
DbVersionInfo::KEY_TYPE => 'data',
114-
DbVersionInfo::KEY_CURRENT => '2.8.7',
115-
DbVersionInfo::KEY_REQUIRED => '5.1.6'
116-
]
117-
];
118-
$message = 'Please upgrade your database: '
119-
. "Run \"bin/magento setup:upgrade\" from the Magento root directory.\n"
120-
. "The following modules are outdated:\n"
121-
. "Magento_Module1 schema: current version - 3.3.3, required version - 4.4.4\n"
122-
. "Magento_Module2 data: current version - 2.8.7, required version - 5.1.6";
123-
124107
$this->cacheMock->expects(static::any())
125108
->method('load')
126109
->with('db_is_up_to_date')
@@ -131,7 +114,76 @@ public function testBeforeDispatchOutOfDateWithErrors()
131114
$this->cacheMock->expects(static::never())
132115
->method('save');
133116

134-
$this->expectException(LocalizedException::class, $message);
117+
$this->expectException(LocalizedException::class, $expectedMessage);
118+
$this->expectExceptionMessage($expectedMessage);
135119
$this->plugin->beforeDispatch($this->frontControllerMock, $this->requestMock);
136120
}
121+
122+
public static function beforeDispatchOutOfDateWithErrorsDataProvider()
123+
{
124+
return [
125+
'module versions too low' => [
126+
'errors' => [
127+
[
128+
DbVersionInfo::KEY_MODULE => 'Magento_Module1',
129+
DbVersionInfo::KEY_TYPE => 'schema',
130+
DbVersionInfo::KEY_CURRENT => 'none',
131+
DbVersionInfo::KEY_REQUIRED => '4.4.4'
132+
],
133+
[
134+
DbVersionInfo::KEY_MODULE => 'Magento_Module2',
135+
DbVersionInfo::KEY_TYPE => 'data',
136+
DbVersionInfo::KEY_CURRENT => '2.8.7',
137+
DbVersionInfo::KEY_REQUIRED => '5.1.6'
138+
],
139+
],
140+
'expectedMessage' => 'Please upgrade your database: '
141+
. "Run \"bin/magento setup:upgrade\" from the Magento root directory.\n"
142+
. "The following modules are outdated:\n"
143+
. "Magento_Module1 schema: current version - none, required version - 4.4.4\n"
144+
. "Magento_Module2 data: current version - 2.8.7, required version - 5.1.6"
145+
],
146+
'module versions too high' => [
147+
'errors' => [
148+
[
149+
DbVersionInfo::KEY_MODULE => 'Magento_Module3',
150+
DbVersionInfo::KEY_TYPE => 'schema',
151+
DbVersionInfo::KEY_CURRENT => '2.0.0',
152+
DbVersionInfo::KEY_REQUIRED => '1.0.0'
153+
],
154+
[
155+
DbVersionInfo::KEY_MODULE => 'Magento_Module4',
156+
DbVersionInfo::KEY_TYPE => 'data',
157+
DbVersionInfo::KEY_CURRENT => '1.0.1',
158+
DbVersionInfo::KEY_REQUIRED => '1.0.0'
159+
],
160+
],
161+
'expectedMessage' => "Please update your modules: "
162+
. "Run \"composer install\" from the Magento root directory.\n"
163+
. "The following modules are outdated:\n"
164+
. "Magento_Module3 schema: code version - 1.0.0, database version - 2.0.0\n"
165+
. "Magento_Module4 data: code version - 1.0.0, database version - 1.0.1",
166+
],
167+
'some versions too high, some too low' => [
168+
'errors' => [
169+
[
170+
DbVersionInfo::KEY_MODULE => 'Magento_Module1',
171+
DbVersionInfo::KEY_TYPE => 'schema',
172+
DbVersionInfo::KEY_CURRENT => '2.0.0',
173+
DbVersionInfo::KEY_REQUIRED => '1.0.0'
174+
],
175+
[
176+
DbVersionInfo::KEY_MODULE => 'Magento_Module2',
177+
DbVersionInfo::KEY_TYPE => 'schema',
178+
DbVersionInfo::KEY_CURRENT => '1.0.0',
179+
DbVersionInfo::KEY_REQUIRED => '2.0.0'
180+
],
181+
],
182+
'expectedMessage' => "Please update your modules: "
183+
. "Run \"composer install\" from the Magento root directory.\n"
184+
. "The following modules are outdated:\n"
185+
. "Magento_Module1 schema: code version - 1.0.0, database version - 2.0.0"
186+
]
187+
];
188+
}
137189
}

0 commit comments

Comments
 (0)