Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Imports a MySQL dump file into the SQLite database. Parses SQL statements using

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

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_*`).
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_*`).

**Parameters:**

Expand Down
17 changes: 17 additions & 0 deletions features/sqlite-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,20 @@ Feature: WP-CLI SQLite Export Command
Success: Imported from 'test_export_escape_bytes.sql'.
"""
And the SQL string escape bytes should be preserved

@require-sqlite
Scenario: Export should load into MySQL in any table order, like mysqldump
Given a SQL dump file named "test_fk_source.sql" with content:
"""
CREATE TABLE wp_aa_child (
id INTEGER PRIMARY KEY,
parent_id INTEGER NOT NULL,
CONSTRAINT fk_parent FOREIGN KEY (parent_id) REFERENCES wp_zz_parent(id)
);
INSERT INTO wp_aa_child (id, parent_id) VALUES (1, 1);
CREATE TABLE wp_zz_parent (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
INSERT INTO wp_zz_parent (id, name) VALUES (1, 'Parent Row');
"""
And I run `wp sqlite --enable-ast-driver import test_fk_source.sql`
When I run `wp sqlite --enable-ast-driver export - --tables=wp_aa_child,wp_zz_parent`
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/s
Comment thread
youknowriad marked this conversation as resolved.
Resolved
9 changes: 9 additions & 0 deletions src/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ protected function close_output_stream( $handle ) {
protected function write_sql_statements( $handle ) {
$include_tables = $this->get_include_tables();
$exclude_tables = $this->get_exclude_tables();

// 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 );

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

// Skip tables that are not in the include_tables list if the list is defined
Expand All @@ -108,6 +115,8 @@ 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, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) );
Comment thread
youknowriad marked this conversation as resolved.
Resolved
}

Expand Down
Loading