Commit 3a3e0375 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch 'reduce-diff-with-ee-in-migration-spec' into 'master'

[EE] Reduce differences with CE in a migration spec

See merge request gitlab-org/gitlab-ee!10052
parents 73c70dcf f747f5a6
# frozen_string_literal: true
# rubocop:disable RSpec/FactoriesInMigrationSpecs
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20180723130817_delete_inconsistent_internal_id_records.rb')
describe DeleteInconsistentInternalIdRecords, :migration do
context 'for milestones (by group)' do
# milestones (by group) is a little different than most of the other models
context 'for epics (by group)' do
let(:groups) { table(:namespaces) }
let(:users) { table(:users) }
let(:group1) { groups.create(name: 'Group 1', type: 'Group', path: 'group_1') }
let(:group2) { groups.create(name: 'Group 2', type: 'Group', path: 'group_2') }
let(:group3) { groups.create(name: 'Group 2', type: 'Group', path: 'group_3') }
let!(:user) { users.create!(email: 'email@email.com', name: 'foo', username: 'foo', projects_limit: 0) }
let(:internal_id_query) { ->(group) { InternalId.where(usage: InternalId.usages['milestones'], namespace: group) } }
let(:internal_id_query) { ->(group) { InternalId.where(usage: InternalId.usages['epics'], namespace_id: group.id) } }
before do
3.times { create(:milestone, group_id: group1.id) }
3.times { create(:milestone, group_id: group2.id) }
3.times { create(:milestone, group_id: group3.id) }
# we use state enum in Epic but state field was added after this migration
epics = table(:epics)
epics.belongs_to(:group)
epics.include(AtomicInternalId)
epics.has_internal_id(:iid, scope: :group, init: ->(s) { s&.group&.epics&.maximum(:iid) })
epics.create!(title: 'Epic 1', title_html: 'Epic 1', group_id: group1.id, author_id: user.id)
epics.create!(title: 'Epic 2', title_html: 'Epic 2', group_id: group1.id, author_id: user.id)
epics.create!(title: 'Epic 3', title_html: 'Epic 3', group_id: group1.id, author_id: user.id)
epics.create!(title: 'Epic 4', title_html: 'Epic 4', group_id: group2.id, author_id: user.id)
epics.create!(title: 'Epic 5', title_html: 'Epic 5', group_id: group2.id, author_id: user.id)
epics.create!(title: 'Epic 6', title_html: 'Epic 6', group_id: group2.id, author_id: user.id)
epics.create!(title: 'Epic 7', title_html: 'Epic 7', group_id: group3.id, author_id: user.id)
epics.create!(title: 'Epic 8', title_html: 'Epic 8', group_id: group3.id, author_id: user.id)
epics.create!(title: 'Epic 9', title_html: 'Epic 9', group_id: group3.id, author_id: user.id)
internal_id_query.call(group1).first.tap do |iid|
iid.last_value = iid.last_value - 2
......@@ -31,11 +45,11 @@ describe DeleteInconsistentInternalIdRecords, :migration do
end
end
it "deletes inconsistent issues" do
it "deletes inconsistent records" do
expect { migrate! }.to change { internal_id_query.call(group1).size }.from(1).to(0)
end
it "retains consistent issues" do
it "retains consistent records" do
expect { migrate! }.not_to change { internal_id_query.call(group2).size }
end
......
# frozen_string_literal: true
# rubocop:disable RSpec/FactoriesInMigrationSpecs
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20180723130817_delete_inconsistent_internal_id_records.rb')
describe DeleteInconsistentInternalIdRecords, :migration do
let!(:project1) { create(:project) }
let!(:project2) { create(:project) }
let!(:project3) { create(:project) }
let!(:namespace) { table(:namespaces).create!(name: 'test', path: 'test') }
let!(:project1) { table(:projects).create!(namespace_id: namespace.id) }
let!(:project2) { table(:projects).create!(namespace_id: namespace.id) }
let!(:project3) { table(:projects).create!(namespace_id: namespace.id) }
let(:internal_id_query) { ->(project) { InternalId.where(usage: InternalId.usages[scope.to_s.tableize], project: project) } }
let(:internal_ids) { table(:internal_ids) }
let(:internal_id_query) { ->(project) { InternalId.where(usage: InternalId.usages[scope.to_s.tableize], project_id: project.id) } }
let(:create_models) do
3.times { create(scope, project: project1) }
3.times { create(scope, project: project2) }
3.times { create(scope, project: project3) }
[project1, project2, project3].each do |project|
3.times do |i|
attributes = required_attributes.merge(project_id: project.id,
iid: i.succ)
table(scope.to_s.pluralize).create!(attributes)
end
end
end
shared_examples_for 'deleting inconsistent internal_id records' do
before do
create_models
[project1, project2, project3].each do |project|
internal_ids.create!(project_id: project.id, usage: InternalId.usages[scope.to_s.tableize], last_value: 3)
end
internal_id_query.call(project1).first.tap do |iid|
iid.last_value = iid.last_value - 2
# This is an inconsistent record
......@@ -33,11 +44,11 @@ describe DeleteInconsistentInternalIdRecords, :migration do
end
end
it "deletes inconsistent issues" do
it "deletes inconsistent records" do
expect { migrate! }.to change { internal_id_query.call(project1).size }.from(1).to(0)
end
it "retains consistent issues" do
it "retains consistent records" do
expect { migrate! }.not_to change { internal_id_query.call(project2).size }
end
......@@ -48,6 +59,8 @@ describe DeleteInconsistentInternalIdRecords, :migration do
context 'for issues' do
let(:scope) { :issue }
let(:required_attributes) { {} }
it_behaves_like 'deleting inconsistent internal_id records'
end
......@@ -55,9 +68,17 @@ describe DeleteInconsistentInternalIdRecords, :migration do
let(:scope) { :merge_request }
let(:create_models) do
3.times { |i| create(scope, target_project: project1, source_project: project1, source_branch: i.to_s) }
3.times { |i| create(scope, target_project: project2, source_project: project2, source_branch: i.to_s) }
3.times { |i| create(scope, target_project: project3, source_project: project3, source_branch: i.to_s) }
[project1, project2, project3].each do |project|
3.times do |i|
table(:merge_requests).create!(
target_project_id: project.id,
source_project_id: project.id,
target_branch: 'master',
source_branch: j.to_s,
iid: i.succ
)
end
end
end
it_behaves_like 'deleting inconsistent internal_id records'
......@@ -66,13 +87,6 @@ describe DeleteInconsistentInternalIdRecords, :migration do
context 'for deployments' do
let(:scope) { :deployment }
let(:deployments) { table(:deployments) }
let(:internal_ids) { table(:internal_ids) }
before do
internal_ids.create!(project_id: project1.id, usage: 2, last_value: 2)
internal_ids.create!(project_id: project2.id, usage: 2, last_value: 2)
internal_ids.create!(project_id: project3.id, usage: 2, last_value: 2)
end
let(:create_models) do
3.times { |i| deployments.create!(project_id: project1.id, iid: i, environment_id: 1, ref: 'master', sha: 'a', tag: false) }
......@@ -85,48 +99,39 @@ describe DeleteInconsistentInternalIdRecords, :migration do
context 'for milestones (by project)' do
let(:scope) { :milestone }
let(:required_attributes) { { title: 'test' } }
it_behaves_like 'deleting inconsistent internal_id records'
end
context 'for ci_pipelines' do
let(:scope) { :ci_pipeline }
let(:create_models) do
create_list(:ci_empty_pipeline, 3, project: project1)
create_list(:ci_empty_pipeline, 3, project: project2)
create_list(:ci_empty_pipeline, 3, project: project3)
end
let(:required_attributes) { { ref: 'test' } }
it_behaves_like 'deleting inconsistent internal_id records'
end
context 'for milestones (by group)' do
# epics (by group) is a little different than most of the other models
# milestones (by group) is a little different than most of the other models
let(:groups) { table(:namespaces) }
let(:group1) { groups.create(name: 'Group 1', type: 'Group', path: 'group_1') }
let(:group2) { groups.create(name: 'Group 2', type: 'Group', path: 'group_2') }
let(:group3) { groups.create(name: 'Group 2', type: 'Group', path: 'group_3') }
let!(:user) { create(:user) }
let(:internal_id_query) { ->(group) { InternalId.where(usage: InternalId.usages['epics'], namespace: group) } }
let(:internal_id_query) { ->(group) { InternalId.where(usage: InternalId.usages['milestones'], namespace_id: group.id) } }
before do
# we use state enum in Epic but state field was added after this migration
epics = table(:epics)
epics.belongs_to(:group)
epics.include(AtomicInternalId)
epics.has_internal_id(:iid, scope: :group, init: ->(s) { s&.group&.epics&.maximum(:iid) })
epics.create!(title: 'Epic 1', title_html: 'Epic 1', group_id: group1.id, author_id: user.id)
epics.create!(title: 'Epic 2', title_html: 'Epic 2', group_id: group1.id, author_id: user.id)
epics.create!(title: 'Epic 3', title_html: 'Epic 3', group_id: group1.id, author_id: user.id)
epics.create!(title: 'Epic 4', title_html: 'Epic 4', group_id: group2.id, author_id: user.id)
epics.create!(title: 'Epic 5', title_html: 'Epic 5', group_id: group2.id, author_id: user.id)
epics.create!(title: 'Epic 6', title_html: 'Epic 6', group_id: group2.id, author_id: user.id)
epics.create!(title: 'Epic 7', title_html: 'Epic 7', group_id: group3.id, author_id: user.id)
epics.create!(title: 'Epic 8', title_html: 'Epic 8', group_id: group3.id, author_id: user.id)
epics.create!(title: 'Epic 9', title_html: 'Epic 9', group_id: group3.id, author_id: user.id)
[group1, group2, group3].each do |group|
3.times do |i|
table(:milestones).create!(
group_id: group.id,
title: 'test',
iid: i.succ
)
end
internal_ids.create!(namespace_id: group.id, usage: InternalId.usages['milestones'], last_value: 3)
end
internal_id_query.call(group1).first.tap do |iid|
iid.last_value = iid.last_value - 2
......
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