PHP Class DB_Command, wp-cli

## EXAMPLES # Create a new database. $ wp db create Success: Database created. # Drop an existing database. $ wp db drop --yes Success: Database dropped. # Reset the current database. $ wp db reset --yes Success: Database reset. # Execute a SQL query stored in a file. $ wp db query < debug.sql
Inheritance: extends WP_CLI_Command
Show file Open project: wp-cli/wp-cli

Public Methods

Method Description
check ( ) Check the current status of the database.
cli ( ) Open a MySQL console using credentials from wp-config.php
create ( $_, $assoc_args ) Create a new database.
drop ( $_, $assoc_args ) Delete the existing database.
export ( $args, $assoc_args ) Exports the database to a file or to STDOUT.
import ( $args, $assoc_args ) Import a database from a file or from STDIN.
optimize ( ) Optimize the database.
query ( $args, $assoc_args ) Execute a SQL query against the database.
repair ( ) Repair the database.
reset ( $_, $assoc_args ) Remove all tables from the database.
tables ( $args, $assoc_args ) List the database tables.

Private Methods

Method Description
get_create_query ( )
get_file_name ( $args )
run ( $cmd, $assoc_args = [], $descriptors = null )
run_query ( $query )

Method Details

check() public method

Runs mysqlcheck utility with --check using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. See docs for more details on the CHECK TABLE statement. ## EXAMPLES $ wp db check Success: Database checked.
public check ( )

cli() public method

## EXAMPLES # Open MySQL console $ wp db cli mysql>
public cli ( )

create() public method

Runs CREATE_DATABASE SQL statement using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. ## EXAMPLES $ wp db create Success: Database created.
public create ( $_, $assoc_args )

drop() public method

Runs DROP_DATABASE SQL statement using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. ## OPTIONS [--yes] : Answer yes to the confirmation message. ## EXAMPLES $ wp db drop --yes Success: Database dropped.
public drop ( $_, $assoc_args )

export() public method

Runs mysqldump utility using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. ## OPTIONS [] : The name of the SQL file to export. If '-', then outputs to STDOUT. If omitted, it will be '{dbname}.sql'. [--=] : Extra arguments to pass to mysqldump [--tables=] : The comma separated list of specific tables to export. Excluding this parameter will export all tables in the database. [--porcelain] : Output filename for the exported database. ## EXAMPLES # Export database with drop query included $ wp db export --add-drop-table Success: Exported to 'wordpress_dbase.sql'. # Export certain tables $ wp db export --tables=wp_options,wp_users Success: Exported to 'wordpress_dbase.sql'. # Export all tables matching a wildcard $ wp db export --tables=$(wp db tables 'wp_user*' --format=csv) Success: Exported to 'wordpress_dbase.sql'. # Export all tables matching prefix $ wp db export --tables=$(wp db tables --all-tables-with-prefix --format=csv) Success: Exported to 'wordpress_dbase.sql'.
public export ( $args, $assoc_args )

import() public method

Runs SQL queries using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. This does not create database by itself and only performs whatever tasks are defined in the SQL. ## OPTIONS [] : The name of the SQL file to import. If '-', then reads from STDIN. If omitted, it will look for '{dbname}.sql'. ## EXAMPLES # Import MySQL from a file. $ wp db import wordpress_dbase.sql Success: Imported from 'wordpress_dbase.sql'.
public import ( $args, $assoc_args )

optimize() public method

Runs mysqlcheck utility with --optimize=true using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. See docs for more details on the OPTIMIZE TABLE statement. ## EXAMPLES $ wp db optimize Success: Database optimized.
public optimize ( )

query() public method

Executes an arbitrary SQL query using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. ## OPTIONS [] : A SQL query. If not passed, will try to read from STDIN. [--=] : Extra arguments to pass to mysql. ## EXAMPLES # Execute a query stored in a file $ wp db query < debug.sql # Check all tables in the database $ wp db query "CHECK TABLE $(wp db tables | paste -s -d',');" +---------------------------------------+-------+----------+----------+ | Table | Op | Msg_type | Msg_text | +---------------------------------------+-------+----------+----------+ | wordpress_dbase.wp_users | check | status | OK | | wordpress_dbase.wp_usermeta | check | status | OK | | wordpress_dbase.wp_posts | check | status | OK | | wordpress_dbase.wp_comments | check | status | OK | | wordpress_dbase.wp_links | check | status | OK | | wordpress_dbase.wp_options | check | status | OK | | wordpress_dbase.wp_postmeta | check | status | OK | | wordpress_dbase.wp_terms | check | status | OK | | wordpress_dbase.wp_term_taxonomy | check | status | OK | | wordpress_dbase.wp_term_relationships | check | status | OK | | wordpress_dbase.wp_termmeta | check | status | OK | | wordpress_dbase.wp_commentmeta | check | status | OK | +---------------------------------------+-------+----------+----------+ # Pass extra arguments through to MySQL $ wp db query 'SELECT * FROM wp_options WHERE option_name="home"' --skip-column-names +---+------+------------------------------+-----+ | 2 | home | http://wordpress-develop.dev | yes | +---+------+------------------------------+-----+
public query ( $args, $assoc_args )

repair() public method

Runs mysqlcheck utility with --repair=true using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. See docs for more details on the REPAIR TABLE statement. ## EXAMPLES $ wp db repair Success: Database repaired.
public repair ( )

reset() public method

Runs DROP_DATABASE and CREATE_DATABASE SQL statements using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. ## OPTIONS [--yes] : Answer yes to the confirmation message. ## EXAMPLES $ wp db reset --yes Success: Database reset.
public reset ( $_, $assoc_args )

tables() public method

Defaults to all tables registered to the $wpdb database handler. ## OPTIONS [...] : List tables based on wildcard search, e.g. 'wp_*_options' or 'wp_post?'. [--scope=] : Can be all, global, ms_global, blog, or old tables. Defaults to all. [--network] : List all the tables in a multisite install. Overrides --scope=. [--all-tables-with-prefix] : List all tables that match the table prefix even if not registered on $wpdb. Overrides --network. [--all-tables] : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix. [--format=] : Render output in a particular format. --- default: list options: - list - csv --- ## EXAMPLES # Export only tables for a single site $ wp db export --tables=$(wp db tables --url=sub.example.com --format=csv) Success: Exported to wordpress_dbase.sql
public tables ( $args, $assoc_args )