Commit f2cf0091 authored by Dylan Griffith's avatar Dylan Griffith

Add started_at/completed_at timestamps to Elasticsearch migrations

When saving Elasticsearch migrations we set a started_at and
completed_at value. This will help us long term with debugging as this
information will make it much easier to find relevant logs for what
happened.

As this index was not declared with [strict dynamic
mapping](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/dynamic.html)
it is totally safe to just add new fields to the payload without them
existing already in the configuration and I have tested locally that
this works as well.

For the sake of clarity, however, we have added them to the mappings as
there is no reason not to.
parent 1634cb5f
......@@ -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