occams-record 1.1.5 → 1.1.6

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
2
  SHA256:
3
- metadata.gz: 216f5b9aff38034e5bcaab036d4a5a20add7053062546eb827bd12269d49c766
4
- data.tar.gz: f481e52c6d3390b7a3f5aa7497ac15d78774ef93f5d295dca472c75f558db9d2
3
+ metadata.gz: 466aabbac2317ba6b65e5d02c4f244f317640bbbea08a2949c4b4ebe59be5bfb
4
+ data.tar.gz: cd50c8465764d9f811acccd1d833357ced98e2ba5812e3c5d12a3f2534495fb1
5
5
  SHA512:
6
- metadata.gz: ee45536cbf4ea6a317647e73ac30788416bebfb3b152350fdcb4ab7d6e6e747ef1eb1763ef2aec731e11a9fb5925a8b234787933418c8c74f0724dd3b1ee1b9e
7
- data.tar.gz: 30ab20cdd8611e0fec40a7a2e1b3d516ac153a5d283a393860650588c2d6f1bc93f02c43f125bb1675824ef019a29e276a05e0900899e311b838eab87a186db4
6
+ metadata.gz: d5935484d5a729eb82fa12eb6402b48d7a54f54d9d8fe2986694eada3af26296c88536c78bbf2a05239483d66deb2c0a7bd6f70097b51547d95d5ded77bbec11
7
+ data.tar.gz: b0162c9054e18a8c1c265de9e47b5cd2bb00dcc53bd7406a166d67b3c4a390389a0e06f80e345fff8ed89fc2b7edb2b87a9e30a4fc165c6a4f9be2541bbebd71
@@ -13,12 +13,13 @@ module OccamsRecord
13
13
  #
14
14
  # @param batch_size [Integer]
15
15
  # @param use_transaction [Boolean] Ensure it runs inside of a database transaction
16
+ # @param append_order_by [String] Append this column to ORDER BY to ensure consistent results. Defaults to the primary key. Pass false to disable.
16
17
  # @yield [OccamsRecord::Results::Row]
17
18
  # @return [Enumerator] will yield each record
18
19
  #
19
- def find_each(batch_size: 1000, use_transaction: true)
20
+ def find_each(batch_size: 1000, use_transaction: true, append_order_by: nil)
20
21
  enum = Enumerator.new { |y|
21
- batches(of: batch_size, use_transaction: use_transaction).each { |batch|
22
+ batches(of: batch_size, use_transaction: use_transaction, append_order_by: append_order_by).each { |batch|
22
23
  batch.each { |record| y.yield record }
23
24
  }
24
25
  }
@@ -39,11 +40,12 @@ module OccamsRecord
39
40
  #
40
41
  # @param batch_size [Integer]
41
42
  # @param use_transaction [Boolean] Ensure it runs inside of a database transaction
43
+ # @param append_order_by [String] Append this column to ORDER BY to ensure consistent results. Defaults to the primary key. Pass false to disable.
42
44
  # @yield [OccamsRecord::Results::Row]
43
45
  # @return [Enumerator] will yield each batch
44
46
  #
45
- def find_in_batches(batch_size: 1000, use_transaction: true)
46
- enum = batches(of: batch_size, use_transaction: use_transaction)
47
+ def find_in_batches(batch_size: 1000, use_transaction: true, append_order_by: nil)
48
+ enum = batches(of: batch_size, use_transaction: use_transaction, append_order_by: append_order_by)
47
49
  if block_given?
48
50
  enum.each { |batch| yield batch }
49
51
  else
@@ -61,34 +63,43 @@ module OccamsRecord
61
63
  #
62
64
  # @param of [Integer] batch size
63
65
  # @param use_transaction [Boolean] Ensure it runs inside of a database transaction
66
+ # @param append_order_by [String] Append this column to ORDER BY to ensure consistent results. Defaults to the primary key. Pass false to disable.
64
67
  # @return [Enumerator] yields batches
65
68
  #
66
- def batches(of:, use_transaction: true)
69
+ def batches(of:, use_transaction: true, append_order_by: nil)
70
+ append_order =
71
+ case append_order_by
72
+ when false then nil
73
+ when nil then model.primary_key
74
+ else append_order_by
75
+ end
76
+
67
77
  Enumerator.new do |y|
68
78
  if use_transaction and model.connection.open_transactions == 0
69
79
  model.connection.transaction {
70
- run_batches y, of
80
+ run_batches y, of, append_order
71
81
  }
72
82
  else
73
- run_batches y, of
83
+ run_batches y, of, append_order
74
84
  end
75
85
  end
76
86
  end
77
87
 
78
- def run_batches(y, of)
88
+ def run_batches(y, of, append_order_by = nil)
79
89
  limit = scope.limit_value
80
90
  batch_size = limit && limit < of ? limit : of
81
91
 
82
92
  offset = scope.offset_value || 0
83
93
  out_of_records, count = false, 0
84
- pkey_regex = /#{model.primary_key}|\*/ # NOTE imperfect
85
- order_by_pkey = !!model.primary_key &&
86
- (scope.select_values.empty? || scope.select_values.any? { |v| v.to_s =~ pkey_regex })
94
+ order_by =
95
+ if append_order_by
96
+ append_order_by.to_s == model.primary_key.to_s ? append_order_by.to_sym : append_order_by
97
+ end
87
98
 
88
99
  until out_of_records
89
100
  l = limit && batch_size > limit - count ? limit - count : batch_size
90
101
  q = scope
91
- q = q.order(model.primary_key.to_sym) if order_by_pkey
102
+ q = q.order(order_by) if order_by
92
103
  q = q.offset(offset).limit(l)
93
104
  results = Query.new(q, use: @use, query_logger: @query_logger, eager_loaders: @eager_loaders).run
94
105
 
@@ -156,7 +156,7 @@ module OccamsRecord
156
156
  # @param use_transaction [Boolean] Ensure it runs inside of a database transaction
157
157
  # @return [Enumerator] yields batches
158
158
  #
159
- def batches(of:, use_transaction: true)
159
+ def batches(of:, use_transaction: true, append_order_by: nil)
160
160
  unless @sql =~ /LIMIT\s+%\{batch_limit\}/i and @sql =~ /OFFSET\s+%\{batch_offset\}/i
161
161
  raise ArgumentError, "When using find_each/find_in_batches you must specify 'LIMIT %{batch_limit} OFFSET %{batch_offset}'. SQL statement: #{@sql}"
162
162
  end
@@ -3,5 +3,5 @@
3
3
  #
4
4
  module OccamsRecord
5
5
  # Library version
6
- VERSION = "1.1.5".freeze
6
+ VERSION = "1.1.6".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.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-13 00:00:00.000000000 Z
11
+ date: 2020-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord