Giter Club home page Giter Club logo

Comments (4)

mberlanda avatar mberlanda commented on August 30, 2024

hi @fredwillmore , it looks like the behaviour you are describing is covered by unit tests https://github.com/jamis/bulk_insert/blob/master/test/bulk_insert_test.rb . Can you confirm you can reproduce it consistently?

from bulk_insert.

travismiller avatar travismiller commented on August 30, 2024

I was surprised by this as well.

The test sets an outer result variable from inside the block. So this is the way to access the worker and result_sets when using values or a block with the existing code.

test "bulk_insert with block should yield worker" do
result = nil
Testing.bulk_insert { |worker| result = worker }
assert_kind_of BulkInsert::Worker, result
end

However, the README indicates that a worker would be returned when using a block.

bulk_insert/README.md

Lines 176 to 192 in ab5db08

### Return Primary Keys (PostgreSQL, PostGIS)
If you want the worker to store primary keys of inserted records, then you can
use the _return_primary_keys_ option. The worker will store a `result_sets`
array of `ActiveRecord::Result` objects. Each `ActiveRecord::Result` object
will contain the primary keys of a batch of inserted records.
```ruby
worker = Book.bulk_insert(*destination_columns, return_primary_keys: true) do
|worker|
worker.add(...)
worker.add(...)
# ...
end
worker.result_sets
```

As mentioned in the original issue comment, the worker is only returned if no values are present and no block is given.

if values.present?
transaction do
worker.add_all(values)
worker.save!
end
nil
elsif block_given?
transaction do
yield worker
worker.save!
end
nil
else
worker
end

I think it should be appropriate to always return the worker. Maybe something like this?

def bulk_insert #…
  # …

  if values.present?
    transaction do
      worker.add_all(values)
      worker.save!
    end
  elsif block_given?
    transaction do
      yield worker
      worker.save!
    end
  else

  worker
end

from bulk_insert.

mberlanda avatar mberlanda commented on August 30, 2024

@jamis do you remember why you choose to return nil when implementing this feature here? 95a0c43

from bulk_insert.

jamis avatar jamis commented on August 30, 2024

I don't remember exactly. :/ However, looking at the diff, I suspect it was an attempt to make the return value consistent, so that it always returns either a worker object, or nil. I vaguely remember being worried about the consequences of someone trusting the method to return self, and introducing a bug when they got a worker object back instead.

from bulk_insert.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.