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
2 changes: 2 additions & 0 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ labels:
color: c5def5
- name: command:db-size
color: c5def5
- name: command:db-columns
color: c5def5
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,63 @@ The size defaults to a human-readable number.
$ wp db size --size_format=mb
6



### wp db columns

Displays information about a given table.

~~~
wp db columns [<table>] [--format]
~~~

**OPTIONS**

[<table>]
Name of the database table.

[--format]
Render output in a particular format.
---
default: table
options:
- table
- csv
- json
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should include yaml

- yaml
---

**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 | |
+-----------------------+---------------------+------+-----+---------------------+----------------+

## Installing

This package is included with WP-CLI itself, no additional installation necessary.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"db import",
"db search",
"db tables",
"db size"
"db size",
"db columns"
]
}
}
41 changes: 41 additions & 0 deletions features/db-columns.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Feature: Display information about a given table.

@require-wp-4.2
Scenario: Display information about the wp_posts table
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
"""
81 changes: 77 additions & 4 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use WP_CLI\Formatter;
use \WP_CLI\Utils;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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 );
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1102,6 +1103,78 @@ public function search( $args, $assoc_args ) {
}
}

/**
* Displays information about a given table.
*
* ## OPTIONS
*
* [<table>]
* : Name of the database table.
*
* [--format]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## 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
*/
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( array( $args[0] ), array() );

$columns = $wpdb->get_results(
'SHOW COLUMNS FROM ' . $args[0]
);

$formatter_fields = array( 'Field', 'Type', 'Null', 'Key', 'Default', 'Extra' );
$formatter_args = array(
'format' => $format,
);

$formatter = new Formatter( $formatter_args, $formatter_fields );
$formatter->display_items( $columns );
}

private static function get_create_query() {

$create_query = sprintf( 'CREATE DATABASE %s', self::esc_sql_ident( DB_NAME ) );
Expand Down