Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/models/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ def self.delete_misplaced_github_rows(host, from_id: 0, batch_size: 10_000)
home_ids = {}
from_id.step(max_id, batch_size) do |start|
rows = where(host_id: host.id, id: start...(start + batch_size)).where.not(node_id: nil)
.where('EXISTS (SELECT 1 FROM issues m WHERE m.repository_id = issues.repository_id AND m.number = issues.number AND m.node_id IS NOT NULL AND m.id <> issues.id)')
.pluck(:id, :repository_id, :node_id)
# A repository's rows sit close together by id, so a bounded cache still hits.
home_ids.clear if home_ids.size > 100_000
misplaced = rows.filter_map do |id, repository_id, node_id|
node_repository_id = github_node_repository_id(node_id)
next unless node_repository_id
home_id = home_ids.fetch(repository_id) { home_ids[repository_id] = github_repository_id_for(repository_id) }
[id, repository_id] if node_repository_id && home_id && node_repository_id != home_id
[id, repository_id] if home_id && node_repository_id != home_id
end
# Matching repository_id skips rows a concurrent sync has already moved to their own repository.
deleted += misplaced.group_by(&:last).sum { |repository_id, pairs| where(id: pairs.map(&:first), repository_id: repository_id).delete_all }
Expand Down
16 changes: 14 additions & 2 deletions test/models/issue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ class IssueTest < ActiveSupport::TestCase
own = (1..10).map { |n| create(:issue, repository: repository, number: n, node_id: node_id(35861629, n)) }
misplaced = create(:issue, repository: repository, number: 1, pull_request: true, node_id: node_id(193140650, 99))
unpaired = create(:issue, repository: repository, number: 99, node_id: node_id(193140650, 98))
legacy = create(:issue, repository: repository, number: 100, node_id: 'MDExOlB1bGxSZXF1ZXN0NDk0NDM4NjM=')

tied = create(:repository, host: host)
tied_rows = [create(:issue, repository: tied, number: 1, node_id: node_id(1, 1)), create(:issue, repository: tied, number: 1, node_id: node_id(2, 1))]

assert_equal 1, Issue.delete_misplaced_github_rows(host, batch_size: 3)
assert_equal 2, Issue.delete_misplaced_github_rows(host, batch_size: 3)
refute Issue.exists?(misplaced.id)
refute Issue.exists?(unpaired.id)
assert Issue.exists?(legacy.id)
assert own.all? { |i| Issue.exists?(i.id) }
assert Issue.exists?(unpaired.id)
assert tied_rows.all? { |i| Issue.exists?(i.id) }
end

Expand All @@ -72,6 +74,16 @@ class IssueTest < ActiveSupport::TestCase
assert legacy.all? { |i| Issue.exists?(i.id) }
end

test "delete_misplaced_github_rows skips the repository lookup for legacy node ids" do
host = create_or_find_github_host
repository = create(:repository, host: host)
legacy = create(:issue, repository: repository, number: 1, node_id: 'MDExOlB1bGxSZXF1ZXN0NDk0NDM4NjM=')
Issue.expects(:github_repository_id_for).never

assert_equal 0, Issue.delete_misplaced_github_rows(host)
assert Issue.exists?(legacy.id)
end

test "delete_misplaced_github_rows skips repositories without a clear majority" do
host = create_or_find_github_host
repository = create(:repository, host: host)
Expand Down
Loading