Skip to content

Commit 1a52962

Browse files
committed
Add tests for the update order
1 parent 85e6141 commit 1a52962

File tree

13 files changed

+368
-20
lines changed

13 files changed

+368
-20
lines changed

src/Commands/core/UpdateDBCommands.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public function updatedb($options = ['cache-clear' => true]): int
109109
'module' => 'Module',
110110
'update_id' => 'Update ID',
111111
'description' => 'Description',
112-
'type' => 'Type'
112+
'type' => 'Type',
113+
'allowed' => 'Allowed',
113114
])]
114115
#[CLI\DefaultTableFields(fields: ['module', 'update_id', 'type', 'description'])]
115116
#[CLI\FilterDefaultField(field: 'type')]
@@ -127,6 +128,17 @@ public function updatedbStatus($options = ['format' => 'table']): ?RowsOfFields
127128
if (empty($pending)) {
128129
$this->logger()->success(dt("No database updates required."));
129130
} else {
131+
if (empty($options['filter'])) {
132+
// Show only allowed updates by default.
133+
// Would ideally set a default filter in the parameter, however
134+
// there's an upstream issue in
135+
// https://github.com/consolidation/filter-via-dot-access-data/pull/51
136+
// that needs to be addressed before that's possible, so
137+
// manually handling the filter for now.
138+
$pending = array_filter($pending, static function ($update_hook) {
139+
return empty($update_hook['allowed']) || $update_hook['allowed'] === 'yes';
140+
});
141+
}
130142
$return = new RowsOfFields($pending);
131143
}
132144
return $return;
@@ -511,19 +523,36 @@ public function getUpdatedbStatus(array $options): array
511523
$description = $pending[$module]['pending'][$update_id];
512524
// Strip cruft from front.
513525
$description = str_replace($update_id . ' - ', '', $description);
514-
if (empty($upcoming_update['allowed'])) {
515-
// This should rarely happen, but it's a good idea to have it
516-
// shown rather than silently skipped.
517-
$description = dt('[SKIPPED] @description', [
518-
'@description' => $description,
519-
]);
520-
}
521-
$return[$module . "_update_$update_id"] = [
526+
$module_update_function = $module . "_update_$update_id";
527+
$return[$module_update_function] = [
522528
'module' => $module,
523529
'update_id' => $update_id,
524530
'description' => $description,
525-
'type' => 'hook_update_n'
531+
'type' => 'hook_update_n',
532+
'allowed' => !empty($upcoming_update['allowed']) ? 'yes' : 'no',
526533
];
534+
if (empty($upcoming_update['allowed'])) {
535+
if ($upcoming_update['missing_dependencies']) {
536+
// This should rarely happen, but the user should be notified
537+
// since skipping them can potentially put the database in an
538+
// inconsistent state.
539+
$missing_warning = dt('Skipping @update_function due to missing dependencies: @missing_dependencies.', [
540+
'@update_function' => "{$module_update_function}()",
541+
'@missing_dependencies' => implode('(), ', $upcoming_update['missing_dependencies']) . '()',
542+
]);
543+
}
544+
else {
545+
$missing_warning = dt("Skipping @update_function due to an error in the module's code.", [
546+
'@update_function' => "{$module_update_function}()",
547+
]);
548+
}
549+
if (isset($pending[$module]['warning'])) {
550+
$pending[$module]['warning'] .= "\n$missing_warning";
551+
}
552+
else {
553+
$pending[$module]['warning'] = $missing_warning;
554+
}
555+
}
527556
if (isset($pending[$module]['warning'])) {
528557
$warnings[$module] = $pending[$module]['warning'];
529558
}
@@ -541,7 +570,8 @@ public function getUpdatedbStatus(array $options): array
541570
'module' => $module,
542571
'update_id' => $id,
543572
'description' => trim($item),
544-
'type' => 'post-update'
573+
'type' => 'post-update',
574+
'allowed' => 'yes',
545575
];
546576
}
547577
}

