Skip to content

Stop reordering most mysql arguments#221

Merged
schlessera merged 2 commits intowp-cli:masterfrom
brandonpayton:fix/mysql-command-argument-order
Jul 14, 2022
Merged

Stop reordering most mysql arguments#221
schlessera merged 2 commits intowp-cli:masterfrom
brandonpayton:fix/mysql-command-argument-order

Conversation

@brandonpayton
Copy link
Contributor

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)

@brandonpayton brandonpayton requested a review from a team as a code owner February 3, 2022 17:25
@schlessera schlessera added this to the 2.0.21 milestone Feb 4, 2022
@schlessera schlessera merged commit 48d4359 into wp-cli:master Jul 14, 2022
@brandonpayton brandonpayton deleted the fix/mysql-command-argument-order branch July 15, 2022 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants