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

Support import on datasets with DelayedEvaluation as from #2232

Closed
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
3 changes: 3 additions & 0 deletions lib/sequel/adapters/shared/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,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 @@ -1806,6 +1806,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
Loading