Skip to content

Commit 92c3558

Browse files
authored
Merge pull request #12 from Automattic/update/escape-hash-values-when-exporting
Fix sqlite export by escaping strings with numbers and e character
2 parents b7593cf + de17b58 commit 92c3558

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

features/bootstrap/SQLiteFeatureContext.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,30 @@ public function theFileShouldNotContain( $filename, PyStringNode $content ) {
166166
throw new Exception( "File contains unexpected content:\n" . $content );
167167
}
168168
}
169+
170+
/**
171+
* @Given /^the SQLite database contains a test table with alphanumeric string hash values$/
172+
*/
173+
public function theSqliteDatabaseContainsATestTableWithAlphanumericStringHashValues() {
174+
$this->connectToDatabase();
175+
176+
// Create a test table with hash values that look like scientific notation
177+
$this->db->exec( 'DROP TABLE IF EXISTS test_export_alphanumeric_string' );
178+
$this->db->exec(
179+
'
180+
CREATE TABLE test_export_alphanumeric_string (
181+
id INTEGER PRIMARY KEY,
182+
hash_value TEXT
183+
)
184+
'
185+
);
186+
187+
// Insert test data with values that might be mistaken for scientific notation
188+
$this->db->exec(
189+
"
190+
INSERT INTO test_export_alphanumeric_string (id, hash_value) VALUES
191+
(1, '123e99')
192+
"
193+
);
194+
}
169195
}

features/sqlite-export.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,21 @@ Feature: WP-CLI SQLite Export Command
115115
"""
116116
CREATE TABLE `wp_users`
117117
"""
118+
119+
@require-sqlite
120+
Scenario: Export should escape alphanumeric strings with e character
121+
Given the SQLite database contains a test table with alphanumeric string hash values
122+
When I run `wp sqlite export test_export_alphanumeric_string.sql --tables=test_export_alphanumeric_string`
123+
Then STDOUT should contain:
124+
"""
125+
Success: Export complete. File written to test_export_alphanumeric_string.sql
126+
"""
127+
And the file "test_export_alphanumeric_string.sql" should exist
128+
And the file "test_export_alphanumeric_string.sql" should contain:
129+
"""
130+
INSERT INTO `test_export_alphanumeric_string` VALUES (1,'123e99');
131+
"""
132+
But the file "test_export_alphanumeric_string.sql" should not contain:
133+
"""
134+
INSERT INTO `test_export_alphanumeric_string` VALUES (1,123e99);
135+
"""

src/Export.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ protected function escape_values( PDO $pdo, $values ) {
239239
foreach ( $values as $value ) {
240240
if ( is_null( $value ) ) {
241241
$escaped_values[] = 'NULL';
242-
} elseif ( is_numeric( $value ) ) {
242+
} elseif ( ctype_digit( $value ) ) {
243243
$escaped_values[] = $value;
244244
} else {
245245
// Quote the values and escape encode the newlines so the insert statement appears on a single line.

0 commit comments

Comments
 (0)