Skip to content

Commit 0a6e265

Browse files
Copilotswissspidy
andcommitted
Add wp db status command implementation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 970cf3f commit 0a6e265

3 files changed

Lines changed: 153 additions & 1 deletion

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"db search",
5151
"db tables",
5252
"db size",
53-
"db columns"
53+
"db columns",
54+
"db status"
5455
]
5556
},
5657
"autoload": {

features/db-status.feature

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Feature: Display database status overview
2+
3+
Scenario: Display database status for a WordPress install
4+
Given a WP install
5+
6+
When I run `wp db status`
7+
Then STDOUT should contain:
8+
"""
9+
Database Name:
10+
"""
11+
And STDOUT should contain:
12+
"""
13+
Tables:
14+
"""
15+
And STDOUT should contain:
16+
"""
17+
Total Size:
18+
"""
19+
And STDOUT should contain:
20+
"""
21+
Prefix: wp_
22+
"""
23+
And STDOUT should contain:
24+
"""
25+
Engine:
26+
"""
27+
And STDOUT should contain:
28+
"""
29+
Charset:
30+
"""
31+
And STDOUT should contain:
32+
"""
33+
Collation:
34+
"""
35+
And STDOUT should contain:
36+
"""
37+
Check Status:
38+
"""
39+
40+
Scenario: Verify database status shows correct database name
41+
Given a WP install
42+
43+
When I run `wp db status`
44+
Then STDOUT should contain:
45+
"""
46+
wp_cli_test
47+
"""
48+
49+
Scenario: Verify database status shows check status as OK
50+
Given a WP install
51+
52+
When I run `wp db status`
53+
Then STDOUT should contain:
54+
"""
55+
Check Status: OK
56+
"""

src/DB_Command.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,101 @@ public function prefix() {
12471247
WP_CLI::log( $wpdb->prefix );
12481248
}
12491249

1250+
/**
1251+
* Displays a quick database status overview.
1252+
*
1253+
* Shows key database information including name, table count, size,
1254+
* prefix, engine, charset, collation, and health check status. This
1255+
* command is useful for getting a quick snapshot of database health
1256+
* without needing to run multiple separate commands.
1257+
*
1258+
* ## EXAMPLES
1259+
*
1260+
* $ wp db status
1261+
* Database Name: wp_cli_test
1262+
* Tables: 54
1263+
* Total Size: 312 KB
1264+
* Prefix: wp_
1265+
* Engine: InnoDB
1266+
* Charset: utf8mb4
1267+
* Collation: utf8mb4_unicode_ci
1268+
* Check Status: OK
1269+
*
1270+
* @when after_wp_load
1271+
*/
1272+
public function status() {
1273+
global $wpdb;
1274+
1275+
// Get database name.
1276+
$db_name = DB_NAME;
1277+
1278+
// Get table count.
1279+
$table_count = count( Utils\wp_get_table_names( [], [ 'scope' => 'all' ] ) );
1280+
1281+
// Get total database size.
1282+
$db_size_bytes = $wpdb->get_var(
1283+
$wpdb->prepare(
1284+
'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema;',
1285+
DB_NAME
1286+
)
1287+
);
1288+
1289+
// Format size to human-readable.
1290+
$size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) );
1291+
$sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
1292+
$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0];
1293+
$divisor = pow( 1000, $size_key );
1294+
$db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format;
1295+
1296+
// Get prefix.
1297+
$prefix = $wpdb->prefix;
1298+
1299+
// Get engine, charset, and collation from information_schema (using a common table).
1300+
$table_info = $wpdb->get_row(
1301+
$wpdb->prepare(
1302+
'SELECT ENGINE as engine, CCSA.character_set_name as charset, TABLE_COLLATION as collation '
1303+
. 'FROM information_schema.TABLES T '
1304+
. 'LEFT JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA '
1305+
. 'ON CCSA.collation_name = T.table_collation '
1306+
. 'WHERE T.table_schema = %s '
1307+
. 'AND T.table_name LIKE %s '
1308+
. 'LIMIT 1',
1309+
DB_NAME,
1310+
$wpdb->esc_like( $prefix ) . '%'
1311+
)
1312+
);
1313+
1314+
$engine = $table_info && isset( $table_info->engine ) ? $table_info->engine : 'N/A';
1315+
$charset = $table_info && isset( $table_info->charset ) ? $table_info->charset : 'N/A';
1316+
$collation = $table_info && isset( $table_info->collation ) ? $table_info->collation : 'N/A';
1317+
1318+
// Run database check silently to get status.
1319+
$check_args = [];
1320+
$command = sprintf(
1321+
'/usr/bin/env %s%s %s',
1322+
Utils\get_sql_check_command(),
1323+
$this->get_defaults_flag_string( $check_args ),
1324+
'%s'
1325+
);
1326+
list( $stdout, $stderr, $exit_code ) = self::run(
1327+
Utils\esc_cmd( $command, DB_NAME ),
1328+
[ 'check' => true ],
1329+
false
1330+
);
1331+
1332+
$check_status = ( 0 === $exit_code ) ? 'OK' : 'Error';
1333+
1334+
// Output formatted status.
1335+
WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', $db_name ) );
1336+
WP_CLI::log( sprintf( '%-18s %d', 'Tables:', $table_count ) );
1337+
WP_CLI::log( sprintf( '%-18s %s', 'Total Size:', $db_size ) );
1338+
WP_CLI::log( sprintf( '%-18s %s', 'Prefix:', $prefix ) );
1339+
WP_CLI::log( sprintf( '%-18s %s', 'Engine:', $engine ) );
1340+
WP_CLI::log( sprintf( '%-18s %s', 'Charset:', $charset ) );
1341+
WP_CLI::log( sprintf( '%-18s %s', 'Collation:', $collation ) );
1342+
WP_CLI::log( sprintf( '%-18s %s', 'Check Status:', $check_status ) );
1343+
}
1344+
12501345
/**
12511346
* Finds a string in the database.
12521347
*

0 commit comments

Comments
 (0)