Skip to content

Commit

Permalink
Don't overwrite flags of timestamp coders
Browse files Browse the repository at this point in the history
It is unexpected, since it makes the coder behave contrary to the class name.
Moreover it causes a regression in rails:
  rails/rails#48049

Fixes ged#524
  • Loading branch information
larskanis committed Apr 24, 2023
1 parent 70460e3 commit 77bd5e8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/pg/binary_decoder/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/pg/binary_encoder/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/pg/text_decoder/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 25 additions & 1 deletion spec/pg/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 77bd5e8

Please sign in to comment.