sut/modules/unish/drush_empty_module/drush_empty_module.install

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@ function drush_empty_module_requirements($phase) {
3333
function drush_empty_module_update_8001() {
3434
return 'The update ran';
3535
}
36+
37+
/**
38+
* Fake update hook 2.
39+
*/
40+
function drush_empty_module_update_8002() {
41+
return 'The second update ran';
42+
}
43+
44+
/**
45+
* Fake update hook 3.
46+
*/
47+
function drush_empty_module_update_8003() {
48+
return 'The third update ran';
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Missing Drush update
2+
type: module
3+
description: Module to always trigger a missing update dependency.
4+
core_version_requirement: '>=8'
5+
package: Other
6+
dependencies: []
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Provides install and update functions for drush_missing_update_dependency.
6+
*/
7+
8+
/**
9+
* Implements hook_update_dependencies().
10+
*/
11+
function drush_missing_update_dependency_update_dependencies() {
12+
// Flag that update hooks in this module will never resolve due to the
13+
// missing "drush_non_existent_update_dependency" module dependency.
14+
$dependencies['drush_missing_update_dependency'][8001] = [
15+
'drush_non_existent_update_dependency' => 8001,
16+
];
17+
$dependencies['drush_missing_update_dependency'][8002] = [
18+
'drush_non_existent_update_dependency' => 8002,
19+
];
20+
return $dependencies;
21+
}
22+
23+
24+
/**
25+
* This should never have run (missing dependency).
26+
*/
27+
function drush_missing_update_dependency_update_8001() {
28+
throw new \LogicException('This update function is not allowed to run, since the schema version should already be at least 8001.');
29+
}
30+
31+
/**
32+
* This should never run (missing dependency).
33+
*/
34+
function drush_missing_update_dependency_update_8002() {
35+
throw new \LogicException('This update function is not allowed to run, since it has a direct dependency on a missing update.');
36+
}
37+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Missing Drush update dependency 1
2+
type: module
3+
description: Module to trigger missing update dependency.
4+
core_version_requirement: '>=8'
5+
package: Other
6+
dependencies: []
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Provides install and update functions for drush_missing_update_dependency1.
6+
*/
7+
8+
/**
9+
* Update hook 1.
10+
*/
11+
function drush_missing_update_dependency1_update_8001() {
12+
return 'The first update ran';
13+
}
14+
15+
/**
16+
* Update hook 2.
17+
*/
18+
function drush_missing_update_dependency1_update_8002() {
19+
return 'The second update ran';
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Missing Drush update dependency 2
2+
type: module
3+
description: Module to trigger missing update dependency.
4+
core_version_requirement: '>=8'
5+
package: Other
6+
dependencies: []
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Provides install and update functions for drush_missing_update_dependency2.
6+
*/
7+
8+
/**
9+
* Update hook 1.
10+
*/
11+
function drush_missing_update_dependency2_update_8001() {
12+
return 'The first update ran';
13+
}
14+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Drush update dependency
2+
type: module
3+
description: Module with hook_update_dependencies() entry.
4+
core_version_requirement: '>=8'
5+
package: Other
6+
dependencies: []
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Provides install and update functions for drush_update_dependency.
6+
*/
7+
8+
/**
9+
* Implements hook_update_dependencies().
10+
*/
11+
function drush_update_dependency_update_dependencies() {
12+
// When this module is enabled, we are declaring here that modules run
13+
// updates in the following order:
14+
// 1. drush_empty_module_update_8001()
15+
// 2. drush_update_dependency_update_8001()
16+
// 3. drush_update_dependency_update_8002()
17+
// ...
18+
// 7. drush_empty_module_update_8002()
19+
$dependencies['drush_update_dependency'][8001] = [
20+
'drush_empty_module' => 8001,
21+
];
22+
23+
// These are coordinated with the corresponding dependencies declared in
24+
// update_test_1_update_dependencies().
25+
$dependencies['drush_missing_update_dependency1'][8002] = [
26+
'drush_update_dependency' => 8002,
27+
];
28+
// Ensure the two update hooks won't run until the module exists.
29+
$dependencies['drush_update_dependency'][8003] = [
30+
'drush_missing_update_dependency1' => 8001,
31+
];
32+
$dependencies['drush_update_dependency'][8004] = [
33+
'drush_missing_update_dependency2' => 8001,
34+
];
35+
36+
return $dependencies;
37+
}
38+
39+
/**
40+
* Dependency update hook 1.
41+
*/
42+
function drush_update_dependency_update_8001() {
43+
return 'The first update ran';
44+
}
45+
46+
/**
47+
* Dependency update hook 2.
48+
*/
49+
function drush_update_dependency_update_8002() {
50+
return 'The second update ran';
51+
}
52+
53+
/**
54+
* Dependency update hook 3.
55+
*/
56+
function drush_update_dependency_update_8003() {
57+
return 'The third update ran';
58+
}
59+
60+
/**
61+
* Dependency update hook 4.
62+
*/
63+
function drush_update_dependency_update_8004() {
64+
return 'The fourth update ran';
65+
}
66+
67+
/**
68+
* Dependency update hook 5 (blocked).
69+
*/
70+
function drush_update_dependency_update_8005() {
71+
// This will not be ran until the previous hooks have been successfully ran.
72+
return 'The fifth update ran';
73+
}

0 commit comments

Comments
 (0)