3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Base class for incremental upgrades
15 class CRM_Upgrade_Incremental_Base
{
16 const BATCH_SIZE
= 5000;
25 public function verifyPreDBstate(&$errors) {
30 * Compute any messages which should be displayed before upgrade.
32 * Note: This function is called iteratively for each upcoming
33 * revision to the database.
35 * @param $preUpgradeMessage
37 * a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'.
38 * @param null $currentVer
40 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
44 * Compute any messages which should be displayed after upgrade.
46 * @param string $postUpgradeMessage
49 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
51 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
55 * (Queue Task Callback)
57 * @param \CRM_Queue_TaskContext $ctx
62 public static function runSql(CRM_Queue_TaskContext
$ctx, $rev) {
63 $upgrade = new CRM_Upgrade_Form();
64 $upgrade->processSQL($rev);
70 * Syntactic sugar for adding a task.
72 * Task is (a) in this class and (b) has a high priority.
74 * After passing the $funcName, you can also pass parameters that will go to
75 * the function. Note that all params must be serializable.
77 * @param string $title
78 * @param string $funcName
80 protected function addTask($title, $funcName) {
81 $queue = CRM_Queue_Service
::singleton()->load([
83 'name' => CRM_Upgrade_Form
::QUEUE_NAME
,
86 $args = func_get_args();
87 $title = array_shift($args);
88 $funcName = array_shift($args);
89 $task = new CRM_Queue_Task(
90 [get_class($this), $funcName],
94 $queue->createItem($task, ['weight' => -1]);
98 * Remove a payment processor if not in use
100 * @param CRM_Queue_TaskContext $ctx
101 * @param string $name
103 * @throws \CiviCRM_API3_Exception
105 public static function removePaymentProcessorType(CRM_Queue_TaskContext
$ctx, $name) {
106 $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]);
107 if (empty($processors['result'])) {
108 $result = civicrm_api3('PaymentProcessorType', 'get', [
112 if (!empty($result['id'])) {
113 civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]);
120 * @param string $table_name
121 * @param string $constraint_name
124 public static function checkFKExists($table_name, $constraint_name) {
125 return CRM_Core_BAO_SchemaHandler
::checkFKExists($table_name, $constraint_name);
129 * Add a column to a table if it doesn't already exist
131 * @param CRM_Queue_TaskContext $ctx
132 * @param string $table
133 * @param string $column
134 * @param string $properties
135 * @param bool $localizable is this a field that should be localized
136 * @param string|null $version CiviCRM version to use if rebuilding multilingual schema
140 public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL) {
141 $domain = new CRM_Core_DAO_Domain();
144 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column, FALSE)) {
145 if ($domain->locales
) {
147 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
148 foreach ($locales as $locale) {
149 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, "{$column}_{$locale}", FALSE)) {
150 $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties";
155 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
159 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
161 foreach ($queries as $query) {
162 CRM_Core_DAO
::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
165 if ($domain->locales
) {
166 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
167 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, $version, TRUE);
173 * Do any relevant message template updates.
175 * @param CRM_Queue_TaskContext $ctx
176 * @param string $version
178 public static function updateMessageTemplates($ctx, $version) {
179 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version);
180 $messageTemplateObject->updateTemplates();
185 * Re-save any valid values from contribute settings into the normal setting
188 * We render the array of contribution_invoice_settings and any that have
189 * metadata defined we add to the correct key. This is safe to run even if no
190 * settings are to be converted, per the test in
191 * testConvertUpgradeContributeSettings.
197 public static function updateContributeSettings($ctx) {
198 $settings = Civi
::settings()->get('contribution_invoice_settings');
199 $metadata = \Civi\Core\SettingsMetadata
::getMetadata();
200 $conversions = array_intersect_key((array) $settings, $metadata);
201 foreach ($conversions as $key => $conversion) {
202 Civi
::settings()->set($key, $conversion);
208 * Do any relevant smart group updates.
210 * @param CRM_Queue_TaskContext $ctx
211 * @param array $actions
215 public function updateSmartGroups($ctx, $actions) {
216 $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups();
217 $groupUpdateObject->updateGroups($actions);
222 * Drop a column from a table if it exist.
224 * @param CRM_Queue_TaskContext $ctx
225 * @param string $table
226 * @param string $column
229 public static function dropColumn($ctx, $table, $column) {
230 if (CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column)) {
231 CRM_Core_DAO
::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`",
232 [], TRUE, NULL, FALSE, FALSE);
238 * Add a index to a table column.
240 * @param CRM_Queue_TaskContext $ctx
241 * @param string $table
242 * @param string|array $column
245 public static function addIndex($ctx, $table, $column) {
246 $tables = [$table => (array) $column];
247 CRM_Core_BAO_SchemaHandler
::createIndexes($tables);
253 * Drop a index from a table if it exist.
255 * @param CRM_Queue_TaskContext $ctx
256 * @param string $table
257 * @param string $indexName
260 public static function dropIndex($ctx, $table, $indexName) {
261 CRM_Core_BAO_SchemaHandler
::dropIndexIfExists($table, $indexName);
267 * Drop a table... but only if it's empty.
269 * @param CRM_Queue_TaskContext $ctx
270 * @param string $table
273 public static function dropTableIfEmpty($ctx, $table) {
274 if (CRM_Core_DAO
::checkTableExists($table)) {
275 if (!CRM_Core_DAO
::checkTableHasData($table)) {
276 CRM_Core_BAO_SchemaHandler
::dropTable($table);
279 $ctx->log
->warning("dropTableIfEmpty($table): Found data. Preserved table.");
287 * Rebuild Multilingual Schema.
288 * @param CRM_Queue_TaskContext $ctx
289 * @param string|null $version CiviCRM version to use if rebuilding multilingual schema
293 public static function rebuildMultilingalSchema($ctx, $version = NULL) {
294 $domain = new CRM_Core_DAO_Domain();
296 if ($domain->locales
) {
297 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
298 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, $version);