Skip to content

Commit 24dbb49

Browse files
claudiu-cristeaweitzman
authored andcommitted
Reduce the useless verbosity of batch update output (#3372) (#4090)
* Provide a regression test (#3372) * Reduce useless verbosity of output (#3372) * Fix tests for Windows platforms.
1 parent 4f53cbc commit 24dbb49

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

src/Commands/core/UpdateDBCommands.php

+21-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Drush\Commands\core;
33

44
use Consolidation\Log\ConsoleLogLevel;
5+
use DrushBatchContext;
56
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
67
use Consolidation\OutputFormatters\StructuredData\UnstructuredListData;
78
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
@@ -169,14 +170,16 @@ public function process($batch_id, $options = ['format' => 'json'])
169170
*
170171
* This method is static since since it is called by _drush_batch_worker().
171172
*
172-
* @param $module
173+
* @param string $module
173174
* The module whose update will be run.
174-
* @param $number
175+
* @param int $number
175176
* The update number to run.
176-
* @param $context
177-
* The batch context array
177+
* @param array $dependency_map
178+
* The update dependency map.
179+
* @param DrushBatchContext $context
180+
* The batch context object.
178181
*/
179-
public static function updateDoOne($module, $number, $dependency_map, &$context)
182+
public static function updateDoOne($module, $number, array $dependency_map, DrushBatchContext $context)
180183
{
181184
$function = $module . '_update_' . $number;
182185

@@ -202,9 +205,13 @@ public static function updateDoOne($module, $number, $dependency_map, &$context)
202205
Database::startLog($function);
203206
}
204207

205-
Drush::logger()->notice("Update started: $function");
208+
if (empty($context['results'][$module][$number]['type'])) {
209+
Drush::logger()->notice("Update started: $function");
210+
}
211+
206212
$ret['results']['query'] = $function($context['sandbox']);
207213
$ret['results']['success'] = true;
214+
$ret['type'] = 'update';
208215
} catch (\Throwable $e) {
209216
// PHP 7 introduces Throwable, which covers both Error and Exception throwables.
210217
$ret['#abort'] = ['success' => false, 'query' => $e->getMessage()];
@@ -263,10 +270,10 @@ public static function updateDoOne($module, $number, $dependency_map, &$context)
263270
*
264271
* @param string $function
265272
* The post-update function to execute.
266-
* @param array $context
267-
* The batch context.
273+
* @param DrushBatchContext $context
274+
* The batch context object.
268275
*/
269-
public static function updateDoOnePostUpdate($function, &$context)
276+
public static function updateDoOnePostUpdate($function, DrushBatchContext $context)
270277
{
271278
$ret = [];
272279

@@ -284,10 +291,13 @@ public static function updateDoOnePostUpdate($function, &$context)
284291
list($module, $name) = explode('_post_update_', $function, 2);
285292
module_load_include('php', $module, $module . '.post_update');
286293
if (function_exists($function)) {
287-
Drush::logger()->notice("Update started: $function");
294+
if (empty($context['results'][$module][$name]['type'])) {
295+
Drush::logger()->notice("Update started: $function");
296+
}
288297
try {
289298
$ret['results']['query'] = $function($context['sandbox']);
290299
$ret['results']['success'] = true;
300+
$ret['type'] = 'post_update';
291301

292302
if (!isset($context['sandbox']['#finished']) || (isset($context['sandbox']['#finished']) && $context['sandbox']['#finished'] >= 1)) {
293303
\Drupal::service('update.post_update_registry')->registerInvokedUpdates([$function]);
@@ -328,7 +338,7 @@ public static function updateDoOnePostUpdate($function, &$context)
328338
// Setting this value will output an error message.
329339
// @see \DrushBatchContext::offsetSet()
330340
$context['error_message'] = "Update failed: $function";
331-
} else {
341+
} elseif ($context['finished'] == 1 && empty($ret['#abort'])) {
332342
// Setting this value will output a success message.
333343
// @see \DrushBatchContext::offsetSet()
334344
$context['message'] = "Update completed: $function";

tests/functional/UpdateDBTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,49 @@ public function testSuccessfulUpdate()
265265
$this->assertNotContains('Failed', $this->getErrorOutput());
266266
}
267267

268+
/**
269+
* Tests the output on batch update.
270+
*/
271+
public function testBatchUpdateLogMessages()
272+
{
273+
$options = [
274+
'yes' => null,
275+
];
276+
$this->setUpDrupal(1, true);
277+
$this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
278+
$this->drush('pm:enable', ['woot'], $options);
279+
280+
// Force re-run of woot_update_8105().
281+
$this->drush('php:eval', ['drupal_set_installed_schema_version("woot", 8104)'], $options);
282+
// Force re-run of woot_post_update_batch().
283+
$this->forcePostUpdate('woot_post_update_batch', $options);
284+
285+
// Run updates.
286+
$this->drush('updatedb', [], $options);
287+
288+
$expected_update_output = <<<UPDATE
289+
> [notice] Update started: woot_update_8105
290+
> [notice] Iteration 1.
291+
> [notice] Iteration 2.
292+
> [notice] Finished at 3.
293+
> [notice] Update completed: woot_update_8105
294+
UPDATE;
295+
$expected_post_update_output = <<<POST_UPDATE
296+
> [notice] Update started: woot_post_update_batch
297+
> [notice] Iteration 1.
298+
> [notice] Iteration 2.
299+
> [notice] Finished at 3.
300+
> [notice] Update completed: woot_post_update_batch
301+
POST_UPDATE;
302+
303+
// On Windows systems the new line delimiter is a CR+LF (\r\n) sequence
304+
// instead of LF (\n) as it is on *nix systems.
305+
$actual_output = str_replace("\r\n", "\n", $this->getErrorOutputRaw());
306+
307+
$this->assertContains($expected_update_output, $actual_output);
308+
$this->assertContains($expected_post_update_output, $actual_output);
309+
}
310+
268311
public function tearDown()
269312
{
270313
$this->recursiveDelete($this->pathPostUpdate, true);

tests/functional/resources/modules/d8/woot/woot.install

+12
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,15 @@ function woot_update_8104() {
3737
\Drupal::service('renderer')->renderPlain($render_array);
3838
return t('This is the update message from woot_update_8104');
3939
}
40+
41+
/**
42+
* Batch update.
43+
*/
44+
function woot_update_8105(array &$sandbox) {
45+
$sandbox['current'] = isset($sandbox['current']) ? ++$sandbox['current'] : 1;
46+
$sandbox['#finished'] = (int) $sandbox['current'] === 3;
47+
if ($sandbox['#finished']) {
48+
return "Finished at {$sandbox['current']}.";
49+
}
50+
return "Iteration {$sandbox['current']}.";
51+
}

tests/functional/resources/modules/d8/woot/woot.post_update.php

+8
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ function woot_post_update_render()
4040
];
4141
\Drupal::service('renderer')->renderPlain($render_array);
4242
}
43+
44+
/**
45+
* Batch post update.
46+
*/
47+
function woot_post_update_batch(array &$sandbox)
48+
{
49+
return woot_update_8105($sandbox);
50+
}

0 commit comments

Comments
 (0)