Enable formatter for search command
Closes #158
Related https://github.com/wp-cli/wp-cli/issues/5859
cc @johnbillion since you were involved in the discussion about this, maybe you'd like to take a look as well
@2ndkauboy Are you planning to continue working on this, or should we close the PR for now?
@danielbachhuber i was hoping to get feedback from @johnbillion about this new feature, since he opened the issue.
I'll take a look!
@2ndkauboy I agree that introducing a --format parameter here is a good idea. Daniel left some good feedback and I agree with all his points. Is this something you'd like to continue with?
Daniel left some good feedback and I agree with all his points. Is this something you'd like to continue with?
So you would also rename the ID to value?
Would people understand that value is for the key column?
+------------+--------------+-----------+-------+-----------------------------+
| table | column | key | value | match |
+------------+--------------+-----------+-------+-----------------------------+
| wp_options | option_value | option_id | 1 | https://localhost:8889 |
| wp_options | option_value | option_id | 2 | https://localhost:8889 |
| wp_posts | guid | ID | 1 | https://localhost:8889/?p=1 |
| wp_users | user_url | ID | 1 | https://localhost:8889 |
+------------+--------------+-----------+-------+-----------------------------+
What's the command for that output?
The command for the output above would be:
wp db search https://localhost:8889 --format=table
I think value is better than ID but overall that table isn't particularly clear.
Perhaps the order of the columns should change to: column, match, key, value.
If we didn't need to support output from multiple database tables we could skip the key column completely and use the key as the name for the value column.
+------------+--------------+-----------+-----------------------------+
| table | column | option_id | match |
+------------+--------------+-----------+-----------------------------+
| wp_options | option_value | 2 | https://localhost:8889 |
+------------+--------------+-----------+-----------------------------+
+------------+--------------+-------+-----------------------------+
| table | column | ID | match |
+------------+--------------+-------+-----------------------------+
| wp_posts | guid | 1 | https://localhost:8889/?p=1 |
+------------+--------------+-------+-----------------------------+
If we didn't need to support output from multiple database tables we could skip the key column completely and use the key as the name for the value column.
By default, the wp db search command searches in all tables and outputs something like this:
wp_comments:comment_author_email
244936:[email protected]
wp_options:option_value
14:mail.example.com
wp_options:option_value
15:[email protected]
So this includes: table, column, primary key value, match.
This is why decided to have all these columns as well. But then the "primary key value" column needed a headline/name, and since it could be different column names, I added that key column as well.
If we allow searching all tables, which the command does by default, we cannot use the "primary key name" as the headline/name, since it's different names. We could just not have that column, but then we need another headline. Maybe something like pk? :thinking:
Yeah. Perhaps primary_key_name and primary_key_value? It's very verbose but it's clear.
I thought about that as well. How about pk_column or pk_key_name and pk_value?
@johnbillion I've simulated some variant, which one would you pick?
Only the primary key value
1. Column pk
+------------+--------------+----+-----------------------------+
| table | column | pk | match |
+------------+--------------+----+-----------------------------+
| wp_options | option_value | 1 | https://localhost:8889 |
| wp_options | option_value | 2 | https://localhost:8889 |
| wp_posts | guid | 1 | https://localhost:8889/?p=1 |
| wp_users | user_url | 1 | https://localhost:8889 |
+------------+--------------+----+-----------------------------+
2. Column value
+------------+--------------+-----+-----------------------------+
| table | column | key | match |
+------------+--------------+-----+-----------------------------+
| wp_options | option_value | 1 | https://localhost:8889 |
| wp_options | option_value | 2 | https://localhost:8889 |
| wp_posts | guid | 1 | https://localhost:8889/?p=1 |
| wp_users | user_url | 1 | https://localhost:8889 |
+------------+--------------+-----+-----------------------------+
3. Column primary_key_value
+------------+--------------+-------------------+-----------------------------+
| table | column | primary_key_value | match |
+------------+--------------+-------------------+-----------------------------+
| wp_options | option_value | 1 | https://localhost:8889 |
| wp_options | option_value | 2 | https://localhost:8889 |
| wp_posts | guid | 1 | https://localhost:8889/?p=1 |
| wp_users | user_url | 1 | https://localhost:8889 |
+------------+--------------+-------------------+-----------------------------+
Primary key column name and value
4. Columns key and value
+------------+--------------+-----------+-------+-----------------------------+
| table | column | key | value | match |
+------------+--------------+-----------+-------+-----------------------------+
| wp_options | option_value | option_id | 1 | https://localhost:8889 |
| wp_options | option_value | option_id | 2 | https://localhost:8889 |
| wp_posts | guid | ID | 1 | https://localhost:8889/?p=1 |
| wp_users | user_url | ID | 1 | https://localhost:8889 |
+------------+--------------+-----------+-------+-----------------------------+
5. Columns pk_column and pk_value
+------------+--------------+-----------+----------+-----------------------------+
| table | column | pk_column | pk_value | match |
+------------+--------------+-----------+----------+-----------------------------+
| wp_options | option_value | option_id | 1 | https://localhost:8889 |
| wp_options | option_value | option_id | 2 | https://localhost:8889 |
| wp_posts | guid | ID | 1 | https://localhost:8889/?p=1 |
| wp_users | user_url | ID | 1 | https://localhost:8889 |
+------------+--------------+-----------+----------+-----------------------------+
6. Columns pk_key_name and pk_value
+------------+--------------+-------------+----------+-----------------------------+
| table | column | pk_key_name | pk_value | match |
+------------+--------------+-------------+----------+-----------------------------+
| wp_options | option_value | option_id | 1 | https://localhost:8889 |
| wp_options | option_value | option_id | 2 | https://localhost:8889 |
| wp_posts | guid | ID | 1 | https://localhost:8889/?p=1 |
| wp_users | user_url | ID | 1 | https://localhost:8889 |
+------------+--------------+-------------+----------+-----------------------------+
7. Columns priamry_key_*
+------------+--------------+------------------+-------------------+-----------------------------+
| table | column | priamry_key_name | priamry_key_value | match |
+------------+--------------+------------------+-------------------+-----------------------------+
| wp_options | option_value | option_id | 1 | https://localhost:8889 |
| wp_options | option_value | option_id | 2 | https://localhost:8889 |
| wp_posts | guid | ID | 1 | https://localhost:8889/?p=1 |
| wp_users | user_url | ID | 1 | https://localhost:8889 |
+------------+--------------+------------------+-------------------+-----------------------------+
Different order
8. Columns pk_key_* and match_*
+------------+-------------+----------+--------------+-----------------------------+
| table | pk_key_name | pk_value | match_column | match_value |
+------------+-------------+----------+--------------+-----------------------------+
| wp_options | option_id | 1 | option_value | https://localhost:8889 |
| wp_options | option_id | 2 | option_value | https://localhost:8889 |
| wp_posts | ID | 1 | guid | https://localhost:8889/?p=1 |
| wp_users | ID | 1 | user_url | https://localhost:8889 |
+------------+-------------+----------+--------------+-----------------------------+
9. Columns obj_id_* and match_*
+------------+------------+--------+--------------+-----------------------------+
| table | obj_id_key | obj_id | match_column | match_value |
+------------+------------+--------+--------------+-----------------------------+
| wp_options | option_id | 1 | option_value | https://localhost:8889 |
| wp_options | option_id | 2 | option_value | https://localhost:8889 |
| wp_posts | ID | 1 | guid | https://localhost:8889/?p=1 |
| wp_users | ID | 1 | user_url | https://localhost:8889 |
+------------+------------+--------+--------------+-----------------------------+
Let's go with this:
+------------+--------------+-----------+------------------+-----------------------------+
| table | column | match | primary_key_name | primary_key_value |
+------------+--------------+-----------+------------------+-----------------------------+
The result would look like this now:
$wp db search admin --format=table
+-------------+---------------+-----------------------------------------------------------------------------------------------+------------------+-------------------+
| table | column | match | primary_key_name | primary_key_value |
+-------------+---------------+-----------------------------------------------------------------------------------------------+------------------+-------------------+
| wp_options | option_name | admin_email | option_id | 6 |
| wp_options | option_name | admin_email_lifespan | option_id | 280 |
| wp_options | option_value | a:5:{s:13:"administrator";a:2:{s:4:"name";s:13:"Administrator";s:12:"capabilities";a:61:{s:13 | option_id | 89 |
| wp_options | option_value | e notification of site activity via the admin toolbar and your Mobile devices.";s:4:" | option_id | 164 |
| wp_usermeta | meta_key | admin_color | umeta_id | 7 |
| wp_usermeta | meta_key | show_admin_bar_front | umeta_id | 9 |
| wp_usermeta | meta_value | admin | umeta_id | 1 |
| wp_usermeta | meta_value | a:1:{s:13:"administrator";b:1;} | umeta_id | 10 |
| wp_users | user_login | admin | ID | 1 |
| wp_users | user_nicename | admin | ID | 1 |
| wp_users | display_name | admin | ID | 1 |
+-------------+---------------+-----------------------------------------------------------------------------------------------+------------------+-------------------+
@2ndkauboy I think that looks good. What do you think?
Let's merge it then :) After my updated tests run ;)
Updated at WP-CLI Hack Day April 2024 #5935