Skip to content

Commit 17e2438

Browse files
authored
Merge pull request #37 from wp-cli/add-example-to-export-stdout
Introduce `wp db export --stdout`
2 parents 5159191 + 4353d95 commit 17e2438

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

features/db-export.feature

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Feature: Export a WordPress database
2020
"""
2121
And the wp_cli_test.sql file should exist
2222

23-
Scenario: Exclude tables when exporting the dtabase
23+
Scenario: Exclude tables when exporting the database
2424
Given a WP install
2525

2626
When I run `wp db export wp_cli_test.sql --exclude_tables=wp_users --porcelain`
@@ -33,3 +33,24 @@ Feature: Export a WordPress database
3333
"""
3434
wp_options
3535
"""
36+
37+
Scenario: Export database to STDOUT
38+
Given a WP install
39+
40+
When I run `wp db export -`
41+
Then STDOUT should contain:
42+
"""
43+
-- MySQL dump
44+
"""
45+
46+
When I run `wp db export --stdout`
47+
Then STDOUT should contain:
48+
"""
49+
-- MySQL dump
50+
"""
51+
52+
When I try `wp db export - --stdout`
53+
Then STDERR should be:
54+
"""
55+
Error: The file name is not allowed when output mode is STDOUT.
56+
"""

src/DB_Command.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ public function query( $args, $assoc_args ) {
269269
* [--exclude_tables=<tables>]
270270
* : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
271271
*
272+
* [--stdout]
273+
* : Output database to STDOUT.
274+
*
272275
* [--porcelain]
273276
* : Output filename for the exported database.
274277
*
@@ -302,6 +305,15 @@ public function query( $args, $assoc_args ) {
302305
* $ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
303306
* Success: Exported to 'wordpress_dbase-db72bb5.sql'.
304307
*
308+
* # Export database to STDOUT.
309+
* $ wp db export --stdout
310+
* -- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)
311+
* --
312+
* -- Host: localhost Database: wpdev
313+
* -- ------------------------------------------------------
314+
* -- Server version 5.7.19
315+
* ...
316+
*
305317
* @alias dump
306318
*/
307319
public function export( $args, $assoc_args ) {
@@ -311,7 +323,19 @@ public function export( $args, $assoc_args ) {
311323
$hash = substr( md5( mt_rand() ), 0, 7 );
312324
$result_file = sprintf( '%s-%s-%s.sql', DB_NAME, date( 'Y-m-d' ), $hash );;
313325
}
314-
$stdout = ( '-' === $result_file );
326+
327+
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'stdout' ) && ! empty( $args[0] ) ) {
328+
WP_CLI::error( 'The file name is not allowed when output mode is STDOUT.' );
329+
}
330+
331+
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'stdout' ) ) {
332+
$stdout = true;
333+
} elseif ( '-' === $result_file ) {
334+
$stdout = true;
335+
} else {
336+
$stdout = false;
337+
}
338+
315339
$porcelain = \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' );
316340

317341
// Bail if both porcelain and STDOUT are set.
@@ -351,6 +375,7 @@ public function export( $args, $assoc_args ) {
351375

352376
// Remove parameters not needed for SQL run.
353377
unset( $assoc_args['porcelain'] );
378+
unset( $assoc_args['stdout'] );
354379

355380
self::run( $escaped_command, $assoc_args );
356381

0 commit comments

Comments
 (0)