From 970cf3f958a5d42cb4eb75ae63bbbf0671efa0ba Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 16:05:09 +0000 Subject: [PATCH 01/14] Initial plan From 0a6e26545b9900fe80b5915df0f2ac9619894a78 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 16:10:08 +0000 Subject: [PATCH 02/14] Add wp db status command implementation Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- composer.json | 3 +- features/db-status.feature | 56 ++++++++++++++++++++++ src/DB_Command.php | 95 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 features/db-status.feature diff --git a/composer.json b/composer.json index e575f01f5..34fbdb20f 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,8 @@ "db search", "db tables", "db size", - "db columns" + "db columns", + "db status" ] }, "autoload": { diff --git a/features/db-status.feature b/features/db-status.feature new file mode 100644 index 000000000..b758e4e10 --- /dev/null +++ b/features/db-status.feature @@ -0,0 +1,56 @@ +Feature: Display database status overview + + Scenario: Display database status for a WordPress install + Given a WP install + + When I run `wp db status` + Then STDOUT should contain: + """ + Database Name: + """ + And STDOUT should contain: + """ + Tables: + """ + And STDOUT should contain: + """ + Total Size: + """ + And STDOUT should contain: + """ + Prefix: wp_ + """ + And STDOUT should contain: + """ + Engine: + """ + And STDOUT should contain: + """ + Charset: + """ + And STDOUT should contain: + """ + Collation: + """ + And STDOUT should contain: + """ + Check Status: + """ + + Scenario: Verify database status shows correct database name + Given a WP install + + When I run `wp db status` + Then STDOUT should contain: + """ + wp_cli_test + """ + + Scenario: Verify database status shows check status as OK + Given a WP install + + When I run `wp db status` + Then STDOUT should contain: + """ + Check Status: OK + """ diff --git a/src/DB_Command.php b/src/DB_Command.php index 776761727..da8b0d907 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1247,6 +1247,101 @@ public function prefix() { WP_CLI::log( $wpdb->prefix ); } + /** + * Displays a quick database status overview. + * + * Shows key database information including name, table count, size, + * prefix, engine, charset, collation, and health check status. This + * command is useful for getting a quick snapshot of database health + * without needing to run multiple separate commands. + * + * ## EXAMPLES + * + * $ wp db status + * Database Name: wp_cli_test + * Tables: 54 + * Total Size: 312 KB + * Prefix: wp_ + * Engine: InnoDB + * Charset: utf8mb4 + * Collation: utf8mb4_unicode_ci + * Check Status: OK + * + * @when after_wp_load + */ + public function status() { + global $wpdb; + + // Get database name. + $db_name = DB_NAME; + + // Get table count. + $table_count = count( Utils\wp_get_table_names( [], [ 'scope' => 'all' ] ) ); + + // Get total database size. + $db_size_bytes = $wpdb->get_var( + $wpdb->prepare( + 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema;', + DB_NAME + ) + ); + + // Format size to human-readable. + $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); + $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; + $size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0]; + $divisor = pow( 1000, $size_key ); + $db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; + + // Get prefix. + $prefix = $wpdb->prefix; + + // Get engine, charset, and collation from information_schema (using a common table). + $table_info = $wpdb->get_row( + $wpdb->prepare( + 'SELECT ENGINE as engine, CCSA.character_set_name as charset, TABLE_COLLATION as collation ' + . 'FROM information_schema.TABLES T ' + . 'LEFT JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA ' + . 'ON CCSA.collation_name = T.table_collation ' + . 'WHERE T.table_schema = %s ' + . 'AND T.table_name LIKE %s ' + . 'LIMIT 1', + DB_NAME, + $wpdb->esc_like( $prefix ) . '%' + ) + ); + + $engine = $table_info && isset( $table_info->engine ) ? $table_info->engine : 'N/A'; + $charset = $table_info && isset( $table_info->charset ) ? $table_info->charset : 'N/A'; + $collation = $table_info && isset( $table_info->collation ) ? $table_info->collation : 'N/A'; + + // Run database check silently to get status. + $check_args = []; + $command = sprintf( + '/usr/bin/env %s%s %s', + Utils\get_sql_check_command(), + $this->get_defaults_flag_string( $check_args ), + '%s' + ); + list( $stdout, $stderr, $exit_code ) = self::run( + Utils\esc_cmd( $command, DB_NAME ), + [ 'check' => true ], + false + ); + + $check_status = ( 0 === $exit_code ) ? 'OK' : 'Error'; + + // Output formatted status. + WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', $db_name ) ); + WP_CLI::log( sprintf( '%-18s %d', 'Tables:', $table_count ) ); + WP_CLI::log( sprintf( '%-18s %s', 'Total Size:', $db_size ) ); + WP_CLI::log( sprintf( '%-18s %s', 'Prefix:', $prefix ) ); + WP_CLI::log( sprintf( '%-18s %s', 'Engine:', $engine ) ); + WP_CLI::log( sprintf( '%-18s %s', 'Charset:', $charset ) ); + WP_CLI::log( sprintf( '%-18s %s', 'Collation:', $collation ) ); + WP_CLI::log( sprintf( '%-18s %s', 'Check Status:', $check_status ) ); + } + /** * Finds a string in the database. * From 7c06ca33ae7cba7bb2ad5b85b9e254c096433a39 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 16:11:23 +0000 Subject: [PATCH 03/14] Fix edge case handling for zero database size Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/DB_Command.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index da8b0d907..dc2d5ac93 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1287,11 +1287,15 @@ public function status() { ); // Format size to human-readable. - $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); - $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; - $size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0]; - $divisor = pow( 1000, $size_key ); - $db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; + if ( empty( $db_size_bytes ) || $db_size_bytes <= 0 ) { + $db_size = '0 B'; + } else { + $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); + $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; + $size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0]; + $divisor = pow( 1000, $size_key ); + $db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; + } // Get prefix. $prefix = $wpdb->prefix; From 94d0fea06600b4686908dbf61636a52361303304 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 2 Feb 2026 20:47:50 -0500 Subject: [PATCH 04/14] Update src/DB_Command.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/DB_Command.php | 48 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index dc2d5ac93..32c83df4c 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1300,25 +1300,59 @@ public function status() { // Get prefix. $prefix = $wpdb->prefix; - // Get engine, charset, and collation from information_schema (using a common table). + // Get engine, charset, and collation from information_schema across all tables with the prefix. $table_info = $wpdb->get_row( $wpdb->prepare( - 'SELECT ENGINE as engine, CCSA.character_set_name as charset, TABLE_COLLATION as collation ' + 'SELECT ' + . 'COUNT(DISTINCT ENGINE) AS engine_count, ' + . 'MIN(ENGINE) AS engine, ' + . 'COUNT(DISTINCT CCSA.character_set_name) AS charset_count, ' + . 'MIN(CCSA.character_set_name) AS charset, ' + . 'COUNT(DISTINCT TABLE_COLLATION) AS collation_count, ' + . 'MIN(TABLE_COLLATION) AS collation ' . 'FROM information_schema.TABLES T ' . 'LEFT JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA ' . 'ON CCSA.collation_name = T.table_collation ' . 'WHERE T.table_schema = %s ' - . 'AND T.table_name LIKE %s ' - . 'LIMIT 1', + . 'AND T.table_name LIKE %s', DB_NAME, $wpdb->esc_like( $prefix ) . '%' ) ); - $engine = $table_info && isset( $table_info->engine ) ? $table_info->engine : 'N/A'; - $charset = $table_info && isset( $table_info->charset ) ? $table_info->charset : 'N/A'; - $collation = $table_info && isset( $table_info->collation ) ? $table_info->collation : 'N/A'; + if ( $table_info ) { + $engine_count = isset( $table_info->engine_count ) ? (int) $table_info->engine_count : 0; + $charset_count = isset( $table_info->charset_count ) ? (int) $table_info->charset_count : 0; + $collation_count = isset( $table_info->collation_count ) ? (int) $table_info->collation_count : 0; + if ( $engine_count > 1 ) { + $engine = 'Mixed'; + } elseif ( isset( $table_info->engine ) && '' !== $table_info->engine ) { + $engine = $table_info->engine; + } else { + $engine = 'N/A'; + } + + if ( $charset_count > 1 ) { + $charset = 'Mixed'; + } elseif ( isset( $table_info->charset ) && '' !== $table_info->charset ) { + $charset = $table_info->charset; + } else { + $charset = 'N/A'; + } + + if ( $collation_count > 1 ) { + $collation = 'Mixed'; + } elseif ( isset( $table_info->collation ) && '' !== $table_info->collation ) { + $collation = $table_info->collation; + } else { + $collation = 'N/A'; + } + } else { + $engine = 'N/A'; + $charset = 'N/A'; + $collation = 'N/A'; + } // Run database check silently to get status. $check_args = []; $command = sprintf( From ba356779b4c43f67ee5c15cc0e5c6fa0f7368fdd Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 2 Feb 2026 20:47:59 -0500 Subject: [PATCH 05/14] Update src/DB_Command.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/DB_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 32c83df4c..60f8286b5 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1290,11 +1290,11 @@ public function status() { if ( empty( $db_size_bytes ) || $db_size_bytes <= 0 ) { $db_size = '0 B'; } else { - $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); + $size_key = floor( log( $db_size_bytes ) / log( 1000 ) ); $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; $size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0]; $divisor = pow( 1000, $size_key ); - $db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; + $db_size = round( $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; } // Get prefix. From 66478f60014b67445129200f1bfc0321662ed911 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 2 Feb 2026 20:48:19 -0500 Subject: [PATCH 06/14] Update src/DB_Command.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/DB_Command.php | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 60f8286b5..7d89b79a1 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1354,21 +1354,24 @@ public function status() { $collation = 'N/A'; } // Run database check silently to get status. - $check_args = []; - $command = sprintf( - '/usr/bin/env %s%s %s', - Utils\get_sql_check_command(), - $this->get_defaults_flag_string( $check_args ), - '%s' - ); - list( $stdout, $stderr, $exit_code ) = self::run( - Utils\esc_cmd( $command, DB_NAME ), - [ 'check' => true ], - false - ); - - $check_status = ( 0 === $exit_code ) ? 'OK' : 'Error'; - + if ( $table_count > 0 ) { + $check_args = []; + $command = sprintf( + '/usr/bin/env %s%s %s', + Utils\get_sql_check_command(), + $this->get_defaults_flag_string( $check_args ), + '%s' + ); + list( $stdout, $stderr, $exit_code ) = self::run( + Utils\esc_cmd( $command, DB_NAME ), + [ 'check' => true ], + false + ); + $check_status = ( 0 === $exit_code ) ? 'OK' : 'Error'; + } else { + // No tables to check; mark status as not applicable. + $check_status = 'N/A'; + } // Output formatted status. WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', $db_name ) ); WP_CLI::log( sprintf( '%-18s %d', 'Tables:', $table_count ) ); From 8a0d032499e026430a5636b3557a39bd760866b7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 02:04:24 +0000 Subject: [PATCH 07/14] Fix wp db status to accept parameters and run before WordPress loads Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/db-status.feature | 66 ++++++++++++++++++++++++++++++++++++++ src/DB_Command.php | 20 +++++++++--- 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/features/db-status.feature b/features/db-status.feature index b758e4e10..1702184f2 100644 --- a/features/db-status.feature +++ b/features/db-status.feature @@ -54,3 +54,69 @@ Feature: Display database status overview """ Check Status: OK """ + + Scenario: Run db status with MySQL defaults to check the database + Given a WP install + + When I run `wp db status --defaults` + Then STDOUT should contain: + """ + Database Name: + """ + And STDOUT should contain: + """ + Check Status: OK + """ + + Scenario: Run db status with --no-defaults to check the database + Given a WP install + + When I run `wp db status --no-defaults` + Then STDOUT should contain: + """ + Database Name: + """ + And STDOUT should contain: + """ + Check Status: OK + """ + + Scenario: Run db status with passed-in options + Given a WP install + + When I run `wp db status --dbuser=wp_cli_test` + Then STDOUT should contain: + """ + Database Name: + """ + And STDOUT should contain: + """ + Check Status: OK + """ + + When I run `wp db status --dbpass=password1` + Then STDOUT should contain: + """ + Database Name: + """ + And STDOUT should contain: + """ + Check Status: OK + """ + + When I run `wp db status --dbuser=wp_cli_test --dbpass=password1` + Then STDOUT should contain: + """ + Database Name: + """ + And STDOUT should contain: + """ + Check Status: OK + """ + + When I try `wp db status --dbuser=no_such_user` + Then the return code should not be 0 + + When I try `wp db status --dbpass=no_such_pass` + Then the return code should not be 0 + diff --git a/src/DB_Command.php b/src/DB_Command.php index 7d89b79a1..a66800b02 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1255,6 +1255,17 @@ public function prefix() { * command is useful for getting a quick snapshot of database health * without needing to run multiple separate commands. * + * ## OPTIONS + * + * [--dbuser=] + * : Username to pass to mysql. Defaults to DB_USER. + * + * [--dbpass=] + * : Password to pass to mysql. Defaults to DB_PASSWORD. + * + * [--defaults] + * : Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration. + * * ## EXAMPLES * * $ wp db status @@ -1267,9 +1278,9 @@ public function prefix() { * Collation: utf8mb4_unicode_ci * Check Status: OK * - * @when after_wp_load + * @when before_wp_load */ - public function status() { + public function status( $_, $assoc_args ) { global $wpdb; // Get database name. @@ -1355,16 +1366,15 @@ public function status() { } // Run database check silently to get status. if ( $table_count > 0 ) { - $check_args = []; $command = sprintf( '/usr/bin/env %s%s %s', Utils\get_sql_check_command(), - $this->get_defaults_flag_string( $check_args ), + $this->get_defaults_flag_string( $assoc_args ), '%s' ); list( $stdout, $stderr, $exit_code ) = self::run( Utils\esc_cmd( $command, DB_NAME ), - [ 'check' => true ], + array_merge( [ 'check' => true ], $assoc_args ), false ); $check_status = ( 0 === $exit_code ) ? 'OK' : 'Error'; From 9da36cfa6b325e76c6d09d9d727c26f1584c5c13 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Feb 2026 20:51:48 -0500 Subject: [PATCH 08/14] Small fixes --- src/DB_Command.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index a66800b02..9945c6fb3 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1278,18 +1278,13 @@ public function prefix() { * Collation: utf8mb4_unicode_ci * Check Status: OK * - * @when before_wp_load + * @when after_wp_load */ public function status( $_, $assoc_args ) { global $wpdb; - // Get database name. - $db_name = DB_NAME; - - // Get table count. $table_count = count( Utils\wp_get_table_names( [], [ 'scope' => 'all' ] ) ); - // Get total database size. $db_size_bytes = $wpdb->get_var( $wpdb->prepare( 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema;', @@ -1297,7 +1292,6 @@ public function status( $_, $assoc_args ) { ) ); - // Format size to human-readable. if ( empty( $db_size_bytes ) || $db_size_bytes <= 0 ) { $db_size = '0 B'; } else { @@ -1308,10 +1302,8 @@ public function status( $_, $assoc_args ) { $db_size = round( $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; } - // Get prefix. $prefix = $wpdb->prefix; - // Get engine, charset, and collation from information_schema across all tables with the prefix. $table_info = $wpdb->get_row( $wpdb->prepare( 'SELECT ' @@ -1382,8 +1374,8 @@ public function status( $_, $assoc_args ) { // No tables to check; mark status as not applicable. $check_status = 'N/A'; } - // Output formatted status. - WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', $db_name ) ); + + WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', DB_NAME ) ); WP_CLI::log( sprintf( '%-18s %d', 'Tables:', $table_count ) ); WP_CLI::log( sprintf( '%-18s %s', 'Total Size:', $db_size ) ); WP_CLI::log( sprintf( '%-18s %s', 'Prefix:', $prefix ) ); From 10a1f7207597c62ed870115da70524be81ca52a8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 4 Feb 2026 02:00:34 +0000 Subject: [PATCH 09/14] Refactor status command for better maintainability and test robustness Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/db-status.feature | 45 +++++--------------------------------- src/DB_Command.php | 34 ++++++++++++++++------------ 2 files changed, 26 insertions(+), 53 deletions(-) diff --git a/features/db-status.feature b/features/db-status.feature index 1702184f2..138bf726d 100644 --- a/features/db-status.feature +++ b/features/db-status.feature @@ -6,51 +6,18 @@ Feature: Display database status overview When I run `wp db status` Then STDOUT should contain: """ - Database Name: - """ - And STDOUT should contain: - """ - Tables: - """ - And STDOUT should contain: - """ - Total Size: + Database Name: wp_cli_test """ + And STDOUT should match /^Tables:\s+\d+$/m + And STDOUT should match /^Total Size:\s+[\d.]+ \wB$/m And STDOUT should contain: """ Prefix: wp_ """ + And STDOUT should match /^Engine:\s+\w+$/m + And STDOUT should match /^Charset:\s+\w+$/m + And STDOUT should match /^Collation:\s+\w+$/m And STDOUT should contain: - """ - Engine: - """ - And STDOUT should contain: - """ - Charset: - """ - And STDOUT should contain: - """ - Collation: - """ - And STDOUT should contain: - """ - Check Status: - """ - - Scenario: Verify database status shows correct database name - Given a WP install - - When I run `wp db status` - Then STDOUT should contain: - """ - wp_cli_test - """ - - Scenario: Verify database status shows check status as OK - Given a WP install - - When I run `wp db status` - Then STDOUT should contain: """ Check Status: OK """ diff --git a/src/DB_Command.php b/src/DB_Command.php index 9945c6fb3..8f55382f1 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1292,14 +1292,14 @@ public function status( $_, $assoc_args ) { ) ); - if ( empty( $db_size_bytes ) || $db_size_bytes <= 0 ) { - $db_size = '0 B'; - } else { - $size_key = floor( log( $db_size_bytes ) / log( 1000 ) ); + if ( ! empty( $db_size_bytes ) && $db_size_bytes > 0 ) { + $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; - $size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0]; + $size_format = $sizes[ $size_key ] ?? $sizes[0]; $divisor = pow( 1000, $size_key ); - $db_size = round( $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; + $db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; + } else { + $db_size = '0 B'; } $prefix = $wpdb->prefix; @@ -1375,14 +1375,20 @@ public function status( $_, $assoc_args ) { $check_status = 'N/A'; } - WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', DB_NAME ) ); - WP_CLI::log( sprintf( '%-18s %d', 'Tables:', $table_count ) ); - WP_CLI::log( sprintf( '%-18s %s', 'Total Size:', $db_size ) ); - WP_CLI::log( sprintf( '%-18s %s', 'Prefix:', $prefix ) ); - WP_CLI::log( sprintf( '%-18s %s', 'Engine:', $engine ) ); - WP_CLI::log( sprintf( '%-18s %s', 'Charset:', $charset ) ); - WP_CLI::log( sprintf( '%-18s %s', 'Collation:', $collation ) ); - WP_CLI::log( sprintf( '%-18s %s', 'Check Status:', $check_status ) ); + $status_items = [ + 'Database Name' => DB_NAME, + 'Tables' => $table_count, + 'Total Size' => $db_size, + 'Prefix' => $prefix, + 'Engine' => $engine, + 'Charset' => $charset, + 'Collation' => $collation, + 'Check Status' => $check_status, + ]; + + foreach ( $status_items as $label => $value ) { + WP_CLI::log( sprintf( '%-18s %s', $label . ':', $value ) ); + } } /** From 13d3155eb651e08dd33a40030289b96f81d037e4 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 26 Feb 2026 10:32:09 +0100 Subject: [PATCH 10/14] PHPStan fix --- src/DB_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index d70c611d6..3b08ccb82 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1319,7 +1319,7 @@ public function status( $_, $assoc_args ) { if ( ! empty( $db_size_bytes ) && $db_size_bytes > 0 ) { $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; - $size_format = $sizes[ $size_key ] ?? $sizes[0]; + $size_format = $sizes[ (int) $size_key ] ?? $sizes[0]; $divisor = pow( 1000, $size_key ); $db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; } else { From 79927292b21f76958ca2d0ba1de2fa6436f1b696 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 26 Feb 2026 09:45:16 +0000 Subject: [PATCH 11/14] Refactor wp db status to work without loading WordPress Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/DB_Command.php | 138 ++++++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 57 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 3b08ccb82..ee5dce537 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1279,6 +1279,10 @@ public function prefix() { * command is useful for getting a quick snapshot of database health * without needing to run multiple separate commands. * + * This command works even when WordPress is not fully installed, + * providing minimal information. If WordPress is installed, additional + * details are included. + * * ## OPTIONS * * [--dbuser=] @@ -1301,21 +1305,52 @@ public function prefix() { * Charset: utf8mb4 * Collation: utf8mb4_unicode_ci * Check Status: OK - * - * @when after_wp_load */ public function status( $_, $assoc_args ) { - global $wpdb; + global $table_prefix; - $table_count = count( Utils\wp_get_table_names( [], [ 'scope' => 'all' ] ) ); + // Get database name from constants (available after wp-config loads). + $db_name = DB_NAME; - $db_size_bytes = $wpdb->get_var( - $wpdb->prepare( - 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema;', - DB_NAME - ) + // Get table prefix from wp-config.php (available after wp-config loads). + $prefix = isset( $table_prefix ) ? $table_prefix : 'wp_'; + + // Escape prefix for use in LIKE pattern by escaping special characters. + $escaped_prefix = addslashes( str_replace( [ '%', '_' ], [ '\\%', '\\_' ], $prefix ) ); + + // Prepare command for executing queries. + $command = sprintf( + '/usr/bin/env %s%s --no-auto-rehash --batch --skip-column-names', + $this->get_mysql_command(), + $this->get_defaults_flag_string( $assoc_args ) + ); + + // Get table count using raw SQL query. + $table_count_query = sprintf( + "SELECT COUNT(*) FROM information_schema.TABLES WHERE table_schema = '%s' AND table_name LIKE '%s%%'", + addslashes( DB_NAME ), + $escaped_prefix + ); + list( $stdout, $stderr, $exit_code ) = self::run( + $command, + array_merge( [ 'execute' => $table_count_query ], $assoc_args ), + false ); + $table_count = (int) trim( $stdout ); + // Get total database size using raw SQL query. + $db_size_query = sprintf( + "SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = '%s'", + addslashes( DB_NAME ) + ); + list( $stdout, $stderr, $exit_code ) = self::run( + $command, + array_merge( [ 'execute' => $db_size_query ], $assoc_args ), + false + ); + $db_size_bytes = trim( $stdout ); + + // Format size to human-readable. if ( ! empty( $db_size_bytes ) && $db_size_bytes > 0 ) { $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; @@ -1326,70 +1361,59 @@ public function status( $_, $assoc_args ) { $db_size = '0 B'; } - $prefix = $wpdb->prefix; - - $table_info = $wpdb->get_row( - $wpdb->prepare( - 'SELECT ' - . 'COUNT(DISTINCT ENGINE) AS engine_count, ' - . 'MIN(ENGINE) AS engine, ' - . 'COUNT(DISTINCT CCSA.character_set_name) AS charset_count, ' - . 'MIN(CCSA.character_set_name) AS charset, ' - . 'COUNT(DISTINCT TABLE_COLLATION) AS collation_count, ' - . 'MIN(TABLE_COLLATION) AS collation ' - . 'FROM information_schema.TABLES T ' - . 'LEFT JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA ' - . 'ON CCSA.collation_name = T.table_collation ' - . 'WHERE T.table_schema = %s ' - . 'AND T.table_name LIKE %s', - DB_NAME, - $wpdb->esc_like( $prefix ) . '%' - ) + // Get engine, charset, and collation from information_schema across all tables with the prefix. + $table_info_query = sprintf( + 'SELECT ' + . 'COUNT(DISTINCT ENGINE) AS engine_count, ' + . 'MIN(ENGINE) AS engine, ' + . 'COUNT(DISTINCT CCSA.character_set_name) AS charset_count, ' + . 'MIN(CCSA.character_set_name) AS charset, ' + . 'COUNT(DISTINCT TABLE_COLLATION) AS collation_count, ' + . 'MIN(TABLE_COLLATION) AS collation ' + . 'FROM information_schema.TABLES T ' + . 'LEFT JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA ' + . 'ON CCSA.collation_name = T.table_collation ' + . "WHERE T.table_schema = '%s' " + . "AND T.table_name LIKE '%s%%'", + addslashes( DB_NAME ), + $escaped_prefix + ); + list( $stdout, $stderr, $exit_code ) = self::run( + $command, + array_merge( [ 'execute' => $table_info_query ], $assoc_args ), + false ); - if ( $table_info ) { - $engine_count = isset( $table_info->engine_count ) ? (int) $table_info->engine_count : 0; - $charset_count = isset( $table_info->charset_count ) ? (int) $table_info->charset_count : 0; - $collation_count = isset( $table_info->collation_count ) ? (int) $table_info->collation_count : 0; - - if ( $engine_count > 1 ) { - $engine = 'Mixed'; - } elseif ( isset( $table_info->engine ) && '' !== $table_info->engine ) { - $engine = $table_info->engine; - } else { - $engine = 'N/A'; - } + // Parse the tab-separated output. + $table_info_parts = explode( "\t", trim( $stdout ) ); - if ( $charset_count > 1 ) { - $charset = 'Mixed'; - } elseif ( isset( $table_info->charset ) && '' !== $table_info->charset ) { - $charset = $table_info->charset; - } else { - $charset = 'N/A'; - } + if ( count( $table_info_parts ) >= 6 ) { + $engine_count = (int) $table_info_parts[0]; + $engine_value = $table_info_parts[1]; + $charset_count = (int) $table_info_parts[2]; + $charset_value = $table_info_parts[3]; + $collation_count = (int) $table_info_parts[4]; + $collation_value = $table_info_parts[5]; - if ( $collation_count > 1 ) { - $collation = 'Mixed'; - } elseif ( isset( $table_info->collation ) && '' !== $table_info->collation ) { - $collation = $table_info->collation; - } else { - $collation = 'N/A'; - } + $engine = $engine_count > 1 ? 'Mixed' : ( ! empty( $engine_value ) && 'NULL' !== $engine_value ? $engine_value : 'N/A' ); + $charset = $charset_count > 1 ? 'Mixed' : ( ! empty( $charset_value ) && 'NULL' !== $charset_value ? $charset_value : 'N/A' ); + $collation = $collation_count > 1 ? 'Mixed' : ( ! empty( $collation_value ) && 'NULL' !== $collation_value ? $collation_value : 'N/A' ); } else { $engine = 'N/A'; $charset = 'N/A'; $collation = 'N/A'; } + // Run database check silently to get status. if ( $table_count > 0 ) { - $command = sprintf( + $check_command = sprintf( '/usr/bin/env %s%s %s', Utils\get_sql_check_command(), $this->get_defaults_flag_string( $assoc_args ), '%s' ); list( $stdout, $stderr, $exit_code ) = self::run( - Utils\esc_cmd( $command, DB_NAME ), + Utils\esc_cmd( $check_command, DB_NAME ), array_merge( [ 'check' => true ], $assoc_args ), false ); @@ -1400,7 +1424,7 @@ public function status( $_, $assoc_args ) { } $status_items = [ - 'Database Name' => DB_NAME, + 'Database Name' => $db_name, 'Tables' => $table_count, 'Total Size' => $db_size, 'Prefix' => $prefix, From dc552dd75ba38eb465782b130d79ad8b1097760f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 26 Feb 2026 09:46:23 +0000 Subject: [PATCH 12/14] Fix escaping order for LIKE pattern in table prefix Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/DB_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index ee5dce537..22fc425cb 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1315,8 +1315,8 @@ public function status( $_, $assoc_args ) { // Get table prefix from wp-config.php (available after wp-config loads). $prefix = isset( $table_prefix ) ? $table_prefix : 'wp_'; - // Escape prefix for use in LIKE pattern by escaping special characters. - $escaped_prefix = addslashes( str_replace( [ '%', '_' ], [ '\\%', '\\_' ], $prefix ) ); + // Escape prefix for SQL: first addslashes, then escape LIKE special characters. + $escaped_prefix = str_replace( [ '%', '_' ], [ '\\%', '\\_' ], addslashes( $prefix ) ); // Prepare command for executing queries. $command = sprintf( From 0a08a9054c3f4d80865a6ea8db3c521f6020fcb3 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 10:27:26 +0000 Subject: [PATCH 13/14] Rework `wp db status` on top of current main The previous implementation called the removed get_mysql_command() helper, so every scenario failed after main moved on. - Read everything with a single information_schema query against the configured database instead of escaping DB_NAME into literals. - Derive the charset from the collation name, as joining COLLATION_CHARACTER_SET_APPLICABILITY duplicates rows on MariaDB 10.10+. - Error out when the database can't be queried, instead of printing zeros. - Detect table errors reported by mysqlcheck, which still exits with 0. - Support SQLite, add --skip-check and --format=json. - Use {DB_NAME}/{DB_USER} in tests and tag them per database type. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- features/db-status.feature | 94 ++++++++----- src/DB_Command.php | 275 +++++++++++++++++++------------------ 2 files changed, 205 insertions(+), 164 deletions(-) diff --git a/features/db-status.feature b/features/db-status.feature index 138bf726d..110c1e69f 100644 --- a/features/db-status.feature +++ b/features/db-status.feature @@ -1,15 +1,16 @@ Feature: Display database status overview + @require-mysql-or-mariadb Scenario: Display database status for a WordPress install Given a WP install When I run `wp db status` Then STDOUT should contain: """ - Database Name: wp_cli_test + Database Name: {DB_NAME} """ And STDOUT should match /^Tables:\s+\d+$/m - And STDOUT should match /^Total Size:\s+[\d.]+ \wB$/m + And STDOUT should match /^Total Size:\s+[\d.]+ [KM]?B$/m And STDOUT should contain: """ Prefix: wp_ @@ -22,68 +23,93 @@ Feature: Display database status overview Check Status: OK """ - Scenario: Run db status with MySQL defaults to check the database - Given a WP install - - When I run `wp db status --defaults` - Then STDOUT should contain: + When I run `wp db status --skip-check --format=json` + Then STDOUT should be JSON containing: """ - Database Name: + {"database":"{DB_NAME}","prefix":"wp_","check":"Skipped"} """ - And STDOUT should contain: + And STDOUT should match /"tables":\d+,"size":\d+,/ + + @require-mysql-or-mariadb + Scenario: Display database status before WordPress is installed + Given an empty directory + And WP files + And wp-config.php + And a database + + When I run `wp db status` + Then STDOUT should contain: """ - Check Status: OK + Database Name: {DB_NAME} + Tables: 0 + Total Size: 0 B + Prefix: wp_ + Engine: N/A + Charset: N/A + Collation: N/A + Check Status: N/A """ - Scenario: Run db status with --no-defaults to check the database + @require-mysql-or-mariadb + Scenario: Report mixed table engines Given a WP install - When I run `wp db status --no-defaults` + When I run `wp db query "CREATE TABLE wp_status_test (id INT) ENGINE=MyISAM"` + And I run `wp db status --skip-check` Then STDOUT should contain: """ - Database Name: - """ - And STDOUT should contain: - """ - Check Status: OK + Engine: Mixed """ + @require-mysql-or-mariadb Scenario: Run db status with passed-in options Given a WP install - When I run `wp db status --dbuser=wp_cli_test` + When I run `wp db status --defaults` Then STDOUT should contain: - """ - Database Name: - """ - And STDOUT should contain: """ Check Status: OK """ - When I run `wp db status --dbpass=password1` + When I run `wp db status --no-defaults` Then STDOUT should contain: - """ - Database Name: - """ - And STDOUT should contain: """ Check Status: OK """ - When I run `wp db status --dbuser=wp_cli_test --dbpass=password1` + When I run `wp db status --dbuser={DB_USER} --dbpass=password1` Then STDOUT should contain: - """ - Database Name: - """ - And STDOUT should contain: """ Check Status: OK """ When I try `wp db status --dbuser=no_such_user` - Then the return code should not be 0 + Then the return code should be 1 + And STDERR should contain: + """ + Error: Could not query the database: + """ When I try `wp db status --dbpass=no_such_pass` - Then the return code should not be 0 + Then the return code should be 1 + And STDERR should contain: + """ + Error: Could not query the database: + """ + @require-sqlite + Scenario: Display database status for a SQLite install + Given a WP install + + When I run `wp db status` + Then STDOUT should match /^Database Name:\s+\S+$/m + And STDOUT should match /^Tables:\s+[1-9]\d*$/m + And STDOUT should match /^Total Size:\s+[\d.]+ [KM]?B$/m + And STDOUT should contain: + """ + Prefix: wp_ + Engine: SQLite + Charset: N/A + Collation: N/A + Check Status: N/A + """ diff --git a/src/DB_Command.php b/src/DB_Command.php index e62c6d555..8536d964e 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1453,24 +1453,38 @@ public function prefix() { } /** - * Displays a quick database status overview. + * Displays an overview of the database. * - * Shows key database information including name, table count, size, - * prefix, engine, charset, collation, and health check status. This - * command is useful for getting a quick snapshot of database health - * without needing to run multiple separate commands. + * Shows the database name, the number of tables and their total size, the + * table prefix, the storage engine, character set and collation of the + * tables, and whether `mysqlcheck` reports any problems. Engine, charset + * and collation read "Mixed" when the tables don't all share the same one. * - * This command works even when WordPress is not fully installed, - * providing minimal information. If WordPress is installed, additional - * details are included. + * Unlike `wp db size` or `wp db prefix`, this doesn't load WordPress, so it + * also works before WordPress is installed. + * + * For SQLite databases, the engine is reported as SQLite, and charset, + * collation and check status are not available. * * ## OPTIONS * * [--dbuser=] - * : Username to pass to mysql. Defaults to DB_USER. + * : Username to pass to mysql and mysqlcheck. Defaults to DB_USER. * * [--dbpass=] - * : Password to pass to mysql. Defaults to DB_PASSWORD. + * : Password to pass to mysql and mysqlcheck. Defaults to DB_PASSWORD. + * + * [--skip-check] + * : Don't run `mysqlcheck`, which reads every table and can take a while on large databases. + * + * [--format=] + * : Render output in a particular format. The JSON output lists the total size in bytes. + * --- + * default: list + * options: + * - list + * - json + * --- * * [--defaults] * : Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration. @@ -1478,148 +1492,149 @@ public function prefix() { * ## EXAMPLES * * $ wp db status - * Database Name: wp_cli_test - * Tables: 54 - * Total Size: 312 KB + * Database Name: wordpress_dbase + * Tables: 12 + * Total Size: 868.35 KB * Prefix: wp_ * Engine: InnoDB * Charset: utf8mb4 - * Collation: utf8mb4_unicode_ci + * Collation: utf8mb4_unicode_520_ci * Check Status: OK + * + * $ wp db status --skip-check --format=json + * {"database":"wordpress_dbase","tables":12,"size":868352,"prefix":"wp_","engine":"InnoDB","charset":"utf8mb4","collation":"utf8mb4_unicode_520_ci","check":"Skipped"} */ public function status( $_, $assoc_args ) { global $table_prefix; - // Get database name from constants (available after wp-config loads). - $db_name = DB_NAME; - - // Get table prefix from wp-config.php (available after wp-config loads). - $prefix = isset( $table_prefix ) ? $table_prefix : 'wp_'; + $format = Utils\get_flag_value( $assoc_args, 'format', 'list' ); + $skip_check = Utils\get_flag_value( $assoc_args, 'skip-check', false ); + + $status = [ + 'database' => DB_NAME, + 'tables' => 0, + 'size' => 0, + 'prefix' => isset( $table_prefix ) && is_string( $table_prefix ) ? $table_prefix : '', + 'engine' => 'N/A', + 'charset' => 'N/A', + 'collation' => 'N/A', + 'check' => $skip_check ? 'Skipped' : 'N/A', + ]; - // Escape prefix for SQL: first addslashes, then escape LIKE special characters. - $escaped_prefix = str_replace( [ '%', '_' ], [ '\\%', '\\_' ], addslashes( $prefix ) ); + $this->maybe_load_sqlite_dropin(); - // Prepare command for executing queries. - $command = sprintf( - '/usr/bin/env %s%s --no-auto-rehash --batch --skip-column-names', - $this->get_mysql_command(), - $this->get_defaults_flag_string( $assoc_args ) - ); + if ( $this->is_sqlite() ) { + global $wpdb; - // Get table count using raw SQL query. - $table_count_query = sprintf( - "SELECT COUNT(*) FROM information_schema.TABLES WHERE table_schema = '%s' AND table_name LIKE '%s%%'", - addslashes( DB_NAME ), - $escaped_prefix - ); - list( $stdout, $stderr, $exit_code ) = self::run( - $command, - array_merge( [ 'execute' => $table_count_query ], $assoc_args ), - false - ); - $table_count = (int) trim( $stdout ); + $db_path = $this->get_sqlite_db_path(); + $status['database'] = $db_path ? basename( $db_path ) : ''; + $status['size'] = $this->sqlite_size(); + $status['engine'] = 'SQLite'; - // Get total database size using raw SQL query. - $db_size_query = sprintf( - "SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = '%s'", - addslashes( DB_NAME ) - ); - list( $stdout, $stderr, $exit_code ) = self::run( - $command, - array_merge( [ 'execute' => $db_size_query ], $assoc_args ), - false - ); - $db_size_bytes = trim( $stdout ); - - // Format size to human-readable. - if ( ! empty( $db_size_bytes ) && $db_size_bytes > 0 ) { - $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); - $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; - $size_format = $sizes[ (int) $size_key ] ?? $sizes[0]; - $divisor = pow( 1000, $size_key ); - $db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; + if ( isset( $wpdb ) && $wpdb instanceof \WP_SQLite_DB ) { + $status['tables'] = count( $wpdb->get_col( 'SHOW TABLES' ) ); + } } else { - $db_size = '0 B'; - } - - // Get engine, charset, and collation from information_schema across all tables with the prefix. - $table_info_query = sprintf( - 'SELECT ' - . 'COUNT(DISTINCT ENGINE) AS engine_count, ' - . 'MIN(ENGINE) AS engine, ' - . 'COUNT(DISTINCT CCSA.character_set_name) AS charset_count, ' - . 'MIN(CCSA.character_set_name) AS charset, ' - . 'COUNT(DISTINCT TABLE_COLLATION) AS collation_count, ' - . 'MIN(TABLE_COLLATION) AS collation ' - . 'FROM information_schema.TABLES T ' - . 'LEFT JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA ' - . 'ON CCSA.collation_name = T.table_collation ' - . "WHERE T.table_schema = '%s' " - . "AND T.table_name LIKE '%s%%'", - addslashes( DB_NAME ), - $escaped_prefix - ); - list( $stdout, $stderr, $exit_code ) = self::run( - $command, - array_merge( [ 'execute' => $table_info_query ], $assoc_args ), - false - ); + $defaults_flag = $this->get_defaults_flag_string( $assoc_args ); + $mysql_args = self::get_dbuser_dbpass_args( $assoc_args ); - // Parse the tab-separated output. - $table_info_parts = explode( "\t", trim( $stdout ) ); + $query = 'SELECT COUNT(*), COALESCE(SUM(DATA_LENGTH + INDEX_LENGTH), 0), ' + . 'COUNT(DISTINCT ENGINE), MIN(ENGINE), ' + . "COUNT(DISTINCT SUBSTRING_INDEX(TABLE_COLLATION, '_', 1)), MIN(SUBSTRING_INDEX(TABLE_COLLATION, '_', 1)), " + . 'COUNT(DISTINCT TABLE_COLLATION), MIN(TABLE_COLLATION) ' + . "FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_TYPE = 'BASE TABLE'"; - if ( count( $table_info_parts ) >= 6 ) { - $engine_count = (int) $table_info_parts[0]; - $engine_value = $table_info_parts[1]; - $charset_count = (int) $table_info_parts[2]; - $charset_value = $table_info_parts[3]; - $collation_count = (int) $table_info_parts[4]; - $collation_value = $table_info_parts[5]; - - $engine = $engine_count > 1 ? 'Mixed' : ( ! empty( $engine_value ) && 'NULL' !== $engine_value ? $engine_value : 'N/A' ); - $charset = $charset_count > 1 ? 'Mixed' : ( ! empty( $charset_value ) && 'NULL' !== $charset_value ? $charset_value : 'N/A' ); - $collation = $collation_count > 1 ? 'Mixed' : ( ! empty( $collation_value ) && 'NULL' !== $collation_value ? $collation_value : 'N/A' ); - } else { - $engine = 'N/A'; - $charset = 'N/A'; - $collation = 'N/A'; - } - - // Run database check silently to get status. - if ( $table_count > 0 ) { - $check_command = sprintf( - '/usr/bin/env %s%s %s', - Utils\get_sql_check_command(), - $this->get_defaults_flag_string( $assoc_args ), - '%s' - ); list( $stdout, $stderr, $exit_code ) = self::run( - Utils\esc_cmd( $check_command, DB_NAME ), - array_merge( [ 'check' => true ], $assoc_args ), + sprintf( + '%s%s --no-auto-rehash --batch --skip-column-names', + Utils\get_mysql_binary_path(), + $defaults_flag + ), + array_merge( + $mysql_args, + [ + 'database' => DB_NAME, + 'execute' => $query, + ] + ), false ); - $check_status = ( 0 === $exit_code ) ? 'OK' : 'Error'; - } else { - // No tables to check; mark status as not applicable. - $check_status = 'N/A'; - } - - $status_items = [ - 'Database Name' => $db_name, - 'Tables' => $table_count, - 'Total Size' => $db_size, - 'Prefix' => $prefix, - 'Engine' => $engine, - 'Charset' => $charset, - 'Collation' => $collation, - 'Check Status' => $check_status, - ]; - foreach ( $status_items as $label => $value ) { - WP_CLI::log( sprintf( '%-18s %s', $label . ':', $value ) ); + if ( $exit_code ) { + WP_CLI::error( 'Could not query the database: ' . trim( $stderr ) ); + } + + $row = array_pad( explode( "\t", trim( $stdout ) ), 8, 'NULL' ); + + $status['tables'] = (int) $row[0]; + $status['size'] = (int) $row[1]; + + $columns = [ + 'engine' => 2, + 'charset' => 4, + 'collation' => 6, + ]; + foreach ( $columns as $key => $index ) { + if ( (int) $row[ $index ] > 1 ) { + $status[ $key ] = 'Mixed'; + } elseif ( '' !== $row[ $index + 1 ] && 'NULL' !== $row[ $index + 1 ] ) { + $status[ $key ] = $row[ $index + 1 ]; + } + } + + $check_command = Utils\get_sql_check_command(); + if ( ! $skip_check && '' !== $check_command && $status['tables'] > 0 ) { + list( $stdout, , $exit_code ) = self::run( + Utils\esc_cmd( sprintf( '%s%s %s', $check_command, $defaults_flag, '%s' ), DB_NAME ), + array_merge( + $mysql_args, + [ + 'check' => true, + 'silent' => true, + ] + ), + false + ); + + $status['check'] = ( 0 === $exit_code && ! preg_match( '/^error\s*:/mi', $stdout ) ) ? 'OK' : 'Error'; + } + } + + if ( 'json' === $format ) { + WP_CLI::line( (string) json_encode( $status ) ); + return; + } + + $status['size'] = self::format_size_si( $status['size'] ); + + $labels = [ + 'database' => 'Database Name', + 'tables' => 'Tables', + 'size' => 'Total Size', + 'prefix' => 'Prefix', + 'engine' => 'Engine', + 'charset' => 'Charset', + 'collation' => 'Collation', + 'check' => 'Check Status', + ]; + foreach ( $labels as $key => $label ) { + WP_CLI::line( sprintf( '%-18s %s', $label . ':', $status[ $key ] ) ); } } + /** + * Formats a number of bytes with SI units, like `wp db size --human-readable`. + * + * @param int $bytes Number of bytes. + * @return string + */ + private static function format_size_si( $bytes ) { + $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; + $key = $bytes > 0 ? (int) min( floor( log( $bytes, 1000 ) ), count( $sizes ) - 1 ) : 0; + + return round( $bytes / pow( 1000, $key ), 2 ) . ' ' . $sizes[ $key ]; + } + /** * Finds a string in the database. * From 7c32defa6318399cef8f2e273daef6af1ec98a41 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 21:27:47 +0000 Subject: [PATCH 14/14] Never auto-repair tables in `wp db status` An option file read with --defaults could enable mysqlcheck's auto-repair, letting a read-only status check modify tables. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- src/DB_Command.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 8536d964e..0a49d2e06 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1589,8 +1589,10 @@ public function status( $_, $assoc_args ) { array_merge( $mysql_args, [ - 'check' => true, - 'silent' => true, + 'check' => true, + 'silent' => true, + // A read-only status check must not repair tables, even if an option file enables auto-repair. + 'skip-auto-repair' => true, ] ), false