Merge pull request #14118 from eileenmcnaughton/no_fin_item
[civicrm-core.git] / CRM / Upgrade / Incremental / Base.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 .alpha1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Base class for incremental upgrades
30 */
31 class CRM_Upgrade_Incremental_Base {
32 const BATCH_SIZE = 5000;
33
34 /**
35 * Verify DB state.
36 *
37 * @param $errors
38 *
39 * @return bool
40 */
41 public function verifyPreDBstate(&$errors) {
42 return TRUE;
43 }
44
45 /**
46 * Compute any messages which should be displayed before upgrade.
47 *
48 * Note: This function is called iteratively for each upcoming
49 * revision to the database.
50 *
51 * @param $preUpgradeMessage
52 * @param string $rev
53 * a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'.
54 * @param null $currentVer
55 */
56 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
57 }
58
59 /**
60 * Compute any messages which should be displayed after upgrade.
61 *
62 * @param string $postUpgradeMessage
63 * alterable.
64 * @param string $rev
65 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
66 */
67 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
68 }
69
70 /**
71 * (Queue Task Callback)
72 *
73 * @param \CRM_Queue_TaskContext $ctx
74 * @param string $rev
75 *
76 * @return bool
77 */
78 public static function runSql(CRM_Queue_TaskContext $ctx, $rev) {
79 $upgrade = new CRM_Upgrade_Form();
80 $upgrade->processSQL($rev);
81
82 return TRUE;
83 }
84
85 /**
86 * Syntactic sugar for adding a task.
87 *
88 * Task is (a) in this class and (b) has a high priority.
89 *
90 * After passing the $funcName, you can also pass parameters that will go to
91 * the function. Note that all params must be serializable.
92 *
93 * @param string $title
94 * @param string $funcName
95 */
96 protected function addTask($title, $funcName) {
97 $queue = CRM_Queue_Service::singleton()->load([
98 'type' => 'Sql',
99 'name' => CRM_Upgrade_Form::QUEUE_NAME,
100 ]);
101
102 $args = func_get_args();
103 $title = array_shift($args);
104 $funcName = array_shift($args);
105 $task = new CRM_Queue_Task(
106 [get_class($this), $funcName],
107 $args,
108 $title
109 );
110 $queue->createItem($task, ['weight' => -1]);
111 }
112
113 /**
114 * Remove a payment processor if not in use
115 *
116 * @param CRM_Queue_TaskContext $ctx
117 * @param string $name
118 * @return bool
119 * @throws \CiviCRM_API3_Exception
120 */
121 public static function removePaymentProcessorType(CRM_Queue_TaskContext $ctx, $name) {
122 $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]);
123 if (empty($processors['result'])) {
124 $result = civicrm_api3('PaymentProcessorType', 'get', [
125 'name' => $name,
126 'return' => 'id',
127 ]);
128 if (!empty($result['id'])) {
129 civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]);
130 }
131 }
132 return TRUE;
133 }
134
135 /**
136 * @param string $table_name
137 * @param string $constraint_name
138 * @return bool
139 */
140 public static function checkFKExists($table_name, $constraint_name) {
141 return CRM_Core_BAO_SchemaHandler::checkFKExists($table_name, $constraint_name);
142 }
143
144 /**
145 * Add a column to a table if it doesn't already exist
146 *
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
152 * @param string|NULL $version CiviCRM version to use if rebuilding multilingual schema
153 * @return bool
154 */
155 public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL) {
156 $domain = new CRM_Core_DAO_Domain();
157 $domain->find(TRUE);
158 $queries = [];
159 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column, FALSE)) {
160 if ($domain->locales) {
161 if ($localizable) {
162 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
163 foreach ($locales as $locale) {
164 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, "{$column}_{$locale}", FALSE)) {
165 $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties";
166 }
167 }
168 }
169 else {
170 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
171 }
172 }
173 else {
174 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
175 }
176 foreach ($queries as $query) {
177 CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
178 }
179 }
180 if ($domain->locales) {
181 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
182 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version, TRUE);
183 }
184 return TRUE;
185 }
186
187 /**
188 * Do any relevant message template updates.
189 *
190 * @param CRM_Queue_TaskContext $ctx
191 * @param string $version
192 */
193 public static function updateMessageTemplates($ctx, $version) {
194 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version);
195 $messageTemplateObject->updateTemplates();
196
197 }
198
199 /**
200 * Do any relevant smart group updates.
201 *
202 * @param CRM_Queue_TaskContext $ctx
203 * @param array $actions
204 *
205 * @return bool
206 */
207 public function updateSmartGroups($ctx, $actions) {
208 $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups();
209 $groupUpdateObject->updateGroups($actions);
210 return TRUE;
211 }
212
213 /**
214 * Drop a column from a table if it exist.
215 *
216 * @param CRM_Queue_TaskContext $ctx
217 * @param string $table
218 * @param string $column
219 * @return bool
220 */
221 public static function dropColumn($ctx, $table, $column) {
222 if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) {
223 CRM_Core_DAO::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`",
224 [], TRUE, NULL, FALSE, FALSE);
225 }
226 return TRUE;
227 }
228
229 /**
230 * Add a index to a table column.
231 *
232 * @param CRM_Queue_TaskContext $ctx
233 * @param string $table
234 * @param string|array $column
235 * @return bool
236 */
237 public static function addIndex($ctx, $table, $column) {
238 $tables = [$table => (array) $column];
239 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
240
241 return TRUE;
242 }
243
244 /**
245 * Drop a index from a table if it exist.
246 *
247 * @param CRM_Queue_TaskContext $ctx
248 * @param string $table
249 * @param string $indexName
250 * @return bool
251 */
252 public static function dropIndex($ctx, $table, $indexName) {
253 CRM_Core_BAO_SchemaHandler::dropIndexIfExists($table, $indexName);
254
255 return TRUE;
256 }
257
258 /**
259 * Rebuild Multilingual Schema.
260 * @param CRM_Queue_TaskContext $ctx
261 * @param string|NULL $version CiviCRM version to use if rebuilding multilingual schema
262 * @return bool
263 */
264 public static function rebuildMultilingalSchema($ctx, $version = NULL) {
265 $domain = new CRM_Core_DAO_Domain();
266 $domain->find(TRUE);
267 if ($domain->locales) {
268 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
269 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version);
270 }
271 return TRUE;
272 }
273
274 }