Skip to content

Commit

Permalink
Merge pull request #3745 from wp-cli/db-cli-flexible
Browse files Browse the repository at this point in the history
Make it possible to pass arguments through to `mysql` executable
  • Loading branch information
danielbachhuber committed Jan 17, 2017
2 parents c718d02 + e1ab7c5 commit d4b15fd
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions php/commands/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ public function repair() {
/**
* Open a MySQL console using credentials from wp-config.php
*
* ## OPTIONS
*
* [--database=<database>]
* : Use a specific database. Defaults to DB_NAME.
*
* [--default-character-set=<character-set>]
* : Use a specific character set. Defaults to DB_CHARSET when defined.
*
* [--<field>=<value>]
* : Extra arguments to pass to the MySQL executable.
*
* ## EXAMPLES
*
* # Open MySQL console
Expand All @@ -174,10 +185,12 @@ public function repair() {
*
* @alias connect
*/
public function cli() {
self::run( 'mysql --no-defaults --no-auto-rehash', array(
'database' => DB_NAME
) );
public function cli( $args, $assoc_args ) {
if ( ! isset( $assoc_args['database'] ) ) {
$assoc_args['database'] = DB_NAME;
}

self::run( 'mysql --no-defaults --no-auto-rehash', $assoc_args );
}

/**
Expand Down Expand Up @@ -275,7 +288,7 @@ public function query( $args, $assoc_args ) {
*
* @alias dump
*/
function export( $args, $assoc_args ) {
public function export( $args, $assoc_args ) {
$result_file = $this->get_file_name( $args );
$stdout = ( '-' === $result_file );
$porcelain = \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' );
Expand Down Expand Up @@ -403,7 +416,7 @@ public function import( $args, $assoc_args ) {
* $ wp db export --tables=$(wp db tables --url=sub.example.com --format=csv)
* Success: Exported to wordpress_dbase.sql
*/
function tables( $args, $assoc_args ) {
public function tables( $args, $assoc_args ) {

$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
unset( $assoc_args['format'] );
Expand Down Expand Up @@ -453,14 +466,15 @@ private static function run( $cmd, $assoc_args = array(), $descriptors = null )
'pass' => DB_PASSWORD,
);

if ( defined( 'DB_CHARSET' ) && constant( 'DB_CHARSET' ) ) {
if ( ! isset( $assoc_args['default-character-set'] )
&& defined( 'DB_CHARSET' ) && constant( 'DB_CHARSET' ) ) {
$required['default-character-set'] = constant( 'DB_CHARSET' );
}

$final_args = array_merge( $assoc_args, $required );

Utils\run_mysql_command( $cmd, $final_args, $descriptors );
}

}

WP_CLI::add_command( 'db', 'DB_Command' );
Expand Down

0 comments on commit d4b15fd

Please sign in to comment.