Commit 29a04635 authored by Alper Akgun's avatar Alper Akgun

Merge branch...

Merge branch '330276-add-data-migration-to-flip-draft-status-column-false-true-for-all-open-wip-draft-mrs' into 'master'

Add data migration to flip draft status column false->true for all open wip/draft MRs

See merge request gitlab-org/gitlab!62627
parents d202326e 590f680b
# frozen_string_literal: true
class BackfillDraftStatusOnMergeRequests < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = "tmp_index_merge_requests_draft_and_status"
disable_ddl_transaction!
def up
add_concurrent_index :merge_requests, :id,
where: "draft = false AND state_id = 1 AND ((title)::text ~* '^\\[draft\\]|\\(draft\\)|draft:|draft|\\[WIP\\]|WIP:|WIP'::text)",
name: INDEX_NAME
update_column_in_batches(:merge_requests, :draft, true, batch_size: 100) do |table, query|
query
.where(table[:state_id].eq(1))
.where(table[:draft].eq(false))
.where(table[:title].matches_regexp('^\\[draft\\]|\\(draft\\)|draft:|draft|\\[WIP\\]|WIP:|WIP', false))
end
remove_concurrent_index_by_name :merge_requests, INDEX_NAME
end
def down
remove_concurrent_index_by_name :merge_requests, INDEX_NAME
end
end
f80787d85538cedaba34cb204c98df2d0bbbf85f438d4df8f1187d2f4d881588
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20210526222715_backfill_draft_status_on_merge_requests.rb')
RSpec.describe BackfillDraftStatusOnMergeRequests, :migration do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:merge_requests) { table(:merge_requests) }
let(:group) { namespaces.create!(name: 'gitlab', path: 'gitlab') }
let(:project) { projects.create!(namespace_id: group.id) }
let(:draft_prefixes) { ["[Draft]", "(Draft)", "Draft:", "Draft", "[WIP]", "WIP:", "WIP"] }
def create_merge_request(params)
common_params = {
target_project_id: project.id,
target_branch: 'feature1',
source_branch: 'master'
}
merge_requests.create!(common_params.merge(params))
end
context "for MRs with #draft? == true titles but draft attribute false" do
before do
draft_prefixes.each do |prefix|
(1..4).each do |n|
merge_request = create_merge_request(title: "#{prefix} This is a title", state_id: n)
merge_request.update_columns(draft: false)
end
end
end
it "updates all open draft merge request's draft field to true" do
mr_count = merge_requests.all.count
expect { disable_migrations_output { migrate! } }
.to change { MergeRequest.where(draft: false).count }
.from(mr_count).to(mr_count - draft_prefixes.length)
end
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