Skip to content

batches: fix incorrect filter for draft batch changes

Administrator requested to merge bo/fix-batch-changes-filter into main

Created by: BolajiOlajide

Context

While working on my local dev branch, I noticed when I toggled the filter button to display Draft batch changes, the list returned included a batch change that was closed. Further investigation revealed the way we query for draft batch change is using the WHERE clause:

SELECT batch_changes.id ,  batch_changes.name ,  batch_changes.description ,  batch_changes.creator_id ,  batch_changes.last_applier_id ,  batch_changes.last_applied_at ,  batch_changes.namespace_user_id ,  batch_changes.namespace_org_id ,  batch_changes.created_at ,  batch_changes.updated_at ,  batch_changes.closed_at ,  batch_changes.batch_spec_id FROM batch_changes
LEFT JOIN users namespace_user ON batch_changes.namespace_user_id = namespace_user.id
 LEFT JOIN orgs namespace_org ON batch_changes.namespace_org_id = namespace_org.id
WHERE namespace_user.deleted_at IS NULL
 AND  namespace_org.deleted_at IS NULL
 AND  (batch_changes.last_applied_at IS NULL)
ORDER BY id DESC
LIMIT 8;

The culprit being (batch_changes.last_applied_at IS NULL) returns an incorrect filtered list because it's possible to have a batch change that hasn't being applied (hence it being a draft) but was closed by the user.

Updating the SQL statement to

SELECT batch_changes.id ,  batch_changes.name ,  batch_changes.description ,  batch_changes.creator_id ,  batch_changes.last_applier_id ,  batch_changes.last_applied_at ,  batch_changes.namespace_user_id ,  batch_changes.namespace_org_id ,  batch_changes.created_at ,  batch_changes.updated_at ,  batch_changes.closed_at ,  batch_changes.batch_spec_id FROM batch_changes
LEFT JOIN users namespace_user ON batch_changes.namespace_user_id = namespace_user.id
 LEFT JOIN orgs namespace_org ON batch_changes.namespace_org_id = namespace_org.id
WHERE namespace_user.deleted_at IS NULL
 AND  namespace_org.deleted_at IS NULL
 AND  (batch_changes.last_applied_at IS NULL AND batch_changes.closed_at IS NULL)
ORDER BY id DESC
LIMIT 8;

fixes this.

Test plan

  • Navigate to the batch changes list page
  • Click on the DRAFT filter and no closed batch change should be included

Before

incorect-bc-filter-before

After

incorrect-bc-filter-after

CleanShot 2022-05-27 at 02 26 55@2x

k8s link

Merge request reports

Loading