Skip to content

Commit 5d7507f

Browse files
committed
Fix handling of empty lines, add test
1 parent 79f24f9 commit 5d7507f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

features/sqlite-import.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,23 @@ Feature: WP-CLI SQLite Import Command
151151
"""
152152

153153
And the SQLite database should contain a table named "a'strange`identifier\name"
154+
155+
@require-sqlite
156+
Scenario: Import a file with whitespace and empty lines
157+
Given a SQL dump file named "test_import.sql" with content:
158+
"""
159+
160+
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
161+
162+
163+
INSERT INTO test_table (name) VALUES ('Test Name');
164+
165+
"""
166+
When I run `wp sqlite --enable-ast-driver import test_import.sql`
167+
Then STDOUT should contain:
168+
"""
169+
Success: Imported from 'test_import.sql'.
170+
"""
171+
172+
And the SQLite database should contain a table named "test_table"
173+
And the "test_table" should contain a row with name "Test Name"

src/Import.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ public function parse_statements( $sql_file_path ) {
139139

140140
// Process statement end
141141
if ( ';' === $ch && null === $starting_quote ) {
142-
yield trim( $buffer );
142+
$buffer = trim( $buffer );
143+
if ( ! empty( $buffer ) ) {
144+
yield $buffer;
145+
}
143146
$buffer = '';
144147
} else {
145148
$buffer .= $ch;
@@ -148,8 +151,9 @@ public function parse_statements( $sql_file_path ) {
148151
}
149152

150153
// Handle any remaining buffer content
154+
$buffer = trim( $buffer );
151155
if ( ! empty( $buffer ) ) {
152-
yield trim( $buffer );
156+
yield $buffer;
153157
}
154158

155159
fclose( $handle );

0 commit comments

Comments
 (0)