From 65a424e95b670718fb153c6c84ad3de7162ac810 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 24 Nov 2026 16:43:18 +0100 Subject: [PATCH 1/2] Set session variables in exported dumps without saving the old values, so importers that split statements across connections don't restore NULL Co-Authored-By: Claude Opus 5.5 --- features/sqlite-export.feature | 2 +- src/Export.php | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/features/sqlite-export.feature b/features/sqlite-export.feature index b27bd62..e7b9689 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 SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\s+SET FOREIGN_KEY_CHECKS=0;\s+--\s+-- Table structure for table `wp_aa_child`.+-- Table structure for table `wp_zz_parent`.+SET FOREIGN_KEY_CHECKS=1;\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..c21f593 100644 --- a/src/Export.php +++ b/src/Export.php @@ -89,9 +89,11 @@ protected function write_sql_statements( $handle ) { // Like mysqldump: tables are written in SHOW TABLES order, so a foreign key can reference // a table created later in the dump, and WordPress tables use zero dates that strict - // SQL modes reject. - fwrite( $handle, "SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';" . PHP_EOL ); - fwrite( $handle, 'SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;' . PHP_EOL . PHP_EOL ); + // SQL modes reject. The values are literals rather than saved in user variables, because + // some importers run the statements over several connections, where a saved variable + // is NULL and restoring it fails. + fwrite( $handle, "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';" . PHP_EOL ); + fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=0;' . PHP_EOL . PHP_EOL ); foreach ( $this->get_table_names() as $table_name ) { @@ -115,8 +117,7 @@ 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 ); + fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=1;' . PHP_EOL . PHP_EOL ); fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) . PHP_EOL ); } From 5c91637b93b3eda58c80b849e7e847a72f3c1a38 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 25 Nov 2026 10:39:20 +0100 Subject: [PATCH 2/2] Restore session variables with COALESCE, so importers that split statements across connections keep the current value Co-Authored-By: Claude Opus 5.5 --- features/sqlite-export.feature | 2 +- src/Export.php | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/features/sqlite-export.feature b/features/sqlite-export.feature index e7b9689..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 SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\s+SET FOREIGN_KEY_CHECKS=0;\s+--\s+-- Table structure for table `wp_aa_child`.+-- Table structure for table `wp_zz_parent`.+SET FOREIGN_KEY_CHECKS=1;\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 c21f593..c511c9c 100644 --- a/src/Export.php +++ b/src/Export.php @@ -89,11 +89,9 @@ protected function write_sql_statements( $handle ) { // Like mysqldump: tables are written in SHOW TABLES order, so a foreign key can reference // a table created later in the dump, and WordPress tables use zero dates that strict - // SQL modes reject. The values are literals rather than saved in user variables, because - // some importers run the statements over several connections, where a saved variable - // is NULL and restoring it fails. - fwrite( $handle, "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';" . PHP_EOL ); - fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=0;' . PHP_EOL . PHP_EOL ); + // SQL modes reject. + fwrite( $handle, "SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';" . PHP_EOL ); + fwrite( $handle, 'SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;' . PHP_EOL . PHP_EOL ); foreach ( $this->get_table_names() as $table_name ) { @@ -117,7 +115,11 @@ protected function write_sql_statements( $handle ) { $this->write_insert_statements( $handle, $table_name ); } - fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=1;' . 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 ); }