Skip to content

Commit 8a0d032

Browse files
Copilotswissspidy
andcommitted
Fix wp db status to accept parameters and run before WordPress loads
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 66478f6 commit 8a0d032

2 files changed

Lines changed: 81 additions & 5 deletions

File tree

features/db-status.feature

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,69 @@ Feature: Display database status overview
5454
"""
5555
Check Status: OK
5656
"""
57+
58+
Scenario: Run db status with MySQL defaults to check the database
59+
Given a WP install
60+
61+
When I run `wp db status --defaults`
62+
Then STDOUT should contain:
63+
"""
64+
Database Name:
65+
"""
66+
And STDOUT should contain:
67+
"""
68+
Check Status: OK
69+
"""
70+
71+
Scenario: Run db status with --no-defaults to check the database
72+
Given a WP install
73+
74+
When I run `wp db status --no-defaults`
75+
Then STDOUT should contain:
76+
"""
77+
Database Name:
78+
"""
79+
And STDOUT should contain:
80+
"""
81+
Check Status: OK
82+
"""
83+
84+
Scenario: Run db status with passed-in options
85+
Given a WP install
86+
87+
When I run `wp db status --dbuser=wp_cli_test`
88+
Then STDOUT should contain:
89+
"""
90+
Database Name:
91+
"""
92+
And STDOUT should contain:
93+
"""
94+
Check Status: OK
95+
"""
96+
97+
When I run `wp db status --dbpass=password1`
98+
Then STDOUT should contain:
99+
"""
100+
Database Name:
101+
"""
102+
And STDOUT should contain:
103+
"""
104+
Check Status: OK
105+
"""
106+
107+
When I run `wp db status --dbuser=wp_cli_test --dbpass=password1`
108+
Then STDOUT should contain:
109+
"""
110+
Database Name:
111+
"""
112+
And STDOUT should contain:
113+
"""
114+
Check Status: OK
115+
"""
116+
117+
When I try `wp db status --dbuser=no_such_user`
118+
Then the return code should not be 0
119+
120+
When I try `wp db status --dbpass=no_such_pass`
121+
Then the return code should not be 0
122+

src/DB_Command.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,17 @@ public function prefix() {
12551255
* command is useful for getting a quick snapshot of database health
12561256
* without needing to run multiple separate commands.
12571257
*
1258+
* ## OPTIONS
1259+
*
1260+
* [--dbuser=<value>]
1261+
* : Username to pass to mysql. Defaults to DB_USER.
1262+
*
1263+
* [--dbpass=<value>]
1264+
* : Password to pass to mysql. Defaults to DB_PASSWORD.
1265+
*
1266+
* [--defaults]
1267+
* : Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
1268+
*
12581269
* ## EXAMPLES
12591270
*
12601271
* $ wp db status
@@ -1267,9 +1278,9 @@ public function prefix() {
12671278
* Collation: utf8mb4_unicode_ci
12681279
* Check Status: OK
12691280
*
1270-
* @when after_wp_load
1281+
* @when before_wp_load
12711282
*/
1272-
public function status() {
1283+
public function status( $_, $assoc_args ) {
12731284
global $wpdb;
12741285

12751286
// Get database name.
@@ -1355,16 +1366,15 @@ public function status() {
13551366
}
13561367
// Run database check silently to get status.
13571368
if ( $table_count > 0 ) {
1358-
$check_args = [];
13591369
$command = sprintf(
13601370
'/usr/bin/env %s%s %s',
13611371
Utils\get_sql_check_command(),
1362-
$this->get_defaults_flag_string( $check_args ),
1372+
$this->get_defaults_flag_string( $assoc_args ),
13631373
'%s'
13641374
);
13651375
list( $stdout, $stderr, $exit_code ) = self::run(
13661376
Utils\esc_cmd( $command, DB_NAME ),
1367-
[ 'check' => true ],
1377+
array_merge( [ 'check' => true ], $assoc_args ),
13681378
false
13691379
);
13701380
$check_status = ( 0 === $exit_code ) ? 'OK' : 'Error';

0 commit comments

Comments
 (0)