Skip to content

Commit 5ddbb28

Browse files
committed
Escape blob literal defaults when reconstructing tables
The reconstructor wrapped the bytes of a blob literal default in quotes as they were. A quote failed to parse, and a backslash changed the value. BINARY and VARBINARY defaults are now blob literals in SQLite, so this broke recovering any such default with these bytes.
1 parent 88dfafa commit 5ddbb28

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-reconstructor.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ private function generate_column_default( string $mysql_type, ?string $default_v
600600
// BLOB literals (string). E.g.: x'1a2f', X'1A2F'
601601
// Checking the prefix is enough as SQLite doesn't allow malformed values.
602602
if ( str_starts_with( $uppercase_default_value, "X'" ) ) {
603-
// Convert the hex string to ASCII bytes.
604-
return "'" . pack( 'H*', substr( $default_value, 2, -1 ) ) . "'";
603+
// Convert the hex string to bytes, and quote them as a string literal.
604+
return $this->quote_mysql_utf8_string_literal( pack( 'H*', substr( $default_value, 2, -1 ) ) );
605605
}
606606

607607
// Unquoted string literal. E.g.: abc

‎packages/mysql-on-sqlite/tests/WP_SQLite_Information_Schema_Reconstructor_Tests.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,38 @@ public function testDefaultValueEscaping(): void {
504504
);
505505
}
506506

507+
public function testBlobDefaultValueEscaping(): void {
508+
$this->engine->get_connection()->query(
509+
"
510+
CREATE TABLE t (
511+
col1 blob DEFAULT x'69742773', -- it's
512+
col2 blob DEFAULT x'615c6e62', -- a\\nb
513+
col3 blob DEFAULT x'610a62', -- a, newline, b
514+
col4 blob DEFAULT x'00ff', -- not UTF-8
515+
last blob -- See the comment in testDefaultValueEscaping().
516+
)
517+
"
518+
);
519+
520+
$this->reconstructor->ensure_correct_information_schema();
521+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
522+
$this->assertSame(
523+
implode(
524+
"\n",
525+
array(
526+
'CREATE TABLE `t` (',
527+
" `col1` varbinary(65535) DEFAULT 'it''s',",
528+
" `col2` varbinary(65535) DEFAULT 'a\\\\nb',",
529+
" `col3` varbinary(65535) DEFAULT 'a\\nb',",
530+
' `col4` varbinary(65535) DEFAULT 0x00FF,',
531+
' `last` blob DEFAULT NULL',
532+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
533+
)
534+
),
535+
$result[0]->{'Create Table'}
536+
);
537+
}
538+
507539
public function testInvalidDataTypeCacheDataForDecimalDefinition(): void {
508540
// Recreate the invalid database state before the following fix:
509541
// https://github.com/WordPress/sqlite-database-integration/pull/126

0 commit comments

Comments
 (0)