Skip to content

Improvements to Database Functions in WP-CLI 0.22.0

Yesterday, version 0.22.0 of the WP-CLI command line library for WordPress was released. There’s some great new features in this release, along with many bug fixes.

I’ve always said that the commands I use the most in this library are those that help with database management. The WP-CLI team has made some great improvements to these commands in this release.

Search and Replace: Export

One of my most common use cases for WP-CLI is to help migrate a database between environments – from a production server to my local development environment, for example.  I’ll use the search-replace command to safely replace all production URLs with my development server URL, ensuring that serialized PHP arrays are handled correctly.  My typical process for doing this was:

  1. Run wp db export production_export.sql on the production server
  2. Copy the file to my development environment.
  3. Run wp db import production_export.sql on the development server
  4. Run wp search-replace 'www.example.com' 'www.example.local' on the development server

This version of WP-CLI now includes an export parameter on the search-replace command, allowing you to exported the modified copy of the database directly from search-replace, without modifying the database you’re running the command against.  This means that I can simplify by workflow to:

  1. Run wp search-replace 'www.example.com' 'www.example.local' --export=db_with_dev_urls.sql on the production server
  2. Copy the file to my development environment.
  3. Run wp db import db_with_dev_urls.sql on the development server

Handy, right?

Search and Replace: Table Name Wildcards

When doing search and replaces, you can specify which tables will be affected.   With this release, you can use wildcards when specifying the affected tables.  So, if you wanted to only perform a search and replace in the meta tables (wp_postmeta, wp_usermeta, etc.) you could run this:

wp search-replace 'old-value' 'new-value' '*meta*'

As noted in the release notes, make sure the parameter is quoted, as the asterisk (*) is a special character in bash.

Update Database: Dry Run

The --dry-run flag has existed in the search & replace function for a long time, it was really useful for checking what was going to happen to your database before actually making the changes.  This flag has now been extended to the wp core update-db command, so that you can see if your database is at the most recent version without actually performing the update.  Just run:

wp core update-db --dry-run

 


There are lots of other changes that came out with WP-CLI 0.22.0. Take a look at the release blog post, or at the changes in GitHub. Thank you to all the contributors who put their time into these changes. As a frequent user of the library, it’s much appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.