Skip to content

Commit d670da0

Browse files
youknowriadclaude
andauthored
Restore session variables with COALESCE in exported dumps, so split imports keep the current value (#42)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0a07a90 commit d670da0

2 files changed

Lines changed: 6 additions & 3 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 @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
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ protected function write_sql_statements( $handle ) {
115115
$this->write_insert_statements( $handle, $table_name );
116116
}
117117

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 );
118+
// Unlike mysqldump, the restore falls back to the current value: some importers, such as
119+
// Jetpack Backup's restore, run the statements over several connections, where the saved
120+
// variable is NULL and MySQL refuses to set it.
121+
fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=COALESCE(@OLD_FOREIGN_KEY_CHECKS, @@FOREIGN_KEY_CHECKS);' . PHP_EOL );
122+
fwrite( $handle, 'SET SQL_MODE=COALESCE(@OLD_SQL_MODE, @@SQL_MODE);' . PHP_EOL . PHP_EOL );
120123
fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) . PHP_EOL );
121124
}
122125

0 commit comments

Comments
 (0)