Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove --str flag from db subcommands

The --str flag assumes that all the information will be passed as CLI
arguments, but that's no longer the case since we started using the
MYSQL_PWD env variable.

And we might want to start using the PDO extension in the future,
instead of the `mysql` binary.

Also introduce run_mysql_query() utility.
  • Loading branch information...
commit 24e45edc6d33385a08ae75349b5050c20d5ef19b 1 parent 30886fd
@scribu scribu authored
Showing with 55 additions and 63 deletions.
  1. +41 −63 php/commands/db.php
  2. +14 −0 php/utils.php
View
104 php/commands/db.php
@@ -10,12 +10,10 @@ class DB_Command extends WP_CLI_Command {
/**
* Create the database, as specified in wp-config.php
*
- * @synopsis [--str]
+ * @synopsis
*/
function create( $_, $assoc_args ) {
- self::run( $assoc_args, self::create_execute_cmd(
- sprintf( 'CREATE DATABASE `%s`', DB_NAME )
- ) );
+ self::run_query( sprintf( 'CREATE DATABASE `%s`', DB_NAME ) );
WP_CLI::success( "Database created." );
}
@@ -23,48 +21,37 @@ function create( $_, $assoc_args ) {
/**
* Delete the database.
*
- * @synopsis [--yes] [--str]
+ * @synopsis [--yes]
*/
function drop( $_, $assoc_args ) {
- $command = self::create_execute_cmd( sprintf( 'DROP DATABASE `%s`', DB_NAME ) );
-
- if ( !isset( $assoc_args['str'] ) ) {
- WP_CLI::confirm( "Are you sure you want to drop the database?", $assoc_args );
- WP_CLI::launch( $command );
- WP_CLI::success( "Database dropped." );
- } else {
- WP_CLI::line( $command );
- }
+ WP_CLI::confirm( "Are you sure you want to drop the database?", $assoc_args );
+
+ self::run_query( sprintf( 'DROP DATABASE `%s`', DB_NAME ) );
+
+ WP_CLI::success( "Database dropped." );
}
/**
* Remove all tables from the database.
*
- * @synopsis [--yes] [--str]
+ * @synopsis [--yes]
*/
function reset( $_, $assoc_args ) {
- $drop_cmd = self::create_execute_cmd( sprintf( 'DROP DATABASE IF EXISTS `%s`', DB_NAME ) );
-
- $create_cmd = self::create_execute_cmd( sprintf( 'CREATE DATABASE `%s`', DB_NAME ) );
-
- if ( !isset( $assoc_args['str'] ) ) {
- WP_CLI::confirm( "Are you sure you want to reset the database?", $assoc_args );
- WP_CLI::launch( $drop_cmd );
- WP_CLI::launch( $create_cmd );
- WP_CLI::success( "Database reset." );
- } else {
- WP_CLI::line( $drop_cmd );
- WP_CLI::line( $create_cmd );
- }
+ WP_CLI::confirm( "Are you sure you want to reset the database?", $assoc_args );
+
+ self::run_query( sprintf( 'DROP DATABASE IF EXISTS `%s`', DB_NAME ) );
+ self::run_query( sprintf( 'CREATE DATABASE `%s`', DB_NAME ) );
+
+ WP_CLI::success( "Database reset." );
}
/**
* Optimize the database.
*
- * @synopsis [--str]
+ * @synopsis
*/
- function optimize( $_, $assoc_args ) {
- self::run( $assoc_args, \WP_CLI\Utils\create_cmd(
+ function optimize() {
+ self::run( \WP_CLI\Utils\create_cmd(
'mysqlcheck --optimize --host=%s --user=%s %s',
DB_HOST, DB_USER, DB_NAME
) );
@@ -75,13 +62,12 @@ function optimize( $_, $assoc_args ) {
/**
* Repair the database.
*
- * @synopsis [--str]
+ * @synopsis
*/
- function repair( $_, $assoc_args ) {
- self::run( $assoc_args, \WP_CLI\Utils\create_cmd(
+ function repair() {
+ self::run( \WP_CLI\Utils\create_cmd(
'mysqlcheck --repair --host=%s --user=%s %s',
- DB_HOST, DB_USER, DB_NAME
- ) );
+ DB_HOST, DB_USER, DB_NAME ) );
WP_CLI::success( "Database repaired." );
}
@@ -91,34 +77,35 @@ function repair( $_, $assoc_args ) {
*
* @alias cli
*
- * @synopsis [--str]
+ * @synopsis
*/
- function connect( $_, $assoc_args ) {
- self::run( $assoc_args, $this->connect_string() );
+ function connect() {
+ self::run( \WP_CLI\Utils\create_cmd(
+ 'mysql --host=%s --user=%s --database=%s',
+ DB_HOST, DB_USER, DB_NAME ) );
}
/**
* Execute a query against the database.
*
- * @synopsis <sql> [--str]
+ * @synopsis <sql>
*/
- function query( $args, $assoc_args ) {
+ function query( $args ) {
list( $query ) = $args;
- self::run( $assoc_args, $this->connect_string() . \WP_CLI\Utils\create_cmd(
- ' --execute=%s', $query ) );
+ self::run_query( $query );
}
/**
* Exports the database using mysqldump.
*
* @alias dump
- * @synopsis [<file>] [--str]
+ * @synopsis [<file>]
*/
function export( $args, $assoc_args ) {
$result_file = $this->get_file_name( $args );
- self::run( $assoc_args, \WP_CLI\Utils\create_cmd(
+ self::run( \WP_CLI\Utils\create_cmd(
'mysqldump %s --user=%s --host=%s --result-file %s',
DB_NAME, DB_USER, DB_HOST, $result_file ) );
@@ -128,23 +115,18 @@ function export( $args, $assoc_args ) {
/**
* Import database from a file.
*
- * @synopsis [<file>] [--str]
+ * @synopsis [<file>]
*/
function import( $args, $assoc_args ) {
$result_file = $this->get_file_name( $args );
- self::run( $assoc_args, \WP_CLI\Utils\create_cmd(
+ self::run( \WP_CLI\Utils\create_cmd(
'mysql %s --user=%s --host=%s < %s',
DB_NAME, DB_USER, DB_HOST, $result_file ) );
WP_CLI::success( sprintf( 'Imported from %s', $result_file ) );
}
- private function connect_string() {
- return \WP_CLI\Utils\create_cmd( 'mysql --host=%s --user=%s --database=%s',
- DB_HOST, DB_USER, DB_NAME );
- }
-
private function get_file_name( $args ) {
if ( empty( $args ) )
return sprintf( '%s.sql', DB_NAME );
@@ -152,19 +134,15 @@ private function get_file_name( $args ) {
return $args[0];
}
- private static function create_execute_cmd( $execute_statement ) {
- return \WP_CLI\Utils\create_cmd(
- 'mysql --host=%s --user=%s --execute=%s',
- DB_HOST, DB_USER, $execute_statement
- );
+ private static function run_query( $query ) {
+ return \WP_CLI\Utils\run_mysql_query( $query, array(
+ 'host' => DB_HOST,
+ 'user' => DB_USER,
+ 'pass' => DB_PASSWORD,
+ ) );
}
- private static function run( $assoc_args, $cmd ) {
- if ( isset( $assoc_args['str'] ) ) {
- WP_CLI::line( $cmd );
- exit;
- }
-
+ private static function run( $cmd ) {
$old_val = getenv( 'MYSQL_PWD' );
putenv( 'MYSQL_PWD=' . DB_PASSWORD );
View
14 php/utils.php
@@ -362,3 +362,17 @@ function find_subcommand( $args ) {
return $command;
}
+function run_mysql_query( $query, $args, $dry_run = false ) {
+ // TODO: use PDO?
+
+ $cmd = \WP_CLI\Utils\create_cmd( 'mysql --host=%s --user=%s --execute=%s',
+ $args['host'], $args['user'], $query
+ );
+
+ $old_val = getenv( 'MYSQL_PWD' );
+
+ putenv( 'MYSQL_PWD=' . $args['pass'] );
+ \WP_CLI::launch( $cmd );
+ putenv( 'MYSQL_PWD=' . $old_val );
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.