Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify autogenerated boilerplate upgrader class #22225

Merged
merged 2 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions CRM/Upgrade/Incremental/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,46 @@ public function verifyPreDBstate(&$errors) {
/**
* Compute any messages which should be displayed before upgrade.
*
* Note: This function is called iteratively for each upcoming
* revision to the database.
* Downstream classes should implement this method to generate their messages.
*
* @param $preUpgradeMessage
* This method will be invoked multiple times. Implementations MUST consult the `$rev`
* before deciding what messages to add. See the examples linked below.
*
* @see \CRM_Upgrade_Incremental_php_FourSeven::setPreUpgradeMessage()
* @see \CRM_Upgrade_Incremental_php_FiveTwenty::setPreUpgradeMessage()
*
* @param string $preUpgradeMessage
* Accumulated list of messages. Alterable.
* @param string $rev
* a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'.
* The incremental version number. (Called repeatedly, once for each increment.)
*
* Ex: Suppose the system upgrades from 5.7.3 to 5.10.0. The method FiveEight::setPreUpgradeMessage()
* will be called for each increment of '5.8.*' ('5.8.alpha1' => '5.8.beta1' => '5.8.0').
* @param null $currentVer
* This is the penultimate version targeted by the upgrader.
* Equivalent to CRM_Utils_System::version().
*/
public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
}

/**
* Compute any messages which should be displayed after upgrade.
*
* Downstream classes should implement this method to generate their messages.
*
* This method will be invoked multiple times. Implementations MUST consult the `$rev`
* before deciding what messages to add. See the examples linked below.
*
* @see \CRM_Upgrade_Incremental_php_FourSeven::setPostUpgradeMessage()
* @see \CRM_Upgrade_Incremental_php_FiveTwentyOne::setPostUpgradeMessage()
*
* @param string $postUpgradeMessage
* alterable.
* Accumulated list of messages. Alterable.
* @param string $rev
* an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
* The incremental version number. (Called repeatedly, once for each increment.)
*
* Ex: Suppose the system upgrades from 5.7.3 to 5.10.0. The method FiveEight::setPreUpgradeMessage()
* will be called for each increment of '5.8.*' ('5.8.alpha1' => '5.8.beta1' => '5.8.0').
*/
public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
}
Expand Down
66 changes: 12 additions & 54 deletions CRM/Upgrade/Incremental/php/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,25 @@
*/

/**
* Upgrade logic for <?php echo $camelNumber; ?>
* Upgrade logic for the <?php echo $versionX = str_replace('alpha1', 'x', $versionNumber); ?> series.
*
* Each minor version in the series is handled by either a `<?php echo $versionX; ?>.mysql.tpl` file,
* or a function in this class named `upgrade_<?php echo str_replace('.', '_', $versionX); ?>`.
* If only a .tpl file exists for a version, it will be run automatically.
* If the function exists, it must explicitly add the 'runSql' task if there is a corresponding .mysql.tpl.
*
* This class may also implement `setPreUpgradeMessage()` and `setPreUpgradeMessage()` functions.
*/
class CRM_Upgrade_Incremental_php_<?php echo $camelNumber; ?> extends CRM_Upgrade_Incremental_Base {

/**
* Compute any messages which should be displayed beforeupgrade.
* Upgrade step; adds tasks including 'runSql'.
*
* Note: This function is called iteratively for each incremental upgrade step.
* There must be a concrete step (eg 'X.Y.Z.mysql.tpl' or 'upgrade_X_Y_Z()').
*
* @param string $preUpgradeMessage
* @param string $rev
* a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'.
* @param null $currentVer
*/
public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL): void {
// Example: Generate a pre-upgrade message.
// if ($rev == '5.12.34') {
// $preUpgradeMessage .= '<p>' . ts('A new permission, "%1", has been added. This permission is now used to control access to the Manage Tags screen.', array(1 => ts('manage tags'))) . '</p>';
// }
}

/**
* Compute any messages which should be displayed after upgrade.
*
* Note: This function is called iteratively for each incremental upgrade step.
* There must be a concrete step (eg 'X.Y.Z.mysql.tpl' or 'upgrade_X_Y_Z()').
*
* @param string $postUpgradeMessage
* alterable.
* @param string $rev
* an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
* The version number matching this function name
*/
public function setPostUpgradeMessage(&$postUpgradeMessage, $rev): void {
// Example: Generate a post-upgrade message.
// if ($rev == '5.12.34') {
// $postUpgradeMessage .= '<br /><br />' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
// }
public function upgrade_<?php echo str_replace('.', '_', $versionNumber); ?>($rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
}

/*
* Important! All upgrade functions MUST add a 'runSql' task.
* Uncomment and use the following template for a new upgrade version
* (change the x in the function name):
*/

// /**
// * Upgrade function.
// *
// * @param string $rev
// */
// public function upgrade_5_0_x($rev): void {
// $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
// $this->addTask('Do the foo change', 'taskFoo', ...);
// // Additional tasks here...
// // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
// // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
// }

// public static function taskFoo(CRM_Queue_TaskContext $ctx, ...): bool {
// return TRUE;
// }

}
6 changes: 4 additions & 2 deletions tools/bin/scripts/set-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
echo "Changing version from $oldVersion to $newVersion...\n";

$verName = makeVerName($newVersion);
$phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName) {
$phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName, $newVersion) {
ob_start();
global $camelNumber;
global $versionNumber;
$camelNumber = $verName;
$versionNumber = $newVersion;
require 'CRM/Upgrade/Incremental/php/Template.php';
unset($camelNumber);
unset($camelNumber, $versionNumber);
return ob_get_clean();
});

Expand Down