Skip to content

Commit 19de1b8

Browse files
authored
Fix #3634. updatedb with long-running updates can end in "MySQL went away" error (#3655)
Also simplify UpdateDB tests a bit.
1 parent e3c4e12 commit 19de1b8

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/Commands/core/UpdateDBCommands.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class UpdateDBCommands extends DrushCommands
1616
{
1717
protected $cache_clear;
1818

19+
protected $maintenanceModeOriginalState;
20+
1921
/**
2022
* Apply any database updates required (as with running update.php).
2123
*
@@ -386,10 +388,10 @@ public function updateBatch($options)
386388
];
387389
batch_set($batch);
388390

389-
$maintenance_mode_original_state = \Drupal::service('state')->get('system.maintenance_mode');
391+
// See updateFinished() for the restore of maint mode.
392+
$this->maintenanceModeOriginalState = \Drupal::service('state')->get('system.maintenance_mode');
390393
\Drupal::service('state')->set('system.maintenance_mode', true);
391394
$result = drush_backend_batch_process('updatedb:batch-process');
392-
\Drupal::service('state')->set('system.maintenance_mode', $maintenance_mode_original_state);
393395

394396
$success = false;
395397
if (!is_array($result)) {
@@ -468,7 +470,7 @@ public function cacheRebuild()
468470
}
469471

470472
/**
471-
* Batch update callback, clears the cache if needed.
473+
* Batch update callback, clears the cache if needed, and restores maint mode.
472474
*
473475
* @see \Drupal\system\Controller\DbUpdateController::batchFinished()
474476
* @see \Drupal\system\Controller\DbUpdateController::results()
@@ -484,6 +486,8 @@ public function updateFinished($success, $results, $operations)
484486
} else {
485487
drupal_flush_all_caches();
486488
}
489+
490+
\Drupal::service('state')->set('system.maintenance_mode', $this->maintenanceModeOriginalState);
487491
}
488492

489493
/**
@@ -587,10 +591,11 @@ public function entityUpdatesMain()
587591
'finished' => [$this, 'updateFinished'],
588592
];
589593
batch_set($batch);
590-
$maintenance_mode_original_state = \Drupal::service('state')->get('system.maintenance_mode');
594+
595+
// See updateFinished() for the restore of maint mode.
596+
$this->maintenanceModeOriginalState = \Drupal::service('state')->get('system.maintenance_mode');
591597
\Drupal::service('state')->set('system.maintenance_mode', true);
592598
drush_backend_batch_process();
593-
\Drupal::service('state')->set('system.maintenance_mode', $maintenance_mode_original_state);
594599
} else {
595600
$this->logger()->success(dt("No entity schema updates required"));
596601
}

tests/UpdateDBTest.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public function testFailedUpdate()
5454
$sites = $this->setUpDrupal(1, true);
5555
$options = [
5656
'yes' => null,
57-
'root' => $this->webroot(),
58-
'uri' => key($sites),
5957
];
6058
$this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
6159
$this->drush('pm-enable', ['woot'], $options);
@@ -105,11 +103,9 @@ public function testFailedUpdate()
105103
*/
106104
public function testFailedPostUpdate()
107105
{
108-
$sites = $this->setUpDrupal(1, true);
106+
$this->setUpDrupal(1, true);
109107
$options = [
110108
'yes' => null,
111-
'root' => $this->webroot(),
112-
'uri' => key($sites),
113109
];
114110
$this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
115111
$this->drush('pm-enable', ['woot'], $options);
@@ -168,11 +164,8 @@ public function testFailedPostUpdate()
168164
public function testUpdateModuleWithServiceDependency()
169165
{
170166
$root = $this->webroot();
171-
$sites = $this->setUpDrupal(1, true);
167+
$this->setUpDrupal(1, true);
172168
$options = [
173-
'yes' => null,
174-
'root' => $root,
175-
'uri' => key($sites),
176169
'include' => __DIR__,
177170
];
178171
$this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
@@ -217,11 +210,9 @@ class: Drupal\woot\DependingService
217210
*/
218211
public function testSuccessfulUpdate()
219212
{
220-
$sites = $this->setUpDrupal(1, true);
213+
$this->setUpDrupal(1, true);
221214
$options = [
222215
'yes' => null,
223-
'root' => $this->webroot(),
224-
'uri' => key($sites),
225216
];
226217
$this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
227218
$this->drush('pm-enable', ['woot'], $options);

0 commit comments

Comments
 (0)