Skip to content

Commit c9b7a5e

Browse files
authored
Merge pull request #100 from wp-cli/show-columns
Show columns
2 parents ce8c51a + 58857b9 commit c9b7a5e

File tree

5 files changed

+176
-2
lines changed

5 files changed

+176
-2
lines changed

.github/settings.yml

+2
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ labels:
5050
color: c5def5
5151
- name: command:db-size
5252
color: c5def5
53+
- name: command:db-columns
54+
color: c5def5

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,63 @@ The size defaults to a human-readable number.
723723
$ wp db size --size_format=mb
724724
6
725725

726+
727+
728+
### wp db columns
729+
730+
Displays information about a given table.
731+
732+
~~~
733+
wp db columns [<table>] [--format]
734+
~~~
735+
736+
**OPTIONS**
737+
738+
[<table>]
739+
Name of the database table.
740+
741+
[--format]
742+
Render output in a particular format.
743+
---
744+
default: table
745+
options:
746+
- table
747+
- csv
748+
- json
749+
- yaml
750+
---
751+
752+
**EXAMPLES**
753+
754+
$ wp db columns wp_posts
755+
+-----------------------+---------------------+------+-----+---------------------+----------------+
756+
| Field | Type | Null | Key | Default | Extra |
757+
+-----------------------+---------------------+------+-----+---------------------+----------------+
758+
| ID | bigint(20) unsigned | NO | PRI | | auto_increment |
759+
| post_author | bigint(20) unsigned | NO | MUL | 0 | |
760+
| post_date | datetime | NO | | 0000-00-00 00:00:00 | |
761+
| post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
762+
| post_content | longtext | NO | | | |
763+
| post_title | text | NO | | | |
764+
| post_excerpt | text | NO | | | |
765+
| post_status | varchar(20) | NO | | publish | |
766+
| comment_status | varchar(20) | NO | | open | |
767+
| ping_status | varchar(20) | NO | | open | |
768+
| post_password | varchar(255) | NO | | | |
769+
| post_name | varchar(200) | NO | MUL | | |
770+
| to_ping | text | NO | | | |
771+
| pinged | text | NO | | | |
772+
| post_modified | datetime | NO | | 0000-00-00 00:00:00 | |
773+
| post_modified_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
774+
| post_content_filtered | longtext | NO | | | |
775+
| post_parent | bigint(20) unsigned | NO | MUL | 0 | |
776+
| guid | varchar(255) | NO | | | |
777+
| menu_order | int(11) | NO | | 0 | |
778+
| post_type | varchar(20) | NO | MUL | post | |
779+
| post_mime_type | varchar(100) | NO | | | |
780+
| comment_count | bigint(20) | NO | | 0 | |
781+
+-----------------------+---------------------+------+-----+---------------------+----------------+
782+
726783
## Installing
727784

728785
This package is included with WP-CLI itself, no additional installation necessary.

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"db import",
4848
"db search",
4949
"db tables",
50-
"db size"
50+
"db size",
51+
"db columns"
5152
]
5253
}
5354
}

features/db-columns.feature

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Feature: Display information about a given table.
2+
3+
@require-wp-4.2
4+
Scenario: Display information about the wp_posts table
5+
Given a WP install
6+
7+
When I run `wp db columns wp_posts --format=table`
8+
Then STDOUT should be a table containing rows:
9+
| Field | Type | Null | Key | Default | Extra |
10+
| ID | bigint(20) unsigned | NO | PRI | | auto_increment |
11+
| post_author | bigint(20) unsigned | NO | MUL | 0 | |
12+
| post_date | datetime | NO | | 0000-00-00 00:00:00 | |
13+
| post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
14+
| post_content | longtext | NO | | | |
15+
| post_title | text | NO | | | |
16+
| post_excerpt | text | NO | | | |
17+
| post_status | varchar(20) | NO | | publish | |
18+
| comment_status | varchar(20) | NO | | open | |
19+
| ping_status | varchar(20) | NO | | open | |
20+
| post_password | varchar(255) | NO | | | |
21+
| post_name | varchar(200) | NO | MUL | | |
22+
| to_ping | text | NO | | | |
23+
| pinged | text | NO | | | |
24+
| post_modified | datetime | NO | | 0000-00-00 00:00:00 | |
25+
| post_modified_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
26+
| post_content_filtered | longtext | NO | | | |
27+
| post_parent | bigint(20) unsigned | NO | MUL | 0 | |
28+
| guid | varchar(255) | NO | | | |
29+
| menu_order | int(11) | NO | | 0 | |
30+
| post_type | varchar(20) | NO | MUL | post | |
31+
| post_mime_type | varchar(100) | NO | | | |
32+
| comment_count | bigint(20) | NO | | 0 | |
33+
34+
Scenario: Display information about non-existing table
35+
Given a WP install
36+
37+
When I try `wp db columns wp_foobar`
38+
Then STDERR should contain:
39+
"""
40+
Couldn't find any tables matching: wp_foobar
41+
"""

