Skip to content

Commit 743439f

Browse files
committed
Fix schema reconstruction with native numeric results
Normalize SQLite’s numeric schema flags before strict comparisons. Cover reconstruction with string fetching enabled and disabled, including column defaults, nullability, and indexes. #422
1 parent 69340fe commit 743439f

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,13 @@ private function generate_column_definition( string $table_name, array $column_i
377377
$definition[] = $mysql_type;
378378

379379
// NULL/NOT NULL.
380-
if ( '1' === $column_info['notnull'] ) {
380+
if ( 1 === (int) $column_info['notnull'] ) {
381381
$definition[] = 'NOT NULL';
382382
}
383383

384384
// Auto increment.
385385
$is_auto_increment = false;
386-
if ( '0' !== $column_info['pk'] ) {
386+
if ( 0 !== (int) $column_info['pk'] ) {
387387
$is_auto_increment = $this->driver->execute_sqlite_query(
388388
'SELECT 1 FROM sqlite_master WHERE tbl_name = ? AND sql LIKE ?',
389389
array( $table_name, '%AUTOINCREMENT%' )
@@ -421,7 +421,7 @@ private function generate_key_definition( string $table_name, array $key_info, a
421421
$definition[] = 'FULLTEXT KEY';
422422
} elseif ( 'SPATIAL' === $cached_type ) {
423423
$definition[] = 'SPATIAL KEY';
424-
} elseif ( 'UNIQUE' === $cached_type || '1' === $key_info['unique'] ) {
424+
} elseif ( 'UNIQUE' === $cached_type || 1 === (int) $key_info['unique'] ) {
425425
$definition[] = 'UNIQUE KEY';
426426
} else {
427427
$definition[] = 'KEY';

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,63 @@ public function testReconstructTable(): void {
181181
);
182182
}
183183

184+
/**
185+
* @dataProvider stringifyFetchesProvider
186+
*/
187+
public function testStringifyFetchesDoesNotAffectReconstruction( bool $enabled ): void {
188+
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, $enabled );
189+
190+
$this->engine->get_connection()->query(
191+
"CREATE TABLE t (
192+
id INTEGER PRIMARY KEY AUTOINCREMENT,
193+
name TEXT NOT NULL UNIQUE,
194+
note TEXT DEFAULT 'default',
195+
score INTEGER
196+
)"
197+
);
198+
$this->engine->get_connection()->query( 'CREATE INDEX t__score ON t (score)' );
199+
200+
$this->reconstructor->ensure_correct_information_schema();
201+
202+
$this->assertSame(
203+
array(
204+
array( 'id', 'NO', null, 'auto_increment' ),
205+
array( 'name', 'NO', null, '' ),
206+
array( 'note', 'YES', 'default', '' ),
207+
array( 'score', 'YES', null, '' ),
208+
),
209+
$this->engine->query(
210+
"SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_DEFAULT, EXTRA
211+
FROM INFORMATION_SCHEMA.COLUMNS
212+
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't'
213+
ORDER BY ORDINAL_POSITION"
214+
)->fetchAll( PDO::FETCH_NUM )
215+
);
216+
217+
// PDO SQLite always returns numeric values as strings before PHP 8.1.
218+
$expect_strings = $enabled || PHP_VERSION_ID < 80100;
219+
$this->assertSame(
220+
array(
221+
array( 'PRIMARY', 'id', $expect_strings ? '0' : 0 ),
222+
array( 'name', 'name', $expect_strings ? '0' : 0 ),
223+
array( 'score', 'score', $expect_strings ? '1' : 1 ),
224+
),
225+
$this->engine->query(
226+
"SELECT INDEX_NAME, COLUMN_NAME, NON_UNIQUE
227+
FROM INFORMATION_SCHEMA.STATISTICS
228+
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't'
229+
ORDER BY COLUMN_NAME"
230+
)->fetchAll( PDO::FETCH_NUM )
231+
);
232+
}
233+
234+
public function stringifyFetchesProvider(): array {
235+
return array(
236+
'native values' => array( false ),
237+
'string values' => array( true ),
238+
);
239+
}
240+
184241
public function testReconstructWpTable(): void {
185242
// Create a WP table with any columns.
186243
$this->engine->get_connection()->query( 'CREATE TABLE wp_posts ( id INTEGER )' );

0 commit comments

Comments
 (0)