Commit ea6e0b81 authored by Furkan Ayhan's avatar Furkan Ayhan

Apply reviewer feedbacks

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