From e3ed50296f79ce177ca3301176dc2cf1a3cd8799 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 29 Oct 2019 17:20:46 -0700 Subject: [PATCH] CRM_Upgrade_Incremental_Base - Add helper dropTableIfEmpty() --- CRM/Upgrade/Incremental/Base.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/CRM/Upgrade/Incremental/Base.php b/CRM/Upgrade/Incremental/Base.php index 48d8ed49ff..1f72699fea 100644 --- a/CRM/Upgrade/Incremental/Base.php +++ b/CRM/Upgrade/Incremental/Base.php @@ -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 -- 2.25.1