@@ -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