Rename RelationshipVortex to RelationshipCache
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FiveTwentyNine.php
CommitLineData
fefec164
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
12/**
13 * Upgrade logic for FiveTwentyNine */
14class CRM_Upgrade_Incremental_php_FiveTwentyNine extends CRM_Upgrade_Incremental_Base {
15
16 /**
17 * Compute any messages which should be displayed beforeupgrade.
18 *
19 * Note: This function is called iteratively for each upcoming
20 * revision to the database.
21 *
22 * @param string $preUpgradeMessage
23 * @param string $rev
24 * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'.
25 * @param null $currentVer
26 */
27 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
28 // Example: Generate a pre-upgrade message.
29 // if ($rev == '5.12.34') {
30 // $preUpgradeMessage .= '<p>' . ts('A new permission, "%1", has been added. This permission is now used to control access to the Manage Tags screen.', array(1 => ts('manage tags'))) . '</p>';
31 // }
32 }
33
34 /**
35 * Compute any messages which should be displayed after upgrade.
36 *
37 * @param string $postUpgradeMessage
38 * alterable.
39 * @param string $rev
40 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
41 */
42 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
43 // Example: Generate a post-upgrade message.
44 // if ($rev == '5.12.34') {
45 // $postUpgradeMessage .= '<br /><br />' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
46 // }
47 }
48
49 /*
50 * Important! All upgrade functions MUST add a 'runSql' task.
51 * Uncomment and use the following template for a new upgrade version
52 * (change the x in the function name):
53 */
54
685bf3d2 55 /**
56 * Upgrade function.
57 *
58 * @param string $rev
59 */
60 public function upgrade_5_29_alpha1($rev) {
61 $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
62 $this->addTask('Install eventcart extension', 'installEventCart');
b53dc556
TO
63
64 list($minId, $maxId) = CRM_Core_DAO::executeQuery("SELECT coalesce(min(id),0), coalesce(max(id),0)
65 FROM civicrm_relationship ")->getDatabaseResult()->fetchRow();
66 for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
67 $endId = $startId + self::BATCH_SIZE - 1;
bcf70e08 68 $title = ts("Upgrade DB to %1: Fill civicrm_relationship_cache (%2 => %3)", [
b53dc556
TO
69 1 => $rev,
70 2 => $startId,
71 3 => $endId,
72 ]);
bcf70e08 73 $this->addTask($title, 'populateRelationshipCache', $startId, $endId);
b53dc556 74 }
685bf3d2 75 }
fefec164 76
685bf3d2 77 /**
78 * Install sequentialCreditNotes extension.
79 *
80 * This feature is restructured as a core extension - which is primarily a code cleanup step.
81 *
82 * @param \CRM_Queue_TaskContext $ctx
83 *
84 * @return bool
85 *
86 * @throws \CiviCRM_API3_Exception
87 * @throws \CRM_Core_Exception
88 */
89 public static function installEventCart(CRM_Queue_TaskContext $ctx) {
90 // Install via direct SQL manipulation. Note that:
91 // (1) This extension has no activation logic.
92 // (2) On new installs, the extension is activated purely via default SQL INSERT.
93 // (3) Caches are flushed at the end of the upgrade.
94 // ($) Over long term, upgrade steps are more reliable in SQL. API/BAO sometimes don't work mid-upgrade.
95 $insert = CRM_Utils_SQL_Insert::into('civicrm_extension')->row([
96 'type' => 'module',
97 'full_name' => 'eventcart',
98 'name' => 'eventcart',
99 'label' => 'Event Cart',
100 'file' => 'eventcart',
101 'schema_version' => NULL,
102 'is_active' => 1,
103 ]);
104 CRM_Core_DAO::executeQuery($insert->usingReplace()->toSQL());
b53dc556
TO
105
106 return TRUE;
107 }
108
109 /**
110 * @param \CRM_Queue_TaskContext $ctx
111 * @param int $startId
112 * The lowest relationship ID that should be updated.
113 * @param int $endId
114 * The highest relationship ID that should be updated.
115 * @return bool
116 * TRUE on success
117 */
bcf70e08
TO
118 public static function populateRelationshipCache(CRM_Queue_TaskContext $ctx, $startId, $endId) {
119 // NOTE: We duplicate CRM_Contact_BAO_RelationshipCache::$mappings in case
b53dc556
TO
120 // the schema evolves over multiple releases.
121 $mappings = [
122 'a_b' => [
123 'relationship_id' => 'rel.id',
124 'relationship_type_id' => 'rel.relationship_type_id',
125 'orientation' => '"a_b"',
126 'near_contact_id' => 'rel.contact_id_a',
127 'near_relation' => 'reltype.name_a_b',
128 'far_contact_id' => 'rel.contact_id_b',
129 'far_relation' => 'reltype.name_b_a',
130 'start_date' => 'rel.start_date',
131 'end_date' => 'rel.end_date',
132 'is_active' => 'rel.is_active',
133 'case_id' => 'rel.case_id',
134 ],
135 'b_a' => [
136 'relationship_id' => 'rel.id',
137 'relationship_type_id' => 'rel.relationship_type_id',
138 'orientation' => '"b_a"',
139 'near_contact_id' => 'rel.contact_id_b',
140 'near_relation' => 'reltype.name_b_a',
141 'far_contact_id' => 'rel.contact_id_a',
142 'far_relation' => 'reltype.name_a_b',
143 'start_date' => 'rel.start_date',
144 'end_date' => 'rel.end_date',
145 'is_active' => 'rel.is_active',
146 'case_id' => 'rel.case_id',
147 ],
148 ];
149 $keyFields = ['relationship_id', 'orientation'];
150
151 foreach ($mappings as $mapping) {
152 $query = CRM_Utils_SQL_Select::from('civicrm_relationship rel')
153 ->join('reltype', 'INNER JOIN civicrm_relationship_type reltype ON rel.relationship_type_id = reltype.id')
bcf70e08 154 ->syncInto('civicrm_relationship_cache', $keyFields, $mapping)
b53dc556
TO
155 ->where('rel.id >= #START AND rel.id <= #END', [
156 '#START' => $startId,
157 '#END' => $endId,
158 ]);
159 $query->execute();
160 }
161
685bf3d2 162 return TRUE;
163 }
fefec164
C
164
165}