Skip to content
代码片段 群组 项目
提交 f65aca83 编辑于 作者: Nicolas Dular's avatar Nicolas Dular
浏览文件

Backfill internal column on notes

As we rename the column from `confidential` to `internal` and already
sync new notes, we now also migrate all the existing note `confidential`
data to `internal`.

Changelog: changed
上级 1004aa2b
No related branches found
No related tags found
无相关合并请求
# frozen_string_literal: true
class BackfillInternalOnNotes < Gitlab::Database::Migration[2.0]
MIGRATION = 'BackfillInternalOnNotes'
DELAY_INTERVAL = 2.minutes
TABLE = :notes
BATCH_SIZE = 2000
SUB_BATCH_SIZE = 10
restrict_gitlab_migration gitlab_schema: :gitlab_main
def up
queue_batched_background_migration(
MIGRATION,
TABLE,
:id,
job_interval: DELAY_INTERVAL,
batch_size: BATCH_SIZE,
sub_batch_size: SUB_BATCH_SIZE
)
end
def down
delete_batched_background_migration(MIGRATION, TABLE, :id, [])
end
end
4a975867dc0539049902229521b4d94f940817ffd9196810856c8eb962c57e62
\ No newline at end of file
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# This syncs the data to `internal` from `confidential` as we rename the column.
class BackfillInternalOnNotes < BatchedMigrationJob
scope_to -> (relation) { relation.where(confidential: true) }
def perform
each_sub_batch(operation_name: :update_all) do |sub_batch|
sub_batch.update_all(internal: true)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillInternalOnNotes, :migration, schema: 20220920124709 do
let(:notes_table) { table(:notes) }
let!(:confidential_note) { notes_table.create!(id: 1, confidential: true, internal: false) }
let!(:non_confidential_note) { notes_table.create!(id: 2, confidential: false, internal: false) }
describe '#perform' do
subject(:perform) do
described_class.new(
start_id: 1,
end_id: 2,
batch_table: :notes,
batch_column: :id,
sub_batch_size: 1,
pause_ms: 0,
connection: ApplicationRecord.connection
).perform
end
it 'backfills internal column on notes when confidential' do
expect { perform }
.to change { confidential_note.reload.internal }.from(false).to(true)
.and not_change { non_confidential_note.reload.internal }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe BackfillInternalOnNotes, :migration do
let(:migration) { described_class::MIGRATION }
describe '#up' do
it 'schedules background jobs for each batch of issues' do
migrate!
expect(migration).to have_scheduled_batched_migration(
table_name: :notes,
column_name: :id,
interval: described_class::DELAY_INTERVAL,
batch_size: described_class::BATCH_SIZE,
sub_batch_size: described_class::SUB_BATCH_SIZE
)
end
end
describe '#down' do
it 'deletes all batched migration records' do
migrate!
schema_migrate_down!
expect(migration).not_to have_scheduled_batched_migration
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册