From 62399657e75cf0c973341830459e84626f439792 Mon Sep 17 00:00:00 2001 From: "B. Endres" Date: Wed, 11 May 2022 20:55:42 +0200 Subject: [PATCH] [#42] added data structure and upgrader/migration --- CRM/Twingle/Upgrader.php | 146 +++++++------------------------- sql/civicrm_twingle_profile.sql | 16 ++++ 2 files changed, 48 insertions(+), 114 deletions(-) create mode 100644 sql/civicrm_twingle_profile.sql diff --git a/CRM/Twingle/Upgrader.php b/CRM/Twingle/Upgrader.php index 35c1283..1724753 100644 --- a/CRM/Twingle/Upgrader.php +++ b/CRM/Twingle/Upgrader.php @@ -6,32 +6,12 @@ use CRM_Twingle_ExtensionUtil as E; */ class CRM_Twingle_Upgrader extends CRM_Twingle_Upgrader_Base { - // By convention, functions that look like "function upgrade_NNNN()" are - // upgrade tasks. They are executed in order (like Drupal's hook_update_N). - /** - * Example: Run an external SQL script when the module is installed. - * + * Installer script + */ public function install() { - $this->executeSqlFile('sql/myinstall.sql'); - } - - /** - * Example: Work with entities usually not available during the install step. - * - * This method can be used for any post-install tasks. For example, if a step - * of your installation depends on accessing an entity that is itself - * created during the installation (e.g., a setting or a managed entity), do - * so here to avoid order of operation problems. - * - public function postInstall() { - $customFieldId = civicrm_api3('CustomField', 'getvalue', array( - 'return' => array("id"), - 'name' => "customFieldCreatedViaManagedHook", - )); - civicrm_api3('Setting', 'create', array( - 'myWeirdFieldSetting' => array('id' => $customFieldId, 'weirdness' => 1), - )); + // create a DB table for the twingle profiles + $this->executeSqlFile('sql/civicrm_twingle_profile.sql'); } /** @@ -41,96 +21,6 @@ class CRM_Twingle_Upgrader extends CRM_Twingle_Upgrader_Base { $this->executeSqlFile('sql/myuninstall.sql'); } - /** - * Example: Run a simple query when a module is enabled. - * - public function enable() { - CRM_Core_DAO::executeQuery('UPDATE foo SET is_active = 1 WHERE bar = "whiz"'); - } - - /** - * Example: Run a simple query when a module is disabled. - * - public function disable() { - CRM_Core_DAO::executeQuery('UPDATE foo SET is_active = 0 WHERE bar = "whiz"'); - } - - /** - * Example: Run a couple simple queries. - * - * @return TRUE on success - * @throws Exception - * - public function upgrade_4200() { - $this->ctx->log->info('Applying update 4200'); - CRM_Core_DAO::executeQuery('UPDATE foo SET bar = "whiz"'); - CRM_Core_DAO::executeQuery('DELETE FROM bang WHERE willy = wonka(2)'); - return TRUE; - } // */ - - - /** - * Example: Run an external SQL script. - * - * @return TRUE on success - * @throws Exception - public function upgrade_4201() { - $this->ctx->log->info('Applying update 4201'); - // this path is relative to the extension base dir - $this->executeSqlFile('sql/upgrade_4201.sql'); - return TRUE; - } // */ - - - /** - * Example: Run a slow upgrade process by breaking it up into smaller chunk. - * - * @return TRUE on success - * @throws Exception - public function upgrade_4202() { - $this->ctx->log->info('Planning update 4202'); // PEAR Log interface - - $this->addTask(E::ts('Process first step'), 'processPart1', $arg1, $arg2); - $this->addTask(E::ts('Process second step'), 'processPart2', $arg3, $arg4); - $this->addTask(E::ts('Process second step'), 'processPart3', $arg5); - return TRUE; - } - public function processPart1($arg1, $arg2) { sleep(10); return TRUE; } - public function processPart2($arg3, $arg4) { sleep(10); return TRUE; } - public function processPart3($arg5) { sleep(10); return TRUE; } - // */ - - - /** - * Example: Run an upgrade with a query that touches many (potentially - * millions) of records by breaking it up into smaller chunks. - * - * @return TRUE on success - * @throws Exception - public function upgrade_4203() { - $this->ctx->log->info('Planning update 4203'); // PEAR Log interface - - $minId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_contribution'); - $maxId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_contribution'); - for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) { - $endId = $startId + self::BATCH_SIZE - 1; - $title = E::ts('Upgrade Batch (%1 => %2)', array( - 1 => $startId, - 2 => $endId, - )); - $sql = ' - UPDATE civicrm_contribution SET foobar = whiz(wonky()+wanker) - WHERE id BETWEEN %1 and %2 - '; - $params = array( - 1 => array($startId, 'Integer'), - 2 => array($endId, 'Integer'), - ); - $this->addTask($title, 'executeSql', $sql, $params); - } - return TRUE; - } // */ - /** * Copy financial_type_id setting to new setting financial_type_id_recur. */ @@ -163,4 +53,32 @@ class CRM_Twingle_Upgrader extends CRM_Twingle_Upgrader_Base { return TRUE; } + /** + * Upgrading to 1.4.0 needs to convert the profiles into the new infrastructure + * + * @return TRUE on success + * @throws Exception + */ + public function upgrade_5140() { + $this->ctx->log->info('Converting twingle profiles.'); + + // create a DB table for the twingle profiles + $this->executeSqlFile('sql/civicrm_twingle_profile.sql'); + + // migrate the current profiles + if ($profiles_data = Civi::settings()->get('twingle_profiles')) { + foreach ($profiles_data as $profile_name => $profile_data) { + $profile = new CRM_Twingle_Profile($profile_name, $profile_data); + $data = json_encode($profile->getData()); + CRM_Core_DAO::executeQuery( + "INSERT IGNORE INTO civicrm_twingle_profile(name,config,last_access,access_counter) VALUES (%1, %2, null, 0)", + [ + 1 => [$profile_name, 'String'], + 2 => [$data, 'String'] + ]); + } + } + + return TRUE; + } } diff --git a/sql/civicrm_twingle_profile.sql b/sql/civicrm_twingle_profile.sql new file mode 100644 index 0000000..479e231 --- /dev/null +++ b/sql/civicrm_twingle_profile.sql @@ -0,0 +1,16 @@ +-- /******************************************************* +-- ** civicrm_twingle_profile +-- ** +-- ** stores twingle profile data v1.4+ +-- ********************************************************/ + +CREATE TABLE IF NOT EXISTS `civicrm_twingle_profile`( + `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', + `name` varchar(255) COMMENT 'configuration name, i.e. internal ID', + `config` text COMMENT 'JSON encoded configuration', + `last_access` datetime COMMENT 'timestamp of the last access (through the api)', + `access_counter` int unsigned COMMENT 'number of accesses (through the api)', + PRIMARY KEY (`id`), + UNIQUE INDEX (`name`) +) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; +