From a7ebf7eca1950f012d66521e593dfa0e8eb5ca94 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Mon, 27 Feb 2017 15:58:35 +0000 Subject: [PATCH] Upgrader: Check for existing columns before trying to add --- CRM/CiviDiscount/Upgrader.php | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/CRM/CiviDiscount/Upgrader.php b/CRM/CiviDiscount/Upgrader.php index d8a50d5..d386f24 100644 --- a/CRM/CiviDiscount/Upgrader.php +++ b/CRM/CiviDiscount/Upgrader.php @@ -12,15 +12,31 @@ class CRM_CiviDiscount_Upgrader extends CRM_CiviDiscount_Upgrader_Base { * */ public function upgrade_2201() { - $this->ctx->log->info('Applying update 2201'); - CRM_Core_DAO::executeQuery('ALTER TABLE cividiscount_item ADD COLUMN discount_msg_enabled TINYINT(1) DEFAULT 0 AFTER is_active'); - CRM_Core_DAO::executeQuery('ALTER TABLE cividiscount_item ADD COLUMN discount_msg VARCHAR(255) AFTER discount_msg_enabled'); + if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists('cividiscount_item', 'discount_msg_enabled')) { + $this->ctx->log->info('Skipped cividiscount update 2201. Column discount_msg_enabled already present on cividiscount_item table.'); + } + else { + $this->ctx->log->info('Applying cividiscount update 2201. Adding discount_msg_enabled to the cividiscount_item table.'); + CRM_Core_DAO::executeQuery('ALTER TABLE cividiscount_item ADD COLUMN discount_msg_enabled TINYINT(1) DEFAULT 0 AFTER is_active'); + } + if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists('cividiscount_item', 'discount_msg')) { + $this->ctx->log->info('Skipped cividiscount update 2201. Column discount_msg already present on cividiscount_item table.'); + } + else { + $this->ctx->log->info('Applying cividiscount update 2201. Adding discount_msg to the cividiscount_item table.'); + CRM_Core_DAO::executeQuery('ALTER TABLE cividiscount_item ADD COLUMN discount_msg VARCHAR(255) AFTER discount_msg_enabled'); + } return TRUE; } public function upgrade_2202() { - $this->ctx->log->info('Applying update 2202'); - CRM_Core_DAO::executeQuery('ALTER TABLE cividiscount_item ADD COLUMN filters VARCHAR(255)'); + if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists('cividiscount_item', 'filters')) { + $this->ctx->log->info('Skipped cividiscount update 2202. Column filters already present on cividiscount_item table.'); + } + else { + $this->ctx->log->info('Applying cividiscount update 2202. Adding filters to the cividiscount_item table.'); + CRM_Core_DAO::executeQuery('ALTER TABLE cividiscount_item ADD COLUMN filters VARCHAR(255)'); + } return TRUE; } @@ -36,4 +52,4 @@ public function upgrade_2203() { } return TRUE; } -} +} \ No newline at end of file