Skip to content

Commit

Permalink
Merge pull request #1379 from Shopify/batch-delete-all
Browse files Browse the repository at this point in the history
Improve DestroyStackJob
  • Loading branch information
casperisfine authored Nov 14, 2024
2 parents 1283edc + 22d05ec commit 50ab00a
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions app/jobs/shipit/destroy_stack_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,39 @@ class DestroyStackJob < BackgroundJob
# +-- chunks

def perform(stack)
Shipit::ApiClient.where(stack_id: stack.id).delete_all
commits_ids = Shipit::Commit.where(stack_id: stack.id).pluck(:id)
tasks_ids = Shipit::Task.where(stack_id: stack.id).pluck(:id)
commit_deployments_ids = Shipit::CommitDeployment.where(task_id: tasks_ids).pluck(:id)
Shipit::CommitDeploymentStatus.where(commit_deployment_id: commit_deployments_ids).in_batches(&:delete_all)
Shipit::CommitDeployment.where(id: commit_deployments_ids).in_batches(&:delete_all)

Shipit::Status.where(commit_id: commits_ids).find_in_batches do |batch|
Shipit::Status.where(id: batch.map(&:id)).delete_all
end
delete(Shipit::ApiClient.where(stack_id: stack.id))

commits_ids.each_slice(1000) do |batch|
Shipit::Commit.where(id: batch).delete_all
end
delete(
Shipit::CommitDeploymentStatus
.joins(commit_deployment: [:task])
.where(commit_deployment: { tasks: { stack_id: stack.id } })
)

Shipit::GithubHook.where(stack_id: stack.id).destroy_all
Shipit::Hook.where(stack_id: stack.id).in_batches(&:delete_all)
Shipit::MergeRequest.where(stack_id: stack.id).in_batches(&:delete_all)
tasks_ids.each_slice(100) do |ids|
Shipit::OutputChunk.where(task_id: ids).in_batches(&:delete_all)
Shipit::Task.where(id: ids).in_batches(&:delete_all)
end
delete(Shipit::CommitDeployment.joins(:task).where(task: { stack_id: stack.id }))
delete(Shipit::Status.joins(:commit).where(commit: { stack_id: stack.id }))
delete(Shipit::GithubHook.where(stack_id: stack.id))
delete(Shipit::Hook.where(stack_id: stack.id))
delete(Shipit::MergeRequest.where(stack_id: stack.id))

delete(Shipit::OutputChunk.joins(:task).where(task: { stack_id: stack.id }))
delete(Shipit::Task.where(stack_id: stack.id))
stack.destroy!
end

private

BATCH_SIZE = 1000

def delete(relation)
if relation.connection.adapter_name.match?(/(mysql|trilogy)/i)
while relation.limit(BATCH_SIZE).delete_all == BATCH_SIZE
true # loop
end
else
while relation.model.where(id: relation.select(:id).limit(BATCH_SIZE)).delete_all == BATCH_SIZE
true # loop
end
end
end
end
end

0 comments on commit 50ab00a

Please sign in to comment.