Skip to content

Commit e14ff7c

Browse files
Merge pull request #69 from marksabbath/feature/default-value-size
Default value of size_format flag is incorrect #64
2 parents ccbac57 + fd96ef1 commit e14ff7c

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

features/db-size.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Feature: Display database size
1313

1414
And STDOUT should contain:
1515
"""
16-
KB
16+
B
1717
"""
1818

1919
Scenario: Display only table sizes for a WordPress install
@@ -22,7 +22,7 @@ Feature: Display database size
2222
When I run `wp db size --tables`
2323
Then STDOUT should contain:
2424
"""
25-
wp_posts 80 KB
25+
wp_posts 81920 B
2626
"""
2727

2828
But STDOUT should not contain:

src/DB_Command.php

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ public function size( $args, $assoc_args ) {
596596
$rows = array();
597597
$fields = array( 'Name', 'Size' );
598598

599+
$default_unit = ( empty( $size_format ) ) ? ' B' : '';
600+
599601
if ( $tables ) {
600602

601603
// Add all of the table sizes
@@ -612,7 +614,7 @@ public function size( $args, $assoc_args ) {
612614
// Add the table size to the list.
613615
$rows[] = array(
614616
'Name' => $table_name,
615-
'Size' => strtoupper( size_format( $table_bytes ) ),
617+
'Size' => strtoupper( $table_bytes ) . $default_unit,
616618
);
617619
}
618620
} else {
@@ -627,39 +629,43 @@ public function size( $args, $assoc_args ) {
627629
// Add the database size to the list.
628630
$rows[] = array(
629631
'Name' => DB_NAME,
630-
'Size' => strtoupper( size_format( $db_bytes ) ),
632+
'Size' => strtoupper( $db_bytes ) . $default_unit,
631633
);
632634
}
633635

634-
if ( ! empty( $size_format ) && isset( $db_bytes ) && ! $tables ) {
636+
if ( ! empty( $size_format ) ) {
637+
foreach( $rows as $index => $row ) {
638+
// These added WP 4.4.0.
639+
if ( ! defined( 'KB_IN_BYTES' ) ) {
640+
define( 'KB_IN_BYTES', 1024 );
641+
}
642+
if ( ! defined( 'MB_IN_BYTES' ) ) {
643+
define( 'MB_IN_BYTES', 1024 * KB_IN_BYTES );
644+
}
635645

636-
// These added WP 4.4.0.
637-
if ( ! defined( 'KB_IN_BYTES' ) ) {
638-
define( 'KB_IN_BYTES', 1024 );
639-
}
640-
if ( ! defined( 'MB_IN_BYTES' ) ) {
641-
define( 'MB_IN_BYTES', 1024 * KB_IN_BYTES );
642-
}
646+
// Display the database size as a number.
647+
switch( $size_format ) {
648+
case 'mb':
649+
$divisor = MB_IN_BYTES;
650+
break;
643651

644-
// Display the database size as a number.
645-
switch( $size_format ) {
646-
case 'mb':
647-
$divisor = MB_IN_BYTES;
648-
break;
652+
case 'kb':
653+
$divisor = KB_IN_BYTES;
654+
break;
649655

650-
case 'kb':
651-
$divisor = KB_IN_BYTES;
652-
break;
656+
case 'b':
657+
default:
658+
$divisor = 1;
659+
break;
660+
}
653661

654-
case 'b':
655-
default:
656-
$divisor = 1;
657-
break;
662+
$rows[ $index ]['Size'] = ceil( $row['Size'] / $divisor ) . " " . strtoupper( $size_format );
658663
}
664+
}
659665

660-
WP_CLI::Line( ceil( $db_bytes / $divisor ) );
666+
if ( ! empty( $size_format) && ! $tables ) {
667+
WP_CLI::Line( filter_var( $rows[0]['Size'], FILTER_SANITIZE_NUMBER_INT ) );
661668
} else {
662-
663669
// Display the rows.
664670
$args = array(
665671
'format' => $format,

0 commit comments

Comments
 (0)