Skip to content

Commit 6068950

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

2 files changed

Lines changed: 66 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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,69 @@ public function testReconstructTable(): void {
181181
);
182182
}
183183

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

0 commit comments

Comments
 (0)