Skip to content

Commit c1f1791

Browse files
Merge pull request #16 from petenelson/feature/db-size
Feature/db size
2 parents 50baea4 + c7d1129 commit c1f1791

File tree

3 files changed

+221
-2
lines changed

3 files changed

+221
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"db query",
4242
"db export",
4343
"db import",
44-
"db tables"
44+
"db tables",
45+
"db size"
4546
]
4647
}
4748
}

features/db-size.feature

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Assumes wp_cli_test has a database size of around 655,360 bytes.
2+
3+
Feature: Display database size
4+
5+
Scenario: Display only database size for a WordPress install
6+
Given a WP install
7+
8+
When I run `wp db size`
9+
Then STDOUT should contain:
10+
"""
11+
wp_cli_test
12+
"""
13+
14+
And STDOUT should contain:
15+
"""
16+
KB
17+
"""
18+
19+
Scenario: Display only table sizes for a WordPress install
20+
Given a WP install
21+
22+
When I run `wp db size --tables`
23+
Then STDOUT should contain:
24+
"""
25+
wp_posts 80 KB
26+
"""
27+
28+
But STDOUT should not contain:
29+
"""
30+
wp_cli_test
31+
"""
32+
33+
Scenario: Display only database size in bytes for a WordPress install
34+
Given a WP install
35+
36+
When I run `wp db size --size_format=b`
37+
Then STDOUT should be a number
38+
39+
40+
Scenario: Display only database size in kilobytes for a WordPress install
41+
Given a WP install
42+
43+
When I run `wp db size --size_format=kb`
44+
Then STDOUT should be a number
45+
46+
47+
Scenario: Display only database size in megabytes for a WordPress install
48+
Given a WP install
49+
50+
When I run `wp db size --size_format=mb`
51+
Then STDOUT should be a number

src/DB_Command.php

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,174 @@ public function tables( $args, $assoc_args ) {
456456
}
457457
}
458458

459+
/**
460+
* Display the database name and size.
461+
*
462+
* Display the database name and size for `DB_NAME` specified in wp-config.php,
463+
* defaults to a human-readable number.
464+
*
465+
* ## OPTIONS
466+
*
467+
* [--size_format]
468+
* : Display only the database size as a number.
469+
* ---
470+
* default: b
471+
* options:
472+
* - b (bytes)
473+
* - kb (kilobytes)
474+
* - mb (megabytes)
475+
* ---
476+
*
477+
* [--tables]
478+
* : Display each table name and size instead of the database size.
479+
*
480+
* [--format]
481+
* : table, csv, json
482+
* ---
483+
* default: table
484+
* options:
485+
* - table
486+
* - csv
487+
* - json
488+
* ---
489+
*
490+
* [--scope=<scope>]
491+
* : Can be all, global, ms_global, blog, or old tables. Defaults to all.
492+
*
493+
* [--network]
494+
* : List all the tables in a multisite install. Overrides --scope=<scope>.
495+
*
496+
* [--all-tables-with-prefix]
497+
* : List all tables that match the table prefix even if not registered on $wpdb. Overrides --network.
498+
*
499+
* [--all-tables]
500+
* : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix.
501+
*
502+
* ## EXAMPLES
503+
*
504+
* $ wp db size
505+
* +-------------------+------+
506+
* | Name | Size |
507+
* +-------------------+------+
508+
* | wordpress_default | 6 MB |
509+
* +-------------------+------+
510+
*
511+
* $ wp db size --tables
512+
* +-----------------------+-------+
513+
* | Name | Size |
514+
* +-----------------------+-------+
515+
* | wp_users | 64 KB |
516+
* | wp_usermeta | 48 KB |
517+
* | wp_posts | 80 KB |
518+
* | wp_comments | 96 KB |
519+
* | wp_links | 32 KB |
520+
* | wp_options | 32 KB |
521+
* | wp_postmeta | 48 KB |
522+
* | wp_terms | 48 KB |
523+
* | wp_term_taxonomy | 48 KB |
524+
* | wp_term_relationships | 32 KB |
525+
* | wp_termmeta | 48 KB |
526+
* | wp_commentmeta | 48 KB |
527+
* +-----------------------+-------+
528+
*
529+
* $ wp db size --size_format=b
530+
* 5865472
531+
*
532+
* $ wp db size --size_format=kb
533+
* 5728
534+
*
535+
* $ wp db size --size_format=mb
536+
* 6
537+
*/
538+
public function size( $args, $assoc_args ) {
539+
540+
@WP_CLI::get_runner()-> load_wordpress();
541+
542+
global $wpdb;
543+
544+
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
545+
$size_format = WP_CLI\Utils\get_flag_value( $assoc_args, 'size_format' );
546+
$tables = WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' );
547+
$tables = ! empty( $tables );
548+
549+
unset( $assoc_args['format'] );
550+
unset( $assoc_args['size_format'] );
551+
unset( $assoc_args['tables'] );
552+
553+
if ( empty( $args ) && empty( $assoc_args ) ) {
554+
$assoc_args['scope'] = 'all';
555+
}
556+
557+
// Build rows for the formatter.
558+
$rows = array();
559+
$fields = array( 'Name', 'Size' );
560+
561+
if ( $tables ) {
562+
563+
// Add all of the table sizes
564+
foreach( WP_CLI\Utils\wp_get_table_names( $args, $assoc_args ) as $table_name ) {
565+
566+
// Get the table size.
567+
$table_bytes = $wpdb->get_var( $wpdb->prepare(
568+
"SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = '%s' and Table_Name = '%s' GROUP BY Table_Name LIMIT 1",
569+
DB_NAME,
570+
$table_name
571+
)
572+
);
573+
574+
// Add the table size to the list.
575+
$rows[] = array(
576+
'Name' => $table_name,
577+
'Size' => size_format( $table_bytes ),
578+
);
579+
}
580+
} else {
581+
582+
// Get the database size.
583+
$db_bytes = $wpdb->get_var( $wpdb->prepare(
584+
"SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = '%s' GROUP BY table_schema;",
585+
DB_NAME
586+
)
587+
);
588+
589+
// Add the database size to the list.
590+
$rows[] = array(
591+
'Name' => DB_NAME,
592+
'Size' => size_format( $db_bytes ),
593+
);
594+
}
595+
596+
if ( ! empty( $size_format ) && isset( $db_bytes ) && ! $tables ) {
597+
598+
// Display the database size as a number.
599+
switch( $size_format ) {
600+
case 'mb':
601+
$divisor = MB_IN_BYTES;
602+
break;
603+
604+
case 'kb':
605+
$divisor = KB_IN_BYTES;
606+
break;
607+
608+
case 'b':
609+
default:
610+
$divisor = 1;
611+
break;
612+
}
613+
614+
WP_CLI::Line( ceil( $db_bytes / $divisor ) );
615+
} else {
616+
617+
// Display the rows.
618+
$args = array(
619+
'format' => $format,
620+
);
621+
622+
$formatter = new \WP_CLI\Formatter( $args, $fields );
623+
$formatter->display_items( $rows );
624+
}
625+
}
626+
459627
private static function get_create_query() {
460628

461629
$create_query = sprintf( 'CREATE DATABASE `%s`', DB_NAME );
@@ -487,5 +655,4 @@ private static function run( $cmd, $assoc_args = array(), $descriptors = null )
487655
$final_args = array_merge( $assoc_args, $required );
488656
Utils\run_mysql_command( $cmd, $final_args, $descriptors );
489657
}
490-
491658
}

0 commit comments

Comments
 (0)