wp-cli / search-replace-command

Searches/replaces strings in the database.
MIT License
57 stars 45 forks source link

Corrects NULL export values. #93

Closed antigenius closed 6 years ago

antigenius commented 6 years ago

The code that exports a search-replace command as SQL creates a prepared statement where all columns values are assumed to be strings. Normally this is fine as MySQL/MariaDB will do the conversion work at import. However, in some cases, a column may allow nulls in a numeric field. In this instance, the call to prepare will translate a null value into null string. Importing will then fail as null string is not a numeric value.

This patch replaces the original code that assumed strings for all bind parameters. Each column value is iterated over and inspected. If the value is not NULL, the value is added to the $values array and a bind parameter, %s is added to a substituions array. If the value causes is_null to return true, only 'NULL' is added to the substituions array.

The code then evaluates as it did previously, with a join call creating a string from the substitutions array and appending that string to the larger SQL query.

This closes #82.

schlessera commented 6 years ago

Thanks for the pull-request, @antigenius. I have only a very minor gripe, and then the PR can be merged.

antigenius commented 6 years ago

You're welcome, @schlessera! My sysadmin found this issue after running into some trouble. Happy to help! PR updated. 😄