wp-cli / db-command

Performs basic database operations using credentials stored in wp-config.php.
MIT License
71 stars 59 forks source link

Added "db get-rows" command #153

Closed petenelson closed 2 years ago

petenelson commented 5 years ago

This code addresses #145 by adding a get-rows command to allow custom DB queries to get results formatted in a list, CSV, JSON, or a single column that can be piped to other commands.

schlessera commented 5 years ago

At first I thought this PR was good for merging, but then I realized that it deviates considerably from #145 .

I think this should be implemented as a [--format=<format>] flag to the existing wp db query:

Are you up for another stab at this?

petenelson commented 5 years ago

This was the part I'm not quite clear on how to implement collect results from mysql and then pass them to the requested formatter which is why I went with this get-rows route. Would the ob_start/ob_get_clean pickup the terminal output from the run_mysql_command() function?

petenelson commented 5 years ago

Aha, think I found what's needed, passing descriptors and reading from pipes. Making some progress on this.

petenelson commented 5 years ago

Something like this, but it will take an update to the run_mysql_command() in Utils in order to pass an arg to be able to capture the output.

$descriptors = [
    [ 'pipe', 'r' ],
    [ 'pipe', 'w' ],
    [ 'pipe', 'w' ],
];

$run_args = [ 'return_results' => true ];

$results = self::run( '/usr/bin/env mysql --no-defaults --no-auto-rehash --skip-column-names', $assoc_args, $descriptors, $run_args );

$results = array_filter( explode( PHP_EOL, $results ) );

I'll need to get the main wp-cli project setup locally for development in order to submit a PR for the run_mysql_command() update.

schlessera commented 4 years ago

@petenelson Looks like you've got ti figured out. Let me know if you need assistance with building something like the above.

schlessera commented 4 years ago

This will be greatly simplified by https://github.com/wp-cli/wp-cli/pull/5384.

schlessera commented 4 years ago

@petenelson Are you still interested in working on this?

It should be more straight-forward now, I've adapted Utils\ run_mysql_command(), so that if you pass false as the fourth argument, it will return an array with [ $stdout, $sterr, $exit_code ].

schlessera commented 3 years ago

Hey @petenelson,

Gentle ping to see if you're still up for giving this another try...

No worries at all if you can't find the time or energy, I would just appreciate knowing about that to decide what to do with the PR.

petenelson commented 3 years ago

Probably not right now. I'd need to get all of the development environment up and running again.

danielbachhuber commented 2 years ago

Proceeding with https://github.com/wp-cli/wp-cli/issues/5594 for this repository. I've captured this PR to https://gist.github.com/danielbachhuber/6924f9d9aa78fa3b2092943943bf45b7 in case this PR is auto-closed or broken in some way.