Commit | Line | Data |
---|---|---|
bf6a5362 CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
bf6a5362 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
bf6a5362 CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
ff6f993e | 12 | use Civi\Core\SettingsBag; |
13 | ||
bf6a5362 CW |
14 | /** |
15 | * Base class for incremental upgrades | |
16 | */ | |
17 | class CRM_Upgrade_Incremental_Base { | |
18 | const BATCH_SIZE = 5000; | |
19 | ||
d7e4d371 TO |
20 | /** |
21 | * @var string|null | |
22 | */ | |
23 | protected $majorMinor; | |
24 | ||
25 | /** | |
26 | * Get the major and minor version for this class (based on English-style class name). | |
27 | * | |
28 | * @return string | |
29 | * Ex: '5.34' or '4.7' | |
30 | */ | |
31 | public function getMajorMinor() { | |
32 | if (!$this->majorMinor) { | |
33 | $className = explode('_', static::CLASS); | |
34 | $numbers = preg_split("/([[:upper:]][[:lower:]]+)/", array_pop($className), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); | |
35 | $major = CRM_Utils_EnglishNumber::toInt(array_shift($numbers)); | |
36 | $minor = CRM_Utils_EnglishNumber::toInt(implode('', $numbers)); | |
37 | $this->majorMinor = $major . '.' . $minor; | |
38 | } | |
39 | return $this->majorMinor; | |
40 | } | |
41 | ||
42 | /** | |
43 | * Get a list of revisions (PATCH releases) related to this class. | |
44 | * | |
45 | * @return array | |
46 | * Ex: ['4.5.6', '4.5.7'] | |
47 | * @throws \ReflectionException | |
48 | */ | |
49 | public function getRevisionSequence() { | |
50 | $revList = []; | |
51 | ||
52 | $sqlGlob = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'sql', $this->getMajorMinor() . '.*.mysql.tpl']); | |
53 | $sqlFiles = glob($sqlGlob);; | |
54 | foreach ($sqlFiles as $file) { | |
55 | $revList[] = str_replace('.mysql.tpl', '', basename($file)); | |
56 | } | |
57 | ||
58 | $c = new ReflectionClass(static::class); | |
59 | foreach ($c->getMethods() as $method) { | |
60 | /** @var \ReflectionMethod $method */ | |
61 | if (preg_match(';^upgrade_([0-9_alphabeta]+)$;', $method->getName(), $m)) { | |
62 | $revList[] = str_replace('_', '.', $m[1]); | |
63 | } | |
64 | } | |
65 | ||
66 | $revList = array_unique($revList); | |
67 | usort($revList, 'version_compare'); | |
68 | return $revList; | |
69 | } | |
70 | ||
bf6a5362 CW |
71 | /** |
72 | * Compute any messages which should be displayed before upgrade. | |
73 | * | |
76a2bc7c | 74 | * Downstream classes should implement this method to generate their messages. |
bf6a5362 | 75 | * |
76a2bc7c TO |
76 | * This method will be invoked multiple times. Implementations MUST consult the `$rev` |
77 | * before deciding what messages to add. See the examples linked below. | |
78 | * | |
79 | * @see \CRM_Upgrade_Incremental_php_FourSeven::setPreUpgradeMessage() | |
80 | * @see \CRM_Upgrade_Incremental_php_FiveTwenty::setPreUpgradeMessage() | |
81 | * | |
82 | * @param string $preUpgradeMessage | |
83 | * Accumulated list of messages. Alterable. | |
bf6a5362 | 84 | * @param string $rev |
76a2bc7c TO |
85 | * The incremental version number. (Called repeatedly, once for each increment.) |
86 | * | |
87 | * Ex: Suppose the system upgrades from 5.7.3 to 5.10.0. The method FiveEight::setPreUpgradeMessage() | |
88 | * will be called for each increment of '5.8.*' ('5.8.alpha1' => '5.8.beta1' => '5.8.0'). | |
bf6a5362 | 89 | * @param null $currentVer |
76a2bc7c TO |
90 | * This is the penultimate version targeted by the upgrader. |
91 | * Equivalent to CRM_Utils_System::version(). | |
bf6a5362 CW |
92 | */ |
93 | public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { | |
94 | } | |
95 | ||
96 | /** | |
97 | * Compute any messages which should be displayed after upgrade. | |
98 | * | |
76a2bc7c TO |
99 | * Downstream classes should implement this method to generate their messages. |
100 | * | |
101 | * This method will be invoked multiple times. Implementations MUST consult the `$rev` | |
102 | * before deciding what messages to add. See the examples linked below. | |
103 | * | |
104 | * @see \CRM_Upgrade_Incremental_php_FourSeven::setPostUpgradeMessage() | |
105 | * @see \CRM_Upgrade_Incremental_php_FiveTwentyOne::setPostUpgradeMessage() | |
106 | * | |
bf6a5362 | 107 | * @param string $postUpgradeMessage |
76a2bc7c | 108 | * Accumulated list of messages. Alterable. |
bf6a5362 | 109 | * @param string $rev |
76a2bc7c TO |
110 | * The incremental version number. (Called repeatedly, once for each increment.) |
111 | * | |
112 | * Ex: Suppose the system upgrades from 5.7.3 to 5.10.0. The method FiveEight::setPreUpgradeMessage() | |
113 | * will be called for each increment of '5.8.*' ('5.8.alpha1' => '5.8.beta1' => '5.8.0'). | |
bf6a5362 CW |
114 | */ |
115 | public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { | |
116 | } | |
117 | ||
bf6a5362 CW |
118 | /** |
119 | * (Queue Task Callback) | |
54957108 | 120 | * |
121 | * @param \CRM_Queue_TaskContext $ctx | |
122 | * @param string $rev | |
123 | * | |
124 | * @return bool | |
bf6a5362 CW |
125 | */ |
126 | public static function runSql(CRM_Queue_TaskContext $ctx, $rev) { | |
127 | $upgrade = new CRM_Upgrade_Form(); | |
128 | $upgrade->processSQL($rev); | |
129 | ||
130 | return TRUE; | |
131 | } | |
132 | ||
133 | /** | |
54957108 | 134 | * Syntactic sugar for adding a task. |
135 | * | |
136 | * Task is (a) in this class and (b) has a high priority. | |
bf6a5362 CW |
137 | * |
138 | * After passing the $funcName, you can also pass parameters that will go to | |
139 | * the function. Note that all params must be serializable. | |
54957108 | 140 | * |
141 | * @param string $title | |
142 | * @param string $funcName | |
bf6a5362 CW |
143 | */ |
144 | protected function addTask($title, $funcName) { | |
be2fb01f | 145 | $queue = CRM_Queue_Service::singleton()->load([ |
bf6a5362 CW |
146 | 'type' => 'Sql', |
147 | 'name' => CRM_Upgrade_Form::QUEUE_NAME, | |
be2fb01f | 148 | ]); |
bf6a5362 CW |
149 | |
150 | $args = func_get_args(); | |
151 | $title = array_shift($args); | |
152 | $funcName = array_shift($args); | |
153 | $task = new CRM_Queue_Task( | |
be2fb01f | 154 | [get_class($this), $funcName], |
bf6a5362 CW |
155 | $args, |
156 | $title | |
157 | ); | |
be2fb01f | 158 | $queue->createItem($task, ['weight' => -1]); |
bf6a5362 CW |
159 | } |
160 | ||
8985d47e TO |
161 | /** |
162 | * Add a task to store a snapshot of some data (if upgrade-snapshots are supported). | |
163 | * | |
164 | * If there is a large amount of data, this may actually add multiple tasks. | |
165 | * | |
166 | * Ex :$this->addSnapshotTask('event_dates', CRM_Utils_SQL_Select::from('civicrm_event') | |
167 | * ->select('id, start_date, end_date')); | |
168 | * | |
169 | * @param string $name | |
170 | * Logical name for the snapshot. This will become part of the table. | |
171 | * @param \CRM_Utils_SQL_Select $select | |
172 | * @throws \CRM_Core_Exception | |
173 | */ | |
174 | protected function addSnapshotTask(string $name, CRM_Utils_SQL_Select $select): void { | |
1602aa08 | 175 | CRM_Upgrade_Snapshot::createTableName('civicrm', $this->getMajorMinor(), $name); |
ba3f169d TO |
176 | // ^^ To simplify QA -- we should always throw an exception for bad snapshot names, even if the local policy doesn't use snapshots. |
177 | ||
8985d47e TO |
178 | if (!empty(CRM_Upgrade_Snapshot::getActivationIssues())) { |
179 | return; | |
180 | } | |
181 | ||
182 | $queue = CRM_Queue_Service::singleton()->load([ | |
183 | 'type' => 'Sql', | |
184 | 'name' => CRM_Upgrade_Form::QUEUE_NAME, | |
185 | ]); | |
1602aa08 | 186 | foreach (CRM_Upgrade_Snapshot::createTasks('civicrm', $this->getMajorMinor(), $name, $select) as $task) { |
8985d47e TO |
187 | $queue->createItem($task, ['weight' => -1]); |
188 | } | |
189 | } | |
190 | ||
6b5c1968 TO |
191 | /** |
192 | * Add a task to activate an extension. This task will run post-upgrade (after all | |
193 | * changes to core DB are settled). | |
194 | * | |
195 | * @param string $title | |
196 | * @param string[] $keys | |
197 | * List of extensions to enable. | |
198 | */ | |
199 | protected function addExtensionTask(string $title, array $keys): void { | |
200 | Civi::queue(CRM_Upgrade_Form::QUEUE_NAME)->createItem( | |
201 | new CRM_Queue_Task([static::CLASS, 'enableExtension'], [$keys], $title), | |
202 | ['weight' => 2000] | |
203 | ); | |
204 | } | |
205 | ||
206 | /** | |
207 | * @param \CRM_Queue_TaskContext $ctx | |
208 | * @param string[] $keys | |
209 | * List of extensions to enable. | |
210 | * @return bool | |
211 | */ | |
212 | public static function enableExtension(CRM_Queue_TaskContext $ctx, array $keys): bool { | |
213 | // The `enableExtension` has a very high value of `weight`, so this runs after all | |
214 | // core DB schema updates have been resolved. We can use high-level services. | |
215 | ||
216 | Civi::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy::get('upgrade.finish')); | |
217 | $restore = \CRM_Utils_AutoClean::with(function() { | |
218 | Civi::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy::get('upgrade.main')); | |
219 | }); | |
220 | ||
221 | $manager = CRM_Extension_System::singleton()->getManager(); | |
222 | $manager->enable($manager->findInstallRequirements($keys)); | |
223 | ||
224 | // Hrm, `enable()` normally does these things... but not during upgrade... | |
225 | // Note: A good test-scenario is to install 5.45; enable logging and CiviGrant; disable searchkit+afform; then upgrade to 5.47. | |
226 | $schema = new CRM_Logging_Schema(); | |
227 | $schema->fixSchemaDifferences(); | |
a1812327 TO |
228 | |
229 | CRM_Core_Invoke::rebuildMenuAndCaches(FALSE, FALSE); | |
230 | // sessionReset is FALSE because upgrade status/postUpgradeMessages are needed by the page. We reset later in doFinish(). | |
6b5c1968 TO |
231 | |
232 | return TRUE; | |
233 | } | |
234 | ||
058b8a5e CW |
235 | /** |
236 | * Remove a payment processor if not in use | |
237 | * | |
87a33a95 CW |
238 | * @param CRM_Queue_TaskContext $ctx |
239 | * @param string $name | |
240 | * @return bool | |
058b8a5e CW |
241 | * @throws \CiviCRM_API3_Exception |
242 | */ | |
87a33a95 | 243 | public static function removePaymentProcessorType(CRM_Queue_TaskContext $ctx, $name) { |
be2fb01f | 244 | $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]); |
058b8a5e | 245 | if (empty($processors['result'])) { |
be2fb01f | 246 | $result = civicrm_api3('PaymentProcessorType', 'get', [ |
058b8a5e CW |
247 | 'name' => $name, |
248 | 'return' => 'id', | |
be2fb01f | 249 | ]); |
058b8a5e | 250 | if (!empty($result['id'])) { |
be2fb01f | 251 | civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]); |
058b8a5e CW |
252 | } |
253 | } | |
87a33a95 | 254 | return TRUE; |
058b8a5e CW |
255 | } |
256 | ||
27e82c24 SL |
257 | /** |
258 | * @param string $table_name | |
259 | * @param string $constraint_name | |
260 | * @return bool | |
261 | */ | |
262 | public static function checkFKExists($table_name, $constraint_name) { | |
263 | return CRM_Core_BAO_SchemaHandler::checkFKExists($table_name, $constraint_name); | |
264 | } | |
265 | ||
b412f764 CW |
266 | /** |
267 | * Add a column to a table if it doesn't already exist | |
268 | * | |
269 | * @param CRM_Queue_TaskContext $ctx | |
270 | * @param string $table | |
271 | * @param string $column | |
272 | * @param string $properties | |
61612722 | 273 | * @param bool $localizable is this a field that should be localized |
9f266042 | 274 | * @param string|null $version CiviCRM version to use if rebuilding multilingual schema |
35fd9d21 | 275 | * @param bool $triggerRebuild should we trigger the rebuild of the multilingual schema |
9f266042 | 276 | * |
b412f764 CW |
277 | * @return bool |
278 | */ | |
35fd9d21 | 279 | public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL, $triggerRebuild = TRUE) { |
394d18d3 | 280 | $locales = CRM_Core_I18n::getMultilingual(); |
be2fb01f | 281 | $queries = []; |
ce33da5a | 282 | if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column, FALSE)) { |
394d18d3 | 283 | if ($locales) { |
61612722 | 284 | if ($localizable) { |
61612722 | 285 | foreach ($locales as $locale) { |
ce33da5a | 286 | if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, "{$column}_{$locale}", FALSE)) { |
41ace555 SL |
287 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties"; |
288 | } | |
61612722 SL |
289 | } |
290 | } | |
291 | else { | |
292 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties"; | |
293 | } | |
294 | } | |
295 | else { | |
296 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties"; | |
297 | } | |
298 | foreach ($queries as $query) { | |
be2fb01f | 299 | CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE); |
61612722 SL |
300 | } |
301 | } | |
35fd9d21 | 302 | if ($locales && $triggerRebuild) { |
76ee148d | 303 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version, TRUE); |
150676ad | 304 | } |
b412f764 CW |
305 | return TRUE; |
306 | } | |
307 | ||
c87e11eb EM |
308 | /** |
309 | * Add the specified option group, gracefully if it already exists. | |
310 | * | |
311 | * @param CRM_Queue_TaskContext $ctx | |
312 | * @param array $params | |
313 | * @param array $options | |
314 | * | |
315 | * @return bool | |
316 | */ | |
317 | public static function addOptionGroup(CRM_Queue_TaskContext $ctx, $params, $options): bool { | |
318 | $defaults = ['is_active' => 1]; | |
319 | $optionDefaults = ['is_active' => 1]; | |
320 | $optionDefaults['option_group_id'] = \CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array_merge($defaults, $params)); | |
321 | ||
322 | foreach ($options as $option) { | |
323 | \CRM_Core_BAO_OptionValue::ensureOptionValueExists(array_merge($optionDefaults, $option)); | |
324 | } | |
325 | return TRUE; | |
326 | } | |
327 | ||
fe83c251 | 328 | /** |
329 | * Do any relevant message template updates. | |
330 | * | |
331 | * @param CRM_Queue_TaskContext $ctx | |
332 | * @param string $version | |
333 | */ | |
334 | public static function updateMessageTemplates($ctx, $version) { | |
335 | $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
336 | $messageTemplateObject->updateTemplates(); | |
8515e10c | 337 | } |
fe83c251 | 338 | |
8515e10c EM |
339 | /** |
340 | * Updated a message token within a template. | |
341 | * | |
342 | * @param CRM_Queue_TaskContext $ctx | |
343 | * @param string $workflowName | |
344 | * @param string $old | |
345 | * @param string $new | |
346 | * @param $version | |
347 | * | |
348 | * @return bool | |
349 | */ | |
350 | public static function updateMessageToken($ctx, string $workflowName, string $old, string $new, $version):bool { | |
351 | $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
fa547918 EM |
352 | if (!empty($workflowName)) { |
353 | $messageObj->replaceTokenInTemplate($workflowName, $old, $new); | |
354 | } | |
355 | else { | |
356 | $messageObj->replaceTokenInMessageTemplates($old, $new); | |
357 | } | |
8515e10c | 358 | return TRUE; |
fe83c251 | 359 | } |
360 | ||
b2148ce1 | 361 | /** |
8ff591cb | 362 | * Updated a message token within a scheduled reminder. |
b2148ce1 EM |
363 | * |
364 | * @param CRM_Queue_TaskContext $ctx | |
365 | * @param string $old | |
366 | * @param string $new | |
367 | * @param $version | |
368 | * | |
369 | * @return bool | |
370 | */ | |
371 | public static function updateActionScheduleToken($ctx, string $old, string $new, $version):bool { | |
372 | $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
373 | $messageObj->replaceTokenInActionSchedule($old, $new); | |
374 | return TRUE; | |
375 | } | |
376 | ||
8ff591cb | 377 | /** |
2f7db718 | 378 | * Updated a message token within a label. |
8ff591cb EM |
379 | * |
380 | * @param CRM_Queue_TaskContext $ctx | |
381 | * @param string $old | |
382 | * @param string $new | |
383 | * @param $version | |
384 | * | |
385 | * @return bool | |
386 | */ | |
387 | public static function updatePrintLabelToken($ctx, string $old, string $new, $version):bool { | |
388 | $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
389 | $messageObj->replaceTokenInPrintLabel($old, $new); | |
390 | return TRUE; | |
391 | } | |
392 | ||
2f7db718 EM |
393 | /** |
394 | * Updated a message token within greeting options. | |
395 | * | |
396 | * @param CRM_Queue_TaskContext $ctx | |
397 | * @param string $old | |
398 | * @param string $new | |
399 | * @param $version | |
400 | * | |
401 | * @return bool | |
402 | */ | |
403 | public static function updateGreetingOptions($ctx, string $old, string $new, $version):bool { | |
404 | $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
405 | $messageObj->replaceTokenInGreetingOptions($old, $new); | |
406 | return TRUE; | |
407 | } | |
408 | ||
28da6cc0 ML |
409 | /** |
410 | * Updated a currency in civicrm_currency and related configurations | |
411 | * | |
412 | * @param CRM_Queue_TaskContext $ctx | |
413 | * @param string $old_name | |
414 | * @param string $new_name | |
415 | * | |
416 | * @return bool | |
417 | */ | |
418 | public static function updateCurrencyName($ctx, string $old_name, string $new_name): bool { | |
419 | CRM_Core_DAO::executeQuery('UPDATE civicrm_currency SET name = %1 WHERE name = %2', [ | |
420 | 1 => [$new_name, 'String'], | |
421 | 2 => [$old_name, 'String'], | |
422 | ]); | |
423 | ||
424 | $oid = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_option_group WHERE name = 'currencies_enabled'"); | |
425 | if ($oid) { | |
426 | CRM_Core_DAO::executeQuery('UPDATE civicrm_option_value SET value = %1 WHERE value = %2 AND option_group_id = %3', [ | |
427 | 1 => [$new_name, 'String'], | |
428 | 2 => [$old_name, 'String'], | |
429 | 3 => [$oid, 'String'], | |
430 | ]); | |
431 | } | |
432 | ||
433 | CRM_Core_DAO::executeQuery('UPDATE civicrm_participant SET fee_currency = %1 WHERE fee_currency = %2', [ | |
434 | 1 => [$new_name, 'String'], | |
435 | 2 => [$old_name, 'String'], | |
436 | ]); | |
437 | ||
438 | $tables = [ | |
439 | 'civicrm_contribution', | |
440 | 'civicrm_contribution_page', | |
441 | 'civicrm_contribution_recur', | |
442 | 'civicrm_contribution_soft', | |
443 | 'civicrm_event', | |
444 | 'civicrm_financial_item', | |
445 | 'civicrm_financial_trxn', | |
446 | 'civicrm_grant', | |
447 | 'civicrm_pcp', | |
448 | 'civicrm_pledge_payment', | |
449 | 'civicrm_pledge', | |
450 | 'civicrm_product', | |
451 | ]; | |
452 | ||
453 | foreach ($tables as $table) { | |
454 | CRM_Core_DAO::executeQuery('UPDATE %3 SET currency = %1 WHERE currency = %2', [ | |
455 | 1 => [$new_name, 'String'], | |
456 | 2 => [$old_name, 'String'], | |
457 | 3 => [$table, 'MysqlColumnNameOrAlias'], | |
458 | ]); | |
459 | } | |
460 | ||
461 | return TRUE; | |
462 | } | |
463 | ||
504770b4 | 464 | /** |
465 | * Re-save any valid values from contribute settings into the normal setting | |
466 | * format. | |
467 | * | |
468 | * We render the array of contribution_invoice_settings and any that have | |
469 | * metadata defined we add to the correct key. This is safe to run even if no | |
470 | * settings are to be converted, per the test in | |
471 | * testConvertUpgradeContributeSettings. | |
472 | * | |
473 | * @param $ctx | |
474 | * | |
475 | * @return bool | |
476 | */ | |
477 | public static function updateContributeSettings($ctx) { | |
ff6f993e | 478 | // Use a direct query as api now does some handling on this. |
479 | $settings = CRM_Core_DAO::executeQuery("SELECT value, domain_id FROM civicrm_setting WHERE name = 'contribution_invoice_settings'"); | |
480 | ||
481 | while ($settings->fetch()) { | |
482 | $contributionSettings = (array) CRM_Utils_String::unserialize($settings->value); | |
483 | foreach (array_merge(SettingsBag::getContributionInvoiceSettingKeys(), ['deferred_revenue_enabled' => 'deferred_revenue_enabled']) as $possibleKeyName => $settingName) { | |
484 | if (!empty($contributionSettings[$possibleKeyName]) && empty(Civi::settings($settings->domain_id)->getExplicit($settingName))) { | |
485 | Civi::settings($settings->domain_id)->set($settingName, $contributionSettings[$possibleKeyName]); | |
486 | } | |
487 | } | |
504770b4 | 488 | } |
489 | return TRUE; | |
490 | } | |
491 | ||
7015248a | 492 | /** |
493 | * Do any relevant smart group updates. | |
494 | * | |
495 | * @param CRM_Queue_TaskContext $ctx | |
ac241c34 | 496 | * @param array $actions |
7015248a | 497 | * |
498 | * @return bool | |
499 | */ | |
adcd5156 | 500 | public static function updateSmartGroups($ctx, $actions) { |
ac241c34 CW |
501 | $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups(); |
502 | $groupUpdateObject->updateGroups($actions); | |
7015248a | 503 | return TRUE; |
504 | } | |
505 | ||
21ca2cb6 | 506 | /** |
507 | * Drop a column from a table if it exist. | |
508 | * | |
509 | * @param CRM_Queue_TaskContext $ctx | |
510 | * @param string $table | |
511 | * @param string $column | |
512 | * @return bool | |
513 | */ | |
514 | public static function dropColumn($ctx, $table, $column) { | |
515 | if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) { | |
516 | CRM_Core_DAO::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`", | |
be2fb01f | 517 | [], TRUE, NULL, FALSE, FALSE); |
21ca2cb6 | 518 | } |
519 | return TRUE; | |
520 | } | |
521 | ||
19415e64 | 522 | /** |
523 | * Add a index to a table column. | |
524 | * | |
525 | * @param CRM_Queue_TaskContext $ctx | |
526 | * @param string $table | |
8a493ab9 CW |
527 | * @param string|array $columns |
528 | * @param string $prefix | |
19415e64 | 529 | * @return bool |
530 | */ | |
8a493ab9 CW |
531 | public static function addIndex($ctx, $table, $columns, $prefix = 'index') { |
532 | $tables = [$table => (array) $columns]; | |
533 | CRM_Core_BAO_SchemaHandler::createIndexes($tables, $prefix); | |
19415e64 | 534 | |
535 | return TRUE; | |
536 | } | |
537 | ||
538 | /** | |
539 | * Drop a index from a table if it exist. | |
540 | * | |
541 | * @param CRM_Queue_TaskContext $ctx | |
542 | * @param string $table | |
543 | * @param string $indexName | |
544 | * @return bool | |
545 | */ | |
546 | public static function dropIndex($ctx, $table, $indexName) { | |
547 | CRM_Core_BAO_SchemaHandler::dropIndexIfExists($table, $indexName); | |
548 | ||
549 | return TRUE; | |
550 | } | |
551 | ||
e3ed5029 TO |
552 | /** |
553 | * Drop a table... but only if it's empty. | |
554 | * | |
555 | * @param CRM_Queue_TaskContext $ctx | |
556 | * @param string $table | |
557 | * @return bool | |
558 | */ | |
559 | public static function dropTableIfEmpty($ctx, $table) { | |
560 | if (CRM_Core_DAO::checkTableExists($table)) { | |
561 | if (!CRM_Core_DAO::checkTableHasData($table)) { | |
562 | CRM_Core_BAO_SchemaHandler::dropTable($table); | |
563 | } | |
564 | else { | |
565 | $ctx->log->warning("dropTableIfEmpty($table): Found data. Preserved table."); | |
566 | } | |
567 | } | |
568 | ||
569 | return TRUE; | |
570 | } | |
571 | ||
81e30d0a SL |
572 | /** |
573 | * Rebuild Multilingual Schema. | |
574 | * @param CRM_Queue_TaskContext $ctx | |
9f266042 | 575 | * @param string|null $version CiviCRM version to use if rebuilding multilingual schema |
576 | * | |
81e30d0a SL |
577 | * @return bool |
578 | */ | |
76ee148d | 579 | public static function rebuildMultilingalSchema($ctx, $version = NULL) { |
394d18d3 CW |
580 | $locales = CRM_Core_I18n::getMultilingual(); |
581 | if ($locales) { | |
76ee148d | 582 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version); |
81e30d0a SL |
583 | } |
584 | return TRUE; | |
585 | } | |
586 | ||
bf6a5362 | 587 | } |