From 883d2009b363047c0c22c31361ad2c7b25e1d26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Miguez?= Date: Thu, 3 Oct 2024 17:39:51 -0300 Subject: [PATCH] Support import on datasets with `DelayedEvaluation` as from --- lib/sequel/adapters/shared/postgres.rb | 3 +++ spec/adapters/postgres_spec.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/sequel/adapters/shared/postgres.rb b/lib/sequel/adapters/shared/postgres.rb index 3ef326f4b9..1848f04700 100644 --- a/lib/sequel/adapters/shared/postgres.rb +++ b/lib/sequel/adapters/shared/postgres.rb @@ -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) diff --git a/spec/adapters/postgres_spec.rb b/spec/adapters/postgres_spec.rb index 1603fa59e0..b976a3a7c2 100644 --- a/spec/adapters/postgres_spec.rb +++ b/spec/adapters/postgres_spec.rb @@ -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