Civi::queue(...$queueSpec) - Accept the 'status' and 'error' options
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FiveFiftyOne.php
CommitLineData
99ee6241
C
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
73edfc10
EM
12use Civi\Api4\MappingField;
13
99ee6241
C
14/**
15 * Upgrade logic for the 5.51.x series.
16 *
17 * Each minor version in the series is handled by either a `5.51.x.mysql.tpl` file,
18 * or a function in this class named `upgrade_5_51_x`.
19 * If only a .tpl file exists for a version, it will be run automatically.
20 * If the function exists, it must explicitly add the 'runSql' task if there is a corresponding .mysql.tpl.
21 *
22 * This class may also implement `setPreUpgradeMessage()` and `setPostUpgradeMessage()` functions.
23 */
24class CRM_Upgrade_Incremental_php_FiveFiftyOne extends CRM_Upgrade_Incremental_Base {
25
26 /**
27 * Upgrade step; adds tasks including 'runSql'.
28 *
29 * @param string $rev
30 * The version number matching this function name
31 */
32 public function upgrade_5_51_alpha1($rev): void {
33 $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
73edfc10 34 $this->addTask(ts('Convert import mappings to use names'), 'convertMappingFieldLabelsToNames', $rev);
0ea8ff07
TO
35 $this->addTask('Add column "civicrm_queue.status"', 'addColumn', 'civicrm_queue',
36 'status', "varchar(16) NULL DEFAULT 'active' COMMENT 'Execution status'");
37 $this->addTask('Add column "civicrm_queue.error"', 'addColumn', 'civicrm_queue',
38 'error', "varchar(16) NULL COMMENT 'Fallback behavior for unhandled errors'");
52374db3
TO
39 $this->addTask('Backfill "civicrm_queue.status" and "civicrm_queue.error")', 'fillQueueColumns');
40 }
41
42 public static function fillQueueColumns($ctx): bool {
43 // Generally, anything we do here is nonsensical because there shouldn't be much real world data,
44 // and the goal is to require something specific going forward (for anything that has an automatic runner).
45 // But this ensures that satisfy the invariant.
46 //
47 // What default value of "error" should apply to pre-existing queues (if they somehow exist)?
48 // Go back to our heuristic "short-term/finite queue <=> abort" vs "long-term/infinite queue <=> log".
49 // We don't have adequate data to differentiate these, so some will be wrong/suboptimal.
50 // What's the impact of getting it wrong?
51 // - For a finite/short-term queue, work has finished already (or will finish soon), so there is
52 // very limited impact to wrongly setting `error=log`.
53 // - For an infinite/long-term queue, work will continue indefinitely into the future. The impact
54 // of wrongly setting `error=abort` would continue indefinitely to the future.
55 // Therefore, backfilling `error=log` is less-problematic than backfilling `error=abort`.
56 CRM_Core_DAO::executeQuery('UPDATE civicrm_queue SET error = "delete" WHERE runner IS NOT NULL AND error IS NULL');
57 CRM_Core_DAO::executeQuery('UPDATE civicrm_queue SET status = IF(runner IS NULL, NULL, "active")');
58 return TRUE;
73edfc10
EM
59 }
60
61 /**
62 * Convert saved mapping fields for contribution imports to use name rather than
63 * label.
64 *
65 * Currently the 'name' column in civicrm_mapping_field holds names like
66 * 'First Name' or, more tragically 'Contact ID (match to contact)'.
67 *
68 * This updates them to hold the name - eg. 'total_amount'.
69 *
70 * @return bool
71 * @throws \API_Exception
72 */
73 public static function convertMappingFieldLabelsToNames(): bool {
74 $mappings = MappingField::get(FALSE)
75 ->setSelect(['id', 'name'])
76 ->addWhere('mapping_id.mapping_type_id:name', '=', 'Import Contribution')
77 ->execute();
78 $fields = CRM_Contribute_BAO_Contribution::importableFields('All', FALSE);
79 $fieldMap = [];
80 foreach ($fields as $fieldName => $field) {
81 $fieldMap[$field['title']] = $fieldName;
82 }
83 $fieldMap[ts('Soft Credit')] = 'soft_credit';
84 $fieldMap[ts('Pledge Payment')] = 'pledge_payment';
85 $fieldMap[ts(ts('Pledge ID'))] = 'pledge_id';
cbc11a37
EM
86 $fieldMap[ts(ts('Financial Type'))] = 'financial_type_id';
87 $fieldMap[ts(ts('Payment Method'))] = 'payment_instrument_id';
73edfc10
EM
88
89 foreach ($mappings as $mapping) {
90 if (!empty($fieldMap[$mapping['name']])) {
91 MappingField::update(FALSE)
92 ->addWhere('id', '=', $mapping['id'])
93 ->addValue('name', $fieldMap[$mapping['name']])
94 ->execute();
95 }
96 }
97 return TRUE;
99ee6241
C
98 }
99
100}