Skip to content

`wp core init-tests` should create the database #374

Merged
merged 3 commits into from Apr 4, 2013
View
4 man-src/db.txt
@@ -1,9 +1,5 @@
## OPTIONS
-* `--str`:
-
- Show the mysql command, instead of executing it.
-
* `--yes`:
Answer yes to the confirmation message.
View
18 man/db.1
@@ -7,31 +7,31 @@
\fBwp\-db\fR \- Perform basic database operations\.
.
.SH "SYNOPSIS"
-wp db create [\-\-str]
+wp db create
.
.P
-wp db drop [\-\-yes] [\-\-str]
+wp db drop [\-\-yes]
.
.P
-wp db reset [\-\-yes] [\-\-str]
+wp db reset [\-\-yes]
.
.P
-wp db optimize [\-\-str]
+wp db optimize
.
.P
-wp db repair [\-\-str]
+wp db repair
.
.P
-wp db connect [\-\-str]
+wp db connect
.
.P
-wp db query \fIsql\fR [\-\-str]
+wp db query \fIsql\fR
.
.P
-wp db export [\fIfile\fR] [\-\-str]
+wp db export [\fIfile\fR]
.
.P
-wp db import [\fIfile\fR] [\-\-str]
+wp db import [\fIfile\fR]
.
.SH "SUBCOMMANDS"
.
View
17 php/commands/core.php
@@ -308,15 +308,30 @@ function init_tests( $args, $assoc_args ) {
else
$tests_dir = ABSPATH . 'unit-tests/';
+ $assoc_args = wp_parse_args( $assoc_args, array(
+ 'dbpass' => '',
+ ) );
+
+ // Download the test suite
WP_CLI::launch( 'svn co https://unit-test.svn.wordpress.org/trunk/ ' . escapeshellarg( $tests_dir ) );
+ // Create the database
+ $query = sprintf( 'CREATE DATABASE IF NOT EXISTS `%s`', $assoc_args['dbname'] );
+
+ \WP_CLI\Utils\run_mysql_query( $query, array(
+ 'host' => 'localhost',
+ 'user' => $assoc_args['dbuser'],
+ 'pass' => $assoc_args['dbpass'],
+ ) );
+
+ // Create the wp-tests-config.php file
$config_file = file_get_contents( $tests_dir . 'wp-tests-config-sample.php' );
$replacements = array(
"dirname( __FILE__ ) . '/wordpress/'" => "'" . ABSPATH . "'",
"yourdbnamehere" => $assoc_args['dbname'],
"yourusernamehere" => $assoc_args['dbuser'],
- "yourpasswordhere" => isset( $assoc_args['dbpass'] ) ? $assoc_args['dbpass'] : ''
+ "yourpasswordhere" => $assoc_args['dbpass'],
);
$config_file = str_replace( array_keys( $replacements ), array_values( $replacements ), $config_file );
View
105 php/commands/db.php
@@ -9,62 +9,45 @@ class DB_Command extends WP_CLI_Command {
/**
* Create the database, as specified in wp-config.php
- *
- * @synopsis [--str]
*/
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." );
}
/**
* 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]
*/
- 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
) );
@@ -74,14 +57,11 @@ function optimize( $_, $assoc_args ) {
/**
* Repair the database.
- *
- * @synopsis [--str]
*/
- 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." );
}
@@ -90,35 +70,35 @@ function repair( $_, $assoc_args ) {
* Open a mysql console using the WordPress credentials.
*
* @alias cli
- *
- * @synopsis [--str]
*/
- 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,43 +108,34 @@ 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 );
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 );
+}
+
Something went wrong with that request. Please try again.