Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds --human-readable parameter to db size command #124

Merged
merged 6 commits into from
Dec 17, 2018
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* ---
Expand Down Expand Up @@ -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( "Cannot use --size_format and --human-readable arguments at the same time." );
}

unset( $assoc_args['format'] );
unset( $assoc_args['size_format'] );
unset( $assoc_args['human-readable'] );
unset( $assoc_args['tables'] );

if ( empty( $args ) && empty( $assoc_args ) ) {
Expand All @@ -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 ) {

Expand Down Expand Up @@ -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' ) ) {
Expand All @@ -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' );
pmgarman marked this conversation as resolved.
Show resolved Hide resolved

$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[ 0 ];
}

// Display the database size as a number.
switch( $size_format ) {
case 'TB':
Expand Down