Skip to content

Commit

Permalink
Rename and reorder the columns
Browse files Browse the repository at this point in the history
  • Loading branch information
2ndkauboy committed Apr 26, 2024
1 parent 1c9b2e8 commit 531b897
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions features/db-search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,8 @@ Feature: Search through the database
When I run `wp db search example.com --format=csv`
Then STDOUT should contain:
"""
wp_options,option_value,option_id,14,mail.example.com
wp_options,option_value,option_id,15,login@example.com
wp_options,option_value,mail.example.com,option_id,14
wp_options,option_value,login@example.com,option_id,15
"""

When I try `wp db search example.com --format=ids`
Expand Down
18 changes: 11 additions & 7 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1484,12 +1484,16 @@ public function search( $args, $assoc_args ) {

if ( $format ) {
$search_results[] = [
'table' => $table,
'column' => $column,
'key' => $primary_key,
'value' => $result->$primary_key,
'table' => $table,
'column' => $column,
// Remove the colors for the format output.
'match' => str_replace( [ $colors['match'][0], $colors['match'][1] ], [ '', '' ], $col_val ),
'match' => str_replace(
[ $colors['match'][0], $colors['match'][1] ],
[ '','' ],
$col_val
),
'primary_key_name' => $primary_key,
'primary_key_value' => $result->$primary_key,
];
} else {
WP_CLI::log( $matches_only ? $col_val : ( $one_line ? "{$table_column_val}:{$pk_val}{$col_val}" : "{$pk_val}{$col_val}" ) );
Expand All @@ -1504,7 +1508,7 @@ public function search( $args, $assoc_args ) {
$formatter_args = [
'format' => $format,
];
$formatter_fields = [ 'table', 'column', 'key', 'value', 'match' ];
$formatter_fields = [ 'table', 'column', 'match', 'primary_key_name', 'primary_key_value' ];

if ( $fields ) {
$fields = explode( ',', $assoc_args['fields'] );
Expand All @@ -1515,7 +1519,7 @@ public function search( $args, $assoc_args ) {
if ( count( $tables ) > 1 ) {
WP_CLI::error( 'The "ids" format can only be used for a single table.' );
}
$search_results = array_column( $search_results, 'value' );
$search_results = array_column( $search_results, 'primary_key_value' );
}

$formatter = new Formatter( $formatter_args, $formatter_fields );
Expand Down

0 comments on commit 531b897

Please sign in to comment.