Commit f9a90786 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch '351282-cleanup-saas-incorrect-max_seats_used-batch-2' into 'master'

Clean up wrong max_seats_used batch two

See merge request gitlab-org/gitlab!79705
parents f25fd8d0 5691252a
# frozen_string_literal: true
class ScheduleFixIncorrectMaxSeatsUsed2 < Gitlab::Database::Migration[1.0]
MIGRATION = 'FixIncorrectMaxSeatsUsed'
TMP_IDX_NAME = 'tmp_gitlab_subscriptions_max_seats_used_migration_2'
disable_ddl_transaction!
def up
add_concurrent_index :gitlab_subscriptions, :id, where: "start_date < '2021-08-02' AND max_seats_used != 0 AND max_seats_used > seats_in_use AND max_seats_used > seats", name: TMP_IDX_NAME
return unless Gitlab.com?
migrate_in(1.hour, MIGRATION, ['batch_2_for_start_date_before_02_aug_2021'])
end
def down
remove_concurrent_index_by_name :gitlab_subscriptions, TMP_IDX_NAME
end
end
c075ee9d6efeae4b7a9b6e310f0c3d0bdd0ac6a58dc214427d4de9ae579db50d
\ No newline at end of file
......@@ -28355,6 +28355,8 @@ CREATE UNIQUE INDEX term_agreements_unique_index ON term_agreements USING btree
CREATE INDEX tmp_gitlab_subscriptions_max_seats_used_migration ON gitlab_subscriptions USING btree (id) WHERE ((start_date >= '2021-08-02'::date) AND (start_date <= '2021-11-20'::date) AND (max_seats_used <> 0) AND (max_seats_used > seats_in_use) AND (max_seats_used > seats));
CREATE INDEX tmp_gitlab_subscriptions_max_seats_used_migration_2 ON gitlab_subscriptions USING btree (id) WHERE ((start_date < '2021-08-02'::date) AND (max_seats_used <> 0) AND (max_seats_used > seats_in_use) AND (max_seats_used > seats));
CREATE INDEX tmp_idx_vulnerability_occurrences_on_id_where_report_type_7_99 ON vulnerability_occurrences USING btree (id) WHERE (report_type = ANY (ARRAY[7, 99]));
CREATE INDEX tmp_index_container_repositories_on_id_migration_state ON container_repositories USING btree (id, migration_state);
......@@ -97,8 +97,14 @@ module EE
BATCH_SIZE = 200
def perform
eligible_gitlab_subscriptions.find_each(batch_size: BATCH_SIZE) do |gs|
def perform(batch = nil)
gitlab_subscriptions = if batch == 'batch_2_for_start_date_before_02_aug_2021'
eligible_gitlab_subscriptions_batch_2
else
eligible_gitlab_subscriptions
end
gitlab_subscriptions.find_each(batch_size: BATCH_SIZE) do |gs|
gs_histories = gs.gitlab_subscription_histories.to_a.sort_by { |gh| gh.id }
gs_histories << gs
......@@ -170,10 +176,10 @@ module EE
end
def eligible_gitlab_subscriptions
# Only search subscriptions with `start_date` in range `['2-Aug-2021', '20-Nov-2021']` because:
# - for subscriptions with `start_date < '2-Aug-2021'`, we do not enable QSR(Quarterly Subscription Reconciliation)
# - for subscriptions with `start_date > '20-Nov-2021'`, they should not have such issue
# because the MR https://gitlab.com/gitlab-org/gitlab/-/merge_requests/73078 was merged on `09-Nov-2021`.
# Only search subscriptions with `start_date` in range `['2021-08-02', '2021-11-20']` because:
# - for subscriptions with `start_date < '2021-08-02'`, we do not enable QSR(Quarterly Subscription Reconciliation)
# - for subscriptions with `start_date > '2021-11-20'`, they should not have such issue
# because the MR https://gitlab.com/gitlab-org/gitlab/-/merge_requests/73078 was merged on `2021-11-09`.
# All rails nodes should have deployed new merged code within 10 days.
#
# Only need to check if max_seats_used is not 0
......@@ -181,8 +187,21 @@ module EE
# Only need to check if max_seats_used > seats (zuora subscription quantity)
GitlabSubscription.preload(:namespace, :hosted_plan, :gitlab_subscription_histories)
.where('gitlab_subscriptions.start_date >= ?', Date.parse('2-Aug-2021'))
.where('gitlab_subscriptions.start_date <= ?', Date.parse('20-Nov-2021'))
.where('gitlab_subscriptions.start_date >= ?', Date.parse('2021-08-02'))
.where('gitlab_subscriptions.start_date <= ?', Date.parse('2021-11-20'))
.where.not(max_seats_used: 0)
.where('gitlab_subscriptions.max_seats_used > gitlab_subscriptions.seats_in_use')
.where('gitlab_subscriptions.max_seats_used > gitlab_subscriptions.seats')
end
def eligible_gitlab_subscriptions_batch_2
# Only search subscriptions with `start_date < '2021-08-02'`
# Only need to check if max_seats_used is not 0
# Only need to check if max_seats_used > seats_in_use
# Only need to check if max_seats_used > seats (zuora subscription quantity)
GitlabSubscription.preload(:namespace, :hosted_plan, :gitlab_subscription_histories)
.where('gitlab_subscriptions.start_date < ?', Date.parse('2021-08-02'))
.where.not(max_seats_used: 0)
.where('gitlab_subscriptions.max_seats_used > gitlab_subscriptions.seats_in_use')
.where('gitlab_subscriptions.max_seats_used > gitlab_subscriptions.seats')
......
......@@ -14,7 +14,7 @@ RSpec.describe Gitlab::BackgroundMigration::FixIncorrectMaxSeatsUsed, :saas do
let(:max_seats_used) { 10 }
let(:seats_owed) { [0, max_seats_used - seats].max }
let(:start_date) { Date.parse('10-Nov-2021') }
let(:start_date) { Date.parse('2021-11-10') }
let(:end_date) { start_date + 1.year }
let(:keep_old_seats_attributes_after_renew) { true }
......@@ -91,30 +91,58 @@ RSpec.describe Gitlab::BackgroundMigration::FixIncorrectMaxSeatsUsed, :saas do
include_examples 'does not reset max_seats_used and seats_owed'
end
context 'when start_date is before 2-Aug-2021' do
let(:start_date) { Date.parse('1-Aug-2021') }
context 'when start_date is before 2021-08-02' do
let(:start_date) { Date.parse('2021-08-01') }
include_examples 'does not reset max_seats_used and seats_owed'
end
context 'when start_date is 2-Aug-2021' do
let(:start_date) { Date.parse('2-Aug-2021') }
context 'when start_date is 2021-08-02' do
let(:start_date) { Date.parse('2021-08-02') }
include_examples 'resets max_seats_used and seats_owed'
end
context 'when start_date is 20-Nov-2021' do
let(:start_date) { Date.parse('20-Nov-2021') }
context 'when start_date is 2021-11-20' do
let(:start_date) { Date.parse('2021-11-20') }
include_examples 'resets max_seats_used and seats_owed'
end
context 'when start_date is after 20-Nov-2021' do
let(:start_date) { Date.parse('21-Nov-2021') }
context 'when start_date is after 2021-11-20' do
let(:start_date) { Date.parse('2021-11-21') }
include_examples 'does not reset max_seats_used and seats_owed'
end
context 'when batch_2_for_start_date_before_02_aug_2021' do
def perform_and_reload
migration.perform(batch)
gitlab_subscription.reload
end
let(:batch) { 'batch_2_for_start_date_before_02_aug_2021' }
context 'when start_date is before 2021-08-02' do
let(:start_date) { Date.parse('2021-08-01') }
include_examples 'resets max_seats_used and seats_owed'
end
context 'when start_date is 2021-08-02' do
let(:start_date) { Date.parse('2021-08-02') }
include_examples 'does not reset max_seats_used and seats_owed'
end
context 'when start_date is after 2021-08-02' do
let(:start_date) { Date.parse('2021-08-03') }
include_examples 'does not reset max_seats_used and seats_owed'
end
end
context 'when max_seats_used is 0' do
let(:max_seats_used) { 0 }
......
......@@ -4,7 +4,7 @@ module Gitlab
module BackgroundMigration
# rubocop: disable Style/Documentation
class FixIncorrectMaxSeatsUsed
def perform
def perform(batch = nil)
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe ScheduleFixIncorrectMaxSeatsUsed2, :migration do
let(:migration_name) { described_class::MIGRATION.to_s.demodulize }
describe '#up' do
it 'schedules a job on Gitlab.com' do
allow(Gitlab).to receive(:com?).and_return(true)
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(migration_name).to be_scheduled_delayed_migration(1.hour, 'batch_2_for_start_date_before_02_aug_2021')
expect(BackgroundMigrationWorker.jobs.size).to eq(1)
end
end
end
it 'does not schedule any jobs when not Gitlab.com' do
allow(Gitlab).to receive(:com?).and_return(false)
Sidekiq::Testing.fake! do
migrate!
expect(migration_name).not_to be_scheduled_delayed_migration
expect(BackgroundMigrationWorker.jobs.size).to eq(0)
end
end
end
end
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