Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename and reorder the columns
  • Loading branch information
2ndkauboy committed Apr 26, 2024
commit 531b897b9e8740f104a094cda1b5f00a7b0d7e98
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