src/DB_Command.php

+74-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use WP_CLI\Formatter;
34
use \WP_CLI\Utils;
45

56
/**
@@ -833,7 +834,7 @@ public function size( $args, $assoc_args ) {
833834
'format' => $format,
834835
);
835836

836-
$formatter = new \WP_CLI\Formatter( $args, $fields );
837+
$formatter = new Formatter( $args, $fields );
837838
$formatter->display_items( $rows );
838839
}
839840
}
@@ -1160,6 +1161,78 @@ public function search( $args, $assoc_args ) {
11601161
}
11611162
}
11621163

1164+
/**
1165+
* Displays information about a given table.
1166+
*
1167+
* ## OPTIONS
1168+
*
1169+
* [<table>]
1170+
* : Name of the database table.
1171+
*
1172+
* [--format]
1173+
* : Render output in a particular format.
1174+
* ---
1175+
* default: table
1176+
* options:
1177+
* - table
1178+
* - csv
1179+
* - json
1180+
* - yaml
1181+
* ---
1182+
*
1183+
* ## EXAMPLES
1184+
*
1185+
* $ wp db columns wp_posts
1186+
* +-----------------------+---------------------+------+-----+---------------------+----------------+
1187+
* | Field | Type | Null | Key | Default | Extra |
1188+
* +-----------------------+---------------------+------+-----+---------------------+----------------+
1189+
* | ID | bigint(20) unsigned | NO | PRI | | auto_increment |
1190+
* | post_author | bigint(20) unsigned | NO | MUL | 0 | |
1191+
* | post_date | datetime | NO | | 0000-00-00 00:00:00 | |
1192+
* | post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
1193+
* | post_content | longtext | NO | | | |
1194+
* | post_title | text | NO | | | |
1195+
* | post_excerpt | text | NO | | | |
1196+
* | post_status | varchar(20) | NO | | publish | |
1197+
* | comment_status | varchar(20) | NO | | open | |
1198+
* | ping_status | varchar(20) | NO | | open | |
1199+
* | post_password | varchar(255) | NO | | | |
1200+
* | post_name | varchar(200) | NO | MUL | | |
1201+
* | to_ping | text | NO | | | |
1202+
* | pinged | text | NO | | | |
1203+
* | post_modified | datetime | NO | | 0000-00-00 00:00:00 | |
1204+
* | post_modified_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
1205+
* | post_content_filtered | longtext | NO | | | |
1206+
* | post_parent | bigint(20) unsigned | NO | MUL | 0 | |
1207+
* | guid | varchar(255) | NO | | | |
1208+
* | menu_order | int(11) | NO | | 0 | |
1209+
* | post_type | varchar(20) | NO | MUL | post | |
1210+
* | post_mime_type | varchar(100) | NO | | | |
1211+
* | comment_count | bigint(20) | NO | | 0 | |
1212+
* +-----------------------+---------------------+------+-----+---------------------+----------------+
1213+
*
1214+
* @when after_wp_load
1215+
*/
1216+
public function columns( $args, $assoc_args ) {
1217+
global $wpdb;
1218+
1219+
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
1220+
1221+
WP_CLI\Utils\wp_get_table_names( array( $args[0] ), array() );
1222+
1223+
$columns = $wpdb->get_results(
1224+
'SHOW COLUMNS FROM ' . $args[0]
1225+
);
1226+
1227+
$formatter_fields = array( 'Field', 'Type', 'Null', 'Key', 'Default', 'Extra' );
1228+
$formatter_args = array(
1229+
'format' => $format,
1230+
);
1231+
1232+
$formatter = new Formatter( $formatter_args, $formatter_fields );
1233+
$formatter->display_items( $columns );
1234+
}
1235+
11631236
private static function get_create_query() {
11641237

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

0 commit comments

Comments
 (0)