wp-cli / db-command

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

Stop reordering most mysql arguments #221

Closed brandonpayton closed 2 years ago

brandonpayton commented 2 years ago

WP-CLI v2.6.0 is breaking mysql argument order, apparently due to how arguments are sorted here.

On WordPress.com, with WP-CLI v2.6.0, we discovered this due to backups that were failing because wp db export output had changed.

The changed argument order can be observed by reading --debug output.

WP-CLI v2.5.0

For the command:

wp --path=/srv/htdocs/__wp__/ --skip-plugins --skip-themes db export - --tables=wp_commentmeta --opt --compact --quick --single-transaction --complete-insert --skip-extended-insert --max-allowed-packet=100M --debug

WP-CLI v2.5.0 shows the following, where the argument order in the final command generally follows the associative argument order.

Debug (db): Associative arguments: {"opt":true,"compact":true,"quick":true,"single-transaction":true,"complete-insert":true,"skip-extended-insert":true,"max-allowed-packet":"100M"} (0.192s)
Debug (db): Final MySQL command: /usr/bin/env mysqldump --no-defaults '149697504' --no-tablespaces --tables 'wp_commentmeta' --opt --compact --quick --single-transaction --complete-insert --skip-extended-insert --max-allowed-packet='100M' --host='127.0.0.1' --user='149697504' --default-character-set='latin1' (0.192s)

WP-CLI v2.6.0

In contrast, WP-CLI v2.6.0 show the following, where the argument order in the final command does not reflect the associative argument order:

Debug (db): Associative arguments: {"opt":true,"compact":true,"quick":true,"single-transaction":true,"complete-insert":true,"skip-extended-insert":true,"max-allowed-packet":"100M"} (0.201s)
Debug (db): Final MySQL command: /usr/bin/env mysqldump --no-defaults '149697504' --no-tablespaces --tables 'wp_commentmeta' --skip-extended-insert --max-allowed-packet='100M' --single-transaction --complete-insert --compact --quick --opt --default-character-set='latin1' --user='149697504' --host='127.0.0.1' (0.201s)

In this specific case, the difference in argument order causes mysqldump to output a single multi-row INSERT statement rather than an INSERT statement per row as the original command specified (with an --opt arg followed by --skip-extended-insert).

The Fix

This PR updates the argument sorting to explicitly sort a force argument to the end while treating all other arguments as equal so their order, relative to one another, does not change. With this fix, the debug output shows the final command reflects the associate argument order:

Debug (db): Associative arguments: {"opt":true,"compact":true,"quick":true,"single-transaction":true,"complete-insert":true,"skip-extended-insert":true,"max-allowed-packet":"100M"} (0.203s)
Debug (db): Final MySQL command: /usr/bin/env mysqldump --no-defaults '149697504' --no-tablespaces --tables 'wp_commentmeta' --host='127.0.0.1' --user='149697504' --default-character-set='latin1' --opt --compact --quick --single-transaction --complete-insert --skip-extended-insert --max-allowed-packet='100M' (0.203s)

In addition, testing this fix with a --force argument in the middle of the arguments shows that the --force argument is moved to the end without affecting the order of the other arguments.

Debug (db): Associative arguments: {"opt":true,"compact":true,"force":true,"quick":true,"single-transaction":true,"complete-insert":true,"skip-extended-insert":true,"max-allowed-packet":"100M"} (0.202s)
Debug (db): Final MySQL command: /usr/bin/env mysqldump --no-defaults '149697504' --no-tablespaces --tables 'wp_commentmeta' --host='127.0.0.1' --user='149697504' --default-character-set='latin1' --opt --compact --quick --single-transaction --complete-insert --skip-extended-insert --max-allowed-packet='100M' --force (0.202s)