Skip to content

Commit 65a424e

Browse files
youknowriadclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 0a07a90 commit 65a424e

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

‎features/sqlite-export.feature‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ Feature: WP-CLI SQLite Export Command
222222
CONSTRAINT `fk_parent` FOREIGN KEY (`parent_id`) REFERENCES `wp_zz_parent` (`id`)
223223
"""
224224
When I run `cat test_fk_export.sql`
225-
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
225+
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
226226

227227
When I run `wp --exec="define( 'DB_PATH', getcwd() . '/test_fk_reimport.sqlite' );" sqlite --enable-ast-driver import test_fk_export.sql`
228228
Then STDOUT should contain:

‎src/Export.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ protected function write_sql_statements( $handle ) {
8989

9090
// Like mysqldump: tables are written in SHOW TABLES order, so a foreign key can reference
9191
// a table created later in the dump, and WordPress tables use zero dates that strict
92-
// SQL modes reject.
93-
fwrite( $handle, "SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';" . PHP_EOL );
94-
fwrite( $handle, 'SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;' . PHP_EOL . PHP_EOL );
92+
// SQL modes reject. The values are literals rather than saved in user variables, because
93+
// some importers run the statements over several connections, where a saved variable
94+
// is NULL and restoring it fails.
95+
fwrite( $handle, "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';" . PHP_EOL );
96+
fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=0;' . PHP_EOL . PHP_EOL );
9597

9698
foreach ( $this->get_table_names() as $table_name ) {
9799

@@ -115,8 +117,7 @@ protected function write_sql_statements( $handle ) {
115117
$this->write_insert_statements( $handle, $table_name );
116118
}
117119

118-
fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;' . PHP_EOL );
119-
fwrite( $handle, 'SET SQL_MODE=@OLD_SQL_MODE;' . PHP_EOL . PHP_EOL );
120+
fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=1;' . PHP_EOL . PHP_EOL );
120121
fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) . PHP_EOL );
121122
}
122123

0 commit comments

Comments
 (0)