diff --git a/composer.json b/composer.json index 30122965..6f613a8d 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,8 @@ "db search", "db tables", "db size", - "db columns" + "db columns", + "db status" ] }, "autoload": { diff --git a/features/db-status.feature b/features/db-status.feature new file mode 100644 index 00000000..110c1e69 --- /dev/null +++ b/features/db-status.feature @@ -0,0 +1,115 @@ +Feature: Display database status overview + + @require-mysql-or-mariadb + Scenario: Display database status for a WordPress install + Given a WP install + + When I run `wp db status` + Then STDOUT should contain: + """ + Database Name: {DB_NAME} + """ + And STDOUT should match /^Tables:\s+\d+$/m + And STDOUT should match /^Total Size:\s+[\d.]+ [KM]?B$/m + And STDOUT should contain: + """ + Prefix: wp_ + """ + And STDOUT should match /^Engine:\s+\w+$/m + And STDOUT should match /^Charset:\s+\w+$/m + And STDOUT should match /^Collation:\s+\w+$/m + And STDOUT should contain: + """ + Check Status: OK + """ + + When I run `wp db status --skip-check --format=json` + Then STDOUT should be JSON containing: + """ + {"database":"{DB_NAME}","prefix":"wp_","check":"Skipped"} + """ + And STDOUT should match /"tables":\d+,"size":\d+,/ + + @require-mysql-or-mariadb + Scenario: Display database status before WordPress is installed + Given an empty directory + And WP files + And wp-config.php + And a database + + When I run `wp db status` + Then STDOUT should contain: + """ + Database Name: {DB_NAME} + Tables: 0 + Total Size: 0 B + Prefix: wp_ + Engine: N/A + Charset: N/A + Collation: N/A + Check Status: N/A + """ + + @require-mysql-or-mariadb + Scenario: Report mixed table engines + Given a WP install + + When I run `wp db query "CREATE TABLE wp_status_test (id INT) ENGINE=MyISAM"` + And I run `wp db status --skip-check` + Then STDOUT should contain: + """ + Engine: Mixed + """ + + @require-mysql-or-mariadb + Scenario: Run db status with passed-in options + Given a WP install + + When I run `wp db status --defaults` + Then STDOUT should contain: + """ + Check Status: OK + """ + + When I run `wp db status --no-defaults` + Then STDOUT should contain: + """ + Check Status: OK + """ + + When I run `wp db status --dbuser={DB_USER} --dbpass=password1` + Then STDOUT should contain: + """ + Check Status: OK + """ + + When I try `wp db status --dbuser=no_such_user` + Then the return code should be 1 + And STDERR should contain: + """ + Error: Could not query the database: + """ + + When I try `wp db status --dbpass=no_such_pass` + Then the return code should be 1 + And STDERR should contain: + """ + Error: Could not query the database: + """ + + @require-sqlite + Scenario: Display database status for a SQLite install + Given a WP install + + When I run `wp db status` + Then STDOUT should match /^Database Name:\s+\S+$/m + And STDOUT should match /^Tables:\s+[1-9]\d*$/m + And STDOUT should match /^Total Size:\s+[\d.]+ [KM]?B$/m + And STDOUT should contain: + """ + Prefix: wp_ + Engine: SQLite + Charset: N/A + Collation: N/A + Check Status: N/A + """ diff --git a/src/DB_Command.php b/src/DB_Command.php index a82392dd..0a49d2e0 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1452,6 +1452,191 @@ public function prefix() { WP_CLI::log( $wpdb->prefix ); } + /** + * Displays an overview of the database. + * + * Shows the database name, the number of tables and their total size, the + * table prefix, the storage engine, character set and collation of the + * tables, and whether `mysqlcheck` reports any problems. Engine, charset + * and collation read "Mixed" when the tables don't all share the same one. + * + * Unlike `wp db size` or `wp db prefix`, this doesn't load WordPress, so it + * also works before WordPress is installed. + * + * For SQLite databases, the engine is reported as SQLite, and charset, + * collation and check status are not available. + * + * ## OPTIONS + * + * [--dbuser=] + * : Username to pass to mysql and mysqlcheck. Defaults to DB_USER. + * + * [--dbpass=] + * : Password to pass to mysql and mysqlcheck. Defaults to DB_PASSWORD. + * + * [--skip-check] + * : Don't run `mysqlcheck`, which reads every table and can take a while on large databases. + * + * [--format=] + * : Render output in a particular format. The JSON output lists the total size in bytes. + * --- + * default: list + * options: + * - list + * - json + * --- + * + * [--defaults] + * : Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration. + * + * ## EXAMPLES + * + * $ wp db status + * Database Name: wordpress_dbase + * Tables: 12 + * Total Size: 868.35 KB + * Prefix: wp_ + * Engine: InnoDB + * Charset: utf8mb4 + * Collation: utf8mb4_unicode_520_ci + * Check Status: OK + * + * $ wp db status --skip-check --format=json + * {"database":"wordpress_dbase","tables":12,"size":868352,"prefix":"wp_","engine":"InnoDB","charset":"utf8mb4","collation":"utf8mb4_unicode_520_ci","check":"Skipped"} + */ + public function status( $_, $assoc_args ) { + global $table_prefix; + + $format = Utils\get_flag_value( $assoc_args, 'format', 'list' ); + $skip_check = Utils\get_flag_value( $assoc_args, 'skip-check', false ); + + $status = [ + 'database' => DB_NAME, + 'tables' => 0, + 'size' => 0, + 'prefix' => isset( $table_prefix ) && is_string( $table_prefix ) ? $table_prefix : '', + 'engine' => 'N/A', + 'charset' => 'N/A', + 'collation' => 'N/A', + 'check' => $skip_check ? 'Skipped' : 'N/A', + ]; + + $this->maybe_load_sqlite_dropin(); + + if ( $this->is_sqlite() ) { + global $wpdb; + + $db_path = $this->get_sqlite_db_path(); + $status['database'] = $db_path ? basename( $db_path ) : ''; + $status['size'] = $this->sqlite_size(); + $status['engine'] = 'SQLite'; + + if ( isset( $wpdb ) && $wpdb instanceof \WP_SQLite_DB ) { + $status['tables'] = count( $wpdb->get_col( 'SHOW TABLES' ) ); + } + } else { + $defaults_flag = $this->get_defaults_flag_string( $assoc_args ); + $mysql_args = self::get_dbuser_dbpass_args( $assoc_args ); + + $query = 'SELECT COUNT(*), COALESCE(SUM(DATA_LENGTH + INDEX_LENGTH), 0), ' + . 'COUNT(DISTINCT ENGINE), MIN(ENGINE), ' + . "COUNT(DISTINCT SUBSTRING_INDEX(TABLE_COLLATION, '_', 1)), MIN(SUBSTRING_INDEX(TABLE_COLLATION, '_', 1)), " + . 'COUNT(DISTINCT TABLE_COLLATION), MIN(TABLE_COLLATION) ' + . "FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_TYPE = 'BASE TABLE'"; + + list( $stdout, $stderr, $exit_code ) = self::run( + sprintf( + '%s%s --no-auto-rehash --batch --skip-column-names', + Utils\get_mysql_binary_path(), + $defaults_flag + ), + array_merge( + $mysql_args, + [ + 'database' => DB_NAME, + 'execute' => $query, + ] + ), + false + ); + + if ( $exit_code ) { + WP_CLI::error( 'Could not query the database: ' . trim( $stderr ) ); + } + + $row = array_pad( explode( "\t", trim( $stdout ) ), 8, 'NULL' ); + + $status['tables'] = (int) $row[0]; + $status['size'] = (int) $row[1]; + + $columns = [ + 'engine' => 2, + 'charset' => 4, + 'collation' => 6, + ]; + foreach ( $columns as $key => $index ) { + if ( (int) $row[ $index ] > 1 ) { + $status[ $key ] = 'Mixed'; + } elseif ( '' !== $row[ $index + 1 ] && 'NULL' !== $row[ $index + 1 ] ) { + $status[ $key ] = $row[ $index + 1 ]; + } + } + + $check_command = Utils\get_sql_check_command(); + if ( ! $skip_check && '' !== $check_command && $status['tables'] > 0 ) { + list( $stdout, , $exit_code ) = self::run( + Utils\esc_cmd( sprintf( '%s%s %s', $check_command, $defaults_flag, '%s' ), DB_NAME ), + array_merge( + $mysql_args, + [ + 'check' => true, + 'silent' => true, + // A read-only status check must not repair tables, even if an option file enables auto-repair. + 'skip-auto-repair' => true, + ] + ), + false + ); + + $status['check'] = ( 0 === $exit_code && ! preg_match( '/^error\s*:/mi', $stdout ) ) ? 'OK' : 'Error'; + } + } + + if ( 'json' === $format ) { + WP_CLI::line( (string) json_encode( $status ) ); + return; + } + + $status['size'] = self::format_size_si( $status['size'] ); + + $labels = [ + 'database' => 'Database Name', + 'tables' => 'Tables', + 'size' => 'Total Size', + 'prefix' => 'Prefix', + 'engine' => 'Engine', + 'charset' => 'Charset', + 'collation' => 'Collation', + 'check' => 'Check Status', + ]; + foreach ( $labels as $key => $label ) { + WP_CLI::line( sprintf( '%-18s %s', $label . ':', $status[ $key ] ) ); + } + } + + /** + * Formats a number of bytes with SI units, like `wp db size --human-readable`. + * + * @param int $bytes Number of bytes. + * @return string + */ + private static function format_size_si( $bytes ) { + $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; + $key = $bytes > 0 ? (int) min( floor( log( $bytes, 1000 ) ), count( $sizes ) - 1 ) : 0; + + return round( $bytes / pow( 1000, $key ), 2 ) . ' ' . $sizes[ $key ]; + } + /** * Finds a string in the database. *