wp-cli / db-command

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

Fatal error using new --orderby=size with --size_format=mb #229

Closed wpbullet closed 2 years ago

wpbullet commented 2 years ago

I was testing WP CLI 2.7.0 and the super handy new --orderby=size and --order=desc flags.

These flags work on their own but not when combined with the --size_format=mb flag (which I would hope to make the default since Bytes are not that human-friendly).

With this command under PHP 8.1:

wp db size --tables --all-tables --orderby=size --order=desc --allow-root --size_format=mb 

Fatal error output

PHP Fatal error:  Uncaught TypeError: Unsupported operand types: string / int in phar:///usr/bin/wp/vendor/wp-cli/db-command/src/DB_Command.php:1105
Stack trace:
#0 [internal function]: DB_Command->size()
#1 phar:///usr/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php(100): call_user_func()
#2 [internal function]: WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}()
#3 phar:///usr/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php(491): call_user_func()
#4 phar:///usr/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(419): WP_CLI\Dispatcher\Subcommand->invoke()
#5 phar:///usr/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(442): WP_CLI\Runner->run_command()
#6 phar:///usr/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1256): WP_CLI\Runner->run_command_and_exit()
#7 phar:///usr/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#8 phar:///usr/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php(78): WP_CLI\Bootstrap\LaunchRunner->process()
#9 phar:///usr/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#10 phar:///usr/bin/wp/php/boot-phar.php(11): include('...')
#11 /usr/bin/wp(4): include('...')
#12 {main}
  thrown in phar:///usr/bin/wp/vendor/wp-cli/db-command/src/DB_Command.php on line 1105

The same error happened without the --tables flag

wp db size --all-tables --orderby=size --order=desc --allow-root --size_format=mb 

PHP Version output

php --version
PHP 8.1.11 (cli) (built: Sep 29 2022 22:28:23) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.11, Copyright (c), by Zend Technologies

WP CLI version

wp cli version --allow-root
WP-CLI 2.7.0
danielbachhuber commented 2 years ago

Thanks for the report, @wpbullet !

Interestingly, it seems to work fine for me:

$ php -v
PHP 8.1.11 (cli) (built: Sep 29 2022 19:44:28) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.11, Copyright (c), by Zend Technologies
$ wp db size --tables --all-tables --orderby=size --order=desc --allow-root --size_format=mb
+-----------------------+------+
| Name                  | Size |
+-----------------------+------+
| wp_options            | 1 MB |
| wp_comments           | 0 MB |
| wp_posts              | 0 MB |
| wp_users              | 0 MB |
| wp_commentmeta        | 0 MB |
| wp_postmeta           | 0 MB |
| wp_term_taxonomy      | 0 MB |
| wp_termmeta           | 0 MB |
| wp_terms              | 0 MB |
| wp_usermeta           | 0 MB |
| wp_links              | 0 MB |
| wp_term_relationships | 0 MB |
+-----------------------+------+

I'll see if I can get it to fail in GitHub actions...

wpbullet commented 2 years ago

Thanks @danielbachhuber that's very interesting it works for you

I tried with the --skip-plugins and the --skip-packages flags and still got the same error.

I was thinking it may have something to do with wp doctor or wp profile commands

danielbachhuber commented 2 years ago

@wpbullet The test seems to pass: https://github.com/wp-cli/db-command/actions/runs/3237164741/jobs/5303893706

However, this does seem incorrect:

https://github.com/wp-cli/db-command/blob/2e57e86f96acd64684a600f2324324030aff7f76/src/DB_Command.php#L1026-L1031

https://github.com/wp-cli/db-command/blob/2e57e86f96acd64684a600f2324324030aff7f76/src/DB_Command.php#L1105

Originated with https://github.com/wp-cli/db-command/pull/69

I think https://github.com/wp-cli/db-command/pull/230/commits/210ebcca1f28275d61c6a248a94da86d566ef9dd should fix the issue for you. Do you have an easy way to test?

wpbullet commented 2 years ago

Thank you for digging further @danielbachhuber !

I don't have an easy way to test that I'm aware of but if there is a guide on how to do that I'm happy to try. I searched the system for find . -iname DB_Command.php but found no such file

danielbachhuber commented 2 years ago

@wpbullet Here's a Phar file you can test out!

wp-cli-size-format-fail.phar.zip

wpbullet commented 2 years ago

@danielbachhuber you are a star! We have success:

./wp-cli-size-format-fail.phar --version --allow-root
WP-CLI 2.8.0-size-format-fail
root@wp-bullet:/var/www/wp-bullet.com# ./wp-cli-size-format-fail.phar db size --tables --all-tables --orderby=size --order=desc --allow-root --size_format=mb

Tada!

