Commit ea6e0b81 authored by Furkan Ayhan's avatar Furkan Ayhan

Apply reviewer feedbacks

parent 052dfa05
......@@ -6,9 +6,9 @@ module Ci
class NamespaceMirror < ApplicationRecord
belongs_to :namespace
scope :contains_namespaces, -> (ids) {
where('traversal_ids @> ARRAY[?]::int[]', ids.join(','))
}
scope :contains_namespace, -> (id) do
where('traversal_ids @> ARRAY[?]::int[]', id)
end
class << self
def sync!(event)
......@@ -25,10 +25,10 @@ module Ci
private
def sync_children_namespaces!(namespace_id, traversal_ids)
contains_namespaces([namespace_id])
contains_namespace(namespace_id)
.where.not(namespace_id: namespace_id)
.update_all(
"traversal_ids = ARRAY[#{traversal_ids.join(',')}]::int[] || traversal_ids[array_position(traversal_ids, #{namespace_id}) + 1:]"
"traversal_ids = ARRAY[#{sanitize_sql(traversal_ids.join(','))}]::int[] || traversal_ids[array_position(traversal_ids, #{sanitize_sql(namespace_id)}) + 1:]"
)
end
end
......
......@@ -619,6 +619,7 @@ class Namespace < ApplicationRecord
path_changed? && !project_namespace?
end
# SyncEvents are created by PG triggers (with the function `insert_namespaces_sync_event`)
def schedule_sync_event_worker
run_after_commit do
Namespaces::SyncEvent.enqueue_worker
......
......@@ -2938,6 +2938,7 @@ class Project < ApplicationRecord
project_namespace.visibility_level = visibility_level
end
# SyncEvents are created by PG triggers (with the function `insert_projects_sync_event`)
def schedule_sync_event_worker
run_after_commit do
Projects::SyncEvent.enqueue_worker
......
......@@ -29,8 +29,8 @@ module Ci
return if events.empty?
min = events[0]
max = events[-1]
min = events.first
max = events.last
events.each { |event| @sync_class.sync!(event) }
@sync_event_class.for_id(min.id..max.id).delete_all
......
......@@ -597,6 +597,8 @@ project:
- security_scans
- ci_feature_usages
- bulk_import_exports
- ci_project_mirror
- sync_events
award_emoji:
- awardable
- user
......
......@@ -9,7 +9,7 @@ RSpec.describe Ci::NamespaceMirror do
let!(:group4) { create(:group, parent: group3) }
describe '.sync!' do
let!(:event) { Namespaces::SyncEvent.create!(namespace: namespace) }
let!(:event) { namespace.sync_events.create! }
subject(:sync) { described_class.sync!(event.reload) }
......
......@@ -1540,7 +1540,9 @@ RSpec.describe User do
allow(user).to receive(:update_highest_role)
end
allow(SecureRandom).to receive(:hex).and_return('3b8ca303')
# Namespace#schedule_sync_event_worker => Sidekiq calls `SecureRandom.hex(12)` to generate `jid`
expect(SecureRandom).to receive(:hex).with(12).and_call_original
expect(SecureRandom).to receive(:hex).with(no_args).and_return('3b8ca303')
user = create(:user)
......
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