You are here

function tmgmt_extension_suit_update_8002 in TMGMT Extension Suite 8

Same name and namespace in other branches
  1. 8.3 tmgmt_extension_suit.install \tmgmt_extension_suit_update_8002()
  2. 8.2 tmgmt_extension_suit.install \tmgmt_extension_suit_update_8002()

Transform all existing continuous jobs into regular jobs.

File

./tmgmt_extension_suit.install, line 47
Update function for the tmgmt_extension_suit module.

Code

function tmgmt_extension_suit_update_8002() {

  // Add active continuous jobs into upload queue.
  $select = \Drupal::database()
    ->select('tmgmt_job', 'tj');
  $select
    ->distinct();
  $select
    ->fields('tj', [
    'tjid',
  ]);
  $select
    ->condition('job_type', JobInterface::TYPE_CONTINUOUS);
  $select
    ->condition('continuous_settings', serialize([]));
  $select
    ->condition('tj.state', [
    JobInterface::STATE_CONTINUOUS,
  ], 'IN');
  $select
    ->join('tmgmt_job_item', 'tji', 'tji.tjid = tj.tjid');
  $select
    ->condition('tji.state', [
    JobItemInterface::STATE_INACTIVE,
    JobItemInterface::STATE_ACTIVE,
  ], 'IN');
  $ids = $select
    ->execute()
    ->fetchCol();
  foreach ($ids as $id) {
    \Drupal::queue('tmgmt_extension_suit_upload')
      ->createItem([
      'id' => $id,
    ]);
  }

  // Drupal::database()->update() doesn't have join method so here we are using
  // Drupal::database()->query() method.
  $query = 'UPDATE tmgmt_job as tj
    INNER JOIN tmgmt_job_item as tji
      ON tj.tjid = tji.tjid
    SET tj.job_type = :job_to_type, tj.state = :job_to_state
    WHERE tj.job_type = :job_from_type
      AND tj.state = :job_from_state
      AND tj.continuous_settings = :job_continuous_settings
      AND tji.state IN (:job_item_states[])';
  $mapping = [
    // Migrate active continuous jobs to active state.
    [
      'job_to_state' => JobInterface::STATE_ACTIVE,
      'job_item_states' => [
        JobItemInterface::STATE_INACTIVE,
        JobItemInterface::STATE_ACTIVE,
        JobItemInterface::STATE_REVIEW,
      ],
    ],
    // Migrate finished continuous jobs to finished state.
    [
      'job_to_state' => JobInterface::STATE_FINISHED,
      'job_item_states' => [
        JobItemInterface::STATE_ACCEPTED,
      ],
    ],
  ];
  foreach ($mapping as $item) {
    Drupal::database()
      ->query($query, [
      ':job_to_type' => JobInterface::TYPE_NORMAL,
      ':job_to_state' => $item['job_to_state'],
      ':job_from_type' => JobInterface::TYPE_CONTINUOUS,
      ':job_from_state' => JobInterface::STATE_CONTINUOUS,
      ':job_continuous_settings' => serialize([]),
      ':job_item_states[]' => $item['job_item_states'],
    ]);
  }

  // Migrate continuous inactive jobs to aborted state.
  Drupal::database()
    ->update('tmgmt_job')
    ->condition('job_type', JobInterface::TYPE_CONTINUOUS)
    ->condition('continuous_settings', serialize([]))
    ->condition('state', JobInterface::STATE_CONTINUOUS_INACTIVE)
    ->fields([
    'job_type' => JobInterface::TYPE_NORMAL,
    'state' => JobInterface::STATE_ABORTED,
  ])
    ->execute();
}