Skip to content

Commit bffd503

Browse files
committed
Use strcasecmp() and test more foreign key references
Compare the column names with strcasecmp() instead of lowercasing both. Extend the test to cover a single-column PRIMARY KEY, a composite UNIQUE key, and a foreign key added with ALTER TABLE.
1 parent 84b1693 commit bffd503

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ private function extract_referential_constraint_data( WP_Parser_Node $node, stri
18231823
// Column names are case-insensitive in MySQL.
18241824
$is_prefix = true;
18251825
foreach ( $reference_parts as $i => $reference_part ) {
1826-
if ( strtolower( $index_columns[ $i ] ) !== strtolower( $this->get_value( $reference_part ) ) ) {
1826+
if ( 0 !== strcasecmp( $index_columns[ $i ], $this->get_value( $reference_part ) ) ) {
18271827
$is_prefix = false;
18281828
break;
18291829
}

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,22 +1943,48 @@ public function testInformationSchemaForeignKeysReferencingShorterUniqueKey(): v
19431943
);
19441944
}
19451945

1946-
public function testInformationSchemaForeignKeysReferencingDifferentlyCasedColumns(): void {
1947-
$this->assertQuery( 'CREATE TABLE t1 (id INT, name VARCHAR(255), PRIMARY KEY (id, name))' );
1946+
public function testInformationSchemaForeignKeysReferencingColumnsInDifferentLetterCase(): void {
1947+
$this->assertQuery(
1948+
'CREATE TABLE t1 (
1949+
id INT PRIMARY KEY,
1950+
code INT,
1951+
a INT NOT NULL,
1952+
b INT NOT NULL,
1953+
UNIQUE KEY u_code (code),
1954+
UNIQUE KEY u_a_b (a, b)
1955+
)'
1956+
);
19481957
$this->assertQuery(
19491958
'CREATE TABLE t2 (
1950-
id INT,
1951-
name VARCHAR(255),
1952-
FOREIGN KEY (id, name) REFERENCES t1 (ID, Name)
1959+
t1_id INT,
1960+
x INT,
1961+
y INT,
1962+
FOREIGN KEY (t1_id) REFERENCES t1 (ID),
1963+
FOREIGN KEY (x, y) REFERENCES t1 (A, b)
19531964
)'
19541965
);
1966+
$this->assertQuery( 'ALTER TABLE t2 ADD COLUMN z INT, ADD CONSTRAINT fk_z FOREIGN KEY (z) REFERENCES t1 (CoDe)' );
19551967

1956-
$result = $this->assertQuery( "SELECT unique_constraint_name FROM information_schema.referential_constraints WHERE table_name = 't2'" );
1968+
$result = $this->assertQuery(
1969+
"SELECT constraint_name, unique_constraint_name
1970+
FROM information_schema.referential_constraints
1971+
WHERE table_name = 't2'
1972+
ORDER BY constraint_name"
1973+
);
19571974
$this->assertEquals(
19581975
array(
19591976
(object) array(
1977+
'CONSTRAINT_NAME' => 'fk_z',
1978+
'UNIQUE_CONSTRAINT_NAME' => 'u_code',
1979+
),
1980+
(object) array(
1981+
'CONSTRAINT_NAME' => 't2_ibfk_1',
19601982
'UNIQUE_CONSTRAINT_NAME' => 'PRIMARY',
19611983
),
1984+
(object) array(
1985+
'CONSTRAINT_NAME' => 't2_ibfk_2',
1986+
'UNIQUE_CONSTRAINT_NAME' => 'u_a_b',
1987+
),
19621988
),
19631989
$result
19641990
);

0 commit comments

Comments
 (0)