Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 26 additions & 4 deletions features/db-size.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,50 @@ Feature: Display database size
When I run `wp db size --size_format=b`
Then STDOUT should be a number


Scenario: Display only database size in kilobytes for a WordPress install
Given a WP install

When I run `wp db size --size_format=kb`
Then STDOUT should be a number


Scenario: Display only database size in megabytes for a WordPress install
Given a WP install

When I run `wp db size --size_format=mb`
Then STDOUT should be a number

Scenario: Display only database size in gigabytes for a WordPress install
Given a WP install

When I run `wp db size --size_format=gb`
Then STDOUT should be a number

Scenario: Display only database size in terabytes for a WordPress install
Given a WP install

When I run `wp db size --size_format=tb`
Then STDOUT should be a number

Scenario: Display only database size in Kibibytes for a WordPress install
Given a WP install

When I run `wp db size --size_format=KB`
Then STDOUT should be a number

Scenario: Display only database size in Mebibytes for a WordPress install
Given a WP install

When I run `wp db size --size_format=MB`
Then STDOUT should be a number

Scenario: Display only database size in Gibibytes for a WordPress install
Given a WP install

When I run `wp db size --size_format=GB`
Then STDOUT should be a number

Scenario: Display only database size in Tebibytes for a WordPress install
Given a WP install

When I run `wp db size --size_format=TB`
Then STDOUT should be a number
33 changes: 32 additions & 1 deletion src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,15 @@ public function tables( $args, $assoc_args ) {
* - mb (megabytes)
* - gb (gigabytes)
* - tb (terabytes)
* - B (ISO Byte setting, with no conversion)
* - KB (ISO Kilobyte setting, with 1 KB = 1,000 B)
* - KiB (ISO Kibibyte setting, with 1 KiB = 1,024 B)
* - MB (ISO Megabyte setting, with 1 MB = 1,000 KB)
* - MiB (ISO Mebibyte setting, with 1 MiB = 1,024 KiB)
* - GB (ISO Gigabyte setting, with 1 GB = 1,000 MB)
* - GiB (ISO Gibibyte setting, with 1 GiB = 1,024 MiB)
* - TB (ISO Terabyte setting, with 1 TB = 1,000 GB)
* - TiB (ISO Tebibyte setting, with 1 TiB = 1,024 GiB)
* ---
*
* [--tables]
Expand Down Expand Up @@ -800,29 +809,51 @@ public function size( $args, $assoc_args ) {

// Display the database size as a number.
switch( $size_format ) {
case 'TB':
$divisor = pow( 1000, 4 );
break;

case 'GB':
$divisor = pow( 1000, 3 );
break;

case 'MB':
$divisor = pow( 1000, 2 );
break;

case 'KB':
$divisor = 1000;
break;

case 'tb':
case 'TiB':
$divisor = TB_IN_BYTES;
break;

case 'gb':
case 'GiB':
$divisor = GB_IN_BYTES;
break;

case 'mb':
case 'MiB':
$divisor = MB_IN_BYTES;
break;

case 'kb':
case 'KiB':
$divisor = KB_IN_BYTES;
break;

case 'b':
case 'B':
default:
$divisor = 1;
break;
}
$size_format_display = preg_replace( '/IB$/u', 'iB', strtoupper( $size_format ) );

$rows[ $index ]['Size'] = ceil( $row['Size'] / $divisor ) . " " . strtoupper( $size_format );
$rows[ $index ]['Size'] = ceil( $row['Size'] / $divisor ) . " " . $size_format_display;
}
}

Expand Down