Commit 99a8edf7 authored by Alessio Caiazza's avatar Alessio Caiazza

Remove background migration from 20200312134637

This migration is deleted because there is no foreign key for
deployments.environment_id and this caused a failure upgrading
deployments_merge_requests.environment_id

Details on the following issues:
 * https://gitlab.com/gitlab-org/gitlab/-/issues/217191
 * https://gitlab.com/gitlab-org/gitlab/-/issues/26229
parent 33b2c1fc
---
title: backfill environment_id on deployment_merge_requests
merge_request: 27219
author:
type: other
......@@ -4,46 +4,19 @@ class BackfillEnvironmentIdOnDeploymentMergeRequests < ActiveRecord::Migration[6
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
BATCH_SIZE = 400
DELAY = 1.minute
disable_ddl_transaction!
def up
max_mr_id = DeploymentMergeRequest
.select(:merge_request_id)
.distinct
.order(merge_request_id: :desc)
.limit(1)
.pluck(:merge_request_id)
.first || 0
last_mr_id = 0
step = 0
while last_mr_id < max_mr_id
stop =
DeploymentMergeRequest
.select(:merge_request_id)
.distinct
.where('merge_request_id > ?', last_mr_id)
.order(:merge_request_id)
.offset(BATCH_SIZE)
.limit(1)
.pluck(:merge_request_id)
.first
stop ||= max_mr_id
migrate_in(
step * DELAY,
'BackfillEnvironmentIdDeploymentMergeRequests',
[last_mr_id + 1, stop]
)
# no-op
last_mr_id = stop
step += 1
end
# this migration is deleted because there is no foreign key for
# deployments.environment_id and this caused a failure upgrading
# deployments_merge_requests.environment_id
#
# Details on the following issues:
# * https://gitlab.com/gitlab-org/gitlab/-/issues/217191
# * https://gitlab.com/gitlab-org/gitlab/-/issues/26229
end
def down
......
......@@ -5,7 +5,14 @@ module Gitlab
# BackfillEnvironmentIdDeploymentMergeRequests deletes duplicates
# from deployment_merge_requests table and backfills environment_id
class BackfillEnvironmentIdDeploymentMergeRequests
def perform(start_mr_id, stop_mr_id)
def perform(_start_mr_id, _stop_mr_id)
# no-op
# Background migration removed due to
# https://gitlab.com/gitlab-org/gitlab/-/issues/217191
end
def backfill_range(start_mr_id, stop_mr_id)
start_mr_id = Integer(start_mr_id)
stop_mr_id = Integer(stop_mr_id)
......
......@@ -32,7 +32,7 @@ describe Gitlab::BackgroundMigration::BackfillEnvironmentIdDeploymentMergeReques
expect(deployment_merge_requests.where(environment_id: nil).count).to eq(3)
migration.perform(1, mr.id)
migration.backfill_range(1, mr.id)
expect(deployment_merge_requests.where(environment_id: nil).count).to be_zero
expect(deployment_merge_requests.count).to eq(2)
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200312134637_backfill_environment_id_on_deployment_merge_requests.rb')
describe BackfillEnvironmentIdOnDeploymentMergeRequests do
let(:environments) { table(:environments) }
let(:merge_requests) { table(:merge_requests) }
let(:deployments) { table(:deployments) }
let(:deployment_merge_requests) { table(:deployment_merge_requests) }
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:migration_worker) { double('BackgroundMigrationWorker') }
before do
stub_const('BackgroundMigrationWorker', migration_worker)
end
it 'schedules nothing when there are no entries' do
expect(migration_worker).not_to receive(:perform_in)
migrate!
end
it 'batches the workload' do
stub_const("#{described_class.name}::BATCH_SIZE", 10)
namespace = namespaces.create!(name: 'foo', path: 'foo')
project = projects.create!(namespace_id: namespace.id)
environment = environments.create!(project_id: project.id, name: 'staging', slug: 'staging')
# Batching is based on DeploymentMergeRequest.merge_request_id, in order to test it
# we must generate more than described_class::BATCH_SIZE merge requests, deployments,
# and deployment_merge_requests entries
entries = 13
expect(entries).to be > described_class::BATCH_SIZE
# merge requests and deployments bulk generation
mrs_params = []
deployments_params = []
entries.times do |i|
mrs_params << { source_branch: 'x', target_branch: 'master', target_project_id: project.id }
deployments_params << { environment_id: environment.id, iid: i + 1, project_id: project.id, ref: 'master', tag: false, sha: '123abcdef', status: 1 }
end
all_mrs = merge_requests.insert_all(mrs_params)
all_deployments = deployments.insert_all(deployments_params)
# deployment_merge_requests bulk generation
dmr_params = []
entries.times do |index|
mr_id = all_mrs.rows[index].first
deployment_id = all_deployments.rows[index].first
dmr_params << { deployment_id: deployment_id, merge_request_id: mr_id }
end
deployment_merge_requests.insert_all(dmr_params)
first_batch_limit = dmr_params[described_class::BATCH_SIZE][:merge_request_id]
second_batch_limit = dmr_params.last[:merge_request_id]
expect(migration_worker).to receive(:perform_in)
.with(
0,
'BackfillEnvironmentIdDeploymentMergeRequests',
[1, first_batch_limit]
)
expect(migration_worker).to receive(:perform_in)
.with(
described_class::DELAY,
'BackfillEnvironmentIdDeploymentMergeRequests',
[first_batch_limit + 1, second_batch_limit]
)
migrate!
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