Do not categorise trashed contacts

This commit is contained in:
Rich Lott / Artful Robot 2025-02-28 10:02:14 +00:00
parent 96a1483976
commit a967d9a538

View file

@ -188,20 +188,37 @@ class Processor {
} }
} }
/**
* Ensure we have one category row per not-trashed contact and zero the next_category field.
*/
protected function resetTable() { protected function resetTable() {
Civi::log()->debug('Resetting table', ['=' => 'start,timed']); Civi::log()->debug('Resetting table', ['=' => 'start,timed']);
// clear out temp space.
// Delete our data for deleted contacts.
$dao = CRM_Core_DAO::executeQuery(<<<SQL
DELETE cat
FROM civicrm_contact_category cat
INNER JOIN civicrm_contact ct ON ct.id = cat.id AND ct.is_deleted = 1
SQL);
if ($dao->N) {
Civi::log()->debug("Deleted category data for {$dao->N} trashed contacts");
}
$dao->free();
// Zero our internal next_category field.
CRM_Core_DAO::executeQuery("UPDATE civicrm_contact_category SET next_category = 0;")->free(); CRM_Core_DAO::executeQuery("UPDATE civicrm_contact_category SET next_category = 0;")->free();
Civi::log()->debug('Resetting table stage 2'); Civi::log()->debug('stage 2');
// ensure we have all our contacts covered. // ensure we have all our contacts covered.
// Q: is it quicker to do a WHERE NOT EXISTS? A: nope. // Q: is it quicker to do a WHERE NOT EXISTS? A: nope.
CRM_Core_DAO::executeQuery(<<<SQL CRM_Core_DAO::executeQuery(<<<SQL
INSERT INTO civicrm_contact_category INSERT INTO civicrm_contact_category
SELECT id, NULL as category_definition_id, 0 next_category SELECT id, NULL AS category_definition_id, 0 AS next_category
FROM civicrm_contact FROM civicrm_contact
WHERE NOT EXISTS (SELECT id FROM civicrm_contact_category WHERE id = civicrm_contact.id) WHERE is_deleted = 0
AND NOT EXISTS (SELECT id FROM civicrm_contact_category WHERE id = civicrm_contact.id)
SQL)->free(); SQL)->free();
Civi::log()->debug('', ['=' => 'pop']); Civi::log()->debug('Done resetting table', ['=' => 'pop']);
} }
/** /**
@ -246,7 +263,8 @@ class Processor {
unset($apiParams['orderBy']); unset($apiParams['orderBy']);
$contactIDs = civicrm_api4($search['api_entity'], 'get', $apiParams)->column($contactIdKey); $contactIDs = civicrm_api4($search['api_entity'], 'get', $apiParams)->column($contactIdKey);
// Unsure if this batching is needed // Unsure if this batching is needed.
// It would be better if we could access the select SQL directly and do it all in an SQL update.
$batchSize = 10000; $batchSize = 10000;
while ($batch = array_splice($contactIDs, 0, $batchSize)) { while ($batch = array_splice($contactIDs, 0, $batchSize)) {
$batch = implode(',', $batch); $batch = implode(',', $batch);
@ -261,7 +279,7 @@ class Processor {
SQL; SQL;
CRM_Core_DAO::executeQuery($sql)->free(); CRM_Core_DAO::executeQuery($sql)->free();
} }
Civi::log()->debug('', ['=' => 'pop']); Civi::log()->debug('Done', ['=' => 'pop']);
} }
protected function assignDefaultCategory(array $cat) { protected function assignDefaultCategory(array $cat) {