CRM_Upgrade_Snapshot - Add helper `createSingleTask()`
authorTim Otten <totten@civicrm.org>
Thu, 3 Aug 2023 08:29:09 +0000 (01:29 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 3 Aug 2023 08:29:09 +0000 (01:29 -0700)
This is an alternative to `createTasks()`. It's suitable for the kinds of small/mid-sized
queries that appear in `*.mysql.tpl` files.

CRM/Upgrade/Snapshot.php

index 90b66e418bf09a02f980d2f2eb6e799fda0be534..4e1addf2fafa3a50fb969518bd63d48f6a40537a 100644 (file)
@@ -165,6 +165,37 @@ class CRM_Upgrade_Snapshot {
     }
   }
 
+  /**
+   * Generate the query/queries for storing a snapshot (if the local policy supports snapshotting).
+   *
+   * This method does all updates in one go. It is suitable for small/moderate data-sets. If you need to support
+   * larger data-sets, use createTasks() instead.
+   *
+   * @param string $owner
+   *   Name of the component/module/extension that owns the snapshot.
+   *   Ex: 'civicrm'
+   * @param string $version
+   *   Ex: '5.50'
+   * @param string $name
+   * @param string $select
+   *   Raw SQL SELECT for finding data.
+   * @throws \CRM_Core_Exception
+   * @return string[]
+   *   SQL statements to execute.
+   *   May be array if there no statements to execute.
+   */
+  public static function createSingleTask(string $owner, string $version, string $name, string $select): array {
+    $destTable = static::createTableName($owner, $version, $name);
+    if (!empty(CRM_Upgrade_Snapshot::getActivationIssues())) {
+      return [];
+    }
+
+    $result = [];
+    $result[] = "DROP TABLE IF EXISTS `{$destTable}`";
+    $result[] = "CREATE TABLE `{$destTable}` ROW_FORMAT=COMPRESSED AS {$select}";
+    return $result;
+  }
+
   /**
    * @param \CRM_Queue_TaskContext $ctx
    * @param string $sql