Commit ebcdf105 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch '290763-simplify-execute-migration' into 'master'

Simplify the execute_migration method

See merge request gitlab-org/gitlab!53824
parents 93f4c907 acc6b3a1
...@@ -27,7 +27,7 @@ module Elastic ...@@ -27,7 +27,7 @@ module Elastic
client.index index: index_name, type: '_doc', id: version, body: { completed: completed, state: load_state.merge(state) } client.index index: index_name, type: '_doc', id: version, body: { completed: completed, state: load_state.merge(state) }
end end
def persisted? def started?
load_from_index.present? load_from_index.present?
end end
...@@ -49,7 +49,7 @@ module Elastic ...@@ -49,7 +49,7 @@ module Elastic
name.underscore name.underscore
end end
def self.persisted_versions(completed:) def self.load_versions(completed:)
helper = Gitlab::Elastic::Helper.default helper = Gitlab::Elastic::Helper.default
helper.client helper.client
.search(index: helper.migrations_index_name, body: { query: { term: { completed: completed } } }) .search(index: helper.migrations_index_name, body: { query: { term: { completed: completed } } })
......
...@@ -51,9 +51,12 @@ module Elastic ...@@ -51,9 +51,12 @@ module Elastic
private private
def execute_migration(migration) def execute_migration(migration)
if migration.persisted? && !migration.batched? if migration.started? && !migration.batched?
logger.info "MigrationWorker: migration[#{migration.name}] did not execute migrate method since it was already executed. Waiting for migration to complete" logger.info "MigrationWorker: migration[#{migration.name}] did not execute migrate method since it was already executed. Waiting for migration to complete"
else
return
end
pause_indexing!(migration) pause_indexing!(migration)
logger.info "MigrationWorker: migration[#{migration.name}] executing migrate method" logger.info "MigrationWorker: migration[#{migration.name}] executing migrate method"
...@@ -64,10 +67,9 @@ module Elastic ...@@ -64,10 +67,9 @@ module Elastic
Elastic::MigrationWorker.perform_in(migration.throttle_delay) Elastic::MigrationWorker.perform_in(migration.throttle_delay)
end end
end end
end
def current_migration def current_migration
completed_migrations = Elastic::MigrationRecord.persisted_versions(completed: true) completed_migrations = Elastic::MigrationRecord.load_versions(completed: true)
Elastic::DataMigrationService.migrations.find { |migration| !completed_migrations.include?(migration.version) } Elastic::DataMigrationService.migrations.find { |migration| !completed_migrations.include?(migration.version) }
end end
......
...@@ -41,13 +41,13 @@ RSpec.describe Elastic::MigrationRecord, :elastic do ...@@ -41,13 +41,13 @@ RSpec.describe Elastic::MigrationRecord, :elastic do
end end
end end
describe '#persisted?' do describe '#started?' do
it 'changes on object save' do it 'changes on object save' do
expect { record.save!(completed: true) }.to change { record.persisted? }.from(false).to(true) expect { record.save!(completed: true) }.to change { record.started? }.from(false).to(true)
end end
end end
describe '.persisted_versions' do describe '.load_versions' do
let(:completed_versions) { 1.upto(5).map { |i| described_class.new(version: i, name: i, filename: nil) } } let(:completed_versions) { 1.upto(5).map { |i| described_class.new(version: i, name: i, filename: nil) } }
let(:in_progress_migration) { described_class.new(version: 10, name: 10, filename: nil) } let(:in_progress_migration) { described_class.new(version: 10, name: 10, filename: nil) }
...@@ -61,15 +61,15 @@ RSpec.describe Elastic::MigrationRecord, :elastic do ...@@ -61,15 +61,15 @@ RSpec.describe Elastic::MigrationRecord, :elastic do
end end
it 'loads all records' do it 'loads all records' do
expect(described_class.persisted_versions(completed: true)).to match_array(completed_versions.map(&:version)) expect(described_class.load_versions(completed: true)).to match_array(completed_versions.map(&:version))
expect(described_class.persisted_versions(completed: false)).to contain_exactly(in_progress_migration.version) expect(described_class.load_versions(completed: false)).to contain_exactly(in_progress_migration.version)
end end
it 'returns empty array if no index present' do it 'returns empty array if no index present' do
es_helper.delete_migrations_index es_helper.delete_migrations_index
expect(described_class.persisted_versions(completed: true)).to eq([]) expect(described_class.load_versions(completed: true)).to eq([])
expect(described_class.persisted_versions(completed: false)).to eq([]) expect(described_class.load_versions(completed: false)).to eq([])
end end
end end
end end
...@@ -75,12 +75,12 @@ RSpec.describe Elastic::DataMigrationService, :elastic do ...@@ -75,12 +75,12 @@ RSpec.describe Elastic::DataMigrationService, :elastic do
end end
it 'creates all migration versions' do it 'creates all migration versions' do
expect(Elastic::MigrationRecord.persisted_versions(completed: true).count).to eq(0) expect(Elastic::MigrationRecord.load_versions(completed: true).count).to eq(0)
subject.mark_all_as_completed! subject.mark_all_as_completed!
refresh_index! refresh_index!
expect(Elastic::MigrationRecord.persisted_versions(completed: true).count).to eq(subject.migrations.count) expect(Elastic::MigrationRecord.load_versions(completed: true).count).to eq(subject.migrations.count)
end end
it 'drops all cache keys' do it 'drops all cache keys' do
......
...@@ -69,13 +69,13 @@ RSpec.describe 'gitlab:elastic namespace rake tasks', :elastic do ...@@ -69,13 +69,13 @@ RSpec.describe 'gitlab:elastic namespace rake tasks', :elastic do
it 'marks all migrations as completed' do it 'marks all migrations as completed' do
expect(Elastic::DataMigrationService).to receive(:mark_all_as_completed!).and_call_original expect(Elastic::DataMigrationService).to receive(:mark_all_as_completed!).and_call_original
expect(Elastic::MigrationRecord.persisted_versions(completed: true)).to eq([]) expect(Elastic::MigrationRecord.load_versions(completed: true)).to eq([])
subject subject
refresh_index! refresh_index!
migrations = Elastic::DataMigrationService.migrations.map(&:version) migrations = Elastic::DataMigrationService.migrations.map(&:version)
expect(Elastic::MigrationRecord.persisted_versions(completed: true)).to eq(migrations) expect(Elastic::MigrationRecord.load_versions(completed: true)).to eq(migrations)
end end
end end
......
...@@ -77,7 +77,7 @@ RSpec.describe Elastic::MigrationWorker, :elastic do ...@@ -77,7 +77,7 @@ RSpec.describe Elastic::MigrationWorker, :elastic do
context 'migration process' do context 'migration process' do
before do before do
allow(migration).to receive(:persisted?).and_return(persisted) allow(migration).to receive(:started?).and_return(started)
allow(migration).to receive(:completed?).and_return(completed) allow(migration).to receive(:completed?).and_return(completed)
allow(migration).to receive(:batched?).and_return(batched) allow(migration).to receive(:batched?).and_return(batched)
end end
...@@ -85,7 +85,7 @@ RSpec.describe Elastic::MigrationWorker, :elastic do ...@@ -85,7 +85,7 @@ RSpec.describe Elastic::MigrationWorker, :elastic do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
# completed is evaluated after migrate method is executed # completed is evaluated after migrate method is executed
where(:persisted, :completed, :execute_migration, :batched) do where(:started, :completed, :execute_migration, :batched) do
false | false | true | false false | false | true | false
false | true | true | false false | true | true | false
false | false | true | true false | false | true | true
...@@ -130,7 +130,7 @@ RSpec.describe Elastic::MigrationWorker, :elastic do ...@@ -130,7 +130,7 @@ RSpec.describe Elastic::MigrationWorker, :elastic do
let(:batched) { true } let(:batched) { true }
where(:persisted, :completed, :expected) do where(:started, :completed, :expected) do
false | false | false false | false | false
true | false | false true | false | false
true | true | true true | true | true
......
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