Commit 12d0a64d authored by Michael Kozono's avatar Michael Kozono

Extract work into background migration

parent 4b3d8f38
......@@ -7,9 +7,6 @@ class UpdateAuthorizedKeysFile < ActiveRecord::Migration
class ApplicationSetting < ActiveRecord::Base
self.table_name = 'application_settings'
end
class Key < ActiveRecord::Base
self.table_name = 'keys'
end
# Set this constant to true if this migration requires downtime.
DOWNTIME = false
......@@ -97,19 +94,7 @@ class UpdateAuthorizedKeysFile < ActiveRecord::Migration
end
def update_authorized_keys_file_since(cutoff_datetime)
add_keys_since(cutoff_datetime)
remove_keys_not_found_in_db
end
def add_keys_since(cutoff_datetime)
start_key = Key.select(:id).where("created_at >= ?", cutoff_datetime).take
if start_key
GitlabShellWorker.perform_async(:batch_add_keys_in_db_starting_from, start_key.id)
end
end
def remove_keys_not_found_in_db
GitlabShellWorker.perform_async(:remove_keys_not_found_in_db)
job = ['UpdateAuthorizedKeysFileSince', [cutoff_datetime]]
BackgroundMigrationWorker.perform_bulk(job)
end
end
module Gitlab
module BackgroundMigration
class UpdateAuthorizedKeysFileSince
class Key < ActiveRecord::Base
self.table_name = 'keys'
end
def perform(cutoff_datetime)
add_keys_since(cutoff_datetime)
remove_keys_not_found_in_db
end
def add_keys_since(cutoff_datetime)
start_key = Key.select(:id).where("created_at >= ?", cutoff_datetime).take
if start_key
GitlabShellWorker.perform_async(:batch_add_keys_in_db_starting_from, start_key.id)
end
end
def remove_keys_not_found_in_db
GitlabShellWorker.perform_async(:remove_keys_not_found_in_db)
end
end
end
end
require 'spec_helper'
describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do
describe '#perform' do
let!(:cutoff_datetime) { DateTime.now }
subject { described_class.new.perform(cutoff_datetime) }
context 'when an SSH key was created after the cutoff datetime' do
before do
Timecop.freeze
end
after do
Timecop.return
end
before do
Timecop.travel 1.day.from_now
@key = create(:key)
end
it 'queues a batch_add_keys_from call to GitlabShellWorker, including the start key ID' do
expect(GitlabShellWorker).to receive(:perform_async).with(:batch_add_keys_in_db_starting_from, @key.id)
allow(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db)
subject
end
end
it 'queues a remove_keys_not_found_in_db call to GitlabShellWorker' do
expect(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db)
subject
end
end
describe '#add_keys_since' do
let!(:cutoff_datetime) { DateTime.now }
subject { described_class.new.add_keys_since(cutoff_datetime) }
before do
Timecop.freeze
end
after do
Timecop.return
end
context 'when an SSH key was created after the cutoff datetime' do
before do
Timecop.travel 1.day.from_now
@key = create(:key)
end
it 'queues a batch_add_keys_from call to GitlabShellWorker, including the start key ID' do
expect(GitlabShellWorker).to receive(:perform_async).with(:batch_add_keys_in_db_starting_from, @key.id)
subject
end
end
context 'when an SSH key was not created after the cutoff datetime' do
it 'does not use GitlabShellWorker' do
expect(GitlabShellWorker).not_to receive(:perform_async)
subject
end
end
end
describe '#remove_keys_not_found_in_db' do
it 'queues a rm_keys_not_in_db call to GitlabShellWorker' do
expect(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db)
described_class.new.remove_keys_not_found_in_db
end
end
end
......@@ -159,74 +159,4 @@ describe UpdateAuthorizedKeysFile do
end
end
end
describe '#update_authorized_keys_file_since' do
let!(:cutoff_datetime) { DateTime.now }
subject { migration.update_authorized_keys_file_since(cutoff_datetime) }
context 'when an SSH key was created after the cutoff datetime' do
before do
Timecop.freeze
end
after do
Timecop.return
end
before do
Timecop.travel 1.day.from_now
@key = create(:key)
end
it 'queues a batch_add_keys_from call to GitlabShellWorker, including the start key ID' do
expect(GitlabShellWorker).to receive(:perform_async).with(:batch_add_keys_in_db_starting_from, @key.id)
allow(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db)
subject
end
end
it 'queues a remove_keys_not_found_in_db call to GitlabShellWorker' do
expect(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db)
subject
end
end
describe '#add_keys_since' do
let!(:cutoff_datetime) { DateTime.now }
subject { migration.add_keys_since(cutoff_datetime) }
before do
Timecop.freeze
end
after do
Timecop.return
end
context 'when an SSH key was created after the cutoff datetime' do
before do
Timecop.travel 1.day.from_now
@key = create(:key)
end
it 'queues a batch_add_keys_from call to GitlabShellWorker, including the start key ID' do
expect(GitlabShellWorker).to receive(:perform_async).with(:batch_add_keys_in_db_starting_from, @key.id)
subject
end
end
context 'when an SSH key was not created after the cutoff datetime' do
it 'does not use GitlabShellWorker' do
expect(GitlabShellWorker).not_to receive(:perform_async)
subject
end
end
end
describe '#remove_keys_not_found_in_db' do
it 'queues a rm_keys_not_in_db call to GitlabShellWorker' do
expect(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db)
migration.remove_keys_not_found_in_db
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