diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php index 813455340..5b053ea89 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php @@ -5646,7 +5646,7 @@ private function recreate_table_from_information_schema( } // 7. If foreign key constraints were enabled, verify and enable them. - if ( '1' === $pragma_foreign_keys ) { + if ( 1 === (int) $pragma_foreign_keys ) { $this->execute_sqlite_query( 'PRAGMA foreign_key_check' ); $this->execute_sqlite_query( 'PRAGMA foreign_keys = ON' ); } @@ -7138,7 +7138,7 @@ function ( $column ) { } $rows[] = sprintf( ' PRIMARY KEY (%s)', implode( ', ', $column_list ) ); } else { - $is_unique = '0' === $info['NON_UNIQUE']; + $is_unique = 0 === (int) $info['NON_UNIQUE']; // Prefix the original index name with the table name. // 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 $sql .= implode( ', ', array_map( $format_index_column, $constraint ) ); $sql .= ')'; } else { - $is_unique = '0' === $info['NON_UNIQUE']; + $is_unique = 0 === (int) $info['NON_UNIQUE']; $sql = sprintf( ' %s%s%sKEY ', diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index 8a9446781..3f1a57e6c 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -5124,6 +5124,25 @@ public function dataLikeBinaryPatterns(): array { ); } + /** + * @dataProvider stringifyFetchesProvider + */ + public function testStringifyFetchesDoesNotAffectUniqueKeys( bool $stringify_fetches ): void { + $this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, $stringify_fetches ); + + $this->assertQuery( 'CREATE TABLE t (id INT, email VARCHAR(50), UNIQUE KEY ue (email))' ); + $result = $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertStringContainsString( 'UNIQUE KEY `ue` (`email`)', $result[0]->{'Create Table'} ); + + // The unique key is kept when ALTER TABLE recreates the table. + $this->assertQuery( 'ALTER TABLE t ADD COLUMN name TEXT' ); + $this->assertQuery( "INSERT INTO t (id, email) VALUES (1, 'a@example.com')" ); + + $this->expectException( 'WP_MySQL_On_SQLite_Exception' ); + $this->expectExceptionMessage( 'UNIQUE constraint failed: t.email' ); + $this->assertQuery( "INSERT INTO t (id, email) VALUES (2, 'a@example.com')" ); + } + public function testUniqueConstraints() { $this->assertQuery( "CREATE TABLE _tmp_table ( diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php index ec24ac419..d6720e5b6 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php @@ -884,7 +884,11 @@ public function testDropTemporaryTable(): void { ); } - public function testAlterTableAddColumn(): void { + /** + * @dataProvider stringifyFetchesProvider + */ + public function testAlterTableAddColumn( bool $stringify_fetches ): void { + $this->driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, $stringify_fetches ); $this->driver->query( 'CREATE TABLE t (id INT)' ); $this->assertQuery( array( @@ -915,6 +919,13 @@ public function testAlterTableAddColumn(): void { ); } + public function stringifyFetchesProvider(): array { + return array( + 'stringification disabled' => array( false ), + 'stringification enabled' => array( true ), + ); + } + public function testAlterTableAddColumnWithNotNull(): void { $this->driver->query( 'CREATE TABLE t (id INT)' ); $this->assertQuery(