From f583aa7d3c37f09df6c8a1bf56d54900bde21c0c Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 18 May 2017 10:01:40 -0500 Subject: [PATCH 01/16] added db size command and tests --- composer.json | 3 ++- features/db-size.feature | 29 +++++++++++++++++++++ src/DB_Command.php | 55 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 features/db-size.feature diff --git a/composer.json b/composer.json index 22c1689d3..7be8ba980 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "db query", "db export", "db import", - "db tables" + "db tables", + "db size" ] } } diff --git a/features/db-size.feature b/features/db-size.feature new file mode 100644 index 000000000..ff4a16fda --- /dev/null +++ b/features/db-size.feature @@ -0,0 +1,29 @@ +Feature: Display database size + + Scenario: Display database size for a WordPress install, standard output + Given a WP install + + When I run `wp db size` + Then STDOUT should contain: + """ + wp_cli_test + """ + + And STDOUT should contain: + """ + 640 KB + """ + + And STDOUT should contain: + """ + 655 + """ + + Scenario: Display database size for a WordPress install, bytes only + Given a WP install + + When I run `wp db size --format=bytes` + Then STDOUT should contain: + """ + 655 + """ diff --git a/src/DB_Command.php b/src/DB_Command.php index 01afef362..0284a41db 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -456,6 +456,60 @@ public function tables( $args, $assoc_args ) { } } + /** + * Display the database size. + * + * Deisplays the database size for `DB_NAME` specified in wp-config.php. + * + * ## OPTIONS + * + * [--format] + * : table, csv, json, bytes (displays only the size in bytes) + * + * ## EXAMPLES + * + * $ wp db size + * +-------------------+ + * | wordpress_default | + * +-------------------+ + * | 6,078,464 | + * +-------------------+ + */ + public function size( $args, $assoc_args ) { + + WP_CLI::get_runner()-> load_wordpress(); + + global $wpdb; + + $format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); + unset( $assoc_args['format'] ); + + // Get the database size. + $bytes = $wpdb->get_var( $wpdb->prepare( 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema;', DB_NAME ) ); + + // Build rows for the formatter. + $rows = array(); + $fields = array( 'Name', 'Size', 'Bytes' ); + + $rows[] = array( + 'Name' => DB_NAME, + 'Size' => size_format( $bytes ), + 'Bytes' => $bytes, + ); + + $args = array( + 'format' => $format, + ); + + if ( 'bytes' === $args['format'] ) { + WP_CLI::Line( $bytes ); + } else { + // Display the rows. + $formatter = new \WP_CLI\Formatter( $args, $fields ); + $formatter->display_items( $rows ); + } + } + private static function get_create_query() { $create_query = sprintf( 'CREATE DATABASE `%s`', DB_NAME ); @@ -487,5 +541,4 @@ private static function run( $cmd, $assoc_args = array(), $descriptors = null ) $final_args = array_merge( $assoc_args, $required ); Utils\run_mysql_command( $cmd, $final_args, $descriptors ); } - } From 2428d0592120b8a50bde18b17f97001970cf1b52 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 18 May 2017 10:06:54 -0500 Subject: [PATCH 02/16] updated db size docblock --- src/DB_Command.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 0284a41db..01915482d 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -459,7 +459,7 @@ public function tables( $args, $assoc_args ) { /** * Display the database size. * - * Deisplays the database size for `DB_NAME` specified in wp-config.php. + * Displays the database size for `DB_NAME` specified in wp-config.php. * * ## OPTIONS * @@ -469,11 +469,11 @@ public function tables( $args, $assoc_args ) { * ## EXAMPLES * * $ wp db size - * +-------------------+ - * | wordpress_default | - * +-------------------+ - * | 6,078,464 | - * +-------------------+ + * +-------------------+------+---------+ + * | Name | Size | Bytes | + * +-------------------+------+---------+ + * | wordpress_default | 6 MB | 5865472 | + * +-------------------+------+---------+ */ public function size( $args, $assoc_args ) { From 1233d913003788343a5aab7f64400d58f4ab285d Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 18 May 2017 10:44:44 -0500 Subject: [PATCH 03/16] added support for table sizes to db size command --- features/db-size.feature | 47 +++++++++++++++++++++++++++-- src/DB_Command.php | 64 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 104 insertions(+), 7 deletions(-) diff --git a/features/db-size.feature b/features/db-size.feature index ff4a16fda..b84e600f1 100644 --- a/features/db-size.feature +++ b/features/db-size.feature @@ -1,6 +1,8 @@ +# Assumes wp_cli_test has a database size of around 655,360 bytes. + Feature: Display database size - Scenario: Display database size for a WordPress install, standard output + Scenario: Display database and table sizes for a WordPress install Given a WP install When I run `wp db size` @@ -19,11 +21,50 @@ Feature: Display database size 655 """ - Scenario: Display database size for a WordPress install, bytes only + And STDOUT should contain: + """ + wp_terms + """ + + And STDOUT should contain: + """ + 48 KB + """ + + Scenario: Display only database size for a WordPress install Given a WP install - When I run `wp db size --format=bytes` + When I run `wp db size --db-only` + Then STDOUT should contain: + """ + wp_cli_test + """ + + And STDOUT should contain: + """ + 640 KB + """ + + And STDOUT should contain: + """ + 655 + """ + + And STDOUT should not contain: + """ + wp_terms + """ + + Scenario: Display only database size in bytes for a WordPress install + Given a WP install + + When I run `wp db size --db-only --format=bytes` Then STDOUT should contain: """ 655 """ + + And STDOUT should not contain: + """ + 640 KB + """ diff --git a/src/DB_Command.php b/src/DB_Command.php index 01915482d..9af421e40 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -459,12 +459,35 @@ public function tables( $args, $assoc_args ) { /** * Display the database size. * - * Displays the database size for `DB_NAME` specified in wp-config.php. + * Displays the database and table sizes for `DB_NAME` specified in wp-config.php. * * ## OPTIONS * + * [--db-only] + * : Only display the database size without including the tables. + * * [--format] - * : table, csv, json, bytes (displays only the size in bytes) + * : table, csv, json, bytes + * --- + * default: table + * options: + * - table + * - csv + * - json + * - bytes, only works when combined with --db-only + * --- + * + * [--scope=] + * : Can be all, global, ms_global, blog, or old tables. Defaults to all. + * + * [--network] + * : List all the tables in a multisite install. Overrides --scope=. + * + * [--all-tables-with-prefix] + * : List all tables that match the table prefix even if not registered on $wpdb. Overrides --network. + * + * [--all-tables] + * : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix. * * ## EXAMPLES * @@ -482,10 +505,20 @@ public function size( $args, $assoc_args ) { global $wpdb; $format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); + $db_only = ! empty( WP_CLI\Utils\get_flag_value( $assoc_args, 'db-only' ) ); unset( $assoc_args['format'] ); + unset( $assoc_args['db-only'] ); + + if ( empty( $args ) && empty( $assoc_args ) ) { + $assoc_args['scope'] = 'all'; + } // Get the database size. - $bytes = $wpdb->get_var( $wpdb->prepare( 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema;', DB_NAME ) ); + $bytes = $wpdb->get_var( $wpdb->prepare( + "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = '%s' GROUP BY table_schema;", + DB_NAME + ) + ); // Build rows for the formatter. $rows = array(); @@ -497,11 +530,34 @@ public function size( $args, $assoc_args ) { 'Bytes' => $bytes, ); + if ( ! $db_only ) { + + // Add all of the table sizes + foreach( WP_CLI\Utils\wp_get_table_names( $args, $assoc_args ) as $table_name ) { + + // Get the table size. + $table_bytes = $wpdb->get_var( $wpdb->prepare( + "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = '%s' and Table_Name = '%s' GROUP BY Table_Name LIMIT 1", + DB_NAME, + $table_name + ) + ); + + // Add the table size to the list. + $rows[] = array( + 'Name' => $table_name, + 'Size' => size_format( $table_bytes ), + 'Bytes' => $table_bytes, + ); + } + } + $args = array( 'format' => $format, ); - if ( 'bytes' === $args['format'] ) { + if ( 'bytes' === $args['format'] && $db_only ) { + // Display the database size. WP_CLI::Line( $bytes ); } else { // Display the rows. From b9e93a0cc36d4cb4c41f4ed97115dabfa203c1b7 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 18 May 2017 11:02:22 -0500 Subject: [PATCH 04/16] updated feature file for db size --- features/db-size.feature | 35 +++++------------------------------ src/DB_Command.php | 2 +- 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/features/db-size.feature b/features/db-size.feature index b84e600f1..79fd49830 100644 --- a/features/db-size.feature +++ b/features/db-size.feature @@ -8,27 +8,12 @@ Feature: Display database size When I run `wp db size` Then STDOUT should contain: """ - wp_cli_test + wp_cli_test 640 KB 655 """ And STDOUT should contain: """ - 640 KB - """ - - And STDOUT should contain: - """ - 655 - """ - - And STDOUT should contain: - """ - wp_terms - """ - - And STDOUT should contain: - """ - 48 KB + wp_terms 48 KB """ Scenario: Display only database size for a WordPress install @@ -37,20 +22,10 @@ Feature: Display database size When I run `wp db size --db-only` Then STDOUT should contain: """ - wp_cli_test - """ - - And STDOUT should contain: - """ - 640 KB - """ - - And STDOUT should contain: - """ - 655 + wp_cli_test 640 KB 655 """ - And STDOUT should not contain: + But STDOUT should not contain: """ wp_terms """ @@ -64,7 +39,7 @@ Feature: Display database size 655 """ - And STDOUT should not contain: + But STDOUT should not contain: """ 640 KB """ diff --git a/src/DB_Command.php b/src/DB_Command.php index 9af421e40..96f96555a 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -457,7 +457,7 @@ public function tables( $args, $assoc_args ) { } /** - * Display the database size. + * Display the database and table sizes. * * Displays the database and table sizes for `DB_NAME` specified in wp-config.php. * From a7daaec784a09c6cf25c6d7608110723b243435a Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 18 May 2017 11:44:51 -0500 Subject: [PATCH 05/16] updated database bytes only test --- features/db-size.feature | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/features/db-size.feature b/features/db-size.feature index 79fd49830..a4c25554d 100644 --- a/features/db-size.feature +++ b/features/db-size.feature @@ -34,12 +34,4 @@ Feature: Display database size Given a WP install When I run `wp db size --db-only --format=bytes` - Then STDOUT should contain: - """ - 655 - """ - - But STDOUT should not contain: - """ - 640 KB - """ + Then STDOUT should be a number From cf5341b1a7fc3c8a2984cb771e40fe60ab25883c Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 18 May 2017 13:28:49 -0500 Subject: [PATCH 06/16] updated db size tests with SQL query to get size for testing the command output --- features/db-size.feature | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/features/db-size.feature b/features/db-size.feature index a4c25554d..c448432bb 100644 --- a/features/db-size.feature +++ b/features/db-size.feature @@ -5,33 +5,52 @@ Feature: Display database size Scenario: Display database and table sizes for a WordPress install Given a WP install + When I run `wp db query "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` + Then save STDOUT '(\d+)' as {DBSIZE} + When I run `wp db size` Then STDOUT should contain: """ - wp_cli_test 640 KB 655 + wp_cli_test """ And STDOUT should contain: """ - wp_terms 48 KB + KB {DBSIZE} """ Scenario: Display only database size for a WordPress install Given a WP install + When I run `wp db query "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` + Then save STDOUT '(\d+)' as {DBSIZE} + When I run `wp db size --db-only` Then STDOUT should contain: """ - wp_cli_test 640 KB 655 + wp_cli_test + """ + + And STDOUT should contain: + """ + KB {DBSIZE} """ But STDOUT should not contain: """ - wp_terms + wp_users """ Scenario: Display only database size in bytes for a WordPress install Given a WP install + When I run `wp db query "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` + Then save STDOUT '(\d+)' as {DBSIZE} + When I run `wp db size --db-only --format=bytes` Then STDOUT should be a number + + And STDOUT should contain: + """ + {DBSIZE} + """ From bdb23e97ec786db3ec87af44944fb48e245ca679 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Fri, 19 May 2017 15:44:52 -0500 Subject: [PATCH 07/16] suppress load_wordpress() notices --- 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 96f96555a..a648ec9a8 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -500,7 +500,7 @@ public function tables( $args, $assoc_args ) { */ public function size( $args, $assoc_args ) { - WP_CLI::get_runner()-> load_wordpress(); + @WP_CLI::get_runner()-> load_wordpress(); global $wpdb; From 132cbec84970e5e2d499c7febd43be0169d7a621 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Fri, 19 May 2017 15:50:40 -0500 Subject: [PATCH 08/16] switch logic to show database size and added --tables flag --- src/DB_Command.php | 57 ++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index a648ec9a8..baae95f67 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -457,24 +457,23 @@ public function tables( $args, $assoc_args ) { } /** - * Display the database and table sizes. + * Display the database name and size as ASCII table. * - * Displays the database and table sizes for `DB_NAME` specified in wp-config.php. + * Displays the Display the database name and size as ASCII table for `DB_NAME` specified in wp-config.php. * * ## OPTIONS * - * [--db-only] - * : Only display the database size without including the tables. + * [--tables] + * : Display each table name and size as ASCII table instead of the database size. * * [--format] - * : table, csv, json, bytes + * : table, csv, json * --- * default: table * options: * - table * - csv * - json - * - bytes, only works when combined with --db-only * --- * * [--scope=] @@ -505,32 +504,19 @@ public function size( $args, $assoc_args ) { global $wpdb; $format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); - $db_only = ! empty( WP_CLI\Utils\get_flag_value( $assoc_args, 'db-only' ) ); + $tables = ! empty( WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' ) ); unset( $assoc_args['format'] ); - unset( $assoc_args['db-only'] ); + unset( $assoc_args['tables'] ); if ( empty( $args ) && empty( $assoc_args ) ) { $assoc_args['scope'] = 'all'; } - // Get the database size. - $bytes = $wpdb->get_var( $wpdb->prepare( - "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = '%s' GROUP BY table_schema;", - DB_NAME - ) - ); - // Build rows for the formatter. $rows = array(); $fields = array( 'Name', 'Size', 'Bytes' ); - $rows[] = array( - 'Name' => DB_NAME, - 'Size' => size_format( $bytes ), - 'Bytes' => $bytes, - ); - - if ( ! $db_only ) { + if ( $tables ) { // Add all of the table sizes foreach( WP_CLI\Utils\wp_get_table_names( $args, $assoc_args ) as $table_name ) { @@ -550,20 +536,31 @@ public function size( $args, $assoc_args ) { 'Bytes' => $table_bytes, ); } + } else { + + // Get the database size. + $db_bytes = $wpdb->get_var( $wpdb->prepare( + "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = '%s' GROUP BY table_schema;", + DB_NAME + ) + ); + + // Just show the database size. + $rows[] = array( + 'Name' => DB_NAME, + 'Size' => size_format( $db_bytes ), + 'Bytes' => $db_bytes, + ); + } $args = array( 'format' => $format, ); - if ( 'bytes' === $args['format'] && $db_only ) { - // Display the database size. - WP_CLI::Line( $bytes ); - } else { - // Display the rows. - $formatter = new \WP_CLI\Formatter( $args, $fields ); - $formatter->display_items( $rows ); - } + // Display the rows. + $formatter = new \WP_CLI\Formatter( $args, $fields ); + $formatter->display_items( $rows ); } private static function get_create_query() { From 3c94009a23756726869f69391d9c4a0dab590d56 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Fri, 19 May 2017 16:08:45 -0500 Subject: [PATCH 09/16] added --size_format command to display numeric database size --- src/DB_Command.php | 75 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index baae95f67..e16e39fad 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -463,6 +463,16 @@ public function tables( $args, $assoc_args ) { * * ## OPTIONS * + * [--size_format] + * : Displays the database size as a number. + * --- + * default: b + * options: + * - b (bytes) + * - kb (kilobytes) + * - mb (megabytes) + * --- + * * [--tables] * : Display each table name and size as ASCII table instead of the database size. * @@ -496,6 +506,33 @@ public function tables( $args, $assoc_args ) { * +-------------------+------+---------+ * | wordpress_default | 6 MB | 5865472 | * +-------------------+------+---------+ + * + * $ wp db size --tables + * +-----------------------+-------+-------+ + * | Name | Size | Bytes | + * +-----------------------+-------+-------+ + * | wp_users | 64 KB | 65536 | + * | wp_usermeta | 48 KB | 49152 | + * | wp_posts | 80 KB | 81920 | + * | wp_comments | 96 KB | 98304 | + * | wp_links | 32 KB | 32768 | + * | wp_options | 32 KB | 32768 | + * | wp_postmeta | 48 KB | 49152 | + * | wp_terms | 48 KB | 49152 | + * | wp_term_taxonomy | 48 KB | 49152 | + * | wp_term_relationships | 32 KB | 32768 | + * | wp_termmeta | 48 KB | 49152 | + * | wp_commentmeta | 48 KB | 49152 | + * +-----------------------+-------+-------+ + * + * $ wp db size --size_format=b + * 5865472 + * + * $ wp db size --size_format=kb + * 5728 + * + * $ wp db size --size_format=mb + * 6 */ public function size( $args, $assoc_args ) { @@ -505,6 +542,7 @@ public function size( $args, $assoc_args ) { $format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); $tables = ! empty( WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' ) ); + $size_format = WP_CLI\Utils\get_flag_value( $assoc_args, 'size_format' ); unset( $assoc_args['format'] ); unset( $assoc_args['tables'] ); @@ -545,22 +583,43 @@ public function size( $args, $assoc_args ) { ) ); - // Just show the database size. + // Add the database size to the list. $rows[] = array( 'Name' => DB_NAME, 'Size' => size_format( $db_bytes ), 'Bytes' => $db_bytes, ); - } - $args = array( - 'format' => $format, - ); + if ( ! empty( $size_format ) && isset( $db_bytes ) && ! $tables ) { + + // Display the database size as a number. + switch( $size_format ) { + case 'mb': + $divisor = MB_IN_BYTES; + break; + + case 'kb': + $divisor = KB_IN_BYTES; + break; - // Display the rows. - $formatter = new \WP_CLI\Formatter( $args, $fields ); - $formatter->display_items( $rows ); + case 'b': + default: + $divisor = 1; + break; + } + + WP_CLI::Line( ceil( $db_bytes / $divisor ) ); + } else { + + // Display the rows. + $args = array( + 'format' => $format, + ); + + $formatter = new \WP_CLI\Formatter( $args, $fields ); + $formatter->display_items( $rows ); + } } private static function get_create_query() { From 956d1fd8bdf3d17de9de993d142c5d968f88fa8a Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Fri, 19 May 2017 16:42:59 -0500 Subject: [PATCH 10/16] updated db size tests for new features --- features/db-size.feature | 49 +++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/features/db-size.feature b/features/db-size.feature index c448432bb..1bf6ef2f5 100644 --- a/features/db-size.feature +++ b/features/db-size.feature @@ -2,7 +2,7 @@ Feature: Display database size - Scenario: Display database and table sizes for a WordPress install + Scenario: Display only database size for a WordPress install Given a WP install When I run `wp db query "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` @@ -19,38 +19,61 @@ Feature: Display database size KB {DBSIZE} """ - Scenario: Display only database size for a WordPress install + Scenario: Display only table sizes for a WordPress install Given a WP install When I run `wp db query "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` Then save STDOUT '(\d+)' as {DBSIZE} - When I run `wp db size --db-only` + When I run `wp db size --tables` Then STDOUT should contain: """ - wp_cli_test - """ - - And STDOUT should contain: - """ - KB {DBSIZE} + wp_posts 80 KB """ But STDOUT should not contain: """ - wp_users + wp_cli_test """ Scenario: Display only database size in bytes for a WordPress install Given a WP install - When I run `wp db query "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` + When I run `wp db query "SELECT SUM(data_length + index_length) as Size FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` Then save STDOUT '(\d+)' as {DBSIZE} - When I run `wp db size --db-only --format=bytes` + When I run `wp db size --size_format=b` Then STDOUT should be a number - And STDOUT should contain: + And STDOUT should be: + """ + {DBSIZE} + """ + + Scenario: Display only database size in kilobytes for a WordPress install + Given a WP install + + When I run `wp db query "SELECT CEILING( SUM(data_length + index_length) / 1024 ) as Size FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` + Then save STDOUT '(\d+)' as {DBSIZE} + + When I run `wp db size --size_format=kb` + Then STDOUT should be a number + + And STDOUT should be: + """ + {DBSIZE} + """ + + Scenario: Display only database size in megabytes for a WordPress install + Given a WP install + + When I run `wp db query "SELECT CEILING( SUM(data_length + index_length) / 1048576 ) as Size FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` + Then save STDOUT '(\d+)' as {DBSIZE} + + When I run `wp db size --size_format=mb` + Then STDOUT should be a number + + And STDOUT should be: """ {DBSIZE} """ From 097f62042581a0b3f4ff4080281a1b7d13538065 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Fri, 19 May 2017 16:47:43 -0500 Subject: [PATCH 11/16] changed some empty() logic for PHP 5.3 --- src/DB_Command.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index e16e39fad..47f5e69a6 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -541,9 +541,14 @@ public function size( $args, $assoc_args ) { global $wpdb; $format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); - $tables = ! empty( WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' ) ); $size_format = WP_CLI\Utils\get_flag_value( $assoc_args, 'size_format' ); + + // For PHP 5.3 compat. + $tables = WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' ); + $tables = ! empty( $empty ); + unset( $assoc_args['format'] ); + unset( $assoc_args['size_format'] ); unset( $assoc_args['tables'] ); if ( empty( $args ) && empty( $assoc_args ) ) { From 73de38c64f18b5e1461cd79feca4731573c33bb1 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Fri, 19 May 2017 16:48:39 -0500 Subject: [PATCH 12/16] changed some empty() logic for PHP 5.3 --- 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 47f5e69a6..4004a60c1 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -545,7 +545,7 @@ public function size( $args, $assoc_args ) { // For PHP 5.3 compat. $tables = WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' ); - $tables = ! empty( $empty ); + $tables = ! empty( $tables ); unset( $assoc_args['format'] ); unset( $assoc_args['size_format'] ); From b66a38175097f0d1252cb9577ec236af5e433fa7 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Mon, 22 May 2017 09:38:30 -0500 Subject: [PATCH 13/16] removed Bytes column, updated docblock --- src/DB_Command.php | 50 ++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 4004a60c1..e58820a56 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -457,9 +457,9 @@ public function tables( $args, $assoc_args ) { } /** - * Display the database name and size as ASCII table. + * Display the database name and size. * - * Displays the Display the database name and size as ASCII table for `DB_NAME` specified in wp-config.php. + * Display the database name and size for `DB_NAME` specified in wp-config.php. * * ## OPTIONS * @@ -501,29 +501,29 @@ public function tables( $args, $assoc_args ) { * ## EXAMPLES * * $ wp db size - * +-------------------+------+---------+ - * | Name | Size | Bytes | - * +-------------------+------+---------+ - * | wordpress_default | 6 MB | 5865472 | - * +-------------------+------+---------+ + * +-------------------+------+ + * | Name | Size | + * +-------------------+------+ + * | wordpress_default | 6 MB | + * +-------------------+------+ * * $ wp db size --tables - * +-----------------------+-------+-------+ - * | Name | Size | Bytes | - * +-----------------------+-------+-------+ - * | wp_users | 64 KB | 65536 | - * | wp_usermeta | 48 KB | 49152 | - * | wp_posts | 80 KB | 81920 | - * | wp_comments | 96 KB | 98304 | - * | wp_links | 32 KB | 32768 | - * | wp_options | 32 KB | 32768 | - * | wp_postmeta | 48 KB | 49152 | - * | wp_terms | 48 KB | 49152 | - * | wp_term_taxonomy | 48 KB | 49152 | - * | wp_term_relationships | 32 KB | 32768 | - * | wp_termmeta | 48 KB | 49152 | - * | wp_commentmeta | 48 KB | 49152 | - * +-----------------------+-------+-------+ + * +-----------------------+-------+ + * | Name | Size | + * +-----------------------+-------+ + * | wp_users | 64 KB | + * | wp_usermeta | 48 KB | + * | wp_posts | 80 KB | + * | wp_comments | 96 KB | + * | wp_links | 32 KB | + * | wp_options | 32 KB | + * | wp_postmeta | 48 KB | + * | wp_terms | 48 KB | + * | wp_term_taxonomy | 48 KB | + * | wp_term_relationships | 32 KB | + * | wp_termmeta | 48 KB | + * | wp_commentmeta | 48 KB | + * +-----------------------+-------+ * * $ wp db size --size_format=b * 5865472 @@ -557,7 +557,7 @@ public function size( $args, $assoc_args ) { // Build rows for the formatter. $rows = array(); - $fields = array( 'Name', 'Size', 'Bytes' ); + $fields = array( 'Name', 'Size' ); if ( $tables ) { @@ -576,7 +576,6 @@ public function size( $args, $assoc_args ) { $rows[] = array( 'Name' => $table_name, 'Size' => size_format( $table_bytes ), - 'Bytes' => $table_bytes, ); } } else { @@ -592,7 +591,6 @@ public function size( $args, $assoc_args ) { $rows[] = array( 'Name' => DB_NAME, 'Size' => size_format( $db_bytes ), - 'Bytes' => $db_bytes, ); } From 18457baba05ecfc40a77fee436269e330b171863 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Mon, 22 May 2017 09:59:56 -0500 Subject: [PATCH 14/16] additional db size docblock updates --- src/DB_Command.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index e58820a56..2585d4699 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -459,12 +459,13 @@ public function tables( $args, $assoc_args ) { /** * Display the database name and size. * - * Display the database name and size for `DB_NAME` specified in wp-config.php. + * Display the database name and size for `DB_NAME` specified in wp-config.php, + * defaults to a human-readable number. * * ## OPTIONS * * [--size_format] - * : Displays the database size as a number. + * : Display only the database size as a number. * --- * default: b * options: @@ -474,7 +475,7 @@ public function tables( $args, $assoc_args ) { * --- * * [--tables] - * : Display each table name and size as ASCII table instead of the database size. + * : Display each table name and size instead of the database size. * * [--format] * : table, csv, json @@ -542,8 +543,6 @@ 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' ); - - // For PHP 5.3 compat. $tables = WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' ); $tables = ! empty( $tables ); From 96665a3b382182a14afff755d48f362a8721ad05 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Mon, 22 May 2017 10:03:49 -0500 Subject: [PATCH 15/16] updated test for default size command --- features/db-size.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/db-size.feature b/features/db-size.feature index 1bf6ef2f5..112c839bf 100644 --- a/features/db-size.feature +++ b/features/db-size.feature @@ -5,7 +5,7 @@ Feature: Display database size Scenario: Display only database size for a WordPress install Given a WP install - When I run `wp db query "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` + When I run `wp db query "SELECT CEILING( SUM(data_length + index_length) / 1024 ) as Size FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` Then save STDOUT '(\d+)' as {DBSIZE} When I run `wp db size` @@ -16,7 +16,7 @@ Feature: Display database size And STDOUT should contain: """ - KB {DBSIZE} + {DBSIZE} KB """ Scenario: Display only table sizes for a WordPress install From c7d11292ac2c4af88eb3b2f59bbe34078630290a Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 25 May 2017 14:00:15 -0500 Subject: [PATCH 16/16] updated db size tests due to fluctuating database sizes --- features/db-size.feature | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/features/db-size.feature b/features/db-size.feature index 112c839bf..924eeafea 100644 --- a/features/db-size.feature +++ b/features/db-size.feature @@ -5,9 +5,6 @@ Feature: Display database size Scenario: Display only database size for a WordPress install Given a WP install - When I run `wp db query "SELECT CEILING( SUM(data_length + index_length) / 1024 ) as Size FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` - Then save STDOUT '(\d+)' as {DBSIZE} - When I run `wp db size` Then STDOUT should contain: """ @@ -16,15 +13,12 @@ Feature: Display database size And STDOUT should contain: """ - {DBSIZE} KB + KB """ Scenario: Display only table sizes for a WordPress install Given a WP install - When I run `wp db query "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` - Then save STDOUT '(\d+)' as {DBSIZE} - When I run `wp db size --tables` Then STDOUT should contain: """ @@ -39,41 +33,19 @@ Feature: Display database size Scenario: Display only database size in bytes for a WordPress install Given a WP install - When I run `wp db query "SELECT SUM(data_length + index_length) as Size FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` - Then save STDOUT '(\d+)' as {DBSIZE} - When I run `wp db size --size_format=b` Then STDOUT should be a number - And STDOUT should be: - """ - {DBSIZE} - """ Scenario: Display only database size in kilobytes for a WordPress install Given a WP install - When I run `wp db query "SELECT CEILING( SUM(data_length + index_length) / 1024 ) as Size FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` - Then save STDOUT '(\d+)' as {DBSIZE} - When I run `wp db size --size_format=kb` Then STDOUT should be a number - And STDOUT should be: - """ - {DBSIZE} - """ Scenario: Display only database size in megabytes for a WordPress install Given a WP install - When I run `wp db query "SELECT CEILING( SUM(data_length + index_length) / 1048576 ) as Size FROM information_schema.TABLES where table_schema = 'wp_cli_test' GROUP BY table_schema;"` - Then save STDOUT '(\d+)' as {DBSIZE} - When I run `wp db size --size_format=mb` Then STDOUT should be a number - - And STDOUT should be: - """ - {DBSIZE} - """