Commit 60d78bce authored by Kerri Miller's avatar Kerri Miller

Merge branch '282557-timestamps-with-migrations' into 'master'

Add started_at/completed_at timestamps to Elasticsearch migrations

See merge request gitlab-org/gitlab!47751
parents 1d54afb2 f2cf0091
......@@ -16,7 +16,9 @@ module Elastic
def save!(completed:)
raise 'Migrations index is not found' unless helper.index_exists?(index_name: index_name)
client.index index: index_name, type: '_doc', id: version, body: { completed: completed }
data = { completed: completed }.merge(timestamps(completed: completed))
client.index index: index_name, type: '_doc', id: version, body: data
end
def persisted?
......@@ -41,6 +43,15 @@ module Elastic
private
def timestamps(completed:)
{}.tap do |data|
existing_data = load_from_index
data[:started_at] = existing_data&.dig('_source', 'started_at') || Time.now.utc
data[:completed_at] = Time.now.utc if completed
end
end
def migration
@migration ||= load_migration
end
......
......@@ -63,6 +63,12 @@ module Gitlab
properties: {
completed: {
type: 'boolean'
},
started_at: {
type: 'date'
},
completed_at: {
type: 'date'
}
}
}
......
......@@ -11,6 +11,34 @@ RSpec.describe Elastic::MigrationRecord, :elastic do
expect { record.save!(completed: true) }.to raise_error(/index is not found/)
end
it 'sets the started_at' do
record.save!(completed: false)
expect(record.load_from_index.dig('_source', 'started_at')).not_to be_nil
end
it 'does not update started_at on subsequent saves' do
record.save!(completed: false)
real_started_at = record.load_from_index.dig('_source', 'started_at')
record.save!(completed: false)
expect(record.load_from_index.dig('_source', 'started_at')).to eq(real_started_at)
end
it 'sets completed_at when completed' do
record.save!(completed: true)
expect(record.load_from_index.dig('_source', 'completed_at')).not_to be_nil
end
it 'does not set completed_at when not completed' do
record.save!(completed: false)
expect(record.load_from_index.dig('_source', 'completed_at')).to be_nil
end
end
describe '#persisted?' do
......
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