Commit 77892b2a authored by Reuben Pereira's avatar Reuben Pereira Committed by Adam Hegyi

Cleanup container_registry_enabled background migration

Cleanup the background migration to copy container_registry_enabled
container_registry_access_level.

Changelog: other
parent 50022004
# frozen_string_literal: true
class CleanupMoveContainerRegistryEnabledToProjectFeature < ActiveRecord::Migration[6.0]
MIGRATION = 'MoveContainerRegistryEnabledToProjectFeature'
disable_ddl_transaction!
def up
Gitlab::BackgroundMigration.steal(MIGRATION)
bg_migration_job_class = define_background_migration_jobs_class
bg_migration_job_class.where(class_name: MIGRATION, status: bg_migration_job_class.statuses['pending']).each do |job|
Gitlab::BackgroundMigration::MoveContainerRegistryEnabledToProjectFeature.new.perform(*job.arguments)
end
bg_migration_job_class.where(class_name: MIGRATION).delete_all
end
def down
# no-op
end
private
def define_background_migration_jobs_class
Class.new(ActiveRecord::Base) do
self.table_name = 'background_migration_jobs'
self.inheritance_column = :_type_disabled
enum status: {
pending: 0,
succeeded: 1
}
end
end
end
3c4905fbe29227da7a2386f73d9df30e82da48efff24a1193ba3db0ac325cfcf
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20210513163904_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(:bg_class_name) { 'MoveContainerRegistryEnabledToProjectFeature' }
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) }
let!(:background_migration_job1) { table(:background_migration_jobs).create!(class_name: bg_class_name, arguments: [project4.id, project5.id], status: 0) }
let!(:background_migration_job2) { table(:background_migration_jobs).create!(class_name: bg_class_name, arguments: [project6.id, project6.id], status: 0) }
let!(:background_migration_job3) { table(:background_migration_jobs).create!(class_name: bg_class_name, arguments: [project1.id, project3.id], status: 1) }
it 'steals remaining jobs, updates any remaining rows and deletes background_migration_jobs rows' do
expect(Gitlab::BackgroundMigration).to receive(:steal).with(bg_class_name).and_call_original
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)
expect(table(:background_migration_jobs).where(class_name: bg_class_name).count).to eq(0)
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