3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7.alpha1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Base class for incremental upgrades
31 class CRM_Upgrade_Incremental_Base
{
32 const BATCH_SIZE
= 5000;
41 public function verifyPreDBstate(&$errors) {
46 * Compute any messages which should be displayed before upgrade.
48 * Note: This function is called iteratively for each upcoming
49 * revision to the database.
51 * @param $preUpgradeMessage
53 * a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'.
54 * @param null $currentVer
56 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
60 * Compute any messages which should be displayed after upgrade.
62 * @param string $postUpgradeMessage
65 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
67 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
71 * (Queue Task Callback)
73 * @param \CRM_Queue_TaskContext $ctx
78 public static function runSql(CRM_Queue_TaskContext
$ctx, $rev) {
79 $upgrade = new CRM_Upgrade_Form();
80 $upgrade->processSQL($rev);
86 * Syntactic sugar for adding a task.
88 * Task is (a) in this class and (b) has a high priority.
90 * After passing the $funcName, you can also pass parameters that will go to
91 * the function. Note that all params must be serializable.
93 * @param string $title
94 * @param string $funcName
96 protected function addTask($title, $funcName) {
97 $queue = CRM_Queue_Service
::singleton()->load(array(
99 'name' => CRM_Upgrade_Form
::QUEUE_NAME
,
102 $args = func_get_args();
103 $title = array_shift($args);
104 $funcName = array_shift($args);
105 $task = new CRM_Queue_Task(
106 array(get_class($this), $funcName),
110 $queue->createItem($task, array('weight' => -1));
114 * Remove a payment processor if not in use
116 * @param CRM_Queue_TaskContext $ctx
117 * @param string $name
119 * @throws \CiviCRM_API3_Exception
121 public static function removePaymentProcessorType(CRM_Queue_TaskContext
$ctx, $name) {
122 $processors = civicrm_api3('PaymentProcessor', 'getcount', array('payment_processor_type_id' => $name));
123 if (empty($processors['result'])) {
124 $result = civicrm_api3('PaymentProcessorType', 'get', array(
128 if (!empty($result['id'])) {
129 civicrm_api3('PaymentProcessorType', 'delete', array('id' => $result['id']));
136 * @param string $table_name
137 * @param string $constraint_name
140 public static function checkFKExists($table_name, $constraint_name) {
141 return CRM_Core_BAO_SchemaHandler
::checkFKExists($table_name, $constraint_name);
145 * Add a column to a table if it doesn't already exist
147 * @param CRM_Queue_TaskContext $ctx
148 * @param string $table
149 * @param string $column
150 * @param string $properties
151 * @param bool $localizable is this a field that should be localized
154 public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE) {
155 $domain = new CRM_Core_DAO_Domain();
158 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column)) {
159 if ($domain->locales
) {
161 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
162 foreach ($locales as $locale) {
163 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, "{$column}_{$locale}")) {
164 $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties";
169 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
173 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
175 foreach ($queries as $query) {
176 CRM_Core_DAO
::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
179 if ($domain->locales
) {
180 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
181 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, NULL, TRUE);
187 * Drop a column from a table if it exist.
189 * @param CRM_Queue_TaskContext $ctx
190 * @param string $table
191 * @param string $column
194 public static function dropColumn($ctx, $table, $column) {
195 if (CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column)) {
196 CRM_Core_DAO
::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`",
197 array(), TRUE, NULL, FALSE, FALSE);
203 * Add a index to a table column.
205 * @param CRM_Queue_TaskContext $ctx
206 * @param string $table
207 * @param string|array $column
210 public static function addIndex($ctx, $table, $column) {
211 $tables = array($table => (array) $column);
212 CRM_Core_BAO_SchemaHandler
::createIndexes($tables);
218 * Drop a index from a table if it exist.
220 * @param CRM_Queue_TaskContext $ctx
221 * @param string $table
222 * @param string $indexName
225 public static function dropIndex($ctx, $table, $indexName) {
226 CRM_Core_BAO_SchemaHandler
::dropIndexIfExists($table, $indexName);
232 * Rebuild Multilingual Schema.
233 * @param CRM_Queue_TaskContext $ctx
236 public static function rebuildMultilingalSchema($ctx) {
237 $domain = new CRM_Core_DAO_Domain();
239 if ($domain->locales
) {
240 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
241 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales);