Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add `db columns` command #100

Merged
merged 10 commits into from May 15, 2018
Copy path View file
@@ -723,6 +723,40 @@ The size defaults to a human-readable number.
$ wp db size --size_format=mb
6
### wp db columns
Display information about a given table.

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

Display => Displays

~~~
wp db columns [<table>] [--format]
~~~
**OPTIONS**
[<table>]
Table name

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

Table name => Name of the database table.

[--format]

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

Should be: [--format=<format>]

table, csv, json

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

Add description:

Render output in a particular format.
---
default: table
options:
- table
- csv
- json

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

Should include yaml

---
**EXAMPLES**
$ wp db columns
+-------------------+------+
| Name | Size |
+-------------------+------+
| wordpress_default | 6 MB |
+-------------------+------+
## Installing
This package is included with WP-CLI itself, no additional installation necessary.
Copy path View file
@@ -47,7 +47,8 @@
"db import",
"db search",
"db tables",
"db size"
"db size",
"db columns"
]
}
}
Copy path View file
@@ -0,0 +1,40 @@
Feature: Display information about a given table.

Scenario: Display information about the wp_posts table

This comment has been minimized.

Copy link
@swissspidy

swissspidy May 2, 2018

Author Member

Schema might not be the same for older WordPress versions. A minimum version should be added.

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

I think the last change to the database schema was for WP 4.2, so adding the following should work: @require-wp-4.2

Given a WP install

When I run `wp db columns wp_posts --format=table`
Then STDOUT should be a table containing rows:
| Field | Type | Null | Key | Default | Extra |
| ID | bigint(20) unsigned | NO | PRI | | auto_increment |
| post_author | bigint(20) unsigned | NO | MUL | 0 | |
| post_date | datetime | NO | | 0000-00-00 00:00:00 | |
| post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
| post_content | longtext | NO | | | |
| post_title | text | NO | | | |
| post_excerpt | text | NO | | | |
| post_status | varchar(20) | NO | | publish | |
| comment_status | varchar(20) | NO | | open | |
| ping_status | varchar(20) | NO | | open | |
| post_password | varchar(255) | NO | | | |
| post_name | varchar(200) | NO | MUL | | |
| to_ping | text | NO | | | |
| pinged | text | NO | | | |
| post_modified | datetime | NO | | 0000-00-00 00:00:00 | |
| post_modified_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
| post_content_filtered | longtext | NO | | | |
| post_parent | bigint(20) unsigned | NO | MUL | 0 | |
| guid | varchar(255) | NO | | | |
| menu_order | int(11) | NO | | 0 | |
| post_type | varchar(20) | NO | MUL | post | |
| post_mime_type | varchar(100) | NO | | | |
| comment_count | bigint(20) | NO | | 0 | |

Scenario: Display information about non-existing table
Given a WP install

When I try `wp db columns wp_foobar`
Then STDERR should contain:
"""
Couldn't find any tables matching: wp_foobar
"""
Copy path View file
@@ -1,5 +1,6 @@
<?php
use WP_CLI\Formatter;
use \WP_CLI\Utils;
/**
@@ -744,11 +745,11 @@ public function size( $args, $assoc_args ) {
case 'tb':
$divisor = TB_IN_BYTES;
break;
case 'gb':
$divisor = GB_IN_BYTES;
break;
case 'mb':
$divisor = MB_IN_BYTES;
break;
@@ -775,7 +776,7 @@ public function size( $args, $assoc_args ) {
'format' => $format,
);
$formatter = new \WP_CLI\Formatter( $args, $fields );
$formatter = new Formatter( $args, $fields );
$formatter->display_items( $rows );
}
}
@@ -865,7 +866,7 @@ public function prefix() {
* : Percent color code to use for the match (unless both before and after context are 0, when no color code is used). For a list of available percent color codes, see below. Default '%3%k' (black on a mustard background).
*
* The percent color codes available are:
*
*
* | Code | Color
* | ---- | -----
* | %y | Yellow (dark) (mustard)
@@ -1102,6 +1103,79 @@ public function search( $args, $assoc_args ) {
}
}
/**
* Display information about a given table.
*
* ## OPTIONS
*
* [<table>]
* : Table name
*
* [--format]
* : table, csv, json
* ---
* default: table
* options:
* - table
* - csv
* - json
* ---
*
* ## EXAMPLES
*
* $ wp db columns wp_posts
* +-----------------------+---------------------+------+-----+---------------------+----------------+
* | Field | Type | Null | Key | Default | Extra |
* +-----------------------+---------------------+------+-----+---------------------+----------------+
* | ID | bigint(20) unsigned | NO | PRI | | auto_increment |
* | post_author | bigint(20) unsigned | NO | MUL | 0 | |
* | post_date | datetime | NO | | 0000-00-00 00:00:00 | |
* | post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
* | post_content | longtext | NO | | | |
* | post_title | text | NO | | | |
* | post_excerpt | text | NO | | | |
* | post_status | varchar(20) | NO | | publish | |
* | comment_status | varchar(20) | NO | | open | |
* | ping_status | varchar(20) | NO | | open | |
* | post_password | varchar(255) | NO | | | |
* | post_name | varchar(200) | NO | MUL | | |
* | to_ping | text | NO | | | |
* | pinged | text | NO | | | |
* | post_modified | datetime | NO | | 0000-00-00 00:00:00 | |
* | post_modified_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
* | post_content_filtered | longtext | NO | | | |
* | post_parent | bigint(20) unsigned | NO | MUL | 0 | |
* | guid | varchar(255) | NO | | | |
* | menu_order | int(11) | NO | | 0 | |
* | post_type | varchar(20) | NO | MUL | post | |
* | post_mime_type | varchar(100) | NO | | | |
* | comment_count | bigint(20) | NO | | 0 | |
* +-----------------------+---------------------+------+-----+---------------------+----------------+
*
* @when after_wp_load
* @alias fields

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

We should skip the alias and only use multiple names for BC reasons.

*/
public function columns( $args, $assoc_args ) {
global $wpdb;
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
WP_CLI\Utils\wp_get_table_names( [ $args[0] ], [] );

This comment has been minimized.

Copy link
@swissspidy

swissspidy May 2, 2018

Author Member

Needs to use array() syntax.

// Get the database size.
$columns = $wpdb->get_results(
'SHOW COLUMNS FROM ' . $args[0]
);
// Display the rows.
$formatter_args = array(
'format' => $format,
);
$formatter = new Formatter($formatter_args, array( 'Field', 'Type', 'Null', 'Key', 'Default', 'Extra' ) );

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

CS - Space missing before $formatter_args.

This comment has been minimized.

Copy link
@schlessera

schlessera May 8, 2018

Member

Given $formatter_args is built in a separate variable, the header fields should get the same treatment, especially as they produce a very long line here.

$formatter->display_items( $columns );
}
private static function get_create_query() {
$create_query = sprintf( 'CREATE DATABASE %s', self::esc_sql_ident( DB_NAME ) );
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.