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 +--------------------------------------------------------------------+
12 use Civi\Core\SettingsBag
;
15 * Base class for incremental upgrades
17 class CRM_Upgrade_Incremental_Base
{
18 const BATCH_SIZE
= 5000;
27 public function verifyPreDBstate(&$errors) {
32 * Compute any messages which should be displayed before upgrade.
34 * Note: This function is called iteratively for each upcoming
35 * revision to the database.
37 * @param $preUpgradeMessage
39 * a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'.
40 * @param null $currentVer
42 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
46 * Compute any messages which should be displayed after upgrade.
48 * @param string $postUpgradeMessage
51 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
53 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
57 * (Queue Task Callback)
59 * @param \CRM_Queue_TaskContext $ctx
64 public static function runSql(CRM_Queue_TaskContext
$ctx, $rev) {
65 $upgrade = new CRM_Upgrade_Form();
66 $upgrade->processSQL($rev);
72 * Syntactic sugar for adding a task.
74 * Task is (a) in this class and (b) has a high priority.
76 * After passing the $funcName, you can also pass parameters that will go to
77 * the function. Note that all params must be serializable.
79 * @param string $title
80 * @param string $funcName
82 protected function addTask($title, $funcName) {
83 $queue = CRM_Queue_Service
::singleton()->load([
85 'name' => CRM_Upgrade_Form
::QUEUE_NAME
,
88 $args = func_get_args();
89 $title = array_shift($args);
90 $funcName = array_shift($args);
91 $task = new CRM_Queue_Task(
92 [get_class($this), $funcName],
96 $queue->createItem($task, ['weight' => -1]);
100 * Remove a payment processor if not in use
102 * @param CRM_Queue_TaskContext $ctx
103 * @param string $name
105 * @throws \CiviCRM_API3_Exception
107 public static function removePaymentProcessorType(CRM_Queue_TaskContext
$ctx, $name) {
108 $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]);
109 if (empty($processors['result'])) {
110 $result = civicrm_api3('PaymentProcessorType', 'get', [
114 if (!empty($result['id'])) {
115 civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]);
122 * @param string $table_name
123 * @param string $constraint_name
126 public static function checkFKExists($table_name, $constraint_name) {
127 return CRM_Core_BAO_SchemaHandler
::checkFKExists($table_name, $constraint_name);
131 * Add a column to a table if it doesn't already exist
133 * @param CRM_Queue_TaskContext $ctx
134 * @param string $table
135 * @param string $column
136 * @param string $properties
137 * @param bool $localizable is this a field that should be localized
138 * @param string|null $version CiviCRM version to use if rebuilding multilingual schema
142 public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL) {
143 $domain = new CRM_Core_DAO_Domain();
146 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column, FALSE)) {
147 if ($domain->locales
) {
149 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
150 foreach ($locales as $locale) {
151 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, "{$column}_{$locale}", FALSE)) {
152 $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties";
157 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
161 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
163 foreach ($queries as $query) {
164 CRM_Core_DAO
::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
167 if ($domain->locales
) {
168 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
169 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, $version, TRUE);
175 * Do any relevant message template updates.
177 * @param CRM_Queue_TaskContext $ctx
178 * @param string $version
180 public static function updateMessageTemplates($ctx, $version) {
181 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version);
182 $messageTemplateObject->updateTemplates();
187 * Re-save any valid values from contribute settings into the normal setting
190 * We render the array of contribution_invoice_settings and any that have
191 * metadata defined we add to the correct key. This is safe to run even if no
192 * settings are to be converted, per the test in
193 * testConvertUpgradeContributeSettings.
199 public static function updateContributeSettings($ctx) {
200 // Use a direct query as api now does some handling on this.
201 $settings = CRM_Core_DAO
::executeQuery("SELECT value, domain_id FROM civicrm_setting WHERE name = 'contribution_invoice_settings'");
203 while ($settings->fetch()) {
204 $contributionSettings = (array) CRM_Utils_String
::unserialize($settings->value
);
205 foreach (array_merge(SettingsBag
::getContributionInvoiceSettingKeys(), ['deferred_revenue_enabled' => 'deferred_revenue_enabled']) as $possibleKeyName => $settingName) {
206 if (!empty($contributionSettings[$possibleKeyName]) && empty(Civi
::settings($settings->domain_id
)->getExplicit($settingName))) {
207 Civi
::settings($settings->domain_id
)->set($settingName, $contributionSettings[$possibleKeyName]);
215 * Do any relevant smart group updates.
217 * @param CRM_Queue_TaskContext $ctx
218 * @param array $actions
222 public function updateSmartGroups($ctx, $actions) {
223 $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups();
224 $groupUpdateObject->updateGroups($actions);
229 * Drop a column from a table if it exist.
231 * @param CRM_Queue_TaskContext $ctx
232 * @param string $table
233 * @param string $column
236 public static function dropColumn($ctx, $table, $column) {
237 if (CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column)) {
238 CRM_Core_DAO
::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`",
239 [], TRUE, NULL, FALSE, FALSE);
245 * Add a index to a table column.
247 * @param CRM_Queue_TaskContext $ctx
248 * @param string $table
249 * @param string|array $column
252 public static function addIndex($ctx, $table, $column) {
253 $tables = [$table => (array) $column];
254 CRM_Core_BAO_SchemaHandler
::createIndexes($tables);
260 * Drop a index from a table if it exist.
262 * @param CRM_Queue_TaskContext $ctx
263 * @param string $table
264 * @param string $indexName
267 public static function dropIndex($ctx, $table, $indexName) {
268 CRM_Core_BAO_SchemaHandler
::dropIndexIfExists($table, $indexName);
274 * Drop a table... but only if it's empty.
276 * @param CRM_Queue_TaskContext $ctx
277 * @param string $table
280 public static function dropTableIfEmpty($ctx, $table) {
281 if (CRM_Core_DAO
::checkTableExists($table)) {
282 if (!CRM_Core_DAO
::checkTableHasData($table)) {
283 CRM_Core_BAO_SchemaHandler
::dropTable($table);
286 $ctx->log
->warning("dropTableIfEmpty($table): Found data. Preserved table.");
294 * Rebuild Multilingual Schema.
295 * @param CRM_Queue_TaskContext $ctx
296 * @param string|null $version CiviCRM version to use if rebuilding multilingual schema
300 public static function rebuildMultilingalSchema($ctx, $version = NULL) {
301 $domain = new CRM_Core_DAO_Domain();
303 if ($domain->locales
) {
304 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
305 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, $version);