Commit 843f965c authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '290008-fix-background-migration-arguments' into 'master'

Fix argument type for background migration

See merge request gitlab-org/gitlab!51475
parents 8366133d 23edbcb2
---
title: Fix argument type for background migration
merge_request: 51475
author:
type: fixed
# frozen_string_literal: true # frozen_string_literal: true
class RemoveDuplicateServices < ActiveRecord::Migration[6.0] class RemoveDuplicateServices < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false DOWNTIME = false
INTERVAL = 2.minutes
BATCH_SIZE = 5_000
MIGRATION = 'RemoveDuplicateServices'
disable_ddl_transaction! disable_ddl_transaction!
def up def up
project_ids_with_duplicates = Gitlab::BackgroundMigration::RemoveDuplicateServices::Service.project_ids_with_duplicates # noop, replaced by 20210112143418_remove_duplicate_services.rb
project_ids_with_duplicates.each_batch(of: BATCH_SIZE, column: :project_id) do |batch, index|
migrate_in(
INTERVAL * index,
MIGRATION,
batch.pluck(:project_id)
)
end
end end
def down def down
......
# frozen_string_literal: true
# This replaces the previous post-deployment migration 20201207165956_remove_duplicate_services_spec.rb,
# we have to run this again due to a bug in how we were receiving the arguments in the background migration.
class RemoveDuplicateServices2 < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INTERVAL = 2.minutes
BATCH_SIZE = 5_000
MIGRATION = 'RemoveDuplicateServices'
disable_ddl_transaction!
def up
project_ids_with_duplicates = Gitlab::BackgroundMigration::RemoveDuplicateServices::Service.project_ids_with_duplicates
project_ids_with_duplicates.each_batch(of: BATCH_SIZE, column: :project_id) do |batch, index|
migrate_in(
INTERVAL * index,
MIGRATION,
batch.pluck(:project_id)
)
end
end
def down
end
end
05d45e25ab9ef1565c04ca6515e0b01f2f98c5e98b1eeb09fa9dd43ebbe3c4d0
\ No newline at end of file
...@@ -29,7 +29,7 @@ module Gitlab ...@@ -29,7 +29,7 @@ module Gitlab
end end
end end
def perform(project_ids) def perform(*project_ids)
types_with_duplicates = Service.types_with_duplicates(project_ids).pluck(:project_id, :type) types_with_duplicates = Service.types_with_duplicates(project_ids).pluck(:project_id, :type)
types_with_duplicates.each do |project_id, type| types_with_duplicates.each do |project_id, type|
......
...@@ -82,7 +82,7 @@ RSpec.describe Gitlab::BackgroundMigration::RemoveDuplicateServices, :migration, ...@@ -82,7 +82,7 @@ RSpec.describe Gitlab::BackgroundMigration::RemoveDuplicateServices, :migration,
end end
expect do expect do
subject.perform([project2.id, project3.id]) subject.perform(project2.id, project3.id)
end.to change { services.count }.from(21).to(12) end.to change { services.count }.from(21).to(12)
services1 = services.where(project_id: project1.id) services1 = services.where(project_id: project1.id)
...@@ -109,13 +109,13 @@ RSpec.describe Gitlab::BackgroundMigration::RemoveDuplicateServices, :migration, ...@@ -109,13 +109,13 @@ RSpec.describe Gitlab::BackgroundMigration::RemoveDuplicateServices, :migration,
it 'does not delete services without duplicates' do it 'does not delete services without duplicates' do
expect do expect do
subject.perform([project1.id, project4.id]) subject.perform(project1.id, project4.id)
end.not_to change { services.count } end.not_to change { services.count }
end end
it 'only deletes duplicate services for the current batch' do it 'only deletes duplicate services for the current batch' do
expect do expect do
subject.perform([project2.id]) subject.perform(project2.id)
end.to change { services.count }.by(-3) end.to change { services.count }.by(-3)
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
require 'spec_helper' require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20201207165956_remove_duplicate_services.rb') require Rails.root.join('db', 'post_migrate', '20210112143418_remove_duplicate_services2.rb')
RSpec.describe RemoveDuplicateServices do RSpec.describe RemoveDuplicateServices2 do
let_it_be(:namespaces) { table(:namespaces) } let_it_be(:namespaces) { table(:namespaces) }
let_it_be(:projects) { table(:projects) } let_it_be(:projects) { table(:projects) }
let_it_be(:services) { table(:services) } let_it_be(:services) { table(:services) }
......
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