-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update current elasticsearch schema rake task #2703
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class SchemaSynchroniser | ||
attr_reader :errors | ||
|
||
def initialize(index_group) | ||
@index = index_group.current | ||
end | ||
|
||
def call | ||
@errors = @index.sync_mappings | ||
end | ||
|
||
def synchronised_types | ||
@index.mappings.keys.difference(@errors.keys) | ||
end | ||
end | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe SchemaSynchroniser do | ||
before do | ||
clean_index_content("govuk_test") | ||
end | ||
|
||
it "synchronises the current Elasticsearch index schema with the schema defined by the search API" do | ||
index_group = search_server.index_group("govuk_test") | ||
mappings = index_group.current.mappings | ||
synchroniser = described_class.new(index_group) | ||
synchroniser.call | ||
expect(synchroniser.synchronised_types).not_to be_empty | ||
expect(synchroniser.synchronised_types).to eq(mappings.keys) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe SearchIndices::Index do | ||
let(:base_uri) { "http://example.com:9200" } | ||
|
||
it "syncs mappings to elasticsearch and returns any failures" do | ||
mappings = { | ||
"generic-document" => { | ||
"properties" => { | ||
"new-field" => { "type": "text" }, | ||
}, | ||
}, | ||
"failing-document" => { | ||
"properties" => { | ||
"invalid-field" => { "type": "text" }, | ||
}, | ||
}, | ||
} | ||
stub = stub_request(:put, %r{#{base_uri}/govuk-abc/_mapping/generic-document}) | ||
.with(body: mappings["generic-document"]) | ||
.to_return({ | ||
status: 200, | ||
body: { "ok" => true, "acknowledged" => true }.to_json, | ||
headers: { "Content-Type" => "application/json" }, | ||
}) | ||
|
||
error_body = { "error" => { | ||
"type" => "illegal_argument_exception", | ||
"reason" => "invalid mapping", | ||
} }.to_json | ||
failing_stub = stub_request(:put, %r{#{base_uri}/govuk-abc/_mapping/failing-document}) | ||
.with(body: mappings["failing-document"]) | ||
.to_return({ | ||
status: 400, | ||
body: error_body, | ||
headers: { "Content-Type" => "application/json" }, | ||
}) | ||
|
||
index = SearchIndices::Index.new(base_uri, "govuk-abc", "govuk", mappings, SearchConfig.default_instance) | ||
|
||
errors = index.sync_mappings | ||
|
||
assert_requested stub | ||
assert_requested failing_stub | ||
expect(errors).not_to be_empty | ||
expect(Elasticsearch::Transport::Transport::Errors::BadRequest.new("[400] #{error_body}").message).to eq(errors["failing-document"].message) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered adding a unit test for this but it ended up basically just testing a mock of the index group, which felt very inflexible. Unfortunately I couldn't see a way to inject a schema into the actual index and index group objects so I decided to stick with the integration test only, but it can't cover the error handling.