Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/sqlite-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions src/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down