From 77bd5e8a77c67cbd194ee811b52de038faa3ce57 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Mon, 24 Apr 2023 20:55:41 +0200 Subject: [PATCH] Don't overwrite flags of timestamp coders It is unexpected, since it makes the coder behave contrary to the class name. Moreover it causes a regression in rails: https://github.com/rails/rails/issues/48049 Fixes #524 --- lib/pg/binary_decoder/timestamp.rb | 6 +++--- lib/pg/binary_encoder/timestamp.rb | 4 ++-- lib/pg/text_decoder/timestamp.rb | 6 +++--- spec/pg/type_spec.rb | 26 +++++++++++++++++++++++++- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/pg/binary_decoder/timestamp.rb b/lib/pg/binary_decoder/timestamp.rb index 880f72bf6..c9c4ef11b 100644 --- a/lib/pg/binary_decoder/timestamp.rb +++ b/lib/pg/binary_decoder/timestamp.rb @@ -7,19 +7,19 @@ module BinaryDecoder class TimestampUtc < Timestamp def initialize(hash={}, **kwargs) warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty? - super(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC, **hash, **kwargs) + super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC) end end class TimestampUtcToLocal < Timestamp def initialize(hash={}, **kwargs) warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty? - super(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL, **hash, **kwargs) + super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL) end end class TimestampLocal < Timestamp def initialize(hash={}, **kwargs) warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty? - super(flags: PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL, **hash, **kwargs) + super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL) end end end diff --git a/lib/pg/binary_encoder/timestamp.rb b/lib/pg/binary_encoder/timestamp.rb index fb41584cb..7834bcc3d 100644 --- a/lib/pg/binary_encoder/timestamp.rb +++ b/lib/pg/binary_encoder/timestamp.rb @@ -7,13 +7,13 @@ module BinaryEncoder class TimestampUtc < Timestamp def initialize(hash={}, **kwargs) warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty? - super(flags: PG::Coder::TIMESTAMP_DB_UTC, **hash, **kwargs) + super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC) end end class TimestampLocal < Timestamp def initialize(hash={}, **kwargs) warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty? - super(flags: PG::Coder::TIMESTAMP_DB_LOCAL, **hash, **kwargs) + super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_LOCAL) end end end diff --git a/lib/pg/text_decoder/timestamp.rb b/lib/pg/text_decoder/timestamp.rb index 8e3e8b209..c0c110243 100644 --- a/lib/pg/text_decoder/timestamp.rb +++ b/lib/pg/text_decoder/timestamp.rb @@ -7,19 +7,19 @@ module TextDecoder class TimestampUtc < Timestamp def initialize(hash={}, **kwargs) warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty? - super(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC, **hash, **kwargs) + super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC) end end class TimestampUtcToLocal < Timestamp def initialize(hash={}, **kwargs) warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty? - super(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL, **hash, **kwargs) + super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL) end end class TimestampLocal < Timestamp def initialize(hash={}, **kwargs) warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty? - super(flags: PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL, **hash, **kwargs) + super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL) end end diff --git a/spec/pg/type_spec.rb b/spec/pg/type_spec.rb index 17965d044..af225c0fd 100644 --- a/spec/pg/type_spec.rb +++ b/spec/pg/type_spec.rb @@ -523,12 +523,15 @@ def expect_deprecated_coder_init end end - it "should overwrite default values as hash" do + it "should overwrite default format" do t = nil expect_deprecated_coder_init do t = PG::BinaryEncoder::Int4.new({format: 0}) end expect( t.format ).to eq( 0 ) + + t = PG::BinaryEncoder::Int4.new(format: 0) + expect( t.format ).to eq( 0 ) end it "should take hash argument" do @@ -559,6 +562,27 @@ def expect_deprecated_coder_init expect( t.name ).to eq( "abcä" ) end + it "shouldn't overwrite timestamp flags" do + t = PG::TextDecoder::TimestampUtc.new({flags: PG::Coder::TIMESTAMP_DB_LOCAL}) + expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC ) + t = PG::TextDecoder::TimestampUtcToLocal.new({flags: PG::Coder::TIMESTAMP_APP_UTC}) + expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL ) + t = PG::TextDecoder::TimestampLocal.new({flags: PG::Coder::TIMESTAMP_DB_UTC}) + expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL ) + + t = PG::BinaryDecoder::TimestampUtc.new({flags: PG::Coder::TIMESTAMP_DB_LOCAL}) + expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC ) + t = PG::BinaryDecoder::TimestampUtcToLocal.new({flags: PG::Coder::TIMESTAMP_APP_UTC}) + expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL ) + t = PG::BinaryDecoder::TimestampLocal.new({flags: PG::Coder::TIMESTAMP_DB_UTC}) + expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL ) + + t = PG::BinaryEncoder::TimestampUtc.new({flags: PG::Coder::TIMESTAMP_DB_LOCAL}) + expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC ) + t = PG::BinaryEncoder::TimestampLocal.new({flags: PG::Coder::TIMESTAMP_APP_LOCAL}) + expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_LOCAL ) + end + it "should deny changes when frozen" do t = PG::TextEncoder::String.new.freeze expect{ t.format = 1 }.to raise_error(FrozenError)