Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binary copy_data in Ractor context #596

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/pg/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def inspect
return str
end

BinarySignature = "PGCOPY\n\377\r\n\0".b
BinarySignature = "PGCOPY\n\377\r\n\0"
private_constant :BinarySignature

# call-seq:
Expand Down
25 changes: 24 additions & 1 deletion spec/pg/basic_type_map_based_on_result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Ractor.make_shareable(basic_type_mapping)
end

it "should be usable with Ractor", :ractor do
it "should be usable with Ractor in text format", :ractor do
vals = Ractor.new(@conninfo) do |conninfo|
conn = PG.connect(conninfo)
basic_type_mapping = PG::BasicTypeMapBasedOnResult.new(conn)
Expand All @@ -52,6 +52,29 @@
expect( vals ).to eq( [['b', '234', '{2,3}']] )
end

it "should be usable with Ractor in binary format", :ractor do
vals = Ractor.new(@conninfo) do |conninfo|
conn = PG.connect(conninfo)
basic_type_mapping = PG::BasicTypeMapBasedOnResult.new(conn)
conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT)" )

# Retrieve table OIDs per empty result set.
res = conn.exec( "SELECT * FROM copytable LIMIT 0", [], 1)
tm = basic_type_mapping.build_column_map( res )
row_encoder = PG::BinaryEncoder::CopyRow.new type_map: tm

conn.copy_data( "COPY copytable FROM STDIN WITH (FORMAT binary)", row_encoder ) do |res|
conn.put_copy_data ['b', 234]
end
res = conn.exec( "SELECT * FROM copytable" )
res.values
ensure
conn&.finish
end.take

expect( vals ).to eq( [['b', '234']] )
end

context "with usage of result oids for bind params encoder selection" do
it "can type cast query params" do
@conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT, ai INT[], by BYTEA)" )
Expand Down
10 changes: 10 additions & 0 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
expect( vals ).to eq( [["123"]] )
end

it "connects using 7 arguments in a Ractor", :ractor do
vals = Ractor.new do
PG.connect( 'localhost', @port, nil, nil, :test, nil, nil ) do |conn|
conn.exec("SELECT 234").values
end
end.take

expect( vals ).to eq( [["234"]] )
end

describe "#inspect", :without_transaction do
it "should print host, port and user of a fresh connection, but not more" do
expect( @conn.inspect ).to match(/<PG::Connection:[0-9a-fx]+ host=localhost port=#{@port} user=\w*>/)
Expand Down
Loading