diff --git a/features/db-export.feature b/features/db-export.feature index 932e12ed..11354f75 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -43,6 +43,7 @@ Feature: Export a WordPress database -- Dump completed on """ + @require-mysql Scenario: Export database with mysql defaults to STDOUT Given a WP install @@ -52,6 +53,7 @@ Feature: Export a WordPress database -- Dump completed on """ + @require-mysql Scenario: Export database with mysql --no-defaults to STDOUT Given a WP install @@ -61,6 +63,7 @@ Feature: Export a WordPress database -- Dump completed on """ + @require-mysql Scenario: Export database with passed-in options Given a WP install @@ -78,6 +81,7 @@ Feature: Export a WordPress database """ And STDOUT should be empty + @require-mysql Scenario: MySQL defaults are available as appropriate with --defaults flag Given a WP install diff --git a/features/db-import.feature b/features/db-import.feature index 83f0bf3b..b06344cb 100644 --- a/features/db-import.feature +++ b/features/db-import.feature @@ -12,6 +12,7 @@ Feature: Import a WordPress database Success: Imported from 'wp_cli_test.sql'. """ + @require-mysql Scenario: Import from database name path by default with mysql defaults Given a WP install @@ -24,6 +25,7 @@ Feature: Import a WordPress database Success: Imported from 'wp_cli_test.sql'. """ + @require-mysql Scenario: Import from database name path by default with --no-defaults Given a WP install @@ -36,27 +38,19 @@ Feature: Import a WordPress database Success: Imported from 'wp_cli_test.sql'. """ -# Scenario: Import from STDIN -# Given a WP install -# -# When I run `wp db import -` -# Then STDOUT should be: -# """ -# Success: Imported from 'STDIN'. -# """ + Scenario: Import from STDIN + Given a WP install - Scenario: Test plugin - Given a WP install - And these installed and active plugins: - """ - sqlite-database-integration - """ - When I run `wp plugin list` - Then STDOUT should contain: - """ - sqlite-database-integration - """ + When I run `wp db export wp_cli_test.sql` + Then the wp_cli_test.sql file should exist + When I run `cat wp_cli_test.sql | wp db import -` + Then STDOUT should be: + """ + Success: Imported from 'STDIN'. + """ + + @require-mysql Scenario: Import from database name path by default and skip speed optimization Given a WP install @@ -68,7 +62,7 @@ Feature: Import a WordPress database """ Success: Imported from 'wp_cli_test.sql'. """ - + @require-mysql Scenario: Import from database name path by default with passed-in dbuser/dbpass Given a WP install @@ -103,6 +97,7 @@ Feature: Import a WordPress database Success: Imported from 'debug.sql'. """ + @require-mysql Scenario: Help runs properly at various points of a functional WP install Given an empty directory @@ -140,6 +135,7 @@ Feature: Import a WordPress database """ wp db import """ + @require-mysql Scenario: MySQL defaults are available as appropriate with --defaults flag Given a WP install @@ -164,7 +160,7 @@ Feature: Import a WordPress database Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash """ - @require-wp-4.2 + @require-wp-4.2 @require-mysql Scenario: Import db that has emoji in post Given a WP install diff --git a/features/db-query.feature b/features/db-query.feature index 627ef534..f3d6bec9 100644 --- a/features/db-query.feature +++ b/features/db-query.feature @@ -75,23 +75,23 @@ Feature: Query the database with WordPress' MySQL config Scenario: MySQL defaults are available as appropriate with --defaults flag Given a WP install - When I try `wp db query --defaults --debug` + When I try `"select 1" | wp db query --defaults --debug` Then STDERR should contain: """ Debug (db): Running shell command: /usr/bin/env mysql --no-auto-rehash """ - When I try `wp db query --debug` + When I try `"select 1" | wp db query --debug` Then STDERR should contain: """ Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash """ - When I try `wp db query --no-defaults --debug` + When I try `"select 1" | wp db query --no-defaults --debug` Then STDERR should contain: """ Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash - """ + """ Scenario: SQL modes do not include any of the modes incompatible with WordPress Given a WP install diff --git a/src/DB_Command.php b/src/DB_Command.php index 65149248..9af41d2c 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1,7 +1,7 @@ run( $result_file, $assoc_args ); - return; - } - $stdout = ( '-' === $result_file ); $porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' ); @@ -614,6 +607,13 @@ public function export( $args, $assoc_args ) { WP_CLI::error( 'Porcelain is not allowed when output mode is STDOUT.' ); } + // Check if SQLite is enabled and use it if it is. + if ( Export::get_sqlite_version() ) { + $export = new Export(); + $export->run( $result_file, $assoc_args ); + return; + } + if ( ! $stdout ) { $assoc_args['result-file'] = $result_file; } @@ -773,9 +773,9 @@ public function import( $args, $assoc_args ) { } // Check if SQLite is enabled and use it if it is. - if ( WP_SQLite_Import::get_sqlite_version() ) { - $importer = new WP_SQLite_Import(); - $importer->run( $result_file, $args ); + if ( Import::get_sqlite_version() ) { + $importer = new Import(); + $importer->run( $result_file, $assoc_args ); return; } diff --git a/src/WP_SQLite_Base.php b/src/SQLite/Base.php similarity index 98% rename from src/WP_SQLite_Base.php rename to src/SQLite/Base.php index 94e725b0..0f249627 100644 --- a/src/WP_SQLite_Base.php +++ b/src/SQLite/Base.php @@ -1,7 +1,7 @@ check_arguments( $args ); $this->load_dependencies(); + $is_stdout = '-' === $result_file; $exclude_tables = isset( $args['exclude_tables'] ) ? explode( ',', $args['exclude_tables'] ) : []; $exclude_tables = array_merge( @@ -43,7 +44,7 @@ public function run( $result_file, $args ) { $include_tables = isset( $args['tables'] ) ? explode( ',', $args['tables'] ) : []; $translator = new WP_SQLite_Translator(); - $handle = fopen( $result_file, 'w' ); + $handle = $is_stdout ? fopen( 'php://stdout', 'w' ) : fopen( $result_file, 'w' ); foreach ( $translator->query( 'SHOW TABLES' ) as $table ) { @@ -65,13 +66,20 @@ public function run( $result_file, $args ) { } } + fwrite( $handle, sprintf( '-- Dump completed on %s', date('c') ) ); + fclose( $handle ); + + if ( $is_stdout ) { + return; + } + if ( isset( $args['porcelain'] ) ) { WP_CLI::line( $result_file ); } else { - WP_CLI::line( 'Export complete. File written to ' . $result_file ); + WP_CLI::success( 'Export complete. File written to ' . $result_file ); } - fclose( $handle ); + } protected function get_create_statement( $table, $translator ) { diff --git a/src/WP_SQLite_Import.php b/src/SQLite/Import.php similarity index 83% rename from src/WP_SQLite_Import.php rename to src/SQLite/Import.php index eddf6b7b..44538158 100644 --- a/src/WP_SQLite_Import.php +++ b/src/SQLite/Import.php @@ -1,17 +1,19 @@ check_arguments( $args ); $this->load_dependencies(); $translator = new WP_SQLite_Translator(); - foreach ( $this->parse_statements( $sql_file_path ) as $statement ) { + + $is_stdin = '-' === $sql_file_path; + $import_file = $is_stdin ? 'php://stdin' : $sql_file_path; + + foreach ( $this->parse_statements( $import_file ) as $statement ) { $result = $translator->query( $statement ); if ( false === $result ) { WP_CLI::warning( 'Could not execute statement: ' . $statement ); } } - WP_CLI::success( sprintf("Imported from '%s'.", $sql_file_path ) ); + $imported_from = $is_stdin ? 'STDIN' : $sql_file_path; + WP_CLI::success( sprintf("Imported from '%s'.", $imported_from ) ); } /** @@ -41,9 +48,11 @@ public function run( $sql_file_path, $args ) { * @throws Exception */ public function parse_statements( $sql_file_path ) { + $handle = fopen( $sql_file_path, 'r' ); + if ( ! $handle ) { - throw new Exception( "Unable to open file: $sql_file_path" ); + WP_CLI::error( "Unable to open file: $sql_file_path" ); } $single_quotes = 0; diff --git a/src/noop.php b/src/SQLite/noop.php similarity index 100% rename from src/noop.php rename to src/SQLite/noop.php