Skip to content

Commit

Permalink
fix: handle DateTimes that come back from Sqlite as Strings
Browse files Browse the repository at this point in the history
Upgrading to ruby 2.7.5-alpine3.14 has changed something in Sqlite or Sequel
  • Loading branch information
bethesque committed Feb 14, 2022
1 parent 9b784bf commit 1312a04
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/pact_broker/api/decorators/format_date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ module PactBroker
module Api
module Decorators
module FormatDateTime
# Keep this in sync with Sequel.datetime_class.
# Needs to be upgraded from DateTime to Time as Time is deprecated
DATE_TIME_CLASS = DateTime

def self.call(date_time)
date_time.to_time.utc.to_datetime.xmlschema if date_time
if date_time.is_a?(String)
DATE_TIME_CLASS.strptime(date_time).to_time.utc.to_datetime.xmlschema
elsif date_time
date_time.to_time.utc.to_datetime.xmlschema if date_time
end
end

def format_date_time(date_time)
Expand Down
35 changes: 35 additions & 0 deletions spec/lib/pact_broker/api/decorators/format_date_time_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "pact_broker/api/decorators/format_date_time"

module PactBroker
module Api
module Decorators
module FormatDateTime
describe ".call" do
context "with a Time object" do
let(:date_time) { Time.parse("2022-02-14T15:18:00+14:00" )}

it "converts the date to a string in utc" do
expect(FormatDateTime.call(date_time)).to eq "2022-02-14T01:18:00+00:00"
end
end

context "with a DateTime object" do
let(:date_time) { DateTime.parse("2022-02-14T15:18:00+14:00" )}

it "converts the date to a string in utc" do
expect(FormatDateTime.call(date_time)).to eq "2022-02-14T01:18:00+00:00"
end
end

context "with a String - MySQL and Sqlite (as of the upgrade to Ruby 2.7.5) return date columns as strings. Postgres returns them as dates." do
let(:date_time) { "2022-02-14T15:18:00+14:00" }

it "converts the date to a string in utc" do
expect(FormatDateTime.call(date_time)).to eq "2022-02-14T01:18:00+00:00"
end
end
end
end
end
end
end

0 comments on commit 1312a04

Please sign in to comment.