From 6b5c19687a334cd32f3369be4971450510bd1619 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 2 Mar 2022 21:48:25 -0800 Subject: [PATCH] Upgrader - Add enable extension as a reusable upgrade task --- CRM/Upgrade/Incremental/Base.php | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/CRM/Upgrade/Incremental/Base.php b/CRM/Upgrade/Incremental/Base.php index 49568dc51a..be5e72d90f 100644 --- a/CRM/Upgrade/Incremental/Base.php +++ b/CRM/Upgrade/Incremental/Base.php @@ -158,6 +158,48 @@ class CRM_Upgrade_Incremental_Base { $queue->createItem($task, ['weight' => -1]); } + /** + * Add a task to activate an extension. This task will run post-upgrade (after all + * changes to core DB are settled). + * + * @param string $title + * @param string[] $keys + * List of extensions to enable. + */ + protected function addExtensionTask(string $title, array $keys): void { + Civi::queue(CRM_Upgrade_Form::QUEUE_NAME)->createItem( + new CRM_Queue_Task([static::CLASS, 'enableExtension'], [$keys], $title), + ['weight' => 2000] + ); + } + + /** + * @param \CRM_Queue_TaskContext $ctx + * @param string[] $keys + * List of extensions to enable. + * @return bool + */ + public static function enableExtension(CRM_Queue_TaskContext $ctx, array $keys): bool { + // The `enableExtension` has a very high value of `weight`, so this runs after all + // core DB schema updates have been resolved. We can use high-level services. + + Civi::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy::get('upgrade.finish')); + $restore = \CRM_Utils_AutoClean::with(function() { + Civi::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy::get('upgrade.main')); + }); + + $manager = CRM_Extension_System::singleton()->getManager(); + $manager->enable($manager->findInstallRequirements($keys)); + + // Hrm, `enable()` normally does these things... but not during upgrade... + // Note: A good test-scenario is to install 5.45; enable logging and CiviGrant; disable searchkit+afform; then upgrade to 5.47. + $schema = new CRM_Logging_Schema(); + $schema->fixSchemaDifferences(); + CRM_Core_Invoke::rebuildMenuAndCaches(FALSE, TRUE); + + return TRUE; + } + /** * Remove a payment processor if not in use * -- 2.25.1