Skip to content

Commit 50d4fbd

Browse files
committed
Evaluate defaults cast to BLOB when reconstructing tables
Reading the cast value as a literal recovered expressions such as CAST(1 + 1 AS BLOB) as the text '1 + 1', while SQLite stores 2. Evaluate the cast in SQLite instead, like the hex literal branch does. See: #596 (comment)
1 parent 8f37255 commit 50d4fbd

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,10 @@ private function generate_column_default( string $mysql_type, ?string $default_v
605605
}
606606

607607
// Values cast to BLOB. E.g.: CAST('abc' AS BLOB), as in binary expression defaults.
608-
if ( 1 === preg_match( '/\ACAST\((.*) AS BLOB\)\z/is', $default_value, $matches ) ) {
609-
return $this->generate_column_default( $mysql_type, $matches[1] );
608+
if ( 1 === preg_match( '/\ACAST\(.* AS BLOB\)\z/is', $default_value ) ) {
609+
// Evaluate the cast in SQLite to get the bytes that the column stores.
610+
$bytes = $this->connection->query( 'SELECT ' . $default_value )->fetchColumn();
611+
return null === $bytes ? null : $this->quote_mysql_utf8_string_literal( $bytes );
610612
}
611613

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

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,10 @@ public function testBlobCastDefaultValues(): void {
542542
CREATE TABLE t (
543543
col1 blob DEFAULT (CAST('abc' AS BLOB)),
544544
col2 blob DEFAULT (CAST(x'00ff' AS BLOB)),
545-
col3 blob DEFAULT (cast(5 as blob))
545+
col3 blob DEFAULT (cast(5 as blob)),
546+
col4 blob DEFAULT (CAST(1 + 1 AS BLOB)),
547+
col5 blob DEFAULT (CAST(1e3 AS BLOB)),
548+
col6 blob DEFAULT (CAST(NULL AS BLOB))
546549
)
547550
"
548551
);
@@ -556,7 +559,10 @@ public function testBlobCastDefaultValues(): void {
556559
'CREATE TABLE `t` (',
557560
" `col1` varbinary(65535) DEFAULT 'abc',",
558561
' `col2` varbinary(65535) DEFAULT 0x00FF,',
559-
" `col3` varbinary(65535) DEFAULT '5'",
562+
" `col3` varbinary(65535) DEFAULT '5',",
563+
" `col4` varbinary(65535) DEFAULT '2',",
564+
" `col5` varbinary(65535) DEFAULT '1000.0',",
565+
' `col6` blob DEFAULT NULL',
560566
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
561567
)
562568
),

0 commit comments

Comments
 (0)