Skip to content

Commit b51199d

Browse files
committed
Report the actual error of failed table maintenance statements
ANALYZE, CHECK, OPTIMIZE, and REPAIR TABLE reported every general SQLite error as "Table '...' doesn't exist", although only the "no such table" error means that. Other errors, such as a failed table rebuild, are now reported as SQLite returns them.
1 parent de58dec commit b51199d

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4155,7 +4155,11 @@ private function execute_administration_statement( WP_Parser_Node $node ): void
41554155
} catch ( PDOException $e ) {
41564156
if ( '42S02' === $e->getCode() && isset( $e->errorInfo[2] ) ) {
41574157
$errors = array( $e->errorInfo[2] );
4158-
} elseif ( 'HY000' === $e->getCode() ) {
4158+
} elseif (
4159+
// SQLite reports a missing table as a general error.
4160+
'HY000' === $e->getCode()
4161+
&& str_starts_with( $e->errorInfo[2] ?? '', 'no such table' )
4162+
) {
41594163
$errors = array( "Table '$table_name' doesn't exist" );
41604164
} else {
41614165
$errors = array( $e->getMessage() );

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,44 @@ private function assertQuery( $sql ) {
234234
return $retval;
235235
}
236236

237+
public function testAnalyzeTable() {
238+
$this->assertQuery( 'CREATE TABLE t1 (id INT, name TEXT)' );
239+
240+
// A good table.
241+
$result = $this->assertQuery( 'ANALYZE TABLE t1' );
242+
$this->assertEquals(
243+
array(
244+
(object) array(
245+
'Table' => 'wp.t1',
246+
'Op' => 'analyze',
247+
'Msg_type' => 'status',
248+
'Msg_text' => 'OK',
249+
),
250+
),
251+
$result
252+
);
253+
254+
// A missing table.
255+
$result = $this->assertQuery( 'ANALYZE TABLE missing' );
256+
$this->assertEquals(
257+
array(
258+
(object) array(
259+
'Table' => 'wp.missing',
260+
'Op' => 'analyze',
261+
'Msg_type' => 'Error',
262+
'Msg_text' => "Table 'missing' doesn't exist",
263+
),
264+
(object) array(
265+
'Table' => 'wp.missing',
266+
'Op' => 'analyze',
267+
'Msg_type' => 'status',
268+
'Msg_text' => 'Operation failed',
269+
),
270+
),
271+
$result
272+
);
273+
}
274+
237275
public function testCheckTable() {
238276
$this->assertQuery( 'CREATE TABLE t1 (id INT, name TEXT)' );
239277
$this->assertQuery( 'CREATE TABLE t2 (id INT, name TEXT)' );
@@ -436,6 +474,33 @@ public function testOptimizeTable() {
436474
);
437475
}
438476

477+
public function testOptimizeTableWithFailedRecreation() {
478+
$this->assertQuery( 'CREATE TABLE t (id INT, name TEXT)' );
479+
480+
// Make recreating the table fail, as its information schema names a column that doesn't exist.
481+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET column_name = 'title' WHERE table_name = 't' AND column_name = 'name'" );
482+
483+
// The error from SQLite is reported, not a missing table.
484+
$result = $this->assertQuery( 'OPTIMIZE TABLE t' );
485+
$this->assertEquals(
486+
array(
487+
(object) array(
488+
'Table' => 'wp.t',
489+
'Op' => 'optimize',
490+
'Msg_type' => 'Error',
491+
'Msg_text' => 'SQLSTATE[HY000]: General error: 1 no such column: title',
492+
),
493+
(object) array(
494+
'Table' => 'wp.t',
495+
'Op' => 'optimize',
496+
'Msg_type' => 'status',
497+
'Msg_text' => 'Operation failed',
498+
),
499+
),
500+
$result
501+
);
502+
}
503+
439504
public function testRepairTable() {
440505
$this->assertQuery( 'CREATE TABLE t1 (id INT, name TEXT)' );
441506
$this->assertQuery( 'CREATE TABLE t2 (id INT, name TEXT)' );

0 commit comments

Comments
 (0)