+------------------------------------+-------+
| Name                               | Size  |
+------------------------------------+-------+
| wp_prli_clicks                     | 27 MB |
| wp_posts                           | 8 MB  |
| wp_options                         | 7 MB  |
| wp_postmeta                        | 2 MB  |
| wp_aiowps_login_lockdown           | 2 MB  |
| wp_ewwwio_images                   | 1 MB  |
| wp_yoast_indexable                 | 1 MB  |
| wp_aiowps_failed_logins            | 0 MB  |
| wp_gf_entry_meta                   | 0 MB  |
| wp_yoast_seo_links                 | 0 MB  |
| wp_prli_links                      | 0 MB  |
| wp_prli_link_metas                 | 0 MB  |
| wp_comments                        | 0 MB  |
| wp_edd_orders                      | 0 MB  |
| wp_edd_order_addresses             | 0 MB  |
| wp_edd_customer_email_addresses    | 0 MB  |
| wp_edd_notes                       | 0 MB  |
| wp_edd_order_transactions          | 0 MB  |
| wp_edd_ordermeta                   | 0 MB  |
| wp_edd_subscriptions               | 0 MB  |
| wp_gf_form_view                    | 0 MB  |
| wp_usermeta                        | 0 MB  |
| wp_yoast_indexable_hierarchy       | 0 MB  |
| wp_edd_adjustments                 | 0 MB  |
| wp_edd_customer_addresses          | 0 MB  |
| wp_edd_customers                   | 0 MB  |
| wp_edd_logs                        | 0 MB  |
| wp_edd_logs_api_requests           | 0 MB  |
| wp_prli_link_rotations             | 0 MB  |
| wp_edd_licensemeta                 | 0 MB  |
| wp_edd_licenses                    | 0 MB  |
| wp_edd_logs_file_downloads         | 0 MB  |
| wp_edd_order_adjustments           | 0 MB  |
| wp_edd_order_items                 | 0 MB  |
| wp_prli_post_keywords              | 0 MB  |
| wp_prli_post_urls                  | 0 MB  |
| wp_users                           | 0 MB  |
| wp_affiliate_wp_affiliatemeta      | 0 MB  |
| wp_affiliate_wp_customermeta       | 0 MB  |
| wp_affiliate_wp_customers          | 0 MB  |
| wp_affiliate_wp_direct_links       | 0 MB  |
| wp_affiliate_wp_payouts            | 0 MB  |
| wp_affiliate_wp_referralmeta       | 0 MB  |
| wp_commentmeta                     | 0 MB  |
| wp_edd_adjustmentmeta              | 0 MB  |
| wp_edd_customermeta                | 0 MB  |
| wp_edd_logmeta                     | 0 MB  |
| wp_edd_logs_api_requestmeta        | 0 MB  |
| wp_edd_logs_file_downloadmeta      | 0 MB  |
| wp_edd_notemeta                    | 0 MB  |
| wp_edd_order_adjustmentmeta        | 0 MB  |
| wp_edd_order_itemmeta              | 0 MB  |
| wp_gf_entry                        | 0 MB  |
| wp_gf_entry_notes                  | 0 MB  |
| wp_gf_form_meta                    | 0 MB  |
| wp_gf_form_revisions               | 0 MB  |
| wp_prli_clicks_rotations           | 0 MB  |
| wp_prli_keywords                   | 0 MB  |
| wp_prli_report_links               | 0 MB  |
| wp_prli_reports                    | 0 MB  |
| wp_term_taxonomy                   | 0 MB  |
| wp_termmeta                        | 0 MB  |
| wp_terms                           | 0 MB  |
| wp_yoast_primary_term              | 0 MB  |
| wp_yoast_seo_meta                  | 0 MB  |
| wp_affiliate_wp_affiliates         | 0 MB  |
| wp_affiliate_wp_coupons            | 0 MB  |
| wp_affiliate_wp_creatives          | 0 MB  |
| wp_affiliate_wp_lifetime_customers | 0 MB  |
| wp_affiliate_wp_referrals          | 0 MB  |
| wp_affiliate_wp_rest_consumers     | 0 MB  |
| wp_affiliate_wp_sales              | 0 MB  |
| wp_affiliate_wp_visits             | 0 MB  |
| wp_edd_license_activations         | 0 MB  |
| wp_edd_notifications               | 0 MB  |
| wp_ewwwio_queue                    | 0 MB  |
| wp_gf_addon_feed                   | 0 MB  |
| wp_gf_draft_submissions            | 0 MB  |
| wp_links                           | 0 MB  |
| wp_prli_groups                     | 0 MB  |
| wp_term_relationships              | 0 MB  |
| wp_yoast_migrations                | 0 MB  |
| wp_aiowps_events                   | 0 MB  |
| wp_aiowps_global_meta              | 0 MB  |
| wp_aiowps_login_activity           | 0 MB  |
| wp_aiowps_permanent_block          | 0 MB  |
| wp_gf_form                         | 0 MB  |
| wp_novashare_meta                  | 0 MB  |
| wp_wpfm_backup                     | 0 MB  |
| wp_datalist                        | 0 MB  |
| wp_affiliate_wp_campaigns          | 0 MB  |
+------------------------------------+-------+
danielbachhuber commented 2 years ago

@wpbullet Sweet! We'll get this in a queue for a release then 😁

schlessera commented 2 years ago

This fix was pushed as a patch release with release v2.7.1.