CRM_Upgrade_Incremental_Base - Add helper dropTableIfEmpty()
authorTim Otten <totten@civicrm.org>
Wed, 30 Oct 2019 00:20:46 +0000 (17:20 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 30 Oct 2019 00:20:46 +0000 (17:20 -0700)
CRM/Upgrade/Incremental/Base.php

index 48d8ed49ffbfea2b88c359512c3fceb15cf14d54..1f72699fea4d7af57b2ce6ff53899f32c5864b47 100644 (file)
@@ -279,6 +279,26 @@ class CRM_Upgrade_Incremental_Base {
     return TRUE;
   }
 
+  /**
+   * Drop a table... but only if it's empty.
+   *
+   * @param CRM_Queue_TaskContext $ctx
+   * @param string $table
+   * @return bool
+   */
+  public static function dropTableIfEmpty($ctx, $table) {
+    if (CRM_Core_DAO::checkTableExists($table)) {
+      if (!CRM_Core_DAO::checkTableHasData($table)) {
+        CRM_Core_BAO_SchemaHandler::dropTable($table);
+      }
+      else {
+        $ctx->log->warning("dropTableIfEmpty($table): Found data. Preserved table.");
+      }
+    }
+
+    return TRUE;
+  }
+
   /**
    * Rebuild Multilingual Schema.
    * @param CRM_Queue_TaskContext $ctx