Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ',
Expand Down
19 changes: 19 additions & 0 deletions packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Loading