Skip to content

Commit 9347b95

Browse files
committed
Refactor export to use a single sqlite3 process
1 parent c9965c1 commit 9347b95

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

src/DB_Command_SQLite.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -388,26 +388,28 @@ protected function sqlite_export( $file, $assoc_args ) {
388388
$drop_statements[] = sprintf( 'DROP TABLE %s;', $escaped_identifier );
389389
}
390390

391-
if ( ! empty( $drop_statements ) ) {
392-
$sql = implode( ' ', $drop_statements );
393-
$command = Utils\esc_cmd( 'sqlite3 %s %s', $temp_db, $sql );
394-
395-
WP_CLI::debug( "Running shell command: {$command}", 'db' );
391+
$init_file = tempnam( sys_get_temp_dir(), 'export_init' );
392+
$init_file = str_replace( '\\', '/', $init_file );
396393

397-
$result = \WP_CLI\Process::create( $command, null, null )->run();
398-
399-
if ( 0 !== $result->return_code ) {
400-
WP_CLI::error( 'Could not export database' );
401-
}
394+
$init_contents = '';
395+
if ( ! empty( $drop_statements ) ) {
396+
$init_contents .= implode( "\n", $drop_statements ) . "\n";
402397
}
398+
$init_contents .= ".dump\n";
399+
400+
file_put_contents( $init_file, $init_contents );
403401

404-
$command = Utils\esc_cmd( 'sqlite3 %s .dump', $temp_db );
402+
$command = Utils\esc_cmd( 'sqlite3 -init %s %s .exit', $init_file, $temp_db );
405403

406404
WP_CLI::debug( "Running shell command: {$command}", 'db' );
407405

408406
$result = \WP_CLI\Process::create( $command, null, null )->run();
407+
unlink( $init_file );
409408

410409
if ( 0 !== $result->return_code ) {
410+
if ( file_exists( $temp_db ) ) {
411+
unlink( $temp_db );
412+
}
411413
WP_CLI::error( 'Could not export database' );
412414
}
413415

0 commit comments

Comments
 (0)