From 1a181f4a3152501db4bcf65f78f4148e11eb7fab Mon Sep 17 00:00:00 2001 From: benjamin Date: Wed, 18 Nov 2026 09:24:45 +0000 Subject: [PATCH 1/2] Upgrader - allow upgrading past released version --- CRM/Upgrade/DispatchPolicy.php | 2 +- CRM/Upgrade/Form.php | 63 +++------------- CRM/Upgrade/Incremental/General.php | 8 ++ CRM/Upgrade/Versions.php | 112 ++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 53 deletions(-) create mode 100644 CRM/Upgrade/Versions.php diff --git a/CRM/Upgrade/DispatchPolicy.php b/CRM/Upgrade/DispatchPolicy.php index e5c8bcc4eb77..c66bca05ec69 100644 --- a/CRM/Upgrade/DispatchPolicy.php +++ b/CRM/Upgrade/DispatchPolicy.php @@ -47,7 +47,7 @@ public static function pick(): ?array { } // Are there outstanding core version upgrades? - $upgradeFinished = !CRM_Core_BAO_Domain::isDBUpdateRequired(); + $upgradeFinished = !\CRM_Upgrade_Versions::upgradesPending(); return CRM_Upgrade_DispatchPolicy::get($upgradeFinished ? 'upgrade.finish' : 'upgrade.main'); } diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index bc9ce9077670..47cfdd21c074 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -96,15 +96,11 @@ public function __construct( */ public static function &incrementalPhpObject($version) { static $incrementalPhpObject = []; - - $versionParts = explode('.', $version); - $versionName = CRM_Utils_EnglishNumber::toCamelCase($versionParts[0]) . CRM_Utils_EnglishNumber::toCamelCase($versionParts[1]); - - if (!array_key_exists($versionName, $incrementalPhpObject)) { - $className = "CRM_Upgrade_Incremental_php_{$versionName}"; - $incrementalPhpObject[$versionName] = new $className(); + $version = (string) $version; + if (!array_key_exists($version, $incrementalPhpObject)) { + $incrementalPhpObject[$version] = CRM_Upgrade_Versions::getIncrementalPhpObject($version); } - return $incrementalPhpObject[$versionName]; + return $incrementalPhpObject[$version]; } /** @@ -112,22 +108,7 @@ public static function &incrementalPhpObject($version) { * ex: ['5.13', '5.14', '5.15'] */ public static function incrementalPhpObjectVersions() { - $versions = []; - - $phpDir = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Incremental', 'php']); - $phpFiles = glob("$phpDir/*.php"); - foreach ($phpFiles as $phpFile) { - $phpWord = substr(basename($phpFile), 0, -4); - if (CRM_Utils_EnglishNumber::isNumeric($phpWord)) { - /** @var \CRM_Upgrade_Incremental_Base $instance */ - $className = 'CRM_Upgrade_Incremental_php_' . $phpWord; - $instance = new $className(); - $versions[] = $instance->getMajorMinor(); - } - } - - usort($versions, 'version_compare'); - return $versions; + return \CRM_Upgrade_Versions::incrementalPhpObjectVersions(); } /** @@ -241,15 +222,7 @@ public function logVersion($newVersion) { * @throws Exception */ public function getRevisionSequence() { - $revList = []; - - foreach (self::incrementalPhpObjectVersions() as $majorMinor) { - $phpUpgrader = self::incrementalPhpObject($majorMinor); - $revList = array_merge($revList, array_values($phpUpgrader->getRevisionSequence())); - } - - usort($revList, 'version_compare'); - return $revList; + return \CRM_Upgrade_Versions::getRevisionSequence(); } /** @@ -315,19 +288,9 @@ public function processSQL($rev) { * @return array(0=>$currentVer, 1=>$latestVer) */ public function getUpgradeVersions() { - $latestVer = CRM_Utils_System::version(); - $currentVer = CRM_Core_BAO_Domain::version(TRUE); - if (!$currentVer) { - throw new CRM_Core_Exception(ts('Version information missing in civicrm database.')); - } - elseif (stripos($currentVer, 'upgrade')) { - throw new CRM_Core_Exception(ts('Database check failed - the database looks to have been partially upgraded. You may want to reload the database with the backup and try the upgrade process again.')); - } - if (!$latestVer) { - throw new CRM_Core_Exception(ts('Version information missing in civicrm codebase.')); - } - - return [$currentVer, $latestVer]; + $versions = \CRM_Upgrade_Versions::getUpgradeVersions(); + \CRM_Upgrade_Versions::validateUpgradeVersions($versions[0], $versions[1]); + return $versions; } /** @@ -546,12 +509,8 @@ public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFi ); $queue->createItem($task, ['weight' => 0]); - $revisions = $upgrade->getRevisionSequence(); - $maxRevision = empty($revisions) ? NULL : end($revisions); + $revisions = \CRM_Upgrade_Versions::getRevisionSequence(); reset($revisions); - if (version_compare($latestVer, $maxRevision, '<')) { - throw new CRM_Core_Exception("Malformed upgrade sequence. The incremental update $maxRevision exceeds target version $latestVer"); - } foreach ($revisions as $rev) { // proceed only if $currentVer < $rev @@ -937,7 +896,7 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVe // Scan through all php files and see if any file is interested in setting pre-upgrade-message // based on $currentVer, $latestVer. // Please note, at this point upgrade hasn't started executing queries. - $revisions = $this->getRevisionSequence(); + $revisions = \CRM_Upgrade_Versions::getRevisionSequence(); foreach ($revisions as $rev) { if (version_compare($currentVer, $rev) < 0) { $versionObject = $this->incrementalPhpObject($rev); diff --git a/CRM/Upgrade/Incremental/General.php b/CRM/Upgrade/Incremental/General.php index bac54b3d6ca1..32bfa0305b91 100644 --- a/CRM/Upgrade/Incremental/General.php +++ b/CRM/Upgrade/Incremental/General.php @@ -108,6 +108,14 @@ public static function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $l ]); $preUpgradeMessage .= '

