BigW Consortium Gitlab

Commit d4e5ac1b by Toon Claes

Use EachBatch concern to loop over batches

parent 2074f39a
...@@ -19,7 +19,7 @@ class NamespacelessProjectDestroyWorker ...@@ -19,7 +19,7 @@ class NamespacelessProjectDestroyWorker
return return
end end
return if namespace?(project) # Reject doing anything for projects that *do* have a namespace return if project.namespace # Reject doing anything for projects that *do* have a namespace
project.team.truncate project.team.truncate
...@@ -30,10 +30,6 @@ class NamespacelessProjectDestroyWorker ...@@ -30,10 +30,6 @@ class NamespacelessProjectDestroyWorker
private private
def namespace?(project)
project.namespace_id && Namespace.exists?(project.namespace_id)
end
def unlink_fork(project) def unlink_fork(project)
merge_requests = project.forked_from_project.merge_requests.opened.from_project(project) merge_requests = project.forked_from_project.merge_requests.opened.from_project(project)
......
...@@ -8,35 +8,31 @@ class CleanupNonexistingNamespacePendingDeleteProjects < ActiveRecord::Migration ...@@ -8,35 +8,31 @@ class CleanupNonexistingNamespacePendingDeleteProjects < ActiveRecord::Migration
disable_ddl_transaction! disable_ddl_transaction!
def up class Project < ActiveRecord::Base
@offset = 0 self.table_name = 'projects'
loop do include ::EachBatch
ids = pending_delete_batch end
break if ids.empty? class Namespace < ActiveRecord::Base
self.table_name = 'namespaces'
end
args = ids.map { |id| Array(id) } def up
find_projects.each_batch do |batch|
args = batch.pluck(:id).map { |id| [id] }
NamespacelessProjectDestroyWorker.bulk_perform_async(args) NamespacelessProjectDestroyWorker.bulk_perform_async(args)
@offset += 1
end end
end end
def down def down
# noop # NOOP
end end
private private
def pending_delete_batch def find_projects
connection.exec_query(find_batch).map { |row| row['id'].to_i }
end
BATCH_SIZE = 5000
def find_batch
projects = Project.arel_table projects = Project.arel_table
namespaces = Namespace.arel_table namespaces = Namespace.arel_table
...@@ -44,11 +40,15 @@ class CleanupNonexistingNamespacePendingDeleteProjects < ActiveRecord::Migration ...@@ -44,11 +40,15 @@ class CleanupNonexistingNamespacePendingDeleteProjects < ActiveRecord::Migration
.where(namespaces[:id].eq(projects[:namespace_id])) .where(namespaces[:id].eq(projects[:namespace_id]))
.exists.not .exists.not
projects.project(projects[:id]) # SELECT "projects"."id"
.where(projects[:pending_delete].eq(true)) # FROM "projects"
# WHERE "projects"."pending_delete" = 't'
# AND (NOT (EXISTS
# (SELECT 1
# FROM "namespaces"
# WHERE "namespaces"."id" = "projects"."namespace_id")))
Project.where(projects[:pending_delete].eq(true))
.where(namespace_query) .where(namespace_query)
.skip(@offset * BATCH_SIZE) .select(:id)
.take(BATCH_SIZE)
.to_sql
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment