Skip to content
Snippets Groups Projects
Commit 9d4a7e85 authored by Reuben Pereira's avatar Reuben Pereira
Browse files

Cleanup background migration

Cleanup the background migration to copy container_registry_enabled
container_registry_access_level.
parent ecb0c75d
No related branches found
No related tags found
No related merge requests found
---
title: Cleanup background migration to copy container_registry_enabled to project_features
table
merge_request: 56626
author:
type: other
# frozen_string_literal: true
class CleanupMoveContainerRegistryEnabledToProjectFeature < ActiveRecord::Migration[6.0]
DOWNTIME = false
BATCH_SIZE = 10_000
disable_ddl_transaction!
class Project < ApplicationRecord
include EachBatch
self.table_name = "projects"
end
def up
Gitlab::BackgroundMigration.steal('MoveContainerRegistryEnabledToProjectFeature')
Project.each_batch(of: BATCH_SIZE) do |batch|
range = batch.pluck('MIN(id)', 'MAX(id)').first
result = ActiveRecord::Base.connection.execute(update_sql(*range))
ids = result.collect { |a| a["id"] }
if ids.empty?
logger.info(message: "#{self.class}: All project rows between #{range} already had their container_registry_enabled values copied to project_features")
else
logger.info(message: "#{self.class}: Project IDs with container_registry_enabled not copied by background migration were copied now: #{ids}")
end
end
end
def down
# no-op
end
private
def update_sql(from_id, to_id)
<<~SQL
with filtered_projects as (
select p.id, p.container_registry_enabled, p.val
from (
select id, container_registry_enabled, (CASE container_registry_enabled
WHEN true THEN 20
WHEN false THEN 0
ELSE 0 END) as val
from projects
where id between #{from_id} AND #{to_id}
) p join project_features pf
on pf.project_id = p.id
where pf.container_registry_access_level != p.val
)
update project_features
set container_registry_access_level = filtered_projects.val
from filtered_projects
where project_features.project_id = filtered_projects.id
returning filtered_projects.id
SQL
end
def logger
@logger ||= Gitlab::BackgroundMigration::Logger.build
end
end
5f528ee5fd393626772f6b9eb9069f08c4e5d7e832e206ac2888d56a1554ec6e
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20210313100214_cleanup_move_container_registry_enabled_to_project_feature.rb')
RSpec.describe CleanupMoveContainerRegistryEnabledToProjectFeature, :migration do
let(:namespace) { table(:namespaces).create!(name: 'gitlab', path: 'gitlab-org') }
let(:non_null_project_features) { { pages_access_level: 20 } }
let!(:project1) { table(:projects).create!(namespace_id: namespace.id, name: 'project 1', container_registry_enabled: true) }
let!(:project2) { table(:projects).create!(namespace_id: namespace.id, name: 'project 2', container_registry_enabled: false) }
let!(:project3) { table(:projects).create!(namespace_id: namespace.id, name: 'project 3', container_registry_enabled: nil) }
let!(:project4) { table(:projects).create!(namespace_id: namespace.id, name: 'project 4', container_registry_enabled: true) }
let!(:project5) { table(:projects).create!(namespace_id: namespace.id, name: 'project 5', container_registry_enabled: false) }
let!(:project6) { table(:projects).create!(namespace_id: namespace.id, name: 'project 6', container_registry_enabled: nil) }
let!(:project_feature1) { table(:project_features).create!(project_id: project1.id, container_registry_access_level: 20, **non_null_project_features) }
let!(:project_feature2) { table(:project_features).create!(project_id: project2.id, container_registry_access_level: 0, **non_null_project_features) }
let!(:project_feature3) { table(:project_features).create!(project_id: project3.id, container_registry_access_level: 0, **non_null_project_features) }
let!(:project_feature4) { table(:project_features).create!(project_id: project4.id, container_registry_access_level: 0, **non_null_project_features) }
let!(:project_feature5) { table(:project_features).create!(project_id: project5.id, container_registry_access_level: 20, **non_null_project_features) }
let!(:project_feature6) { table(:project_features).create!(project_id: project6.id, container_registry_access_level: 20, **non_null_project_features) }
before do
stub_const("#{described_class}::BATCH_SIZE", 3)
end
it 'steals remaining jobs and updates any remaining rows' do
expect(Gitlab::BackgroundMigration).to receive(:steal).with('MoveContainerRegistryEnabledToProjectFeature').and_call_original
expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |logger|
expect(logger).to receive(:info)
.with(message: "#{described_class}: All project rows between [#{project1.id}, #{project3.id}] already had their container_registry_enabled values copied to project_features")
expect(logger).to receive(:info)
.with(message: "#{described_class}: Project IDs with container_registry_enabled not copied by background migration were copied now: [#{project4.id}, #{project5.id}, #{project6.id}]")
end
migrate!
expect(project_feature1.reload.container_registry_access_level).to eq(20)
expect(project_feature2.reload.container_registry_access_level).to eq(0)
expect(project_feature3.reload.container_registry_access_level).to eq(0)
expect(project_feature4.reload.container_registry_access_level).to eq(20)
expect(project_feature5.reload.container_registry_access_level).to eq(0)
expect(project_feature6.reload.container_registry_access_level).to eq(0)
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment