Skip to content

Commit bacb4ef

Browse files
ruziaolavloite
andauthored
fix: failed to convert active model type to spanner type under certain condition (#299)
* fix: failed to convert active model type to spanner type. under certain condition Added support for the case where a DelegateClass such as ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter is passed to type. * test: add test for DelegateClass conversions --------- Co-authored-by: Knut Olav Løite <koloite@gmail.com>
1 parent 2f5ffd0 commit bacb4ef

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/active_record/type/spanner/spanner_active_record_converter.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def self.serialize_with_transaction_isolation_level type, value, isolation_level
2323
##
2424
# Converts an ActiveModel::Type to a Spanner type code.
2525
def self.convert_active_model_type_to_spanner type # rubocop:disable Metrics/CyclomaticComplexity
26+
# Unwrap the underlying object if the type is a DelegateClass.
27+
type = type.__getobj__ if type.respond_to? :__getobj__
28+
2629
case type
2730
when NilClass then nil
2831
when ActiveModel::Type::Integer, ActiveModel::Type::BigInteger then :INT64

test/activerecord_spanner_mock_server/spanner_active_record_with_mock_server_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,23 @@ def test_create_singer_with_last_performance_as_time
306306
assert_equal "2021-05-12T08:30:00.000000000Z", request.params["p3"]
307307
end
308308

309+
def test_create_singer_with_time_zone_aware_attributes
310+
Singer.time_zone_aware_attributes=true
311+
312+
insert_sql = "INSERT INTO `singers` (`first_name`, `last_name`, `last_performance`, `id`) VALUES (@p1, @p2, @p3, @p4)"
313+
@mock.put_statement_result insert_sql, StatementResult.new(1)
314+
315+
Singer.transaction do
316+
Singer.create(first_name: "Dave", last_name: "Allison", last_performance: ::Time.parse("2021-05-12T10:30:00+02:00"))
317+
end
318+
319+
request = @mock.requests.select {|req| req.is_a?(Google::Cloud::Spanner::V1::ExecuteSqlRequest) && req.sql == insert_sql }.first
320+
assert_equal :TIMESTAMP, request.param_types["p3"].code
321+
assert_equal "2021-05-12T08:30:00.000000000Z", request.params["p3"]
322+
ensure
323+
Singer.time_zone_aware_attributes=false
324+
end
325+
309326
def test_create_singer_with_last_performance_as_non_iso_string
310327
insert_sql = "INSERT INTO `singers` (`first_name`, `last_name`, `last_performance`, `id`) VALUES (@p1, @p2, @p3, @p4)"
311328
@mock.put_statement_result insert_sql, StatementResult.new(1)

0 commit comments

Comments
 (0)