summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb')
-rw-r--r--spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb b/spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb
new file mode 100644
index 00000000000..8980a26932b
--- /dev/null
+++ b/spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::BackgroundMigration::BackfillCiNamespaceMirrors, :migration, schema: 20211208122200 do
+ let(:namespaces) { table(:namespaces) }
+ let(:ci_namespace_mirrors) { table(:ci_namespace_mirrors) }
+
+ subject { described_class.new }
+
+ describe '#perform' do
+ it 'creates hierarchies for all namespaces in range' do
+ namespaces.create!(id: 5, name: 'test1', path: 'test1')
+ namespaces.create!(id: 7, name: 'test2', path: 'test2')
+ namespaces.create!(id: 8, name: 'test3', path: 'test3')
+
+ subject.perform(5, 7)
+
+ expect(ci_namespace_mirrors.all).to contain_exactly(
+ an_object_having_attributes(namespace_id: 5, traversal_ids: [5]),
+ an_object_having_attributes(namespace_id: 7, traversal_ids: [7])
+ )
+ end
+
+ it 'handles existing hierarchies gracefully' do
+ namespaces.create!(id: 5, name: 'test1', path: 'test1')
+ test2 = namespaces.create!(id: 7, name: 'test2', path: 'test2')
+ namespaces.create!(id: 8, name: 'test3', path: 'test3', parent_id: 7)
+ namespaces.create!(id: 9, name: 'test4', path: 'test4')
+
+ # Simulate a situation where a user has had a chance to move a group to another parent
+ # before the background migration has had a chance to run
+ test2.update!(parent_id: 5)
+ ci_namespace_mirrors.create!(namespace_id: test2.id, traversal_ids: [5, 7])
+
+ subject.perform(5, 8)
+
+ expect(ci_namespace_mirrors.all).to contain_exactly(
+ an_object_having_attributes(namespace_id: 5, traversal_ids: [5]),
+ an_object_having_attributes(namespace_id: 7, traversal_ids: [5, 7]),
+ an_object_having_attributes(namespace_id: 8, traversal_ids: [5, 7, 8])
+ )
+ end
+ end
+end