Skip to content

Commit 659be05

Browse files
authored
Normalize BIT column default literals (#439)
## What BIT column defaults were stored and rendered incorrectly. A column such as `BIT(1) DEFAULT '0'` (e.g., as produced by Ninja Forms) emitted an invalid `SHOW CREATE TABLE` / dump line: ```sql `maintenance` bit(1) DEFAULT '0' ``` MySQL does not accept a quoted-string default on a BIT column—the value has to be a bit literal (`b'0'`) or a number (`0`). The invalid output broke dump/restore flows (e.g., Studio → Pressable). ## Fix Normalize BIT column default literals end to end: - **Store:** Record BIT defaults in the information schema in a normalized format (`b'...'`). - **`SHOW CREATE TABLE`:** Emit the stored bit literal verbatim (`bit(1) DEFAULT b'0'`). - **Internal SQLite DDL / `column = DEFAULT`:** Render the bit literal as the SQLite integer it maps to. - **Legacy migration:** The information-schema reconstructor converts integer BIT defaults into a bit literal. ## Tests - `SHOW CREATE TABLE` output **and** the inserted/read-back values for every default form (quoted string, integer, `b'…'`, `0b…`, `x'…'`, `TRUE`, `FALSE`). - `UPDATE … SET col = DEFAULT` on a BIT column. - Reconstruction of BIT defaults for legacy databases. - Full unit suite green; coding standards (PHPCS) pass.
1 parent dcd64c7 commit 659be05

5 files changed

Lines changed: 213 additions & 22 deletions

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5605,7 +5605,7 @@ private function translate_update_list( string $table_name, WP_Parser_Node $pare
56055605
// Get the UPDATE value. It's either an expression or a DEFAULT keyword.
56065606
if ( null === $expr ) {
56075607
// Emulate "column = DEFAULT".
5608-
$value = null === $default ? 'NULL' : $this->quote_sqlite_value( $default );
5608+
$value = null === $default ? 'NULL' : $this->format_sqlite_column_default( $column_info );
56095609
} else {
56105610
$value = $this->translate( $expr );
56115611
}
@@ -6317,7 +6317,7 @@ private function get_sqlite_create_table_statement(
63176317
$default_clause = $this->translate( $expr );
63186318
$query .= sprintf( ' DEFAULT (%s)', $default_clause );
63196319
} else {
6320-
$query .= ' DEFAULT ' . $this->quote_sqlite_value( $column['COLUMN_DEFAULT'] );
6320+
$query .= ' DEFAULT ' . $this->format_sqlite_column_default( $column );
63216321
}
63226322
}
63236323
$rows[] = $query;
@@ -6624,7 +6624,7 @@ private function get_mysql_create_table_statement( bool $table_is_temporary, str
66246624
if ( str_contains( $column['EXTRA'], 'DEFAULT_GENERATED' ) ) {
66256625
$sql .= sprintf( ' DEFAULT (%s)', $column['COLUMN_DEFAULT'] );
66266626
} else {
6627-
$sql .= ' DEFAULT ' . $this->quote_mysql_utf8_string_literal( $column['COLUMN_DEFAULT'] );
6627+
$sql .= ' DEFAULT ' . $this->format_mysql_column_default( $column );
66286628
}
66296629
} elseif ( 'YES' === $column['IS_NULLABLE'] ) {
66306630
$sql .= ' DEFAULT NULL';
@@ -6838,6 +6838,36 @@ private function get_column_on_update_trigger_query( string $table, string $colu
68386838
);
68396839
}
68406840

6841+
/**
6842+
* Format a column default value as a literal for an SQLite CREATE TABLE statement.
6843+
*
6844+
* @param array $column The information schema column data.
6845+
* @return string The default value as an SQLite literal.
6846+
*/
6847+
private function format_sqlite_column_default( array $column ): string {
6848+
if ( 'bit' === $column['DATA_TYPE'] ) {
6849+
// BIT defaults are stored as b'...' literals; reformat as an integer.
6850+
return (string) bindec( substr( $column['COLUMN_DEFAULT'], 2, -1 ) );
6851+
}
6852+
6853+
return $this->quote_sqlite_value( $column['COLUMN_DEFAULT'] );
6854+
}
6855+
6856+
/**
6857+
* Format a column default value as a literal for a MySQL SHOW CREATE TABLE statement.
6858+
*
6859+
* @param array $column The information schema column data.
6860+
* @return string The default value as a MySQL literal.
6861+
*/
6862+
private function format_mysql_column_default( array $column ): string {
6863+
// BIT defaults are already stored as b'...' literals.
6864+
if ( 'bit' === $column['DATA_TYPE'] ) {
6865+
return $column['COLUMN_DEFAULT'];
6866+
}
6867+
6868+
return $this->quote_mysql_utf8_string_literal( $column['COLUMN_DEFAULT'] );
6869+
}
6870+
68416871
/**
68426872
* Unquote a quoted SQLite identifier.
68436873
*

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

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,13 +1503,14 @@ private function record_drop_check_constraint(
15031503
* @return array Column data for the information schema.
15041504
*/
15051505
private function extract_column_data( string $table_name, string $column_name, WP_Parser_Node $node, int $position ): array {
1506-
$default = $this->get_column_default( $node );
1506+
list ( $data_type, $column_type ) = $this->get_column_data_types( $node );
1507+
1508+
$default = $this->get_column_default( $node, $data_type );
15071509
$nullable = $this->get_column_nullable( $node );
15081510
$key = $this->get_column_key( $node );
15091511
$extra = $this->get_column_extra( $node );
15101512
$comment = $this->get_column_comment( $node );
15111513

1512-
list ( $data_type, $column_type ) = $this->get_column_data_types( $node );
15131514
list ( $charset, $collation ) = $this->get_column_charset_and_collation( $node, $data_type );
15141515
list ( $char_length, $octet_length ) = $this->get_column_lengths( $node, $data_type, $charset );
15151516
list ( $precision, $scale ) = $this->get_column_numeric_attributes( $node, $data_type );
@@ -2040,10 +2041,11 @@ private function get_table_comment( WP_Parser_Node $node ): string {
20402041
/**
20412042
* Extract column default value from the "columnDefinition" or "fieldDefinition" AST node.
20422043
*
2043-
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2044-
* @return string The column default as stored in information schema.
2044+
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2045+
* @param string $data_type The column data type as stored in information schema.
2046+
* @return string|null The column default as stored in information schema.
20452047
*/
2046-
private function get_column_default( WP_Parser_Node $node ): ?string {
2048+
private function get_column_default( WP_Parser_Node $node, string $data_type ): ?string {
20472049
$default_attr = null;
20482050
foreach ( $node->get_descendant_nodes( 'columnAttribute' ) as $attr ) {
20492051
if ( $attr->has_child_token( WP_MySQL_Lexer::DEFAULT_SYMBOL ) ) {
@@ -2073,21 +2075,11 @@ private function get_column_default( WP_Parser_Node $node ): ?string {
20732075
$signed_literal = $default_attr->get_first_child_node( 'signedLiteral' );
20742076
if ( $signed_literal ) {
20752077
$literal = $signed_literal->get_first_child_node( 'literal' );
2076-
2077-
// DEFAULT NULL
2078-
if ( $literal && $literal->has_child_node( 'nullLiteral' ) ) {
2079-
return null;
2080-
}
2081-
2082-
// DEFAULT TRUE or DEFAULT FALSE
2083-
if ( $literal && $literal->has_child_node( 'boolLiteral' ) ) {
2084-
$bool_literal = $literal->get_first_child_node( 'boolLiteral' );
2085-
return $bool_literal->has_child_token( WP_MySQL_Lexer::TRUE_SYMBOL ) ? '1' : '0';
2078+
if ( null === $literal ) {
2079+
// A signed number, such as "-5", has no "literal" child node.
2080+
return $this->get_value( $signed_literal );
20862081
}
2087-
2088-
// @TODO: MySQL seems to normalize default values for numeric
2089-
// columns, such as 1.0 to 1, 1e3 to 1000, etc.
2090-
return $this->get_value( $signed_literal );
2082+
return $this->get_literal_default( $literal, $data_type );
20912083
}
20922084

20932085
// DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
@@ -2099,6 +2091,74 @@ private function get_column_default( WP_Parser_Node $node ): ?string {
20992091
throw new Exception( 'DEFAULT value of this type is not supported.' );
21002092
}
21012093

2094+
/**
2095+
* Extract and normalize a literal default value.
2096+
*
2097+
* @param WP_Parser_Node $literal The "literal" AST node.
2098+
* @param string $data_type The column data type as stored in information schema.
2099+
* @return string|null The default value as stored in information schema.
2100+
*/
2101+
private function get_literal_default( WP_Parser_Node $literal, string $data_type ): ?string {
2102+
// DEFAULT NULL
2103+
if ( $literal->has_child_node( 'nullLiteral' ) ) {
2104+
return null;
2105+
}
2106+
2107+
// DEFAULT TRUE or DEFAULT FALSE
2108+
if ( $literal->has_child_node( 'boolLiteral' ) ) {
2109+
$bool_literal = $literal->get_first_child_node( 'boolLiteral' );
2110+
$bool_value = $bool_literal->has_child_token( WP_MySQL_Lexer::TRUE_SYMBOL ) ? '1' : '0';
2111+
return 'bit' === $data_type ? "b'{$bool_value}'" : $bool_value;
2112+
}
2113+
2114+
$default = $this->get_value( $literal );
2115+
2116+
if ( 'bit' === $data_type ) {
2117+
return $this->get_bit_default( $default ) ?? $default;
2118+
}
2119+
2120+
/*
2121+
* @TODO: Non-BIT literal defaults are currently stored verbatim. To
2122+
* match MySQL, they should be normalized. For example:
2123+
* - BINARY/VARBINARY defaults stored as 0x hex literals.
2124+
* - Hex and bit literals coerced to raw bytes for text columns and to
2125+
* their decimal value for numeric columns.
2126+
* - The charset introducer dropped from text-literal defaults.
2127+
* - Numeric defaults normalized, e.g. 1.0 to 1 or 1e3 to 1000.
2128+
*/
2129+
return $default;
2130+
}
2131+
2132+
/**
2133+
* Normalize a BIT default value to a MySQL bit literal.
2134+
*
2135+
* The value may be a bit literal, a hex literal, or a decimal.
2136+
*
2137+
* @param string $default_value The BIT default value.
2138+
* @return string|null The default as a bit literal, e.g. "b'101'", or null when not a bit value.
2139+
*/
2140+
private function get_bit_default( string $default_value ): ?string {
2141+
$value = strtolower( $default_value );
2142+
2143+
// Bit literal, e.g. b'101' or 0b101.
2144+
if ( str_starts_with( $value, "b'" ) || str_starts_with( $value, '0b' ) ) {
2145+
$bits = ltrim( rtrim( substr( $value, 2 ), "'" ), '0' );
2146+
return "b'" . ( '' === $bits ? '0' : $bits ) . "'";
2147+
}
2148+
2149+
// Hex literal, e.g. x'05' or 0x05.
2150+
if ( str_starts_with( $value, "x'" ) || str_starts_with( $value, '0x' ) ) {
2151+
return "b'" . decbin( hexdec( rtrim( substr( $value, 2 ), "'" ) ) ) . "'";
2152+
}
2153+
2154+
// Decimal, e.g. 5, or a numeric string literal such as '0'.
2155+
if ( is_numeric( $value ) ) {
2156+
return "b'" . decbin( (int) $value ) . "'";
2157+
}
2158+
2159+
return null;
2160+
}
2161+
21022162
/**
21032163
* Extract column nullability from the "columnDefinition" or "fieldDefinition" AST node.
21042164
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,11 @@ private function generate_column_default( string $mysql_type, ?string $default_v
496496
}
497497
$mysql_type = strtolower( $mysql_type );
498498

499+
if ( str_starts_with( $mysql_type, 'bit' ) ) {
500+
// BIT columns are stored as INTEGER in SQLite.
501+
return "b'" . decbin( (int) $default_value ) . "'";
502+
}
503+
499504
/*
500505
* In MySQL, geometry columns can't have a default value.
501506
*

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,69 @@ public function testShowCreateTableWithDefaultValues(): void {
658658
);
659659
}
660660

661+
public function testShowCreateTableWithBitDefaultValues(): void {
662+
$this->assertQuery(
663+
"CREATE TABLE _tmp_bit_defaults (
664+
id INT DEFAULT 0,
665+
quoted_zero BIT(1) DEFAULT '0',
666+
integer_five BIT(4) DEFAULT 5,
667+
bit_literal_five BIT(4) DEFAULT b'0101',
668+
binary_number_five BIT(4) DEFAULT 0b0101,
669+
hex_literal_five BIT(4) DEFAULT x'05',
670+
hex_number_five BIT(4) DEFAULT 0x05,
671+
true_literal BIT(4) DEFAULT TRUE,
672+
false_literal BIT(4) DEFAULT FALSE
673+
)"
674+
);
675+
676+
$this->assertQuery( 'SHOW CREATE TABLE _tmp_bit_defaults;' );
677+
$results = $this->engine->get_query_results();
678+
$create_table = $results[0]->{'Create Table'};
679+
$this->assertStringContainsString( "`quoted_zero` bit(1) DEFAULT b'0'", $create_table );
680+
$this->assertStringContainsString( "`integer_five` bit(4) DEFAULT b'101'", $create_table );
681+
$this->assertStringContainsString( "`bit_literal_five` bit(4) DEFAULT b'101'", $create_table );
682+
$this->assertStringContainsString( "`binary_number_five` bit(4) DEFAULT b'101'", $create_table );
683+
$this->assertStringContainsString( "`hex_literal_five` bit(4) DEFAULT b'101'", $create_table );
684+
$this->assertStringContainsString( "`hex_number_five` bit(4) DEFAULT b'101'", $create_table );
685+
$this->assertStringContainsString( "`true_literal` bit(4) DEFAULT b'1'", $create_table );
686+
$this->assertStringContainsString( "`false_literal` bit(4) DEFAULT b'0'", $create_table );
687+
688+
$this->assertQuery( 'INSERT INTO _tmp_bit_defaults (id) VALUES (1)' );
689+
$this->assertQuery( 'SELECT * FROM _tmp_bit_defaults' );
690+
$results = $this->engine->get_query_results();
691+
$this->assertEquals(
692+
array(
693+
(object) array(
694+
'id' => '1',
695+
'quoted_zero' => '0',
696+
'integer_five' => '5',
697+
'bit_literal_five' => '5',
698+
'binary_number_five' => '5',
699+
'hex_literal_five' => '5',
700+
'hex_number_five' => '5',
701+
'true_literal' => '1',
702+
'false_literal' => '0',
703+
),
704+
),
705+
$results
706+
);
707+
}
708+
709+
public function testUpdateWithBitColumnDefaultValue(): void {
710+
$this->assertQuery(
711+
"CREATE TABLE _tmp_bit_defaults (
712+
id INT,
713+
maintenance BIT(4) DEFAULT b'0101'
714+
)"
715+
);
716+
717+
$this->assertQuery( 'INSERT INTO _tmp_bit_defaults (id, maintenance) VALUES (1, 0)' );
718+
$this->assertQuery( 'UPDATE _tmp_bit_defaults SET maintenance = DEFAULT' );
719+
$this->assertQuery( 'SELECT maintenance FROM _tmp_bit_defaults' );
720+
$results = $this->engine->get_query_results();
721+
$this->assertEquals( array( (object) array( 'maintenance' => '5' ) ), $results );
722+
}
723+
661724
public function testSelectIndexHintForce() {
662725
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('first');" );
663726
$result = $this->assertQuery(

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,39 @@ public function testDefaultValues(): void {
265265
);
266266
}
267267

268+
public function testReconstructBitDefaultValuesFromMysqlDataTypesCache(): void {
269+
$connection = $this->engine->get_connection();
270+
271+
$connection->query( self::CREATE_DATA_TYPES_CACHE_TABLE_SQL );
272+
$connection->query( "INSERT INTO _mysql_data_types_cache (`table`, column_or_index, mysql_type) VALUES ('t', 'zero', 'bit(1)')" );
273+
$connection->query( "INSERT INTO _mysql_data_types_cache (`table`, column_or_index, mysql_type) VALUES ('t', 'five', 'bit(4)')" );
274+
275+
// BIT columns are stored as INTEGER in SQLite.
276+
$connection->query(
277+
'
278+
CREATE TABLE t (
279+
zero INTEGER DEFAULT 0,
280+
five INTEGER DEFAULT 5
281+
)
282+
'
283+
);
284+
285+
$this->reconstructor->ensure_correct_information_schema();
286+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
287+
$this->assertSame(
288+
implode(
289+
"\n",
290+
array(
291+
'CREATE TABLE `t` (',
292+
" `zero` bit(1) DEFAULT b'0',",
293+
" `five` bit(4) DEFAULT b'101'",
294+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
295+
)
296+
),
297+
$result[0]->{'Create Table'}
298+
);
299+
}
300+
268301
public function testDefaultValueEscaping(): void {
269302
$this->engine->get_connection()->query(
270303
"

0 commit comments

Comments
 (0)