Skip to content

Commit 8965e56

Browse files
Add support for --order, --orderby flags in wp db size command (#226)
Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent eaa49d0 commit 8965e56

File tree

2 files changed

+80
-5
lines changed

2 files changed

+80
-5
lines changed

features/db-size.feature

+35
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,38 @@ Feature: Display database size
202202
"""
203203
KB
204204
"""
205+
206+
@broken
207+
Scenario: Display ordered table names for a WordPress install
208+
Given a WP install
209+
And I run `wp site empty --yes`
210+
211+
When I run `wp db size --tables --order=asc --format=json`
212+
Then STDOUT should contain:
213+
"""
214+
[{"Name":"wp_commentmeta",
215+
"""
216+
217+
When I run `wp db size --tables --order=desc --format=json`
218+
Then STDOUT should contain:
219+
"""
220+
[{"Name":"wp_users",
221+
"""
222+
223+
@broken
224+
Scenario: Display ordered table sizes for a WordPress install
225+
Given a WP install
226+
And I run `wp site empty --yes`
227+
And I run `wp post generate --post_type=page --post_status=draft --count=300`
228+
229+
When I run `wp db size --tables --order=desc --orderby=size --format=json`
230+
Then STDOUT should contain:
231+
"""
232+
[{"Name":"wp_posts",
233+
"""
234+
235+
When I run `wp db size --tables --order=asc --orderby=size --format=json`
236+
Then STDOUT should not contain:
237+
"""
238+
[{"Name":"wp_posts",
239+
"""

src/DB_Command.php

+45-5
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,24 @@ public function tables( $args, $assoc_args ) {
904904
* [--all-tables]
905905
* : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix.
906906
*
907+
* [--order=<order>]
908+
* : Ascending or Descending order.
909+
* ---
910+
* default: asc
911+
* options:
912+
* - asc
913+
* - desc
914+
* ---
915+
*
916+
* [--orderby=<orderby>]
917+
* : Order by fields.
918+
* ---
919+
* default: name
920+
* options:
921+
* - name
922+
* - size
923+
* ---
924+
*
907925
* ## EXAMPLES
908926
*
909927
* $ wp db size
@@ -943,7 +961,6 @@ public function tables( $args, $assoc_args ) {
943961
* @when after_wp_load
944962
*/
945963
public function size( $args, $assoc_args ) {
946-
947964
global $wpdb;
948965

949966
$format = Utils\get_flag_value( $assoc_args, 'format' );
@@ -953,6 +970,8 @@ public function size( $args, $assoc_args ) {
953970
$tables = ! empty( $tables );
954971
$all_tables = Utils\get_flag_value( $assoc_args, 'all-tables' );
955972
$all_tables_with_prefix = Utils\get_flag_value( $assoc_args, 'all-tables-with-prefix' );
973+
$order = Utils\get_flag_value( $assoc_args, 'order', 'asc' );
974+
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
956975

957976
if ( ! is_null( $size_format ) && $human_readable ) {
958977
WP_CLI::error( 'Cannot use --size_format and --human-readable arguments at the same time.' );
@@ -989,8 +1008,9 @@ public function size( $args, $assoc_args ) {
9891008

9901009
// Add the table size to the list.
9911010
$rows[] = [
992-
'Name' => $table_name,
993-
'Size' => strtoupper( $table_bytes ) . $default_unit,
1011+
'Name' => $table_name,
1012+
'Size' => strtoupper( $table_bytes ) . $default_unit,
1013+
'Bytes' => strtoupper( $table_bytes ),
9941014
];
9951015
}
9961016
} else {
@@ -1005,8 +1025,9 @@ public function size( $args, $assoc_args ) {
10051025

10061026
// Add the database size to the list.
10071027
$rows[] = [
1008-
'Name' => DB_NAME,
1009-
'Size' => strtoupper( $db_bytes ) . $default_unit,
1028+
'Name' => DB_NAME,
1029+
'Size' => strtoupper( $db_bytes ) . $default_unit,
1030+
'Bytes' => strtoupper( $db_bytes ),
10101031
];
10111032
}
10121033

@@ -1088,6 +1109,25 @@ public function size( $args, $assoc_args ) {
10881109
if ( ! empty( $size_format ) && ! $tables && ! $format && ! $human_readable && true !== $all_tables && true !== $all_tables_with_prefix ) {
10891110
WP_CLI::line( str_replace( " {$size_format_display}", '', $rows[0]['Size'] ) );
10901111
} else {
1112+
1113+
// Sort the rows by user input
1114+
if ( $orderby ) {
1115+
usort(
1116+
$rows,
1117+
function( $a, $b ) use ( $order, $orderby ) {
1118+
1119+
$orderby_array = 'asc' === $order ? array( $a, $b ) : array( $b, $a );
1120+
list( $first, $second ) = $orderby_array;
1121+
1122+
if ( 'size' === $orderby ) {
1123+
return $first['Bytes'] > $second['Bytes'];
1124+
}
1125+
1126+
return strcmp( $first['Name'], $second['Name'] );
1127+
}
1128+
);
1129+
}
1130+
10911131
// Display the rows.
10921132
$args = [
10931133
'format' => $format,

0 commit comments

Comments
 (0)