Skip to content

Commit 6239eac

Browse files
Merge remote-tracking branch 'origin/main' into drop-legacy-driver-support
# Conflicts: # AGENTS.md
2 parents b5b46dd + 1e95555 commit 6239eac

5 files changed

Lines changed: 53 additions & 6 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ jobs:
1515
with:
1616
php-version: '8.1'
1717

18-
- name: Install dependencies
19-
run: composer install --no-dev --optimize-autoloader --ignore-platform-reqs
18+
- name: Generate autoloader
19+
run: composer dump-autoload --no-dev --optimize
2020

2121
- name: Set release version
2222
id: set_version

‎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. Internal SQLite system tables are never included; the driver's `SHOW TABLES` already excludes them.
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. Internal SQLite system tables are never included; the driver's `SHOW TABLES` already excludes them.
1919

2020
**Parameters:**
2121

‎composer.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
],
2626
"require": {
2727
"php": ">=7.4",
28-
"wp-cli/wp-cli": "^2.12"
28+
"wp-cli/wp-cli": "^2.12 || ^3.0@dev"
2929
},
3030
"require-dev": {
31-
"wp-cli/wp-cli-tests": "^5.0",
31+
"wp-cli/wp-cli-tests": "^5.3.4",
3232
"phpcompatibility/php-compatibility": "^10.0@alpha",
3333
"wp-coding-standards/wpcs": "@dev"
3434
},
@@ -48,6 +48,7 @@
4848
"@behat"
4949
]
5050
},
51+
"prefer-stable": true,
5152
"config": {
5253
"allow-plugins": {
5354
"dealerdirect/phpcodesniffer-composer-installer": true,

‎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
@@ -84,6 +84,13 @@ protected function close_output_stream( $handle ) {
8484
protected function write_sql_statements( $handle ) {
8585
$include_tables = $this->get_include_tables();
8686
$exclude_tables = $this->get_exclude_tables();
87+
88+
// Like mysqldump: tables are written in SHOW TABLES order, so a foreign key can reference
89+
// a table created later in the dump, and WordPress tables use zero dates that strict
90+
// SQL modes reject.
91+
fwrite( $handle, "SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';" . PHP_EOL );
92+
fwrite( $handle, 'SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;' . PHP_EOL . PHP_EOL );
93+
8794
foreach ( $this->get_table_names() as $table_name ) {
8895

8996
// Skip tables that are not in the include_tables list if the list is defined
@@ -100,7 +107,9 @@ protected function write_sql_statements( $handle ) {
100107
$this->write_insert_statements( $handle, $table_name );
101108
}
102109

103-
fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) );
110+
fwrite( $handle, 'SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;' . PHP_EOL );
111+
fwrite( $handle, 'SET SQL_MODE=@OLD_SQL_MODE;' . PHP_EOL . PHP_EOL );
112+
fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) . PHP_EOL );
104113
}
105114

106115
/**

0 commit comments

Comments
 (0)