Skip to content

Commit b40fe28

Browse files
committed
Support DROP INDEX edge case, improve the tests
1 parent 7df37d0 commit b40fe28

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5665,12 +5665,57 @@ public function testDropIndex(): void {
56655665
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, val_unique INT UNIQUE, val_index INT)' );
56665666
$this->assertQuery( 'CREATE INDEX idx_val_index ON t (val_index)' );
56675667

5668+
// Verify that the indexes were saved in the information schema.
56685669
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
56695670
$this->assertCount( 3, $result );
56705671
$this->assertEquals( 'PRIMARY', $result[0]->Key_name );
56715672
$this->assertEquals( 'val_unique', $result[1]->Key_name );
56725673
$this->assertEquals( 'idx_val_index', $result[2]->Key_name );
56735674

5675+
// Verify that the indexes exist in the SQLite database.
5676+
$result = $this->engine->execute_sqlite_query( "PRAGMA index_list('t')" )->fetchAll( PDO::FETCH_ASSOC );
5677+
$this->assertCount( 3, $result );
5678+
$this->assertEquals( 't__idx_val_index', $result[0]['name'] );
5679+
$this->assertEquals( 't__val_unique', $result[1]['name'] );
5680+
$this->assertEquals( 'sqlite_autoindex_t_1', $result[2]['name'] );
5681+
5682+
// DROP the explicitly named index.
56745683
$this->assertQuery( 'DROP INDEX idx_val_index ON t' );
5684+
5685+
// Verify that the index was removed from the information schema.
5686+
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
5687+
$this->assertCount( 2, $result );
5688+
$this->assertEquals( 'PRIMARY', $result[0]->Key_name );
5689+
$this->assertEquals( 'val_unique', $result[1]->Key_name );
5690+
5691+
// Verify that the index was removed from the SQLite database.
5692+
$result = $this->engine->execute_sqlite_query( "PRAGMA index_list('t')" )->fetchAll( PDO::FETCH_ASSOC );
5693+
$this->assertCount( 2, $result );
5694+
$this->assertEquals( 't__val_unique', $result[0]['name'] );
5695+
$this->assertEquals( 'sqlite_autoindex_t_1', $result[1]['name'] );
5696+
5697+
// DROP the UNIQUE index.
5698+
$this->assertQuery( 'DROP INDEX val_unique ON t' );
5699+
5700+
// Verify that the index was removed from the information schema.
5701+
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
5702+
$this->assertCount( 1, $result );
5703+
$this->assertEquals( 'PRIMARY', $result[0]->Key_name );
5704+
5705+
// Verify that the index was removed from the SQLite database.
5706+
$result = $this->engine->execute_sqlite_query( "PRAGMA index_list('t')" )->fetchAll( PDO::FETCH_ASSOC );
5707+
$this->assertCount( 1, $result );
5708+
$this->assertEquals( 'sqlite_autoindex_t_1', $result[0]['name'] );
5709+
5710+
// DROP the PRIMARY KEY index.
5711+
$this->assertQuery( 'DROP INDEX `PRIMARY` ON t' );
5712+
5713+
// Verify that the index was removed from the information schema.
5714+
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
5715+
$this->assertCount( 0, $result );
5716+
5717+
// Verify that the index was removed from the SQLite database.
5718+
$result = $this->engine->execute_sqlite_query( "PRAGMA index_list('t')" )->fetchAll( PDO::FETCH_ASSOC );
5719+
$this->assertCount( 0, $result );
56755720
}
56765721
}

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,17 @@ private function execute_drop_index_statement( WP_Parser_Node $node ): void {
15311531
$this->translate( $drop_index->get_first_child_node( 'indexRef' ) )
15321532
);
15331533

1534+
/*
1535+
* In MySQL, "DROP INDEX `PRIMARY` ON <table>" removes the PRIMARY KEY.
1536+
* This is not supported in SQLite, so in such cases, we need to recreate
1537+
* the table without the PRIMARY KEY using the updated information schema.
1538+
*/
1539+
if ( 'PRIMARY' === strtoupper( $index_name ) ) {
1540+
$table_is_temporary = $this->information_schema_builder->temporary_table_exists( $table_name );
1541+
$this->recreate_table_from_information_schema( $table_is_temporary, $table_name );
1542+
return;
1543+
}
1544+
15341545
$sqlite_index_name = $this->get_sqlite_index_name( $table_name, $index_name );
15351546
$this->execute_sqlite_query(
15361547
sprintf(

0 commit comments

Comments
 (0)