occams-record 1.0.0.rc7 → 1.0.0.rc8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 964e9c20b250ab9599801912147978d7d477104d
4
- data.tar.gz: 48184b52dbf038c40508bb6ab06e93399567196e
2
+ SHA256:
3
+ metadata.gz: 77e8b9c5da26d6d76932bfca57a9470648e97bb07ca33ee3fbdfe7fa5e67aed7
4
+ data.tar.gz: 694e75291ee550813f0732cd8d4d2361e5c6e44f324637789af0afa4637e7865
5
5
  SHA512:
6
- metadata.gz: 90243f9b079d7e94a6d474ffdee700dd1212e3066e96e2af89d662d70d7a253e85cbf1a793d9bd377b1962715205efa941a35d48f3b5d984710570f359c6cd24
7
- data.tar.gz: 302edf479ab834b775394a6b1962fb0f1a707b81e4ea5e6db9d8441b5ad72e00ca5adf3c86a389f18e0923b621b425192b7eb7eb1fe5cead18c49aa3fbf2c29a
6
+ metadata.gz: 5831bafffa9f1da1677d2dff25d59d9a1480be2db886a0e275fa9a7e0d7f874dd22314bd18632df383e7213cb5e0fd50b046a526e44d5cae553c7d807af6bb0a
7
+ data.tar.gz: 40ab48b4680d2cf5aabbd48fa90e5564111fc77239bbfa6a0ec7ecdd7f3cc05bb4a906fd28134706a9a216c0eb8c2c9b9cf449791ec1a6dc225334d9fe356233
data/README.md CHANGED
@@ -55,7 +55,9 @@ orders = OccamsRecord.
55
55
  run
56
56
  ````
57
57
 
58
- `each`, `map`, `reduce`, and other Enumerable methods may be used instead of *run*. `find_each` and `find_in_batches` are also supported, and unlike their ActiveRecord counterparts they respect *ORDER BY*. Occams Record has great support for raw SQL queries too, but we'll get to those later.
58
+ `each`, `map`, `reduce`, and other Enumerable methods may be used instead of *run*. `find_each` and `find_in_batches` are also supported. Occams Record has great support for raw SQL queries too, but we'll get to those later.
59
+
60
+ NOTE Unlike in Active Record, `find_each` and `find_in_batches` respect *ORDER BY*. However, to ensure consistency, it's strongly recomended to use `find_each`/`find_in_batches` inside of a transaction.
59
61
 
60
62
  ## Basic eager loading
61
63
 
@@ -212,6 +214,10 @@ orders = OccamsRecord.
212
214
  run
