db-command icon indicating copy to clipboard operation
db-command copied to clipboard

Enable formatter for search command

Open 2ndkauboy opened this issue 2 years ago • 4 comments

Closes #158

Related https://github.com/wp-cli/wp-cli/issues/5859

2ndkauboy avatar Nov 10 '23 18:11 2ndkauboy

cc @johnbillion since you were involved in the discussion about this, maybe you'd like to take a look as well

swissspidy avatar Nov 10 '23 20:11 swissspidy

@2ndkauboy Are you planning to continue working on this, or should we close the PR for now?

danielbachhuber avatar Dec 12 '23 23:12 danielbachhuber

@danielbachhuber i was hoping to get feedback from @johnbillion about this new feature, since he opened the issue.

2ndkauboy avatar Dec 13 '23 15:12 2ndkauboy

I'll take a look!

johnbillion avatar Dec 14 '23 09:12 johnbillion

@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?

johnbillion avatar Apr 26 '24 11:04 johnbillion

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?

2ndkauboy avatar Apr 26 '24 13:04 2ndkauboy

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

2ndkauboy avatar Apr 26 '24 13:04 2ndkauboy

What's the command for that output?

johnbillion avatar Apr 26 '24 13:04 johnbillion

The command for the output above would be:

wp db search https://localhost:8889 --format=table

2ndkauboy avatar Apr 26 '24 13:04 2ndkauboy

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

johnbillion avatar Apr 26 '24 13:04 johnbillion

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:

2ndkauboy avatar Apr 26 '24 14:04 2ndkauboy

Yeah. Perhaps primary_key_name and primary_key_value? It's very verbose but it's clear.

johnbillion avatar Apr 26 '24 14:04 johnbillion

I thought about that as well. How about pk_column or pk_key_name and pk_value?

2ndkauboy avatar Apr 26 '24 14:04 2ndkauboy

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

2ndkauboy avatar Apr 26 '24 14:04 2ndkauboy

Let's go with this:

+------------+--------------+-----------+------------------+-----------------------------+
| table      | column       | match     | primary_key_name | primary_key_value           |
+------------+--------------+-----------+------------------+-----------------------------+

danielbachhuber avatar Apr 26 '24 15:04 danielbachhuber

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 avatar Apr 26 '24 15:04 2ndkauboy

@2ndkauboy I think that looks good. What do you think?

danielbachhuber avatar Apr 26 '24 15:04 danielbachhuber

Let's merge it then :) After my updated tests run ;)

2ndkauboy avatar Apr 26 '24 16:04 2ndkauboy

Updated at WP-CLI Hack Day April 2024 #5935

2ndkauboy avatar Apr 29 '24 11:04 2ndkauboy