Skip to content

Commit 2ab6bd0

Browse files
authored
Merge pull request #124 from pmgarman/issue/108
Adds `--human-readable` parameter to `db size` command
2 parents f5662ed + 0af95c6 commit 2ab6bd0

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

features/db-size.feature

+49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,55 @@ Feature: Display database size
3030
wp_cli_test
3131
"""
3232

33+
Scenario: Display only database size in a human readable format for a WordPress install
34+
Given a WP install
35+
36+
When I run `wp db size --human-readable`
37+
Then STDOUT should contain:
38+
"""
39+
wp_cli_test
40+
"""
41+
42+
And STDOUT should contain:
43+
"""
44+
KB
45+
"""
46+
47+
When I try `wp db size --human-readable --size_format=b`
48+
Then the return code should not be 0
49+
And STDERR should contain:
50+
"""
51+
Cannot use --size_format and --human-readable arguments at the same time.
52+
"""
53+
And STDOUT should be empty
54+
55+
Scenario: Display only table sizes in a human readable format for a WordPress install
56+
Given a WP install
57+
58+
When I run `wp db size --tables --human-readable`
59+
Then STDOUT should contain:
60+
"""
61+
wp_posts
62+
"""
63+
64+
And STDOUT should contain:
65+
"""
66+
KB
67+
"""
68+
69+
But STDOUT should not contain:
70+
"""
71+
wp_cli_test
72+
"""
73+
74+
When I try `wp db size --tables --human-readable --size_format=b`
75+
Then the return code should not be 0
76+
And STDERR should contain:
77+
"""
78+
Cannot use --size_format and --human-readable arguments at the same time.
79+
"""
80+
And STDOUT should be empty
81+
3382
Scenario: Display only database size in bytes for a WordPress install
3483
Given a WP install
3584

src/DB_Command.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ public function tables( $args, $assoc_args ) {
685685
* [--tables]
686686
* : Display each table name and size instead of the database size.
687687
*
688+
* [--human-readable]
689+
* : Display database sizes in human readable formats.
690+
*
688691
* [--format]
689692
* : table, csv, json
690693
* ---
@@ -751,11 +754,17 @@ public function size( $args, $assoc_args ) {
751754

752755
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
753756
$size_format = WP_CLI\Utils\get_flag_value( $assoc_args, 'size_format' );
757+
$human_readable = WP_CLI\Utils\get_flag_value( $assoc_args, 'human-readable', false );
754758
$tables = WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' );
755759
$tables = ! empty( $tables );
756760

761+
if( ! is_null( $size_format ) && $human_readable ) {
762+
WP_CLI::error( "Cannot use --size_format and --human-readable arguments at the same time." );
763+
}
764+
757765
unset( $assoc_args['format'] );
758766
unset( $assoc_args['size_format'] );
767+
unset( $assoc_args['human-readable'] );
759768
unset( $assoc_args['tables'] );
760769

761770
if ( empty( $args ) && empty( $assoc_args ) ) {
@@ -766,7 +775,7 @@ public function size( $args, $assoc_args ) {
766775
$rows = array();
767776
$fields = array( 'Name', 'Size' );
768777

769-
$default_unit = ( empty( $size_format ) ) ? ' B' : '';
778+
$default_unit = ( empty( $size_format ) && ! $human_readable ) ? ' B' : '';
770779

771780
if ( $tables ) {
772781

@@ -803,7 +812,7 @@ public function size( $args, $assoc_args ) {
803812
);
804813
}
805814

806-
if ( ! empty( $size_format ) ) {
815+
if ( ! empty( $size_format ) || $human_readable ) {
807816
foreach( $rows as $index => $row ) {
808817
// These added WP 4.4.0.
809818
if ( ! defined( 'KB_IN_BYTES' ) ) {
@@ -819,6 +828,13 @@ public function size( $args, $assoc_args ) {
819828
define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
820829
}
821830

831+
if ( $human_readable ) {
832+
$size_key = floor( log( $row['Size'] ) / log( 1000 ) );
833+
$sizes = array( 'B', 'KB', 'MB', 'GB', 'TB' );
834+
835+
$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[ 0 ];
836+
}
837+
822838
// Display the database size as a number.
823839
switch( $size_format ) {
824840
case 'TB':
@@ -869,7 +885,7 @@ public function size( $args, $assoc_args ) {
869885
}
870886
}
871887

872-
if ( ! empty( $size_format) && ! $tables && ! $format ) {
888+
if ( ! empty( $size_format) && ! $tables && ! $format && ! $human_readable ) {
873889
WP_CLI::Line( filter_var( $rows[0]['Size'], FILTER_SANITIZE_NUMBER_INT ) );
874890
} else {
875891
// Display the rows.

0 commit comments

Comments
 (0)