diff --git a/features/sqlite-export.feature b/features/sqlite-export.feature index b27bd62..2d35844 100644 --- a/features/sqlite-export.feature +++ b/features/sqlite-export.feature @@ -222,7 +222,7 @@ Feature: WP-CLI SQLite Export Command CONSTRAINT `fk_parent` FOREIGN KEY (`parent_id`) REFERENCES `wp_zz_parent` (`id`) """ When I run `cat test_fk_export.sql` - Then STDOUT should match /^SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\s+SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;\s+--\s+-- Table structure for table `wp_aa_child`.+-- Table structure for table `wp_zz_parent`.+SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;\s+SET SQL_MODE=@OLD_SQL_MODE;\s+-- Dump completed on \S+\n$/s + Then STDOUT should match /^SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\s+SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;\s+--\s+-- Table structure for table `wp_aa_child`.+-- Table structure for table `wp_zz_parent`.+SET FOREIGN_KEY_CHECKS=COALESCE\(@OLD_FOREIGN_KEY_CHECKS, @@FOREIGN_KEY_CHECKS\);\s+SET SQL_MODE=COALESCE\(@OLD_SQL_MODE, @@SQL_MODE\);\s+-- Dump completed on \S+\n$/s When I run `wp --exec="define( 'DB_PATH', getcwd() . '/test_fk_reimport.sqlite' );" sqlite --enable-ast-driver import test_fk_export.sql` Then STDOUT should contain: diff --git a/src/Export.php b/src/Export.php index 3e36da1..c511c9c 100644 --- a/src/Export.php +++ b/src/Export.php @@ -115,8 +115,11 @@ protected function write_sql_statements( $handle ) { $this->write_insert_statements( $handle, $table_name ); } - fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;' . PHP_EOL ); - fwrite( $handle, 'SET SQL_MODE=@OLD_SQL_MODE;' . PHP_EOL . PHP_EOL ); + // Unlike mysqldump, the restore falls back to the current value: some importers, such as + // Jetpack Backup's restore, run the statements over several connections, where the saved + // variable is NULL and MySQL refuses to set it. + fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=COALESCE(@OLD_FOREIGN_KEY_CHECKS, @@FOREIGN_KEY_CHECKS);' . PHP_EOL ); + fwrite( $handle, 'SET SQL_MODE=COALESCE(@OLD_SQL_MODE, @@SQL_MODE);' . PHP_EOL . PHP_EOL ); fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) . PHP_EOL ); }