Commit 11c6cdd0 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '288715-populate-and-migration-issue_email_participants' into 'master'

Populate and migrate issue_email_participants

See merge request gitlab-org/gitlab!48711
parents 897d7235 449dc6c4
---
title: Populate and migrate issue_email_participants
merge_request: 48711
author: Lee Tickett @leetickett
type: added
# frozen_string_literal: true
class AddServiceDeskReplyToIsNotNullIndexOnIssues < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'idx_on_issues_where_service_desk_reply_to_is_not_null'
disable_ddl_transaction!
def up
add_concurrent_index(:issues, [:id], name: INDEX_NAME, where: 'service_desk_reply_to IS NOT NULL')
end
def down
remove_concurrent_index_by_name(:issues, INDEX_NAME)
end
end
# frozen_string_literal: true
class SchedulePopulateIssueEmailParticipants < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
BATCH_SIZE = 1_000
DELAY_INTERVAL = 2.minutes
MIGRATION = 'PopulateIssueEmailParticipants'
disable_ddl_transaction!
class Issue < ActiveRecord::Base
include EachBatch
self.table_name = 'issues'
end
def up
queue_background_migration_jobs_by_range_at_intervals(
Issue.where.not(service_desk_reply_to: nil),
MIGRATION,
DELAY_INTERVAL,
batch_size: BATCH_SIZE
)
end
def down
# no-op
end
end
7c2f83a5821765f3657a6dd6f69bc8711a3b9388aec7335293748b883f7ab872
\ No newline at end of file
06dcd1a1f4bc0598357d48d9a0e838d9af1cf078234f2aabaa53ff9df01d2c17
\ No newline at end of file
...@@ -21257,6 +21257,8 @@ CREATE INDEX idx_mr_cc_diff_files_on_mr_cc_id_and_sha ON merge_request_context_c ...@@ -21257,6 +21257,8 @@ CREATE INDEX idx_mr_cc_diff_files_on_mr_cc_id_and_sha ON merge_request_context_c
CREATE UNIQUE INDEX idx_on_compliance_management_frameworks_namespace_id_name ON compliance_management_frameworks USING btree (namespace_id, name); CREATE UNIQUE INDEX idx_on_compliance_management_frameworks_namespace_id_name ON compliance_management_frameworks USING btree (namespace_id, name);
CREATE INDEX idx_on_issues_where_service_desk_reply_to_is_not_null ON issues USING btree (id) WHERE (service_desk_reply_to IS NOT NULL);
CREATE INDEX idx_packages_build_infos_on_package_id ON packages_build_infos USING btree (package_id); CREATE INDEX idx_packages_build_infos_on_package_id ON packages_build_infos USING btree (package_id);
CREATE INDEX idx_packages_debian_group_component_files_on_architecture_id ON packages_debian_group_component_files USING btree (architecture_id); CREATE INDEX idx_packages_debian_group_component_files_on_architecture_id ON packages_debian_group_component_files USING btree (architecture_id);
......
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Class to migrate service_desk_reply_to email addresses to issue_email_participants
class PopulateIssueEmailParticipants
# rubocop:disable Style/Documentation
class TmpIssue < ActiveRecord::Base
self.table_name = 'issues'
end
def perform(start_id, stop_id)
issues = TmpIssue.select(:id, :service_desk_reply_to, :created_at).where(id: (start_id..stop_id)).where.not(service_desk_reply_to: nil)
rows = issues.map do |issue|
{
issue_id: issue.id,
email: issue.service_desk_reply_to,
created_at: issue.created_at,
updated_at: issue.created_at
}
end
Gitlab::Database.bulk_insert(:issue_email_participants, rows, on_conflict: :do_nothing) # rubocop:disable Gitlab/BulkInsert
end
end
end
end
...@@ -35,7 +35,11 @@ module Gitlab ...@@ -35,7 +35,11 @@ module Gitlab
raise ProjectNotFound if project.nil? raise ProjectNotFound if project.nil?
create_issue! create_issue!
send_thank_you_email! if from_address
if from_address
add_email_participant
send_thank_you_email!
end
end end
def metrics_params def metrics_params
...@@ -146,6 +150,10 @@ module Gitlab ...@@ -146,6 +150,10 @@ module Gitlab
def author def author
User.support_bot User.support_bot
end end
def add_email_participant
@issue.issue_email_participants.create(email: from_address)
end
end end
end end
end end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::PopulateIssueEmailParticipants, schema: 20201128210234 do
let!(:namespace) { table(:namespaces).create!(name: 'namespace', path: 'namespace') }
let!(:project) { table(:projects).create!(id: 1, namespace_id: namespace.id) }
let!(:issue1) { table(:issues).create!(id: 1, project_id: project.id, service_desk_reply_to: "a@gitlab.com") }
let!(:issue2) { table(:issues).create!(id: 2, project_id: project.id, service_desk_reply_to: "b@gitlab.com") }
let(:issue_email_participants) { table(:issue_email_participants) }
describe '#perform' do
it 'migrates email addresses from service desk issues', :aggregate_failures do
expect { subject.perform(1, 2) }.to change { issue_email_participants.count }.by(2)
expect(issue_email_participants.find_by(issue_id: 1).email).to eq("a@gitlab.com")
expect(issue_email_participants.find_by(issue_id: 2).email).to eq("b@gitlab.com")
end
end
end
...@@ -40,6 +40,13 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler do ...@@ -40,6 +40,13 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler do
expect(new_issue.description).to eq(expected_description.strip) expect(new_issue.description).to eq(expected_description.strip)
end end
it 'creates an issue_email_participant' do
receiver.execute
new_issue = Issue.last
expect(new_issue.issue_email_participants.first.email).to eq("jake@adventuretime.ooo")
end
it 'sends thank you email' do it 'sends thank you email' do
expect { receiver.execute }.to have_enqueued_job.on_queue('mailers') expect { receiver.execute }.to have_enqueued_job.on_queue('mailers')
end end
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20201128210234_schedule_populate_issue_email_participants.rb')
RSpec.describe SchedulePopulateIssueEmailParticipants do
let!(:namespace) { table(:namespaces).create!(name: 'namespace', path: 'namespace') }
let!(:project) { table(:projects).create!(id: 1, namespace_id: namespace.id) }
let!(:issue1) { table(:issues).create!(id: 1, project_id: project.id, service_desk_reply_to: "a@gitlab.com") }
let!(:issue2) { table(:issues).create!(id: 2, project_id: project.id) }
let!(:issue3) { table(:issues).create!(id: 3, project_id: project.id, service_desk_reply_to: "b@gitlab.com") }
let!(:issue4) { table(:issues).create!(id: 4, project_id: project.id, service_desk_reply_to: "c@gitlab.com") }
let!(:issue5) { table(:issues).create!(id: 5, project_id: project.id, service_desk_reply_to: "d@gitlab.com") }
let(:issue_email_participants) { table(:issue_email_participants) }
it 'correctly schedules background migrations' do
stub_const("#{described_class.name}::BATCH_SIZE", 2)
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(2.minutes, 1, 3)
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(4.minutes, 4, 5)
expect(BackgroundMigrationWorker.jobs.size).to eq(2)
end
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