From 4754f268a4b3a95198779b8099d7d300b31f15c6 Mon Sep 17 00:00:00 2001 From: Patrick Garman Date: Sun, 9 Dec 2018 11:56:36 -0600 Subject: [PATCH 1/6] =?UTF-8?q?Adds=20=E2=80=94human-readable=20parameter?= =?UTF-8?q?=20to=20db=20size=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DB_Command.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 024c373ff..49ef7afe4 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -685,6 +685,9 @@ public function tables( $args, $assoc_args ) { * [--tables] * : Display each table name and size instead of the database size. * + * [--human-readable] + * : Display database sizes in human readable formats. + * * [--format] * : table, csv, json * --- @@ -751,11 +754,17 @@ public function size( $args, $assoc_args ) { $format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); $size_format = WP_CLI\Utils\get_flag_value( $assoc_args, 'size_format' ); + $human_readable = WP_CLI\Utils\get_flag_value( $assoc_args, 'human-readable', false ); $tables = WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' ); $tables = ! empty( $tables ); + if( ! is_null( $size_format ) && $human_readable ) { + WP_CLI::error( "The size_format and human-readable arguments were both passed." ); + } + unset( $assoc_args['format'] ); unset( $assoc_args['size_format'] ); + unset( $assoc_args['human-readable'] ); unset( $assoc_args['tables'] ); if ( empty( $args ) && empty( $assoc_args ) ) { @@ -766,7 +775,7 @@ public function size( $args, $assoc_args ) { $rows = array(); $fields = array( 'Name', 'Size' ); - $default_unit = ( empty( $size_format ) ) ? ' B' : ''; + $default_unit = ( empty( $size_format ) && ! $human_readable ) ? ' B' : ''; if ( $tables ) { @@ -803,7 +812,7 @@ public function size( $args, $assoc_args ) { ); } - if ( ! empty( $size_format ) ) { + if ( ! empty( $size_format ) || $human_readable ) { foreach( $rows as $index => $row ) { // These added WP 4.4.0. if ( ! defined( 'KB_IN_BYTES' ) ) { @@ -819,6 +828,13 @@ public function size( $args, $assoc_args ) { define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES ); } + if( $human_readable ) { + $size_key = floor( log( $row['Size'] ) / log( 1024 ) ); + $sizes = array( 'B', 'KB', 'MB', 'GB', 'TB' ); + + $size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[ 0 ]; + } + // Display the database size as a number. switch( $size_format ) { case 'TB': From cef37097eb647ddd3cb492a9190087982346a87a Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 9 Dec 2018 13:43:55 -0600 Subject: [PATCH 2/6] Update error more user friendly. Co-Authored-By: pmgarman --- 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 49ef7afe4..1df8d1dd7 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -759,7 +759,7 @@ public function size( $args, $assoc_args ) { $tables = ! empty( $tables ); if( ! is_null( $size_format ) && $human_readable ) { - WP_CLI::error( "The size_format and human-readable arguments were both passed." ); + WP_CLI::error( "Cannot use --size_format and --human-readable arguments at the same time." ); } unset( $assoc_args['format'] ); From 26d20f8eb9382b79335f5aa39a6a14e908a3e185 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 9 Dec 2018 13:44:14 -0600 Subject: [PATCH 3/6] Code formatting Co-Authored-By: pmgarman --- 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 1df8d1dd7..cf6fb1dbd 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -828,7 +828,7 @@ public function size( $args, $assoc_args ) { define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES ); } - if( $human_readable ) { + if ( $human_readable ) { $size_key = floor( log( $row['Size'] ) / log( 1024 ) ); $sizes = array( 'B', 'KB', 'MB', 'GB', 'TB' ); From 62886b43a21642f912af3c9865dfb8cd6f62be6f Mon Sep 17 00:00:00 2001 From: Patrick Garman Date: Mon, 10 Dec 2018 11:10:34 -0600 Subject: [PATCH 4/6] =?UTF-8?q?=E2=80=94human-readable=20should=20not=20al?= =?UTF-8?q?low=20plain=20numeric=20repsonses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 cf6fb1dbd..4e3970d1b 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -885,7 +885,7 @@ public function size( $args, $assoc_args ) { } } - if ( ! empty( $size_format) && ! $tables && ! $format ) { + if ( ! empty( $size_format) && ! $tables && ! $format && ! $human_readable ) { WP_CLI::Line( filter_var( $rows[0]['Size'], FILTER_SANITIZE_NUMBER_INT ) ); } else { // Display the rows. From 22a90cc7aaf25921241983a0dcb858b0191ed1d7 Mon Sep 17 00:00:00 2001 From: Patrick Garman Date: Mon, 10 Dec 2018 11:10:52 -0600 Subject: [PATCH 5/6] =?UTF-8?q?Adding=20tests=20for=20=E2=80=94human-reada?= =?UTF-8?q?ble?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- features/db-size.feature | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/features/db-size.feature b/features/db-size.feature index 0758cbe67..45faf3215 100644 --- a/features/db-size.feature +++ b/features/db-size.feature @@ -30,6 +30,55 @@ Feature: Display database size wp_cli_test """ + Scenario: Display only database size in a human readable format for a WordPress install + Given a WP install + + When I run `wp db size --human-readable` + Then STDOUT should contain: + """ + wp_cli_test + """ + + And STDOUT should contain: + """ + KB + """ + + When I try `wp db size --human-readable --size_format=b` + Then the return code should not be 0 + And STDERR should contain: + """ + Cannot use --size_format and --human-readable arguments at the same time. + """ + And STDOUT should be empty + + Scenario: Display only table sizes in a human readable format for a WordPress install + Given a WP install + + When I run `wp db size --tables --human-readable` + Then STDOUT should contain: + """ + wp_posts + """ + + And STDOUT should contain: + """ + KB + """ + + But STDOUT should not contain: + """ + wp_cli_test + """ + + When I try `wp db size --tables --human-readable --size_format=b` + Then the return code should not be 0 + And STDERR should contain: + """ + Cannot use --size_format and --human-readable arguments at the same time. + """ + And STDOUT should be empty + Scenario: Display only database size in bytes for a WordPress install Given a WP install From 0af95c6ee5e67b83dc6b66aece375588fb18c216 Mon Sep 17 00:00:00 2001 From: Patrick Garman Date: Wed, 12 Dec 2018 16:09:28 -0600 Subject: [PATCH 6/6] Base 1000 vs 1024 --- 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 4e3970d1b..afcddbdfd 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -829,7 +829,7 @@ public function size( $args, $assoc_args ) { } if ( $human_readable ) { - $size_key = floor( log( $row['Size'] ) / log( 1024 ) ); + $size_key = floor( log( $row['Size'] ) / log( 1000 ) ); $sizes = array( 'B', 'KB', 'MB', 'GB', 'TB' ); $size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[ 0 ];