Skip to content

Commit ba9d450

Browse files
Nandan108claudeJanJakes
authored
Fix unique keys and foreign key restore without stringified fetches (#532)
## Summary Fixes #451 together with #522. `ALTER TABLE`, `CREATE TABLE`, and `SHOW CREATE TABLE` compare fetched metadata to string values. PDO SQLite returns native types unless `PDO::ATTR_STRINGIFY_FETCHES` is set, so on PHP 8.1 and newer, these comparisons fail for callers that don't set that attribute. The WordPress plugin and the MySQL proxy set it, so they are unaffected, but the PDO-compatible API is: ```php $pdo = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:dbname=wp', ... ); $pdo->query( 'CREATE TABLE u (id INT, email VARCHAR(50), UNIQUE KEY ue (email))' ); $pdo->query( "INSERT INTO u VALUES (1, 'a@example.com')" ); $pdo->query( "INSERT INTO u VALUES (2, 'a@example.com')" ); // Accepted: the unique key was created as a plain index. ``` The affected comparisons are the remaining ones listed in #451: - `NON_UNIQUE` is compared to `'0'` when generating the SQLite schema and when generating `SHOW CREATE TABLE` output, so every `UNIQUE KEY` becomes a plain index. That also breaks `INSERT ... ON DUPLICATE KEY UPDATE`, which then inserts duplicates. - `PRAGMA foreign_keys` is compared to `'1'` after `ALTER TABLE` recreates a table, so the foreign key check is skipped. #522 fixes the temporary table lookup, and #506 already fixed schema reconstruction. ## Fix The values are compared as integers. ## Tests `testUniqueKeysWithoutStringifiedFetches` disables stringified fetches, creates a table with a unique key, checks `SHOW CREATE TABLE`, recreates the table with `ALTER TABLE`, and checks that a duplicate row is rejected. It fails on `trunk`. Each of the two `NON_UNIQUE` comparisons fails it separately. The `PRAGMA foreign_keys` comparison has no test. `ALTER TABLE` runs inside the wrapper transaction, where `PRAGMA foreign_keys` is a no-op, so the skipped restore has no observable effect today. It is fixed for consistency. I also ran the whole suite with stringified fetches disabled in every test class. With this PR and #522, the only remaining failures are type differences in returned values (`1` vs `'1'`) and the two tests of the attribute itself. `packages/mysql-on-sqlite`: 1044 tests pass (PHP 8.3, SQLite 3.45.1); PHPCS is clean. ## Use of AI This change was prepared with Claude Code (Claude Opus 5.5). I reviewed the code, the tests, and this description, and I take responsibility for the change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Fixed unique database indexes being lost when tables are recreated after a schema change. Duplicate values in uniquely constrained fields are now correctly rejected. - Improved consistency when adding columns to tables: unique constraints are preserved whether database results are returned as strings or other value types. Database table definitions also continue to report unique indexes correctly in either case. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Jan Jakeš <jan@jakes.pro>
1 parent 8dc5c27 commit ba9d450

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5646,7 +5646,7 @@ private function recreate_table_from_information_schema(
56465646
}
56475647

56485648
// 7. If foreign key constraints were enabled, verify and enable them.
5649-
if ( '1' === $pragma_foreign_keys ) {
5649+
if ( 1 === (int) $pragma_foreign_keys ) {
56505650
$this->execute_sqlite_query( 'PRAGMA foreign_key_check' );
56515651
$this->execute_sqlite_query( 'PRAGMA foreign_keys = ON' );
56525652
}
@@ -7138,7 +7138,7 @@ function ( $column ) {
71387138
}
71397139
$rows[] = sprintf( ' PRIMARY KEY (%s)', implode( ', ', $column_list ) );
71407140
} else {
7141-
$is_unique = '0' === $info['NON_UNIQUE'];
7141+
$is_unique = 0 === (int) $info['NON_UNIQUE'];
71427142

71437143
// Prefix the original index name with the table name.
71447144
// This is to avoid conflicting index names in SQLite.
@@ -7424,7 +7424,7 @@ private function get_mysql_create_table_statement( bool $table_is_temporary, str
74247424
$sql .= implode( ', ', array_map( $format_index_column, $constraint ) );
74257425
$sql .= ')';
74267426
} else {
7427-
$is_unique = '0' === $info['NON_UNIQUE'];
7427+
$is_unique = 0 === (int) $info['NON_UNIQUE'];
74287428

74297429
$sql = sprintf(
74307430
' %s%s%sKEY ',

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5124,6 +5124,25 @@ public function dataLikeBinaryPatterns(): array {
51245124
);
51255125
}
51265126

5127+
/**
5128+
* @dataProvider stringifyFetchesProvider
5129+
*/
5130+
public function testStringifyFetchesDoesNotAffectUniqueKeys( bool $stringify_fetches ): void {
5131+
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, $stringify_fetches );
5132+
5133+
$this->assertQuery( 'CREATE TABLE t (id INT, email VARCHAR(50), UNIQUE KEY ue (email))' );
5134+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
5135+
$this->assertStringContainsString( 'UNIQUE KEY `ue` (`email`)', $result[0]->{'Create Table'} );
5136+
5137+
// The unique key is kept when ALTER TABLE recreates the table.
5138+
$this->assertQuery( 'ALTER TABLE t ADD COLUMN name TEXT' );
5139+
$this->assertQuery( "INSERT INTO t (id, email) VALUES (1, 'a@example.com')" );
5140+
5141+
$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
5142+
$this->expectExceptionMessage( 'UNIQUE constraint failed: t.email' );
5143+
$this->assertQuery( "INSERT INTO t (id, email) VALUES (2, 'a@example.com')" );
5144+
}
5145+
51275146
public function testUniqueConstraints() {
51285147
$this->assertQuery(
51295148
"CREATE TABLE _tmp_table (

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,11 @@ public function testDropTemporaryTable(): void {
884884
);
885885
}
886886

887-
public function testAlterTableAddColumn(): void {
887+
/**
888+
* @dataProvider stringifyFetchesProvider
889+
*/
890+
public function testAlterTableAddColumn( bool $stringify_fetches ): void {
891+
$this->driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, $stringify_fetches );
888892
$this->driver->query( 'CREATE TABLE t (id INT)' );
889893
$this->assertQuery(
890894
array(
@@ -915,6 +919,13 @@ public function testAlterTableAddColumn(): void {
915919
);
916920
}
917921

922+
public function stringifyFetchesProvider(): array {
923+
return array(
924+
'stringification disabled' => array( false ),
925+
'stringification enabled' => array( true ),
926+
);
927+
}
928+
918929
public function testAlterTableAddColumnWithNotNull(): void {
919930
$this->driver->query( 'CREATE TABLE t (id INT)' );
920931
$this->assertQuery(

0 commit comments

Comments
 (0)