From a082d2dfee954ad30b35261a2a9411ab2c843016 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 14 Apr 2022 16:04:39 +1200 Subject: [PATCH] dev/core#1307 Add civicrm_import_job table --- CRM/Core/BAO/Job.php | 2 - CRM/Core/BAO/UserJob.php | 68 +++ CRM/Core/DAO/AllCoreTables.data.php | 5 + CRM/Core/DAO/UserJob.php | 436 ++++++++++++++++++ .../Incremental/sql/5.50.alpha1.mysql.tpl | 19 + Civi/Api4/UserJob.php | 24 + tests/phpunit/CRM/Dedupe/MergerTest.php | 3 + xml/schema/Core/UserJob.xml | 156 +++++++ xml/schema/Core/files.xml | 1 + 9 files changed, 712 insertions(+), 2 deletions(-) create mode 100644 CRM/Core/BAO/UserJob.php create mode 100644 CRM/Core/DAO/UserJob.php create mode 100644 Civi/Api4/UserJob.php create mode 100644 xml/schema/Core/UserJob.xml diff --git a/CRM/Core/BAO/Job.php b/CRM/Core/BAO/Job.php index a8dbcdde44..e07d3b2398 100644 --- a/CRM/Core/BAO/Job.php +++ b/CRM/Core/BAO/Job.php @@ -13,8 +13,6 @@ * * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing - * $Id: $ - * */ /** diff --git a/CRM/Core/BAO/UserJob.php b/CRM/Core/BAO/UserJob.php new file mode 100644 index 0000000000..29e7a69897 --- /dev/null +++ b/CRM/Core/BAO/UserJob.php @@ -0,0 +1,68 @@ + 1, + 'name' => 'completed', + 'label' => ts('Completed'), + ], + [ + 'id' => 2, + 'name' => 'scheduled', + 'label' => ts('Scheduled'), + ], + [ + 'id' => 3, + 'name' => 'in_progress', + 'label' => ts('In Progress'), + ], + ]; + } + + /** + * Get the types Import Jobs. + * + * This is largely a placeholder at this stage. It will likely wind + * up as an option value so extensions can add different types. + * + * However, for now it just holds the one type being worked on. + * + * @return array + */ + public static function getTypes(): array { + return [ + [ + 'id' => 1, + 'name' => 'contact_import', + 'label' => ts('Contact Import'), + ], + ]; + } + +} diff --git a/CRM/Core/DAO/AllCoreTables.data.php b/CRM/Core/DAO/AllCoreTables.data.php index 5dd43a0831..872b1cddea 100644 --- a/CRM/Core/DAO/AllCoreTables.data.php +++ b/CRM/Core/DAO/AllCoreTables.data.php @@ -367,6 +367,11 @@ return [ 'class' => 'CRM_Core_DAO_UFMatch', 'table' => 'civicrm_uf_match', ], + 'CRM_Core_DAO_UserJob' => [ + 'name' => 'UserJob', + 'class' => 'CRM_Core_DAO_UserJob', + 'table' => 'civicrm_user_job', + ], 'CRM_Core_DAO_Timezone' => [ 'name' => 'Timezone', 'class' => 'CRM_Core_DAO_Timezone', diff --git a/CRM/Core/DAO/UserJob.php b/CRM/Core/DAO/UserJob.php new file mode 100644 index 0000000000..ebe7e1ee75 --- /dev/null +++ b/CRM/Core/DAO/UserJob.php @@ -0,0 +1,436 @@ +__table = 'civicrm_user_job'; + parent::__construct(); + } + + /** + * Returns localized title of this entity. + * + * @param bool $plural + * Whether to return the plural version of the title. + */ + public static function getEntityTitle($plural = FALSE) { + return $plural ? ts('User Jobs') : ts('User Job'); + } + + /** + * Returns foreign keys and entity references. + * + * @return array + * [CRM_Core_Reference_Interface] + */ + public static function getReferenceColumns() { + if (!isset(Civi::$statics[__CLASS__]['links'])) { + Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__); + Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'created_id', 'civicrm_contact', 'id'); + Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'queue_id', 'civicrm_queue', 'id'); + CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']); + } + return Civi::$statics[__CLASS__]['links']; + } + + /** + * Returns all the column names of this table + * + * @return array + */ + public static function &fields() { + if (!isset(Civi::$statics[__CLASS__]['fields'])) { + Civi::$statics[__CLASS__]['fields'] = [ + 'id' => [ + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('User Job ID'), + 'description' => ts('Job ID'), + 'required' => TRUE, + 'where' => 'civicrm_user_job.id', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'html' => [ + 'type' => 'Number', + ], + 'readonly' => TRUE, + 'add' => '5.50', + ], + 'name' => [ + 'name' => 'name', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('User job name'), + 'description' => ts('Unique name for job.'), + 'maxlength' => 64, + 'size' => CRM_Utils_Type::BIG, + 'where' => 'civicrm_user_job.name', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'add' => '5.50', + ], + 'created_id' => [ + 'name' => 'created_id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('Created By Contact ID'), + 'description' => ts('FK to contact table.'), + 'where' => 'civicrm_user_job.created_id', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'FKClassName' => 'CRM_Contact_DAO_Contact', + 'html' => [ + 'label' => ts("Created By"), + ], + 'add' => '5.50', + ], + 'created_date' => [ + 'name' => 'created_date', + 'type' => CRM_Utils_Type::T_TIMESTAMP, + 'title' => ts('Import Job Created Date'), + 'description' => ts('Date and time this job was created.'), + 'required' => TRUE, + 'where' => 'civicrm_user_job.created_date', + 'default' => 'CURRENT_TIMESTAMP', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'html' => [ + 'type' => 'Select Date', + 'formatType' => 'activityDateTime', + ], + 'readonly' => TRUE, + 'add' => '5.50', + ], + 'start_date' => [ + 'name' => 'start_date', + 'type' => CRM_Utils_Type::T_TIMESTAMP, + 'title' => ts('Import Job Started Date'), + 'description' => ts('Date and time this import job started.'), + 'required' => FALSE, + 'where' => 'civicrm_user_job.start_date', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'html' => [ + 'type' => 'Select Date', + 'formatType' => 'activityDateTime', + ], + 'readonly' => TRUE, + 'add' => '5.50', + ], + 'end_date' => [ + 'name' => 'end_date', + 'type' => CRM_Utils_Type::T_TIMESTAMP, + 'title' => ts('Job Ended Date'), + 'description' => ts('Date and time this import job ended.'), + 'required' => FALSE, + 'where' => 'civicrm_user_job.end_date', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'html' => [ + 'type' => 'Select Date', + 'formatType' => 'activityDateTime', + ], + 'add' => '5.50', + ], + 'expires_date' => [ + 'name' => 'expires_date', + 'type' => CRM_Utils_Type::T_TIMESTAMP, + 'title' => ts('Import Job Expires Date'), + 'description' => ts('Date and time to clean up after this import job (temp table deletion date).'), + 'required' => FALSE, + 'where' => 'civicrm_user_job.expires_date', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'html' => [ + 'type' => 'Select Date', + 'formatType' => 'activityDateTime', + ], + 'add' => '5.50', + ], + 'status_id' => [ + 'name' => 'status_id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('User Job Status ID'), + 'required' => TRUE, + 'where' => 'civicrm_user_job.status_id', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'html' => [ + 'label' => ts("Job Status"), + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_BAO_UserJob::getStatuses', + ], + 'add' => '5.50', + ], + 'type_id' => [ + 'name' => 'type_id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('User Job Type ID'), + 'required' => TRUE, + 'where' => 'civicrm_user_job.type_id', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'html' => [ + 'label' => ts("Job Type"), + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_BAO_UserJob::getTypes', + ], + 'add' => '5.50', + ], + 'queue_id' => [ + 'name' => 'queue_id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('Queue ID'), + 'description' => ts('FK to Queue'), + 'where' => 'civicrm_user_job.queue_id', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'FKClassName' => 'CRM_Queue_DAO_Queue', + 'html' => [ + 'label' => ts("Queue"), + ], + 'add' => NULL, + ], + 'metadata' => [ + 'name' => 'metadata', + 'type' => CRM_Utils_Type::T_TEXT, + 'title' => ts('Job metadata'), + 'description' => ts('Data pertaining to job configuration'), + 'where' => 'civicrm_user_job.metadata', + 'table_name' => 'civicrm_user_job', + 'entity' => 'UserJob', + 'bao' => 'CRM_Core_BAO_UserJob', + 'localizable' => 0, + 'serialize' => self::SERIALIZE_JSON, + 'add' => '5.50', + ], + ]; + CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']); + } + return Civi::$statics[__CLASS__]['fields']; + } + + /** + * Return a mapping from field-name to the corresponding key (as used in fields()). + * + * @return array + * Array(string $name => string $uniqueName). + */ + public static function &fieldKeys() { + if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) { + Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields())); + } + return Civi::$statics[__CLASS__]['fieldKeys']; + } + + /** + * Returns the names of this table + * + * @return string + */ + public static function getTableName() { + return self::$_tableName; + } + + /** + * Returns if this table needs to be logged + * + * @return bool + */ + public function getLog() { + return self::$_log; + } + + /** + * Returns the list of fields that can be imported + * + * @param bool $prefix + * + * @return array + */ + public static function &import($prefix = FALSE) { + $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'user_job', $prefix, []); + return $r; + } + + /** + * Returns the list of fields that can be exported + * + * @param bool $prefix + * + * @return array + */ + public static function &export($prefix = FALSE) { + $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'user_job', $prefix, []); + return $r; + } + + /** + * Returns the list of indices + * + * @param bool $localize + * + * @return array + */ + public static function indices($localize = TRUE) { + $indices = [ + 'UI_name' => [ + 'name' => 'UI_name', + 'field' => [ + 0 => 'name', + ], + 'localizable' => FALSE, + 'unique' => TRUE, + 'sig' => 'civicrm_user_job::1::name', + ], + ]; + return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices; + } + +} diff --git a/CRM/Upgrade/Incremental/sql/5.50.alpha1.mysql.tpl b/CRM/Upgrade/Incremental/sql/5.50.alpha1.mysql.tpl index 7002962990..e85f239dc9 100644 --- a/CRM/Upgrade/Incremental/sql/5.50.alpha1.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/5.50.alpha1.mysql.tpl @@ -1 +1,20 @@ {* file to handle db changes in 5.50.alpha1 during upgrade *} + +CREATE TABLE `civicrm_user_job` ( +`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'Job ID', +`name` varchar(64) COMMENT 'Unique name for job.', +`created_id` int unsigned COMMENT 'FK to contact table.', +`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date and time this job was created.', +`start_date` timestamp NULL COMMENT 'Date and time this import job started.', +`end_date` timestamp NULL COMMENT 'Date and time this import job ended.', +`expires_date` timestamp NULL COMMENT 'Date and time to clean up after this import job (temp table deletion date).', +`status_id` int unsigned NOT NULL, +`type_id` int unsigned NOT NULL, +`queue_id` int unsigned COMMENT 'FK to Queue', +`metadata` text COMMENT 'Data pertaining to job configuration', +PRIMARY KEY (`id`), +UNIQUE INDEX `UI_name`(name), +CONSTRAINT FK_civicrm_user_job_created_id FOREIGN KEY (`created_id`) REFERENCES `civicrm_contact`(`id`) ON DELETE SET NULL, +CONSTRAINT FK_civicrm_user_job_queue_id FOREIGN KEY (`queue_id`) REFERENCES `civicrm_queue`(`id`) ON DELETE SET NULL +) +ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC; diff --git a/Civi/Api4/UserJob.php b/Civi/Api4/UserJob.php new file mode 100644 index 0000000000..80761394e1 --- /dev/null +++ b/Civi/Api4/UserJob.php @@ -0,0 +1,24 @@ + [ 0 => 'contact_id', ], + 'civicrm_user_job' => [ + 0 => 'created_id', + ], 'civicrm_value_testgetcidref_1' => [ 0 => 'entity_id', ], diff --git a/xml/schema/Core/UserJob.xml b/xml/schema/Core/UserJob.xml new file mode 100644 index 0000000000..0c8113e0c9 --- /dev/null +++ b/xml/schema/Core/UserJob.xml @@ -0,0 +1,156 @@ + + + + CRM/Core + UserJob + civicrm_user_job + Tracking for user jobs (eg. imports). + 5.50 + false + + id + User Job ID + int unsigned + true + Job ID + + Number + + 5.50 + + + id + true + + + name + User job name + varchar + 64 + Unique name for job. + 5.50 + + + UI_name + name + true + 5.50 + + + created_id + int unsigned + Created By Contact ID + FK to contact table. + + + + 5.50 + + + created_id +
civicrm_contact
+ id + 5.50 + SET NULL + + + created_date + timestamp + CURRENT_TIMESTAMP + true + Import Job Created Date + true + Date and time this job was created. + 5.50 + + Select Date + activityDateTime + + + + start_date + timestamp + false + Import Job Started Date + Date and time this import job started. + 5.50 + + Select Date + activityDateTime + + true + + + end_date + timestamp + false + Job Ended Date + Date and time this import job ended. + 5.50 + + Select Date + activityDateTime + + + + expires_date + timestamp + false + Import Job Expires Date + Date and time to clean up after this import job (temp table deletion date). + 5.50 + + Select Date + activityDateTime + + + + status_id + User Job Status ID + int unsigned + true + + + + + CRM_Core_BAO_UserJob::getStatuses + + 5.50 + + + type_id + User Job Type ID + int unsigned + true + + + + + CRM_Core_BAO_UserJob::getTypes + + 5.50 + + + queue_id + Queue ID + int unsigned + FK to Queue + + + + + + queue_id + civicrm_queue
+ id + SET NULL +
+ + metadata + text + Job metadata + Data pertaining to job configuration + JSON + 5.50 + + diff --git a/xml/schema/Core/files.xml b/xml/schema/Core/files.xml index 05ae0fac88..8c45d2cdb8 100644 --- a/xml/schema/Core/files.xml +++ b/xml/schema/Core/files.xml @@ -42,6 +42,7 @@ + -- 2.25.1