Simplify autogenerated boilerplate upgrader class and clarify comments
authorColeman Watts <coleman@civicrm.org>
Wed, 8 Dec 2021 14:22:27 +0000 (09:22 -0500)
committerColeman Watts <coleman@civicrm.org>
Fri, 10 Dec 2021 02:58:38 +0000 (21:58 -0500)
CRM/Upgrade/Incremental/php/Template.php
tools/bin/scripts/set-version.php

index 94bc6339fed90541a0796e18e8454b4dcfb9c3ef..8400001ecfa6b54334108e09bb254bcbbe49dba6 100644 (file)
@@ -15,67 +15,25 @@ echo "<?php\n";
  */
 
 /**
- * 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;
-  // }
-
 }
index b4be5a31ca28ee8b9cc8cc3e2096f4277cd7fa02..153e2af57617aae11af9e60e3757602320407186 100755 (executable)
@@ -40,12 +40,14 @@ if (!isVersionValid($newVersion)) {
 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();
 });