summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMayra Cabrera <mcabrera@gitlab.com>2019-06-17 13:45:44 -0500
committerMayra Cabrera <mcabrera@gitlab.com>2019-06-19 12:05:52 -0500
commita338b50f76bb7f492fbe3e68760566e64fd607be (patch)
treec8a0b1579bfe42fd3d4683306051ca1fc459ed03 /spec
parent6d4f33ceafbf55ae1283352d092c873221fdcbf1 (diff)
downloadgitlab-ce-62214-namespace-database-migration.tar.gz
Migrate root_id column on Namespaces table62214-namespace-database-migration
Includes: - Migration to add root_id column on Namespaces table - Background migration to schedule the population of this new field
Diffstat (limited to 'spec')
-rw-r--r--spec/db/schema_spec.rb2
-rw-r--r--spec/lib/gitlab/background_migration/populate_namespace_root_id_column_spec.rb76
2 files changed, 77 insertions, 1 deletions
diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb
index 6cfec5f4017..55b42dbe35f 100644
--- a/spec/db/schema_spec.rb
+++ b/spec/db/schema_spec.rb
@@ -46,7 +46,7 @@ describe 'Database schema' do
ldap_group_links: %w[group_id],
members: %w[source_id created_by_id],
merge_requests: %w[last_edited_by_id state_id],
- namespaces: %w[owner_id parent_id],
+ namespaces: %w[owner_id parent_id root_id],
notes: %w[author_id commit_id noteable_id updated_by_id resolved_by_id discussion_id],
notification_settings: %w[source_id],
oauth_access_grants: %w[resource_owner_id application_id],
diff --git a/spec/lib/gitlab/background_migration/populate_namespace_root_id_column_spec.rb b/spec/lib/gitlab/background_migration/populate_namespace_root_id_column_spec.rb
new file mode 100644
index 00000000000..57e926b7c55
--- /dev/null
+++ b/spec/lib/gitlab/background_migration/populate_namespace_root_id_column_spec.rb
@@ -0,0 +1,76 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe Gitlab::BackgroundMigration::PopulateNamespaceRootIdColumn, :migration, schema: 20190617181054 do
+ let(:namespaces_table) { table(:namespaces) }
+
+ def create_namespace_for(parent:, iid:)
+ namespaces_table.create!(
+ name: "#{parent.name}-group_#{iid}",
+ path: "#{parent.name}-group_#{iid}",
+ parent_id: parent.id
+ )
+ end
+
+ describe '#perform' do
+ let(:root_namespace_a) { namespaces_table.create!(name: 'root_a', path: 'root-a') }
+ let(:root_namespace_b) { namespaces_table.create!(name: 'root_b', path: 'root-b') }
+
+ before do
+ (1..10).each do |subgroup_id|
+ create_namespace_for(parent: root_namespace_a, iid: subgroup_id)
+ create_namespace_for(parent: root_namespace_b, iid: subgroup_id)
+ end
+ end
+
+ it 'updates the root id of root namespaces' do
+ subject.perform(root_namespace_a.id, root_namespace_b.id)
+
+ expect(root_namespace_a.reload.root_id).to eq(root_namespace_a.id)
+ expect(root_namespace_b.reload.root_id).to eq(root_namespace_b.id)
+ end
+
+ it 'updates the root id of all namespaces' do
+ subject.perform(root_namespace_a.id, root_namespace_b.id)
+
+ namespace_a_children = namespaces_table.where(parent_id: root_namespace_a.id)
+ namespace_b_children = namespaces_table.where(parent_id: root_namespace_b.id)
+
+ namespace_a_children.each do |group|
+ expect(group.root_id).to eq(root_namespace_a.id)
+ end
+
+ namespace_b_children.each do |group|
+ expect(group.root_id).to eq(root_namespace_b.id)
+ end
+ end
+
+ context 'when a subgroup has children' do
+ let(:subgroup) do
+ create_namespace_for(
+ parent: root_namespace_a,
+ iid: 50
+ )
+ end
+
+ before do
+ (1..10).each do |subgroup_id|
+ create_namespace_for(parent: subgroup, iid: subgroup_id)
+ end
+ end
+
+ it 'updates inner groups' do
+ subject.perform(root_namespace_a.id, root_namespace_b.id)
+
+ subgroup_children = namespaces_table.where(parent_id: subgroup.id)
+
+ expect(subgroup.reload.root_id).to eq(root_namespace_a.id)
+
+ subgroup_children.each do |group|
+ expect(group.root_id).to eq(root_namespace_a.id)
+ end
+ end
+ end
+ end
+end