Skip to content

Commit

Permalink
Merge pull request #22888 from civicrm/5.47
Browse files Browse the repository at this point in the history
5.47
  • Loading branch information
seamuslee001 authored Mar 4, 2022
2 parents 63e9585 + 26eefaf commit 3675754
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
6 changes: 1 addition & 5 deletions CRM/Core/ManagedEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,10 @@ public function get($moduleName, $name) {
* existing entities, and remove orphaned (stale) entities.
*
* @param bool $ignoreUpgradeMode
*
* Unused.
* @throws \CRM_Core_Exception
*/
public function reconcile($ignoreUpgradeMode = FALSE) {
// Do not reconcile whilst we are in upgrade mode
if (CRM_Core_Config::singleton()->isUpgradeMode() && !$ignoreUpgradeMode) {
return;
}
$this->loadDeclarations();
if ($error = $this->validate($this->getDeclarations())) {
throw new CRM_Core_Exception($error);
Expand Down
50 changes: 43 additions & 7 deletions CRM/Upgrade/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,14 @@ public function checkCurrentVersion($currentVer, $latestVer) {
/**
* Fill the queue with upgrade tasks.
*
* The queue is a priority-queue (sorted by tuple weight+id). Here are some common weights:
*
* - `weight=0`: Add a typical upgrade step for revising core schema.
* - `weight=-1`: In the middle of the upgrade, add an extra step for immediate execution.
* - `weight=1000`: Add some general core upgrade-logic that runs after all schema have change.d
* - `weight=2000`: Add some post-upgrade logic. If a task absolutely requires full system services
* (eg enabling a new extension), then place it here.
*
* @param string $currentVer
* the original revision.
* @param string $latestVer
Expand Down Expand Up @@ -526,14 +534,14 @@ public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFi
[$postUpgradeMessageFile],
"Cleanup old files"
);
$queue->createItem($task);
$queue->createItem($task, ['weight' => 0]);

$task = new CRM_Queue_Task(
['CRM_Upgrade_Form', 'disableOldExtensions'],
[$postUpgradeMessageFile],
"Checking extensions"
);
$queue->createItem($task);
$queue->createItem($task, ['weight' => 0]);

$revisions = $upgrade->getRevisionSequence();
$maxRevision = empty($revisions) ? NULL : end($revisions);
Expand All @@ -552,7 +560,7 @@ public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFi
[$rev],
"Begin Upgrade to $rev"
);
$queue->createItem($beginTask);
$queue->createItem($beginTask, ['weight' => 0]);

$task = new CRM_Queue_Task(
// callback
Expand All @@ -561,7 +569,7 @@ public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFi
[$rev, $currentVer, $latestVer, $postUpgradeMessageFile],
"Upgrade DB to $rev"
);
$queue->createItem($task);
$queue->createItem($task, ['weight' => 0]);

$task = new CRM_Queue_Task(
// callback
Expand All @@ -570,7 +578,7 @@ public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFi
[$rev, $currentVer, $latestVer, $postUpgradeMessageFile],
"Finish Upgrade DB to $rev"
);
$queue->createItem($task);
$queue->createItem($task, ['weight' => 0]);
}
}

Expand All @@ -581,9 +589,16 @@ public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFi
[$rev, $latestVer, $latestVer, $postUpgradeMessageFile],
"Finish Upgrade DB to $latestVer"
);
$queue->createItem($task);
$queue->createItem($task, ['weight' => 0]);
}

$task = new CRM_Queue_Task(
['CRM_Upgrade_Form', 'doCoreFinish'],
[$rev, $latestVer, $latestVer, $postUpgradeMessageFile],
"Finish core DB updates $latestVer"
);
$queue->createItem($task, ['weight' => 1000]);

return $queue;
}

Expand Down Expand Up @@ -780,7 +795,13 @@ public static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext $ctx, $r
return TRUE;
}

public static function doFinish() {
/**
* Finalize the core upgrade.
*
* @return bool
* @throws \CRM_Core_Exception
*/
public static function doCoreFinish(): bool {
Civi::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy::get('upgrade.finish'));
$restore = \CRM_Utils_AutoClean::with(function() {
Civi::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy::get('upgrade.main'));
Expand All @@ -805,6 +826,21 @@ public static function doFinish() {
$logging->fixSchemaDifferences();
// Force a rebuild of CiviCRM asset cache in case things have changed.
\Civi::service('asset_builder')->clear(FALSE);

return TRUE;
}

/**
* After finishing the queue, the upgrade-runner calls `doFinish()`.
*
* This is called by all upgrade-runners (inside or outside of `civicrm-core.git`).
* Removing it would be a breaky-annoying process; it would foreclose future use;
* and it would produce no tangible benefits.
*
* @return bool
*/
public static function doFinish(): bool {
return TRUE;
}

/**
Expand Down
42 changes: 42 additions & 0 deletions CRM/Upgrade/Incremental/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,48 @@ protected function addTask($title, $funcName) {
$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
*
Expand Down
15 changes: 14 additions & 1 deletion CRM/Upgrade/Incremental/php/FiveFortySeven.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NU
public function upgrade_5_47_alpha1($rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask('Migrate CiviGrant component to an extension', 'migrateCiviGrant');
if (CRM_Core_Component::isEnabled('CiviGrant')) {
$this->addExtensionTask('Enable CiviGrant extension', ['civigrant']);
}
$this->addTask('Add created_date to civicrm_relationship', 'addColumn', 'civicrm_relationship', 'created_date',
"timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Relationship created date'"
);
Expand All @@ -72,6 +75,12 @@ public function upgrade_5_47_alpha1($rev): void {
$this->addTask('core-issue#2122 - Set the timezone to the default for existing Events', 'setEventTZDefault');
$this->addTask('Drop CustomGroup UI_name_extends index', 'dropIndex', 'civicrm_custom_group', 'UI_name_extends');
$this->addTask('Add CustomGroup UI_name index', 'addIndex', 'civicrm_custom_group', ['name'], 'UI');
if (CRM_Core_DAO::checkTableExists('civicrm_search_display')) {
$this->addTask('Add SearchDisplay.acl_bypass', 'addColumn',
'civicrm_search_display', 'acl_bypass',
"tinyint DEFAULT 0 COMMENT 'Skip permission checks and ACLs when running this display.'"
);
}
}

/**
Expand Down Expand Up @@ -107,15 +116,19 @@ public static function migrateCiviGrant(CRM_Queue_TaskContext $ctx): bool {
}
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_component WHERE name = 'CiviGrant'", [], TRUE, NULL, FALSE, FALSE);
}

// There are existing records which should be managed by `civigrant`. To assign ownership, we need
// placeholders in `civicrm_extension` and `civicrm_managed`.
$ext = new CRM_Core_DAO_Extension();
$ext->full_name = 'civigrant';
if (!$ext->find(TRUE)) {
$ext->type = 'module';
$ext->name = 'CiviGrant';
$ext->label = ts('CiviGrant');
$ext->file = 'civigrant';
$ext->is_active = (int) $civiGrantEnabled;
$ext->is_active = 0; /* Not active _yet_. If site uses CiviGrant, we will re-activate once the core-schema has been revised. */
$ext->save();
CRM_Extension_System::singleton()->getManager()->refresh();

$managedItems = [
'OptionGroup_advanced_search_options_OptionValue_CiviGrant' => [
Expand Down

0 comments on commit 3675754

Please sign in to comment.