Skip to content

Commit 7a01ec4

Browse files
Fix '0' column default preservation during non-strict INSERT (#569)
Fixes #552. ### Description In MySQL non-strict mode, omitted columns during `INSERT` are filtered out from the synthetic wrapper subquery if they have a default value defined, allowing SQLite to populate their schema default. Previously, `translate_insert_or_replace_body()` evaluated `$column['COLUMN_DEFAULT']` with a loose truthiness check (`$has_default = $column['COLUMN_DEFAULT']`). In PHP, string `'0'` evaluates to `false`, treating the column as having no default value and incorrectly populating it with an implicit default empty string (`''`). This PR: - Updates the check to `null !== $column['COLUMN_DEFAULT']`, consistent with other default checks in the driver. - Adds regression test assertions in `testNonStrictSqlModeNotNullWithDefault` covering `DEFAULT '0'` and `DEFAULT 0`. ### Testing instructions Run unit tests in `packages/mysql-on-sqlite`: ```bash cd packages/mysql-on-sqlite composer run test -- --filter testNonStrictSqlModeNotNullWithDefault ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Corrected handling of columns with a default value of `0` when inserting rows without explicitly providing those columns. Omitted values now receive their configured defaults rather than being treated as though no default were set. * **Tests** * Added coverage confirming that omitted values in columns with text or numeric zero defaults are populated as expected. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Jan Jakeš <jan@jakes.pro>
1 parent a5314e3 commit 7a01ec4

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6077,7 +6077,7 @@ function ( $column ) use ( $is_strict_mode, $insert_map ) {
60776077
return false;
60786078
}
60796079
$is_nullable = 'YES' === $column['IS_NULLABLE'];
6080-
$has_default = $column['COLUMN_DEFAULT'];
6080+
$has_default = null !== $column['COLUMN_DEFAULT'];
60816081
$is_generated = str_contains( $column['EXTRA'], 'auto_increment' );
60826082
return ! ( $is_nullable || $has_default || $is_generated );
60836083
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8626,6 +8626,14 @@ public function testNonStrictSqlModeNotNullWithDefault(): void {
86268626
$result = $this->assertQuery( 'SELECT * FROM t3' );
86278627
$this->assertCount( 1, $result );
86288628
$this->assertSame( '', $result[0]->value );
8629+
8630+
// No value saves DEFAULT '0'
8631+
$this->assertQuery( "CREATE TABLE t4 (id INT NOT NULL, label VARCHAR(10) NOT NULL DEFAULT '0', code VARCHAR(10) NOT NULL DEFAULT 0)" );
8632+
$this->assertQuery( 'INSERT INTO t4 (id) VALUES (1)' );
8633+
$result = $this->assertQuery( 'SELECT * FROM t4 WHERE id = 1' );
8634+
$this->assertCount( 1, $result );
8635+
$this->assertSame( '0', $result[0]->label );
8636+
$this->assertSame( '0', $result[0]->code );
86298637
}
86308638

86318639
public function testNonStrictModeWithDefaultCurrentTimestamp(): void {

0 commit comments

Comments
 (0)