213
215
  ```
214
216
 
217
+ ## Ugly Module
218
+
219
+ `OccamsRecord::Ugly` contains helpers for things that shouldn't, but sometimes must, be done in legacy codebases. See the docs.
220
+
215
221
  ---
216
222
 
217
223
  # Unsupported features
data/lib/occams-record.rb CHANGED
@@ -6,3 +6,7 @@ require 'occams-record/results'
6
6
  require 'occams-record/query'
7
7
  require 'occams-record/raw_query'
8
8
  require 'occams-record/errors'
9
+
10
+ module OccamsRecord
11
+ autoload :Ugly, 'occams-record/ugly'
12
+ end
@@ -7,8 +7,11 @@ module OccamsRecord
7
7
  # Load records in batches of N and yield each record to a block if given.
8
8
  # If no block is given, returns an Enumerator.
9
9
  #
10
- # NOTE Unlike ActiveRecord's find_each, order IS respected. The primary key will be appended
11
- # to the ORDER BY clause to help ensure consistent batches.
10
+ # NOTE Unlike ActiveRecord's find_each, order is respected. The primary key will be appended
11
+ # to the ORDER BY clause to help ensure consistent batches. HOWEVER, it's still possible for
12
+ # batches to be "corrupted" (miss records or repeat records) if table data changes out from
13
+ # enderneath them. To prevent this, it's strongly recomended to always run find_each inside
14
+ # of a transaction.
12
15
  #
13
16
  # @param batch_size [Integer]
14
17
  # @yield [OccamsRecord::Results::Row]
@@ -31,8 +34,11 @@ module OccamsRecord
31
34
  # Load records in batches of N and yield each batch to a block if given.
32
35
  # If no block is given, returns an Enumerator.
33
36
  #
34
- # NOTE Unlike ActiveRecord's find_in_batches, order IS respected. The primary key will be appended
35
- # to the ORDER BY clause to help ensure consistent batches.
37
+ # NOTE Unlike ActiveRecord's find_in_batches, order is respected. The primary key will be appended
38
+ # to the ORDER BY clause to help ensure consistent batches. HOWEVER, it's still possible for
39
+ # batches to be "corrupted" (miss records or repeat records) if table data changes out from
40
+ # enderneath them. To prevent this, it's strongly recomended to always run find_in_batches inside
41
+ # of a transaction.
36
42
  #
37
43
  # @param batch_size [Integer]
38
44
  # @yield [OccamsRecord::Results::Row]
@@ -59,6 +65,10 @@ module OccamsRecord
59
65
  # @return [Enumerator] yields batches
60
66
  #
61
67
  def batches(of:)
68
+ if model.connection.open_transactions == 0
69
+ $stderr.puts "Occams Record Warning: find_each or find_in_batches is being run outside of transaction. Batch consistency can only be ensured within a transaction."
70
+ end
71
+
62
72
  limit = scope.limit_value
63
73
  batch_size = limit && limit < of ? limit : of
64
74
  Enumerator.new do |y|
@@ -0,0 +1,43 @@
1
+ module OccamsRecord
2
+ #
3
+ # This module contains helpers for things you shouldn't, but sometimes must, do in legacy codebases.
4
+ #
5
+ module Ugly
6
+ #
7
+ # Loads an Occams Record object into an ActiveRecord model. THIS WILL NEGATE ALL PERFORMANCE IMPROVEMENTS!
8
+ # The ONLY reason to use this is if you absolutely need ActiveRecord objects but still want to use Occams's
9
+ # more advanced eager loading or find_each/find_in_batches features.
10
+ #
11
+ # OccamsRecord.
12
+ # query(Order.order("created_at DESC")).
13
+ # eager_load(:line_items, ->(q) { q.order("price") }).
14
+ # find_each do |o|
15
+ # order = OccamsRecord::Ugly.active_record(o)
16
+ # ...
17
+ # end
18
+ #
19
+ # @param model [ActiveRecord::Base] The model to load the record into
20
+ # @param record [OccamsRecord::Result::Row] The OccamsRecord row
21
+ # @return [ActiveRecord::Base]
22
+ #
23
+ def self.active_record(model, record)
24
+ active = model.new(record.to_h)
25
+
26
+ record.class.associations.each do |assoc_name|
27
+ assoc = active.class.reflections[assoc_name]
28
+ obj = record.send assoc_name
29
+ next if assoc.nil? or obj.nil?
30
+
31
+ if obj.is_a? Array
32
+ active.send(assoc_name).load_target.replace obj.map { |x|
33
+ active_record(assoc.klass, x)
34
+ }
35
+ else
36
+ active.send "#{assoc_name}=", active_record(assoc.klass, obj)
37
+ end
38
+ end
39
+
40
+ active
41
+ end
42
+ end
43
+ end
@@ -3,5 +3,5 @@
3
3
  #
4
4
  module OccamsRecord
5
5
  # Library version
6
- VERSION = '1.0.0.rc7'.freeze
6
+ VERSION = '1.0.0.rc8'.freeze
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: occams-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc7
4
+ version: 1.0.0.rc8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-18 00:00:00.000000000 Z
11
+ date: 2019-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -58,6 +58,7 @@ files:
58
58
  - lib/occams-record/query.rb
59
59
  - lib/occams-record/raw_query.rb
60
60
  - lib/occams-record/results.rb
61
+ - lib/occams-record/ugly.rb
61
62
  - lib/occams-record/version.rb
62
63
  homepage: https://jhollinger.github.io/occams-record/
63
64
  licenses:
@@ -79,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
80
  version: 1.3.1
80
81
  requirements: []
81
82
  rubyforge_project:
82
- rubygems_version: 2.5.2.3
83
+ rubygems_version: 2.7.6
83
84
  signing_key:
84
85
  specification_version: 4
85
86
  summary: The missing high-efficiency query API for ActiveRecord