You are here

function drush_salesforce_mapping_sf_prune_revisions in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 modules/salesforce_mapping/salesforce_mapping.drush.inc \drush_salesforce_mapping_sf_prune_revisions()
  2. 5.0.x modules/salesforce_mapping/salesforce_mapping.drush.inc \drush_salesforce_mapping_sf_prune_revisions()

Support for drush 8 is deprecated and will be removed in a future release.

File

modules/salesforce_mapping/salesforce_mapping.drush.inc, line 56
Drush integration for Salesforce.

Code

function drush_salesforce_mapping_sf_prune_revisions() {
  _drush_salesforce_deprecated();
  $limit = \Drupal::service('config.factory')
    ->get('salesforce.settings')
    ->get('limit_mapped_object_revisions');
  if ($limit <= 0) {
    drush_log('Mapped Object revisions limit is 0. No action taken.', 'warning');
    return;
  }
  $etm = \Drupal::service('entity_type.manager');

  /** @var \Drupal\salesforce_mapping\MappedObjectStorage $storage */
  $storage = $etm
    ->getStorage('salesforce_mapped_object');
  $revision_table = $etm
    ->getDefinition('salesforce_mapped_object')
    ->getRevisionTable();
  $ids = \Drupal::service('database')
    ->select($revision_table, 'r')
    ->fields('r', [
    'id',
  ])
    ->having('COUNT(r.id) > ' . $limit)
    ->groupBy('r.id')
    ->execute()
    ->fetchCol();
  if (empty($ids)) {
    drush_log("No Mapped Objects with more than {$limit} revision(s). No action taken.", 'warning');
    return;
  }
  drush_log('Found ' . count($ids) . ' mapped objects with excessive revisions. Will prune to ' . $limit . ' revision(s) each. This may take a while.', 'ok');
  $total = count($ids);
  $i = 0;
  $buckets = ceil($total / 20);
  if ($buckets == 0) {
    $buckets = 1;
  }
  foreach ($ids as $id) {
    if ($i++ % $buckets == 0) {
      drush_log("Pruned {$i} of {$total} records.", 'ok');
    }

    /** @var \Drupal\salesforce_mapping\Entity\MappedObject $mapped_object */
    if ($mapped_object = $storage
      ->load($id)) {
      $mapped_object
        ->pruneRevisions($storage);
    }
  }
}