Skip to content

Commit f0d82f2

Browse files
committed
Roll back failed table rebuilds
Wrap each table administration operation in a savepoint before converting errors into result rows. Preserve successful table rebuilds and surrounding user transactions when another table fails. Fixes #549
1 parent 958f029 commit f0d82f2

2 files changed

Lines changed: 151 additions & 6 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4107,6 +4107,7 @@ private function execute_set_user_variable_statement(
41074107
private function execute_administration_statement( WP_Parser_Node $node ): void {
41084108
$first_token = $node->get_first_child_token();
41094109
$table_ref_list = $node->get_first_child_node( 'tableRefList' );
4110+
$savepoint_name = $this->get_internal_savepoint_name( 'table_administration' );
41104111
$results = array();
41114112
foreach ( $table_ref_list->get_child_nodes( 'tableRef' ) as $table_ref ) {
41124113
$database = $this->get_database_name( $table_ref );
@@ -4116,6 +4117,9 @@ private function execute_administration_statement( WP_Parser_Node $node ): void
41164117

41174118
$table_name = $this->unquote_sqlite_identifier( $this->translate( $table_ref ) );
41184119
$quoted_table_name = $this->quote_sqlite_identifier( $table_name );
4120+
4121+
// Each table must be atomic because failures are reported as result rows.
4122+
$this->execute_sqlite_query( sprintf( 'SAVEPOINT %s', $savepoint_name ) );
41194123
try {
41204124
switch ( $first_token->id ) {
41214125
case WP_MySQL_Lexer::ANALYZE_SYMBOL:
@@ -4153,6 +4157,7 @@ private function execute_administration_statement( WP_Parser_Node $node ): void
41534157
);
41544158
}
41554159
} catch ( PDOException $e ) {
4160+
$this->execute_sqlite_query( sprintf( 'ROLLBACK TO SAVEPOINT %s', $savepoint_name ) );
41564161
if ( '42S02' === $e->getCode() && isset( $e->errorInfo[2] ) ) {
41574162
$errors = array( $e->errorInfo[2] );
41584163
} elseif (
@@ -4165,6 +4170,7 @@ private function execute_administration_statement( WP_Parser_Node $node ): void
41654170
$errors = array( $e->getMessage() );
41664171
}
41674172
}
4173+
$this->execute_sqlite_query( sprintf( 'RELEASE SAVEPOINT %s', $savepoint_name ) );
41684174

41694175
$operation = strtolower( $first_token->get_value() );
41704176
foreach ( $errors as $error ) {

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

Lines changed: 145 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,31 +474,40 @@ public function testOptimizeTable() {
474474
);
475475
}
476476

477-
public function testOptimizeTableWithFailedRecreation() {
478-
$this->assertQuery( 'CREATE TABLE t (id INT, name TEXT)' );
477+
/**
478+
* @dataProvider data_rebuild_operations_and_table_types
479+
*/
480+
public function testAdministrationWithFailedRecreation( string $operation, bool $temporary ): void {
481+
$this->assertQuery( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . 'TABLE t (id INT, name TEXT)' );
482+
$this->assertQuery( "INSERT INTO t VALUES (1, 'a'), (2, 'b')" );
479483

480484
// 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'" );
485+
$columns_table = $temporary ? '_wp_sqlite_mysql_information_schema_tmp_columns' : '_wp_sqlite_mysql_information_schema_columns';
486+
$this->sqlite->exec( "UPDATE $columns_table SET column_name = 'title' WHERE table_name = 't' AND column_name = 'name'" );
487+
$schema = $this->getSqliteSchema();
488+
$data = $this->sqlite->query( 'SELECT rowid, * FROM t ORDER BY rowid' )->fetchAll( PDO::FETCH_ASSOC );
482489

483490
// The error from SQLite is reported, not a missing table.
484-
$result = $this->assertQuery( 'OPTIMIZE TABLE t' );
491+
$result = $this->assertQuery( "$operation TABLE t" );
485492
$this->assertEquals(
486493
array(
487494
(object) array(
488495
'Table' => 'wp.t',
489-
'Op' => 'optimize',
496+
'Op' => strtolower( $operation ),
490497
'Msg_type' => 'Error',
491498
'Msg_text' => 'SQLSTATE[HY000]: General error: 1 no such column: title',
492499
),
493500
(object) array(
494501
'Table' => 'wp.t',
495-
'Op' => 'optimize',
502+
'Op' => strtolower( $operation ),
496503
'Msg_type' => 'status',
497504
'Msg_text' => 'Operation failed',
498505
),
499506
),
500507
$result
501508
);
509+
$this->assertSame( $schema, $this->getSqliteSchema() );
510+
$this->assertSame( $data, $this->sqlite->query( 'SELECT rowid, * FROM t ORDER BY rowid' )->fetchAll( PDO::FETCH_ASSOC ) );
502511
}
503512

504513
public function testRepairTable() {
@@ -586,6 +595,127 @@ public function testRepairTable() {
586595
);
587596
}
588597

598+
/**
599+
* @dataProvider data_rebuild_operations
600+
*/
601+
public function testAdministrationRollsBackFailedDrop( string $operation ): void {
602+
$this->assertQuery( 'CREATE TABLE parent (id INT PRIMARY KEY, name TEXT)' );
603+
$this->assertQuery( 'CREATE TABLE child (id INT PRIMARY KEY, parent_id INT, FOREIGN KEY (parent_id) REFERENCES parent (id))' );
604+
$this->assertQuery( "INSERT INTO parent VALUES (1, 'a'), (2, 'b')" );
605+
$this->assertQuery( 'INSERT INTO child VALUES (1, 1)' );
606+
607+
$schema = $this->getSqliteSchema();
608+
$parent = $this->sqlite->query( 'SELECT * FROM parent ORDER BY id' )->fetchAll( PDO::FETCH_ASSOC );
609+
$child = $this->sqlite->query( 'SELECT * FROM child ORDER BY id' )->fetchAll( PDO::FETCH_ASSOC );
610+
611+
// Repeated failures must not leave copies of the parent table behind.
612+
for ( $i = 0; $i < 2; ++$i ) {
613+
$result = $this->assertQuery( "$operation TABLE parent" );
614+
$this->assertCount( 2, $result );
615+
$this->assertStringContainsString( 'FOREIGN KEY constraint failed', $result[0]->Msg_text );
616+
$this->assertSame( 'Operation failed', $result[1]->Msg_text );
617+
$this->assertSame( $schema, $this->getSqliteSchema() );
618+
$this->assertSame( $parent, $this->sqlite->query( 'SELECT * FROM parent ORDER BY id' )->fetchAll( PDO::FETCH_ASSOC ) );
619+
$this->assertSame( $child, $this->sqlite->query( 'SELECT * FROM child ORDER BY id' )->fetchAll( PDO::FETCH_ASSOC ) );
620+
}
621+
}
622+
623+
/**
624+
* @dataProvider data_rebuild_operations_and_table_types
625+
*/
626+
public function testAdministrationRollsBackFailedIndexRecreation( string $operation, bool $temporary ): void {
627+
$this->assertQuery(
628+
'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . 'TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT, UNIQUE KEY u (name), KEY k (id)) AUTO_INCREMENT = 100'
629+
);
630+
631+
// These values fit the existing binary index but conflict when its NOCASE definition is restored.
632+
$this->sqlite->exec( 'DROP INDEX t__u' );
633+
$this->sqlite->exec( 'CREATE UNIQUE INDEX t__u ON t (name COLLATE BINARY)' );
634+
$this->sqlite->exec( "INSERT INTO t VALUES (1, 'a'), (2, 'A')" );
635+
$schema = $this->getSqliteSchema();
636+
$data = $this->sqlite->query( 'SELECT rowid, * FROM t ORDER BY rowid' )->fetchAll( PDO::FETCH_ASSOC );
637+
$sequence_table = $temporary ? 'temp.sqlite_sequence' : 'main.sqlite_sequence';
638+
$sequence = $this->sqlite->query( "SELECT seq FROM $sequence_table WHERE name = 't'" )->fetchColumn();
639+
640+
$result = $this->assertQuery( "$operation TABLE t" );
641+
$this->assertCount( 2, $result );
642+
$this->assertStringContainsString( 'UNIQUE constraint failed', $result[0]->Msg_text );
643+
$this->assertSame( 'Operation failed', $result[1]->Msg_text );
644+
$this->assertSame( $schema, $this->getSqliteSchema() );
645+
$this->assertSame( $data, $this->sqlite->query( 'SELECT rowid, * FROM t ORDER BY rowid' )->fetchAll( PDO::FETCH_ASSOC ) );
646+
$this->assertSame( $sequence, $this->sqlite->query( "SELECT seq FROM $sequence_table WHERE name = 't'" )->fetchColumn() );
647+
}
648+
649+
/**
650+
* @dataProvider data_rebuild_operations_and_transactions
651+
*/
652+
public function testAdministrationKeepsSuccessfulRebuilds( string $operation, bool $in_transaction ): void {
653+
$this->assertQuery( 'CREATE TABLE good_before (id INT, KEY k (id))' );
654+
$this->assertQuery( 'CREATE TABLE bad (id INT)' );
655+
$this->assertQuery( 'CREATE TABLE good_after (id INT, KEY k (id))' );
656+
$this->assertQuery( 'INSERT INTO bad VALUES (NULL)' );
657+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET is_nullable = 'NO' WHERE table_name = 'bad'" );
658+
659+
// Rebuilding the good tables must restore their missing indexes.
660+
$this->sqlite->exec( 'DROP INDEX good_before__k' );
661+
$this->sqlite->exec( 'DROP INDEX good_after__k' );
662+
$schema = $this->getSqliteSchema();
663+
if ( $in_transaction ) {
664+
$this->assertQuery( 'START TRANSACTION' );
665+
$this->assertQuery( 'INSERT INTO good_before VALUES (1)' );
666+
$this->assertQuery( 'SAVEPOINT caller' );
667+
$this->assertQuery( 'INSERT INTO good_before VALUES (2)' );
668+
}
669+
670+
$result = $this->assertQuery( "$operation TABLE good_before, bad, good_after" );
671+
$this->assertSame( array( 'wp.good_before', 'wp.bad', 'wp.bad', 'wp.good_after' ), array_column( $result, 'Table' ) );
672+
$this->assertSame( array( 'status', 'Error', 'status', 'status' ), array_column( $result, 'Msg_type' ) );
673+
$this->assertSame( 'OK', $result[0]->Msg_text );
674+
$this->assertStringContainsString( 'NOT NULL constraint failed', $result[1]->Msg_text );
675+
$this->assertSame( 'Operation failed', $result[2]->Msg_text );
676+
$this->assertSame( 'OK', $result[3]->Msg_text );
677+
$this->assertSame(
678+
array( 'good_after__k', 'good_before__k' ),
679+
$this->sqlite->query( "SELECT name FROM sqlite_master WHERE type = 'index' AND name IN ('good_before__k', 'good_after__k') ORDER BY name" )->fetchAll( PDO::FETCH_COLUMN )
680+
);
681+
$this->assertSame( array( null ), $this->sqlite->query( 'SELECT id FROM bad' )->fetchAll( PDO::FETCH_COLUMN ) );
682+
$this->assertSame( array(), $this->sqlite->query( "SELECT name FROM sqlite_master WHERE name GLOB '_wp_sqlite_tmp_*'" )->fetchAll( PDO::FETCH_COLUMN ) );
683+
$this->assertSame( $in_transaction, $this->engine->inTransaction() );
684+
685+
if ( $in_transaction ) {
686+
$this->assertQuery( 'ROLLBACK TO SAVEPOINT caller' );
687+
$this->assertEquals( array( (object) array( 'id' => '1' ) ), $this->assertQuery( 'SELECT id FROM good_before' ) );
688+
$this->assertSame( $schema, $this->getSqliteSchema() );
689+
$this->assertQuery( 'COMMIT' );
690+
$this->assertEquals( array( (object) array( 'id' => '1' ) ), $this->assertQuery( 'SELECT id FROM good_before' ) );
691+
}
692+
}
693+
694+
public function data_rebuild_operations(): array {
695+
return array(
696+
'optimize' => array( 'OPTIMIZE' ),
697+
'repair' => array( 'REPAIR' ),
698+
);
699+
}
700+
701+
public function data_rebuild_operations_and_table_types(): array {
702+
return array(
703+
'optimize permanent' => array( 'OPTIMIZE', false ),
704+
'optimize temporary' => array( 'OPTIMIZE', true ),
705+
'repair permanent' => array( 'REPAIR', false ),
706+
'repair temporary' => array( 'REPAIR', true ),
707+
);
708+
}
709+
710+
public function data_rebuild_operations_and_transactions(): array {
711+
return array(
712+
'optimize autocommit' => array( 'OPTIMIZE', false ),
713+
'optimize transaction' => array( 'OPTIMIZE', true ),
714+
'repair autocommit' => array( 'REPAIR', false ),
715+
'repair transaction' => array( 'REPAIR', true ),
716+
);
717+
}
718+
589719
public function testShowCollation(): void {
590720
// Simple.
591721
$this->assertQuery( 'SHOW COLLATION' );
@@ -2713,4 +2843,13 @@ public function testInformationSchemaAlterTableDropCheckConstraint(): void {
27132843
$result = $this->assertQuery( 'SELECT * FROM information_schema.check_constraints' );
27142844
$this->assertCount( 0, $result );
27152845
}
2846+
2847+
private function getSqliteSchema(): array {
2848+
return $this->sqlite->query(
2849+
"SELECT 'main' AS db, type, name, tbl_name, sql FROM sqlite_master
2850+
UNION ALL
2851+
SELECT 'temp' AS db, type, name, tbl_name, sql FROM sqlite_temp_master
2852+
ORDER BY db, type, name"
2853+
)->fetchAll( PDO::FETCH_ASSOC );
2854+
}
27162855
}

0 commit comments

Comments
 (0)