Add check to see if these indices are actually removed
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 21 Jul 2021 01:45:15 +0000 (13:45 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 21 Jul 2021 07:25:54 +0000 (19:25 +1200)
This check for indices seems to be much more reliable - the question is whether we can
run it against a range of dbs to confirm

tests/phpunit/CRM/Core/InnoDBIndexerTest.php

index 7121591d4283af820fccb3c516e08fca5fecd9ff..f113a40decf7032f7c83bd7c39db5cecae99203e 100644 (file)
@@ -6,18 +6,31 @@
  */
 class CRM_Core_InnoDBIndexerTest extends CiviUnitTestCase {
 
+  /**
+   * Indices to be created or removed.
+   *
+   * @var array
+   */
+  protected $indices = [];
+
+  /**
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
   public function tearDown(): void {
-    // May or may not cleanup well if there's a bug in the indexer.
-    // This is better than nothing -- and better than duplicating the
-    // cleanup logic.
     $idx = new CRM_Core_InnoDBIndexer(FALSE, []);
-    $idx->fixSchemaDifferences();
-
+    foreach (array_keys($this->indices) as $table) {
+      foreach ($idx->dropIndexSql($table) as $sql) {
+        CRM_Core_DAO::executeQuery($sql);
+      }
+    }
+    $this->assertFullTextIndexesNotPresent();
     parent::tearDown();
   }
 
-  public function testHasDeclaredIndex() {
-    $idx = new CRM_Core_InnoDBIndexer(TRUE, [
+  public function testHasDeclaredIndex(): void {
+    $this->indices = [
       'civicrm_contact' => [
         ['first_name', 'last_name'],
         ['foo'],
@@ -25,7 +38,8 @@ class CRM_Core_InnoDBIndexerTest extends CiviUnitTestCase {
       'civicrm_email' => [
         ['whiz'],
       ],
-    ]);
+    ];
+    $idx = new CRM_Core_InnoDBIndexer(TRUE, $this->indices);
 
     $this->assertTrue($idx->hasDeclaredIndex('civicrm_contact', ['first_name', 'last_name']));
     $this->assertTrue($idx->hasDeclaredIndex('civicrm_contact', ['last_name', 'first_name']));
@@ -44,17 +58,18 @@ class CRM_Core_InnoDBIndexerTest extends CiviUnitTestCase {
   /**
    * When disabled, there is no FTS index, so queries that rely on FTS index fail.
    */
-  public function testDisabled() {
-    $idx = new CRM_Core_InnoDBIndexer(FALSE, [
+  public function testDisabled(): void {
+    $this->indices = [
       'civicrm_contact' => [
         ['first_name', 'last_name'],
       ],
-    ]);
+    ];
+    $idx = new CRM_Core_InnoDBIndexer(FALSE, $this->indices);
     $idx->fixSchemaDifferences();
 
     try {
       CRM_Core_DAO::executeQuery('SELECT id FROM civicrm_contact WHERE MATCH(first_name,last_name) AGAINST ("joe")');
-      $this->fail("Missed expected exception");
+      $this->fail('Missed expected exception');
     }
     catch (Exception $e) {
       $this->assertTrue(TRUE, 'Received expected exception');
@@ -64,27 +79,26 @@ class CRM_Core_InnoDBIndexerTest extends CiviUnitTestCase {
   /**
    * When enabled, the FTS index is created, so queries that rely on FTS work.
    */
-  public function testEnabled() {
-    if (!$this->supportsFts()) {
-      $this->markTestSkipped("Local installation of InnoDB does not support FTS.");
-      return;
-    }
-
-    $idx = new CRM_Core_InnoDBIndexer(TRUE, [
+  public function testEnabled(): void {
+    $this->indices = [
       'civicrm_contact' => [
         ['first_name', 'last_name'],
       ],
-    ]);
+    ];
+    $idx = new CRM_Core_InnoDBIndexer(TRUE, $this->indices);
     $idx->fixSchemaDifferences();
-
     CRM_Core_DAO::executeQuery('SELECT id FROM civicrm_contact WHERE MATCH(first_name,last_name) AGAINST ("joe")');
   }
 
   /**
-   * @return mixed
+   * Assert that all indices have been removed.
    */
-  public function supportsFts() {
-    return version_compare(CRM_Core_DAO::singleValueQuery('SELECT VERSION()'), '5.6.0', '>=');
+  protected function assertFullTextIndexesNotPresent(): void {
+    $this->assertEmpty(CRM_Core_DAO::singleValueQuery("
+  SELECT GROUP_CONCAT(CONCAT(table_name, ' ', index_name))
+  FROM information_Schema.STATISTICS
+  WHERE table_schema = '" . CRM_Core_DAO::getDatabaseName() . "'
+    AND index_type = 'FULLTEXT'"), 'Full text indices should have been removed');
   }
 
 }