Skip to content

Commit 1e95555

Browse files
authored
Disable foreign key checks and strict SQL modes in exported dumps (#40)
1 parent 2946261 commit 1e95555

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Imports a MySQL dump file into the SQLite database. Parses SQL statements using
1515

1616
### `wp sqlite export [<file>]`
1717

18-
Exports the SQLite database to a MySQL-compatible SQL dump file. Generates `DROP TABLE IF EXISTS`, `CREATE TABLE`, and `INSERT INTO` statements for each table. Automatically excludes internal SQLite system tables (`_mysql_data_types_cache`, `sqlite_master`, `sqlite_sequence`, `_wp_sqlite_*`).
18+
Exports the SQLite database to a MySQL-compatible SQL dump file. Generates `DROP TABLE IF EXISTS`, `CREATE TABLE`, and `INSERT INTO` statements for each table, wrapped, like `mysqldump`, in statements that disable foreign key checks and set `SQL_MODE` to `NO_AUTO_VALUE_ON_ZERO` while the dump loads. Automatically excludes internal SQLite system tables (`_mysql_data_types_cache`, `sqlite_master`, `sqlite_sequence`, `_wp_sqlite_*`).
1919

2020
**Parameters:**
2121

‎features/sqlite-export.feature‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,40 @@ Feature: WP-CLI SQLite Export Command
201201
Success: Imported from 'test_export_escape_bytes.sql'.
202202
"""
203203
And the SQL string escape bytes should be preserved
204+
205+
@require-sqlite
206+
Scenario: Export should load into MySQL in any table order, like mysqldump
207+
Given a SQL dump file named "test_fk_source.sql" with content:
208+
"""
209+
CREATE TABLE wp_aa_child (
210+
id INTEGER PRIMARY KEY,
211+
parent_id INTEGER NOT NULL,
212+
CONSTRAINT fk_parent FOREIGN KEY (parent_id) REFERENCES wp_zz_parent(id)
213+
);
214+
INSERT INTO wp_aa_child (id, parent_id) VALUES (1, 1);
215+
CREATE TABLE wp_zz_parent (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
216+
INSERT INTO wp_zz_parent (id, name) VALUES (1, 'Parent Row');
217+
"""
218+
And I run `wp sqlite --enable-ast-driver import test_fk_source.sql`
219+
When I run `wp sqlite --enable-ast-driver export test_fk_export.sql --tables=wp_aa_child,wp_zz_parent`
220+
Then the file "test_fk_export.sql" should contain:
221+
"""
222+
CONSTRAINT `fk_parent` FOREIGN KEY (`parent_id`) REFERENCES `wp_zz_parent` (`id`)
223+
"""
224+
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
226+
227+
When I run `wp --exec="define( 'DB_PATH', getcwd() . '/test_fk_reimport.sqlite' );" sqlite --enable-ast-driver import test_fk_export.sql`
228+
Then STDOUT should contain:
229+
"""
230+
Success: Imported from 'test_fk_export.sql'.
231+
"""
232+
When I run `wp --exec="define( 'DB_PATH', getcwd() . '/test_fk_reimport.sqlite' );" sqlite --enable-ast-driver export - --tables=wp_aa_child,wp_zz_parent`
233+
Then STDOUT should contain:
234+
"""
235+
INSERT INTO `wp_aa_child` VALUES (1,1);
236+
"""
237+
And STDOUT should contain:
238+
"""
239+
INSERT INTO `wp_zz_parent` VALUES (1,'Parent Row');
240+
"""

‎src/Export.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ protected function close_output_stream( $handle ) {
8686
protected function write_sql_statements( $handle ) {
8787
$include_tables = $this->get_include_tables();
8888
$exclude_tables = $this->get_exclude_tables();
89+
90+
// Like mysqldump: tables are written in SHOW TABLES order, so a foreign key can reference
91+
// 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 );
95+
8996
foreach ( $this->get_table_names() as $table_name ) {
9097

9198
// Skip tables that are not in the include_tables list if the list is defined
@@ -108,7 +115,9 @@ protected function write_sql_statements( $handle ) {
108115
$this->write_insert_statements( $handle, $table_name );
109116
}
110117

111-
fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) );
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, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) . PHP_EOL );
112121
}
113122

114123
/**

0 commit comments

Comments
 (0)