Skip to content

Commit f563d54

Browse files
committed
Read binary defaults only in their stored hex form
The migration converts the forms that older versions stored, so the driver no longer decodes them on every read. Until the migration runs, such as on a build before the next version bump, an old default is handled as before this PR, so inserts fail rather than store the wrong bytes.
1 parent 936a4c4 commit f563d54

3 files changed

Lines changed: 33 additions & 14 deletions

File tree

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7906,8 +7906,8 @@ private function format_sqlite_column_default( array $column ): string {
79067906
}
79077907

79087908
// A BLOB column in a STRICT table only accepts a BLOB default.
7909-
if ( 'binary' === $column['DATA_TYPE'] || 'varbinary' === $column['DATA_TYPE'] ) {
7910-
$bytes = WP_SQLite_Information_Schema_Builder::get_binary_default_bytes( $column['COLUMN_DEFAULT'] );
7909+
$bytes = $this->get_binary_column_default_bytes( $column );
7910+
if ( null !== $bytes ) {
79117911
return sprintf( "x'%s'", bin2hex( $bytes ) );
79127912
}
79137913

@@ -7926,18 +7926,38 @@ private function format_mysql_column_default( array $column ): string {
79267926
return $column['COLUMN_DEFAULT'];
79277927
}
79287928

7929-
// Like MySQL, use a hex literal for bytes that aren't valid utf8mb3,
7929+
// Like MySQL, keep the hex literal for bytes that aren't valid utf8mb3,
79307930
// i.e., UTF-8 with no characters above U+FFFF.
7931-
if ( 'binary' === $column['DATA_TYPE'] || 'varbinary' === $column['DATA_TYPE'] ) {
7932-
$bytes = WP_SQLite_Information_Schema_Builder::get_binary_default_bytes( $column['COLUMN_DEFAULT'] );
7931+
$bytes = $this->get_binary_column_default_bytes( $column );
7932+
if ( null !== $bytes ) {
79337933
return 1 === preg_match( '/\A[\x{0}-\x{FFFF}]*\z/u', $bytes )
79347934
? $this->quote_mysql_utf8_string_literal( $bytes )
7935-
: '0x' . strtoupper( bin2hex( $bytes ) );
7935+
: $column['COLUMN_DEFAULT'];
79367936
}
79377937

79387938
return $this->quote_mysql_utf8_string_literal( $column['COLUMN_DEFAULT'] );
79397939
}
79407940

7941+
/**
7942+
* Get the bytes of a BINARY or VARBINARY column default.
7943+
*
7944+
* These defaults are stored as "0x..." hex literals, or as an empty string.
7945+
* The forms that older driver versions stored are migrated to this format.
7946+
*
7947+
* @param array $column The information schema column data.
7948+
* @return string|null The default value bytes, or null when the column
7949+
* is not binary or its default is not migrated.
7950+
*/
7951+
private function get_binary_column_default_bytes( array $column ): ?string {
7952+
if (
7953+
( 'binary' !== $column['DATA_TYPE'] && 'varbinary' !== $column['DATA_TYPE'] )
7954+
|| 1 !== preg_match( '/\A(?:0x((?:[0-9A-F]{2})+))?\z/', $column['COLUMN_DEFAULT'], $matches )
7955+
) {
7956+
return null;
7957+
}
7958+
return hex2bin( $matches[1] ?? '' );
7959+
}
7960+
79417961
/**
79427962
* Unquote a quoted SQLite identifier.
79437963
*

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -879,13 +879,12 @@ public function record_drop_index( WP_Parser_Node $node ): void {
879879
}
880880

881881
/**
882-
* Get the bytes of a BINARY or VARBINARY column default.
882+
* Get the bytes of a BINARY or VARBINARY default from a hex or bit literal.
883883
*
884-
* Defaults are stored as "0x..." hex literals. In tables from older versions,
885-
* they can also be any other hex or bit literal as written, or the bytes of
886-
* a text default.
884+
* Older driver versions stored these defaults as written, so this also reads
885+
* any other value as the bytes of a text default, for their migration.
887886
*
888-
* @param string $default_value A stored default, or a hex or bit literal.
887+
* @param string $default_value A hex or bit literal, or a default stored by an older version.
889888
* @return string The default value bytes.
890889
*/
891890
public static function get_binary_default_bytes( string $default_value ): string {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,14 +948,14 @@ public function testBinaryColumnDefaultsFromOlderVersions(
948948
);
949949
$statement->execute( array( $stored_default ) );
950950

951+
// The driver upgrade migrates the default and recreates the table.
952+
$this->reconnectAfterDriverUpgrade();
953+
951954
$this->assertQuery( 'SHOW CREATE TABLE t' );
952955
$this->assertStringContainsString(
953956
"`v` varbinary(16) NOT NULL DEFAULT $expected_create_default,",
954957
$this->last_result[0]->{'Create Table'}
955958
);
956-
957-
// A table rebuild writes the column default to SQLite as bytes.
958-
$this->assertQuery( 'ALTER TABLE t MODIFY COLUMN id BIGINT' );
959959
$this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
960960
$this->assertQuery( 'SELECT HEX(v) AS v FROM t' );
961961
$this->assertSame( $expected_value, $this->last_result[0]->v );

0 commit comments

Comments
 (0)