Commit 77e0371b authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'fix-iteration-migr' into 'master'

Fix SetDefaultIterationCadences migration

See merge request gitlab-org/gitlab!54598
parents 161745ea 12388f9f
---
title: Fix setting default cadences migration
merge_request: 54598
author:
type: other
......@@ -17,11 +17,7 @@ class ScheduleSetDefaultIterationCadences < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def up
Iteration.select(:group_id).distinct.each_batch(of: BATCH_SIZE, column: :group_id) do |batch, index|
group_ids = batch.pluck(:group_id)
migrate_in(index * DELAY_INTERVAL, MIGRATION_CLASS, group_ids)
end
# Do nothing, rescheduling migration: 20210219102900_reschedule_set_default_iteration_cadences.rb
end
def down
......
# frozen_string_literal: true
class RescheduleSetDefaultIterationCadences < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
BATCH_SIZE = 1_000
DELAY_INTERVAL = 2.minutes.to_i
MIGRATION_CLASS = 'SetDefaultIterationCadences'
class Iteration < ActiveRecord::Base # rubocop:disable Style/Documentation
include EachBatch
self.table_name = 'sprints'
end
disable_ddl_transaction!
def up
Iteration.select(:group_id).distinct.each_batch(of: BATCH_SIZE, column: :group_id) do |batch, index|
group_ids = batch.pluck(:group_id)
migrate_in(index * DELAY_INTERVAL, MIGRATION_CLASS, group_ids)
end
end
def down
# Not needed
end
end
42e06332b279aaac7044243df0a8bd5525025db7d8c22bc474c0874e85f525f5
\ No newline at end of file
......@@ -16,6 +16,8 @@ module Gitlab
class Group < ApplicationRecord
self.table_name = 'namespaces'
self.inheritance_column = :_type_disabled
end
def perform(*group_ids)
......@@ -27,6 +29,7 @@ module Gitlab
def create_iterations_cadences(group_ids)
groups_with_cadence = IterationCadence.select(:group_id)
new_cadences = Group.where(id: group_ids).where.not(id: groups_with_cadence).map do |group|
last_iteration = Iteration.where(group_id: group.id).order(:start_date)&.last
......@@ -44,7 +47,7 @@ module Gitlab
)
end
IterationCadence.bulk_insert!(new_cadences.compact)
IterationCadence.bulk_insert!(new_cadences.compact, skip_duplicates: true)
end
def assign_iterations_cadences(group_ids)
......
......@@ -17,8 +17,10 @@ RSpec.describe Gitlab::BackgroundMigration::SetDefaultIterationCadences, schema:
let!(:iteration_2) { iterations.create!(group_id: group_3.id, iid: 1, title: 'Iteration 2', start_date: 10.days.ago, due_date: 8.days.ago) }
let!(:iteration_3) { iterations.create!(group_id: group_3.id, iid: 1, title: 'Iteration 3', start_date: 5.days.ago, due_date: 2.days.ago) }
subject { described_class.new.perform(group_1.id, group_2.id, group_3.id, namespaces.last.id + 1) }
before do
described_class.new.perform(group_1.id, group_2.id, group_3.id, namespaces.last.id + 1)
subject
end
it 'creates iterations_cadence records for the requested groups' do
......@@ -45,6 +47,12 @@ RSpec.describe Gitlab::BackgroundMigration::SetDefaultIterationCadences, schema:
expect(iteration_records.first.id).to eq(iteration_2.id)
expect(iteration_records.second.id).to eq(iteration_3.id)
end
it 'does not call Group class' do
expect(::Group).not_to receive(:where)
subject
end
end
context 'when an iteration cadence exists for a group' do
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
require_migration!
RSpec.describe ScheduleSetDefaultIterationCadences do
RSpec.describe RescheduleSetDefaultIterationCadences do
let(:namespaces) { table(:namespaces) }
let(:iterations) { table(:sprints) }
......
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