Skip to content

Commit

Permalink
Merge pull request #649 from samvera-labs/updated_at_fix
Browse files Browse the repository at this point in the history
Enforce updated_at being set correctly.
  • Loading branch information
escowles committed Jan 30, 2019
2 parents 42dead6 + 03fb1c0 commit 1c8458c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/valkyrie/persistence/solr/model_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,25 @@ def created_at
if resource_attributes[:created_at]
DateTime.parse(resource_attributes[:created_at].to_s).utc.iso8601
else
Time.current.utc.iso8601
Time.current.utc.iso8601(6)
end
end

# @return [String] ISO-8601 timestamp in UTC of the updated_at for solr
# @note Solr stores its own updated_at timestamp, but for performance
# reasons we're generating our own. Without doing so, every time we add a
# new document we'd have to do a GET to find out the timestamp.
def updated_at
Time.current.utc.iso8601(6)
end

# @return [Hash] Solr document to index.
def to_h
{
"id": id,
"join_id_ssi": "id-#{id}",
"created_at_dtsi": created_at
"created_at_dtsi": created_at,
"updated_at_dtsi": updated_at
}.merge(add_single_values(attribute_hash)).merge(lock_hash)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/valkyrie/persistence/solr/orm_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def created_at
# Construct a Time object from the datestamp for the date of the last resource update indexed in Solr
# @return [Time]
def updated_at
DateTime.parse(solr_document["timestamp"] || solr_document.fetch("created_at_dtsi").to_s).utc
DateTime.parse(solr_document.fetch("updated_at_dtsi").to_s || solr_document["timestamp"] || solr_document.fetch("created_at_dtsi").to_s).utc
end

# Construct the OptimisticLockToken object using the "_version_" field value in the Solr Document
Expand Down
1 change: 1 addition & 0 deletions lib/valkyrie/specs/shared_specs/persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class CustomResource < Valkyrie::Resource
expect(book.updated_at).not_to be_blank
expect(book.created_at).not_to be_kind_of Array
expect(book.updated_at).not_to be_kind_of Array
expect(book.updated_at > book.created_at).to eq true
end

it "can handle Boolean RDF properties" do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'pry'
require 'action_dispatch'
require 'webmock/rspec'
require 'timecop'

SOLR_TEST_URL = "http://127.0.0.1:#{ENV['TEST_JETTY_PORT'] || 8984}/solr/blacklight-core-test"

Expand Down
7 changes: 7 additions & 0 deletions spec/valkyrie/persistence/solr/model_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class Resource < Valkyrie::Resource
end

describe "#to_h" do
before do
Timecop.freeze
end
after do
Timecop.return
end
it "maps all available properties to the solr record" do
expect(mapper.convert!).to eq(
id: resource.id.to_s,
Expand All @@ -60,6 +66,7 @@ class Resource < Valkyrie::Resource
author_tesi: ["Author"],
author_tsi: ["Author"],
created_at_dtsi: created_at.iso8601,
updated_at_dtsi: Time.current.utc.iso8601(6),
internal_resource_ssim: ["Resource"],
internal_resource_tesim: ["Resource"],
internal_resource_tsim: ["Resource"],
Expand Down
1 change: 1 addition & 0 deletions valkyrie.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'fcrepo_wrapper'
spec.add_development_dependency 'docker-stack', '~> 0.2.6'
spec.add_development_dependency 'activerecord', '~> 5.1.0'
spec.add_development_dependency 'timecop'
end

0 comments on commit 1c8458c

Please sign in to comment.