Skip to content

Commit

Permalink
Support import on datasets with DelayedEvaluation as from
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmiguez authored and jeremyevans committed Oct 3, 2024
1 parent b982668 commit 67de9f2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/sequel/adapters/shared/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,9 @@ def insert_into_sql(sql)
# Return the primary key to use for RETURNING in an INSERT statement
def insert_pk
(f = opts[:from]) && !f.empty? && (t = f.first)

t = t.call(self) if t.is_a? Sequel::SQL::DelayedEvaluation

case t
when Symbol, String, SQL::Identifier, SQL::QualifiedIdentifier
if pk = db.primary_key(t)
Expand Down
16 changes: 16 additions & 0 deletions spec/adapters/postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,22 @@ def before_create
end
end

describe "Postgres::Dataset#import with delayed evaluation as source table" do
before do
@db = DB
@db.create_table!(:test){primary_key :x; Integer :y}
@ds = @db[Sequel::SQL::DelayedEvaluation.new(lambda{:test})]
end
after do
@db.drop_table?(:test)
end

it "#import should work correctly when returning primary keys" do
@ds.import([:x, :y], [[1, 2], [3, 4]], :return=>:primary_key).must_equal [1, 3]
@ds.all.must_equal [{:x=>1, :y=>2}, {:x=>3, :y=>4}]
end
end

describe "Postgres::Dataset#insert" do
before do
@db = DB
Expand Down

0 comments on commit 67de9f2

Please sign in to comment.