Skip to content

Commit 71c946c

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 71c946c

2 files changed

Lines changed: 61 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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,64 @@ 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+
// Use consistent result types when checking the reconstructed metadata.
203+
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
204+
205+
$this->assertSame(
206+
array(
207+
array( 'id', 'NO', null, 'auto_increment' ),
208+
array( 'name', 'NO', null, '' ),
209+
array( 'note', 'YES', 'default', '' ),
210+
array( 'score', 'YES', null, '' ),
211+
),
212+
$this->engine->query(
213+
"SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_DEFAULT, EXTRA
214+
FROM INFORMATION_SCHEMA.COLUMNS
215+
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't'
216+
ORDER BY ORDINAL_POSITION"
217+
)->fetchAll( PDO::FETCH_NUM )
218+
);
219+
220+
$this->assertSame(
221+
array(
222+
array( 'PRIMARY', 'id', '0' ),
223+
array( 'name', 'name', '0' ),
224+
array( 'score', 'score', '1' ),
225+
),
226+
$this->engine->query(
227+
"SELECT INDEX_NAME, COLUMN_NAME, NON_UNIQUE
228+
FROM INFORMATION_SCHEMA.STATISTICS
229+
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't'
230+
ORDER BY COLUMN_NAME"
231+
)->fetchAll( PDO::FETCH_NUM )
232+
);
233+
}
234+
235+
public function stringifyFetchesProvider(): array {
236+
return array(
237+
'stringification disabled' => array( false ),
238+
'stringification enabled' => array( true ),
239+
);
240+
}
241+
184242
public function testReconstructWpTable(): void {
185243
// Create a WP table with any columns.
186244
$this->engine->get_connection()->query( 'CREATE TABLE wp_posts ( id INTEGER )' );

0 commit comments

Comments
 (0)