'; } + if (version_compare(CRM_Utils_System::version(), $latestVer, '<')) { + $preUpgradeMessage .= '

'; + $preUpgradeMessage .= ts('This upgrade includes pre-release database upgrade steps up to %1. The released code version is %2. You should only continue if you know what you are doing.', [ + 1 => $latestVer, + 2 => CRM_Utils_System::version(), + ]); + $preUpgradeMessage .= '

'; + } // http://issues.civicrm.org/jira/browse/CRM-13572 // Depending on how the code was upgraded, some sites may still have copies of old diff --git a/CRM/Upgrade/Versions.php b/CRM/Upgrade/Versions.php new file mode 100644 index 000000000000..4599cd140ea6 --- /dev/null +++ b/CRM/Upgrade/Versions.php @@ -0,0 +1,112 @@ +getMajorMinor(); + } + } + usort($versions, 'version_compare'); + + return $versions; + } + + /** + * Get a list of all patch-versions that appear in upgrade steps, whether + * as *.mysql.tpl or as *.php. + * + * @return array + * @throws Exception + */ + public static function getRevisionSequence() { + $revList = []; + + foreach (self::incrementalPhpObjectVersions() as $majorMinor) { + $phpUpgrader = self::getIncrementalPhpObject($majorMinor); + $revList = array_merge($revList, array_values($phpUpgrader->getRevisionSequence())); + } + + usort($revList, 'version_compare'); + return $revList; + } + + public static function getLatestVersion() { + $xmlVersion = \CRM_Utils_System::version(); + + $revisions = self::getRevisionSequence(); + $maxRevision = empty($revisions) ? NULL : end($revisions); + if ($maxRevision && version_compare($xmlVersion, $maxRevision, '<')) { + // e.g. running upgrader pre-release + return $maxRevision; + } + return $xmlVersion; + } + + /** + * Determine the start and end version of the upgrade process. + * + * @return array(0=>$currentVer, 1=>$latestVer) + */ + public static function getUpgradeVersions() { + $latestVer = self::getLatestVersion(); + $currentVer = CRM_Core_BAO_Domain::version(TRUE); + return [$currentVer, $latestVer]; + } + + public static function validateUpgradeVersions(string $currentVer, string $latestVer) { + if (!$currentVer) { + throw new CRM_Core_Exception(ts('Version information missing in civicrm database.')); + } + elseif (stripos($currentVer, 'upgrade')) { + throw new CRM_Core_Exception(ts('Database check failed - the database looks to have been partially upgraded. You may want to reload the database with the backup and try the upgrade process again.')); + } + if (!$latestVer) { + throw new CRM_Core_Exception(ts('Version information missing in civicrm codebase.')); + } + } + + public static function upgradesPending(): bool { + [$dbVersion, $codeVersion] = self::getUpgradeVersions(); + return version_compare($dbVersion, $codeVersion) < 0; + } + +} From 2dbb9cb396f8c55f6ea57cd5cfaef4baf575e13b Mon Sep 17 00:00:00 2001 From: benjamin Date: Thu, 19 Nov 2026 18:54:40 +0000 Subject: [PATCH 2/2] set-version - check new version >= declared upgrade steps --- tools/bin/scripts/set-version.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tools/bin/scripts/set-version.php b/tools/bin/scripts/set-version.php index 780720a8543a..49abc6d78c11 100755 --- a/tools/bin/scripts/set-version.php +++ b/tools/bin/scripts/set-version.php @@ -22,7 +22,7 @@ $oldVersion = (string) simplexml_load_file("xml/version.xml")->version_no; if (!isVersionValid($oldVersion)) { - fatal("failed to read old version from \"xml/version.xml\"\n"); + fatal("failed to read old version from \"xml/version.xml\""); } /** @@ -34,7 +34,7 @@ extract(parseArgs($argv)); if (!isVersionValid($newVersion)) { - fatal("failed to read new version\n"); + fatal("failed to read new version"); } /* *********************************************************************** */ @@ -44,6 +44,11 @@ echo "Changing version from $oldVersion to $newVersion...\n"; +$latestVersion = exec("cv ev --level=classloader 'echo \CRM_Upgrade_Versions::getLatestVersion();'"); +if (version_compare($newVersion, $latestVersion, '<')) { + fatal("db upgrade steps are declared up to {$latestVersion} - cannot set release version lower than this!"); +} + $verName = makeVerName($newVersion); $phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName, $newVersion) { ob_start(); @@ -121,7 +126,7 @@ function($file) { */ function updateFile($file, $callback) { if (!file_exists($file)) { - die("File does not exist: $file\n"); + die("File does not exist: $file"); } echo "Update \"$file\"\n"; $content = file_get_contents($file); @@ -187,7 +192,9 @@ function isVersionValid($v) { * @param $error */ function fatal($error) { - echo $error; + echo "\n"; + echo "ERROR: {$error}\n"; + echo "\n"; echo "usage: set-version.php [] [--sql|--no-sql] [--commit|--no-commit]\n"; echo " --sql A placeholder *.sql file will be created.\n"; echo " --no-sql A placeholder *.sql file will not be created.\n"; @@ -236,7 +243,7 @@ function parseArgs($argv) { $positional[] = $arg; } else { - fatal("Unrecognized argument: $arg\n"); + fatal("Unrecognized argument: $arg"); } break; } @@ -248,7 +255,7 @@ function parseArgs($argv) { } if (!isset($parsed['doCommit'])) { - fatal("Must specify --commit or --no-commit\n"); + fatal("Must specify --commit or --no-commit"); } return $parsed;