activerecord 7.0.8 → 7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (229) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1412 -1507
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +15 -16
  5. data/lib/active_record/aggregations.rb +16 -13
  6. data/lib/active_record/association_relation.rb +1 -1
  7. data/lib/active_record/associations/association.rb +18 -3
  8. data/lib/active_record/associations/association_scope.rb +16 -9
  9. data/lib/active_record/associations/belongs_to_association.rb +14 -6
  10. data/lib/active_record/associations/builder/association.rb +3 -3
  11. data/lib/active_record/associations/builder/belongs_to.rb +21 -8
  12. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
  13. data/lib/active_record/associations/builder/singular_association.rb +4 -0
  14. data/lib/active_record/associations/collection_association.rb +15 -9
  15. data/lib/active_record/associations/collection_proxy.rb +15 -10
  16. data/lib/active_record/associations/foreign_association.rb +10 -3
  17. data/lib/active_record/associations/has_many_association.rb +20 -13
  18. data/lib/active_record/associations/has_many_through_association.rb +10 -6
  19. data/lib/active_record/associations/has_one_association.rb +10 -3
  20. data/lib/active_record/associations/join_dependency.rb +10 -8
  21. data/lib/active_record/associations/preloader/association.rb +27 -6
  22. data/lib/active_record/associations/preloader.rb +13 -10
  23. data/lib/active_record/associations/singular_association.rb +1 -1
  24. data/lib/active_record/associations/through_association.rb +22 -11
  25. data/lib/active_record/associations.rb +295 -199
  26. data/lib/active_record/attribute_assignment.rb +0 -2
  27. data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
  28. data/lib/active_record/attribute_methods/dirty.rb +40 -26
  29. data/lib/active_record/attribute_methods/primary_key.rb +76 -24
  30. data/lib/active_record/attribute_methods/query.rb +28 -16
  31. data/lib/active_record/attribute_methods/read.rb +18 -5
  32. data/lib/active_record/attribute_methods/serialization.rb +150 -31
  33. data/lib/active_record/attribute_methods/write.rb +3 -3
  34. data/lib/active_record/attribute_methods.rb +105 -21
  35. data/lib/active_record/attributes.rb +3 -3
  36. data/lib/active_record/autosave_association.rb +55 -9
  37. data/lib/active_record/base.rb +7 -2
  38. data/lib/active_record/callbacks.rb +10 -24
  39. data/lib/active_record/coders/column_serializer.rb +61 -0
  40. data/lib/active_record/coders/json.rb +1 -1
  41. data/lib/active_record/coders/yaml_column.rb +70 -42
  42. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +163 -88
  43. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
  44. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +3 -1
  45. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +63 -43
  46. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
  47. data/lib/active_record/connection_adapters/abstract/database_statements.rb +128 -32
  48. data/lib/active_record/connection_adapters/abstract/query_cache.rb +60 -22
  49. data/lib/active_record/connection_adapters/abstract/quoting.rb +41 -6
  50. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  51. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
  52. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
  53. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +289 -124
  54. data/lib/active_record/connection_adapters/abstract/transaction.rb +287 -58
  55. data/lib/active_record/connection_adapters/abstract_adapter.rb +502 -91
  56. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +207 -108
  57. data/lib/active_record/connection_adapters/column.rb +9 -0
  58. data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
  59. data/lib/active_record/connection_adapters/mysql/database_statements.rb +22 -143
  60. data/lib/active_record/connection_adapters/mysql/quoting.rb +16 -12
  61. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
  62. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
  63. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
  64. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +18 -13
  65. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +148 -0
  66. data/lib/active_record/connection_adapters/mysql2_adapter.rb +98 -53
  67. data/lib/active_record/connection_adapters/pool_config.rb +14 -5
  68. data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
  69. data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
  70. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +71 -40
  71. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
  72. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +1 -1
  73. data/lib/active_record/connection_adapters/postgresql/quoting.rb +10 -6
  74. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
  75. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
  76. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +131 -2
  77. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
  78. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +349 -55
  79. data/lib/active_record/connection_adapters/postgresql_adapter.rb +338 -176
  80. data/lib/active_record/connection_adapters/schema_cache.rb +287 -59
  81. data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
  82. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +45 -39
  83. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +4 -3
  84. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +1 -0
  85. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +26 -7
  86. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +209 -79
  87. data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
  88. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +98 -0
  89. data/lib/active_record/connection_adapters/trilogy_adapter.rb +254 -0
  90. data/lib/active_record/connection_adapters.rb +3 -1
  91. data/lib/active_record/connection_handling.rb +71 -94
  92. data/lib/active_record/core.rb +136 -148
  93. data/lib/active_record/counter_cache.rb +46 -25
  94. data/lib/active_record/database_configurations/database_config.rb +9 -3
  95. data/lib/active_record/database_configurations/hash_config.rb +22 -12
  96. data/lib/active_record/database_configurations/url_config.rb +17 -11
  97. data/lib/active_record/database_configurations.rb +86 -33
  98. data/lib/active_record/delegated_type.rb +8 -3
  99. data/lib/active_record/deprecator.rb +7 -0
  100. data/lib/active_record/destroy_association_async_job.rb +2 -0
  101. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  102. data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
  103. data/lib/active_record/encryption/config.rb +25 -1
  104. data/lib/active_record/encryption/configurable.rb +12 -19
  105. data/lib/active_record/encryption/context.rb +10 -3
  106. data/lib/active_record/encryption/contexts.rb +5 -1
  107. data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
  108. data/lib/active_record/encryption/encryptable_record.rb +36 -18
  109. data/lib/active_record/encryption/encrypted_attribute_type.rb +17 -6
  110. data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -54
  111. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
  112. data/lib/active_record/encryption/key_generator.rb +12 -1
  113. data/lib/active_record/encryption/message_serializer.rb +2 -0
  114. data/lib/active_record/encryption/properties.rb +3 -3
  115. data/lib/active_record/encryption/scheme.rb +19 -22
  116. data/lib/active_record/encryption.rb +1 -0
  117. data/lib/active_record/enum.rb +113 -26
  118. data/lib/active_record/errors.rb +108 -15
  119. data/lib/active_record/explain.rb +23 -3
  120. data/lib/active_record/fixture_set/model_metadata.rb +14 -4
  121. data/lib/active_record/fixture_set/render_context.rb +2 -0
  122. data/lib/active_record/fixture_set/table_row.rb +29 -8
  123. data/lib/active_record/fixtures.rb +119 -71
  124. data/lib/active_record/future_result.rb +30 -5
  125. data/lib/active_record/gem_version.rb +3 -3
  126. data/lib/active_record/inheritance.rb +30 -16
  127. data/lib/active_record/insert_all.rb +55 -8
  128. data/lib/active_record/integration.rb +8 -8
  129. data/lib/active_record/internal_metadata.rb +120 -30
  130. data/lib/active_record/locking/pessimistic.rb +5 -2
  131. data/lib/active_record/log_subscriber.rb +29 -12
  132. data/lib/active_record/marshalling.rb +56 -0
  133. data/lib/active_record/message_pack.rb +124 -0
  134. data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
  135. data/lib/active_record/middleware/database_selector.rb +5 -7
  136. data/lib/active_record/middleware/shard_selector.rb +3 -1
  137. data/lib/active_record/migration/command_recorder.rb +104 -5
  138. data/lib/active_record/migration/compatibility.rb +131 -5
  139. data/lib/active_record/migration/default_strategy.rb +23 -0
  140. data/lib/active_record/migration/execution_strategy.rb +19 -0
  141. data/lib/active_record/migration.rb +213 -109
  142. data/lib/active_record/model_schema.rb +60 -40
  143. data/lib/active_record/nested_attributes.rb +21 -3
  144. data/lib/active_record/normalization.rb +159 -0
  145. data/lib/active_record/persistence.rb +184 -34
  146. data/lib/active_record/promise.rb +84 -0
  147. data/lib/active_record/query_cache.rb +3 -21
  148. data/lib/active_record/query_logs.rb +77 -52
  149. data/lib/active_record/query_logs_formatter.rb +41 -0
  150. data/lib/active_record/querying.rb +15 -2
  151. data/lib/active_record/railtie.rb +109 -47
  152. data/lib/active_record/railties/controller_runtime.rb +10 -5
  153. data/lib/active_record/railties/databases.rake +139 -145
  154. data/lib/active_record/railties/job_runtime.rb +23 -0
  155. data/lib/active_record/readonly_attributes.rb +32 -5
  156. data/lib/active_record/reflection.rb +162 -44
  157. data/lib/active_record/relation/batches/batch_enumerator.rb +5 -3
  158. data/lib/active_record/relation/batches.rb +190 -61
  159. data/lib/active_record/relation/calculations.rb +160 -63
  160. data/lib/active_record/relation/delegation.rb +22 -8
  161. data/lib/active_record/relation/finder_methods.rb +77 -16
  162. data/lib/active_record/relation/merger.rb +2 -0
  163. data/lib/active_record/relation/predicate_builder/association_query_value.rb +11 -2
  164. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
  165. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  166. data/lib/active_record/relation/predicate_builder.rb +26 -14
  167. data/lib/active_record/relation/query_attribute.rb +2 -1
  168. data/lib/active_record/relation/query_methods.rb +352 -63
  169. data/lib/active_record/relation/spawn_methods.rb +18 -1
  170. data/lib/active_record/relation.rb +76 -35
  171. data/lib/active_record/result.rb +19 -5
  172. data/lib/active_record/runtime_registry.rb +10 -1
  173. data/lib/active_record/sanitization.rb +51 -11
  174. data/lib/active_record/schema.rb +2 -3
  175. data/lib/active_record/schema_dumper.rb +46 -7
  176. data/lib/active_record/schema_migration.rb +68 -33
  177. data/lib/active_record/scoping/default.rb +15 -5
  178. data/lib/active_record/scoping/named.rb +2 -2
  179. data/lib/active_record/scoping.rb +2 -1
  180. data/lib/active_record/secure_password.rb +60 -0
  181. data/lib/active_record/secure_token.rb +21 -3
  182. data/lib/active_record/signed_id.rb +7 -5
  183. data/lib/active_record/store.rb +8 -8
  184. data/lib/active_record/suppressor.rb +3 -1
  185. data/lib/active_record/table_metadata.rb +10 -1
  186. data/lib/active_record/tasks/database_tasks.rb +127 -105
  187. data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
  188. data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
  189. data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
  190. data/lib/active_record/test_fixtures.rb +113 -96
  191. data/lib/active_record/timestamp.rb +27 -15
  192. data/lib/active_record/token_for.rb +113 -0
  193. data/lib/active_record/touch_later.rb +11 -6
  194. data/lib/active_record/transactions.rb +36 -10
  195. data/lib/active_record/type/adapter_specific_registry.rb +1 -8
  196. data/lib/active_record/type/internal/timezone.rb +7 -2
  197. data/lib/active_record/type/time.rb +4 -0
  198. data/lib/active_record/validations/absence.rb +1 -1
  199. data/lib/active_record/validations/numericality.rb +5 -4
  200. data/lib/active_record/validations/presence.rb +5 -28
  201. data/lib/active_record/validations/uniqueness.rb +47 -2
  202. data/lib/active_record/validations.rb +8 -4
  203. data/lib/active_record/version.rb +1 -1
  204. data/lib/active_record.rb +121 -16
  205. data/lib/arel/errors.rb +10 -0
  206. data/lib/arel/factory_methods.rb +4 -0
  207. data/lib/arel/nodes/binary.rb +6 -1
  208. data/lib/arel/nodes/bound_sql_literal.rb +61 -0
  209. data/lib/arel/nodes/cte.rb +36 -0
  210. data/lib/arel/nodes/fragments.rb +35 -0
  211. data/lib/arel/nodes/homogeneous_in.rb +0 -8
  212. data/lib/arel/nodes/leading_join.rb +8 -0
  213. data/lib/arel/nodes/node.rb +111 -2
  214. data/lib/arel/nodes/sql_literal.rb +6 -0
  215. data/lib/arel/nodes/table_alias.rb +4 -0
  216. data/lib/arel/nodes.rb +4 -0
  217. data/lib/arel/predications.rb +2 -0
  218. data/lib/arel/table.rb +9 -5
  219. data/lib/arel/visitors/mysql.rb +8 -1
  220. data/lib/arel/visitors/to_sql.rb +81 -17
  221. data/lib/arel/visitors/visitor.rb +2 -2
  222. data/lib/arel.rb +16 -2
  223. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  224. data/lib/rails/generators/active_record/migration.rb +3 -1
  225. data/lib/rails/generators/active_record/model/USAGE +113 -0
  226. data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
  227. metadata +46 -11
  228. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
  229. data/lib/active_record/null_relation.rb +0 -63
@@ -3,8 +3,10 @@
3
3
  require "active_record/relation/batches/batch_enumerator"
4
4
 
5
5
  module ActiveRecord
6
+ # = Active Record \Batches
6
7
  module Batches
7
8
  ORDER_IGNORE_MESSAGE = "Scoped order is ignored, it's forced to be batch order."
9
+ DEFAULT_ORDER = :asc
8
10
 
9
11
  # Looping through a collection of records from the database
10
12
  # (using the Scoping::Named::ClassMethods.all method, for example)
@@ -37,7 +39,16 @@ module ActiveRecord
37
39
  # * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
38
40
  # * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
39
41
  # an order is present in the relation.
40
- # * <tt>:order</tt> - Specifies the primary key order (can be +:asc+ or +:desc+). Defaults to +:asc+.
42
+ # * <tt>:order</tt> - Specifies the primary key order (can be +:asc+ or +:desc+ or an array consisting
43
+ # of :asc or :desc). Defaults to +:asc+.
44
+ #
45
+ # class Order < ActiveRecord::Base
46
+ # self.primary_key = [:id_1, :id_2]
47
+ # end
48
+ #
49
+ # Order.find_each(order: [:asc, :desc])
50
+ #
51
+ # In the above code, +id_1+ is sorted in ascending order and +id_2+ in descending order.
41
52
  #
42
53
  # Limits are honored, and if present there is no requirement for the batch
43
54
  # size: it can be less than, equal to, or greater than the limit.
@@ -65,7 +76,7 @@ module ActiveRecord
65
76
  #
66
77
  # NOTE: By its nature, batch processing is subject to race conditions if
67
78
  # other processes are modifying the database.
68
- def find_each(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil, order: :asc, &block)
79
+ def find_each(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil, order: DEFAULT_ORDER, &block)
69
80
  if block_given?
70
81
  find_in_batches(start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore, order: order) do |records|
71
82
  records.each(&block)
@@ -73,7 +84,7 @@ module ActiveRecord
73
84
  else
74
85
  enum_for(:find_each, start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore, order: order) do
75
86
  relation = self
76
- apply_limits(relation, start, finish, order).size
87
+ apply_limits(relation, start, finish, build_batch_orders(order)).size
77
88
  end
78
89
  end
79
90
  end
@@ -102,7 +113,16 @@ module ActiveRecord
102
113
  # * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
103
114
  # * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
104
115
  # an order is present in the relation.
105
- # * <tt>:order</tt> - Specifies the primary key order (can be +:asc+ or +:desc+). Defaults to +:asc+.
116
+ # * <tt>:order</tt> - Specifies the primary key order (can be +:asc+ or +:desc+ or an array consisting
117
+ # of :asc or :desc). Defaults to +:asc+.
118
+ #
119
+ # class Order < ActiveRecord::Base
120
+ # self.primary_key = [:id_1, :id_2]
121
+ # end
122
+ #
123
+ # Order.find_in_batches(order: [:asc, :desc])
124
+ #
125
+ # In the above code, +id_1+ is sorted in ascending order and +id_2+ in descending order.
106
126
  #
107
127
  # Limits are honored, and if present there is no requirement for the batch
108
128
  # size: it can be less than, equal to, or greater than the limit.
@@ -125,11 +145,11 @@ module ActiveRecord
125
145
  #
126
146
  # NOTE: By its nature, batch processing is subject to race conditions if
127
147
  # other processes are modifying the database.
128
- def find_in_batches(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil, order: :asc)
148
+ def find_in_batches(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil, order: DEFAULT_ORDER)
129
149
  relation = self
130
150
  unless block_given?
131
151
  return to_enum(:find_in_batches, start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore, order: order) do
132
- total = apply_limits(relation, start, finish, order).size
152
+ total = apply_limits(relation, start, finish, build_batch_orders(order)).size
133
153
  (total - 1).div(batch_size) + 1
134
154
  end
135
155
  end
@@ -167,7 +187,22 @@ module ActiveRecord
167
187
  # * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
168
188
  # * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
169
189
  # an order is present in the relation.
170
- # * <tt>:order</tt> - Specifies the primary key order (can be +:asc+ or +:desc+). Defaults to +:asc+.
190
+ # * <tt>:order</tt> - Specifies the primary key order (can be +:asc+ or +:desc+ or an array consisting
191
+ # of :asc or :desc). Defaults to +:asc+.
192
+ #
193
+ # class Order < ActiveRecord::Base
194
+ # self.primary_key = [:id_1, :id_2]
195
+ # end
196
+ #
197
+ # Order.in_batches(order: [:asc, :desc])
198
+ #
199
+ # In the above code, +id_1+ is sorted in ascending order and +id_2+ in descending order.
200
+ #
201
+ # * <tt>:use_ranges</tt> - Specifies whether to use range iteration (id >= x AND id <= y).
202
+ # It can make iterating over the whole or almost whole tables several times faster.
203
+ # Only whole table iterations use this style of iteration by default. You can disable this behavior by passing +false+.
204
+ # If you iterate over the table and the only condition is, e.g., <tt>archived_at: nil</tt> (and only a tiny fraction
205
+ # of the records are archived), it makes sense to opt in to this approach.
171
206
  #
172
207
  # Limits are honored, and if present there is no requirement for the batch
173
208
  # size, it can be less than, equal, or greater than the limit.
@@ -201,14 +236,13 @@ module ActiveRecord
201
236
  #
202
237
  # NOTE: By its nature, batch processing is subject to race conditions if
203
238
  # other processes are modifying the database.
204
- def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil, order: :asc)
205
- relation = self
206
- unless block_given?
207
- return BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
239
+ def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil, order: DEFAULT_ORDER, use_ranges: nil, &block)
240
+ unless Array(order).all? { |ord| [:asc, :desc].include?(ord) }
241
+ raise ArgumentError, ":order must be :asc or :desc or an array consisting of :asc or :desc, got #{order.inspect}"
208
242
  end
209
243
 
210
- unless [:asc, :desc].include?(order)
211
- raise ArgumentError, ":order must be :asc or :desc, got #{order.inspect}"
244
+ unless block
245
+ return BatchEnumerator.new(of: of, start: start, finish: finish, relation: self, order: order, use_ranges: use_ranges)
212
246
  end
213
247
 
214
248
  if arel.orders.present?
@@ -216,71 +250,76 @@ module ActiveRecord
216
250
  end
217
251
 
218
252
  batch_limit = of
253
+
219
254
  if limit_value
220
255
  remaining = limit_value
221
256
  batch_limit = remaining if remaining < batch_limit
222
257
  end
223
258
 
224
- relation = relation.reorder(batch_order(order)).limit(batch_limit)
225
- relation = apply_limits(relation, start, finish, order)
226
- relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching
227
- batch_relation = relation
228
-
229
- loop do
230
- if load
231
- records = batch_relation.records
232
- ids = records.map(&:id)
233
- yielded_relation = where(primary_key => ids)
234
- yielded_relation.load_records(records)
235
- else
236
- ids = batch_relation.pluck(primary_key)
237
- yielded_relation = where(primary_key => ids)
238
- end
239
-
240
- break if ids.empty?
241
-
242
- primary_key_offset = ids.last
243
- raise ArgumentError.new("Primary key not included in the custom select clause") unless primary_key_offset
244
-
245
- yield yielded_relation
246
-
247
- break if ids.length < batch_limit
248
-
249
- if limit_value
250
- remaining -= ids.length
251
-
252
- if remaining == 0
253
- # Saves a useless iteration when the limit is a multiple of the
254
- # batch size.
255
- break
256
- elsif remaining < batch_limit
257
- relation = relation.limit(remaining)
258
- end
259
- end
260
-
261
- batch_relation = relation.where(
262
- predicate_builder[primary_key, primary_key_offset, order == :desc ? :lt : :gt]
259
+ if self.loaded?
260
+ batch_on_loaded_relation(
261
+ relation: self,
262
+ start: start,
263
+ finish: finish,
264
+ order: order,
265
+ batch_limit: batch_limit,
266
+ &block
267
+ )
268
+ else
269
+ batch_on_unloaded_relation(
270
+ relation: self,
271
+ start: start,
272
+ finish: finish,
273
+ load: load,
274
+ order: order,
275
+ use_ranges: use_ranges,
276
+ remaining: remaining,
277
+ batch_limit: batch_limit,
278
+ &block
263
279
  )
264
280
  end
265
281
  end
266
282
 
267
283
  private
268
- def apply_limits(relation, start, finish, order)
269
- relation = apply_start_limit(relation, start, order) if start
270
- relation = apply_finish_limit(relation, finish, order) if finish
284
+ def apply_limits(relation, start, finish, batch_orders)
285
+ relation = apply_start_limit(relation, start, batch_orders) if start
286
+ relation = apply_finish_limit(relation, finish, batch_orders) if finish
271
287
  relation
272
288
  end
273
289
 
274
- def apply_start_limit(relation, start, order)
275
- relation.where(predicate_builder[primary_key, start, order == :desc ? :lteq : :gteq])
290
+ def apply_start_limit(relation, start, batch_orders)
291
+ operators = batch_orders.map do |_column, order|
292
+ order == :desc ? :lteq : :gteq
293
+ end
294
+ batch_condition(relation, primary_key, start, operators)
295
+ end
296
+
297
+ def apply_finish_limit(relation, finish, batch_orders)
298
+ operators = batch_orders.map do |_column, order|
299
+ order == :desc ? :gteq : :lteq
300
+ end
301
+ batch_condition(relation, primary_key, finish, operators)
276
302
  end
277
303
 
278
- def apply_finish_limit(relation, finish, order)
279
- relation.where(predicate_builder[primary_key, finish, order == :desc ? :gteq : :lteq])
304
+ def batch_condition(relation, columns, values, operators)
305
+ cursor_positions = Array(columns).zip(Array(values), operators)
306
+
307
+ first_clause_column, first_clause_value, operator = cursor_positions.pop
308
+ where_clause = predicate_builder[first_clause_column, first_clause_value, operator]
309
+
310
+ cursor_positions.reverse_each do |column_name, value, operator|
311
+ where_clause = predicate_builder[column_name, value, operator == :lteq ? :lt : :gt].or(
312
+ predicate_builder[column_name, value, :eq].and(where_clause)
313
+ )
314
+ end
315
+
316
+ relation.where(where_clause)
280
317
  end
281
318
 
282
- def batch_order(order)
283
- table[primary_key].public_send(order)
319
+ def build_batch_orders(order)
320
+ get_the_order_of_primary_key(order).map do |column, ord|
321
+ [column, ord || DEFAULT_ORDER]
322
+ end
284
323
  end
285
324
 
286
325
  def act_on_ignored_order(error_on_ignore)
@@ -292,5 +331,95 @@ module ActiveRecord
292
331
  logger.warn(ORDER_IGNORE_MESSAGE)
293
332
  end
294
333
  end
334
+
335
+ def get_the_order_of_primary_key(order)
336
+ Array(primary_key).zip(Array(order))
337
+ end
338
+
339
+ def batch_on_loaded_relation(relation:, start:, finish:, order:, batch_limit:)
340
+ records = relation.to_a
341
+
342
+ if start || finish
343
+ records = records.filter do |record|
344
+ (start.nil? || record.id >= start) && (finish.nil? || record.id <= finish)
345
+ end
346
+ end
347
+
348
+ records = records.sort_by { |record| record.id }
349
+
350
+ if order == :desc
351
+ records.reverse!
352
+ end
353
+
354
+ (0...records.size).step(batch_limit).each do |start|
355
+ subrelation = relation.spawn
356
+ subrelation.load_records(records[start, batch_limit])
357
+
358
+ yield subrelation
359
+ end
360
+
361
+ nil
362
+ end
363
+
364
+ def batch_on_unloaded_relation(relation:, start:, finish:, load:, order:, use_ranges:, remaining:, batch_limit:)
365
+ batch_orders = build_batch_orders(order)
366
+ relation = relation.reorder(batch_orders.to_h).limit(batch_limit)
367
+ relation = apply_limits(relation, start, finish, batch_orders)
368
+ relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching
369
+ batch_relation = relation
370
+ empty_scope = to_sql == klass.unscoped.all.to_sql
371
+
372
+ loop do
373
+ if load
374
+ records = batch_relation.records
375
+ ids = records.map(&:id)
376
+ yielded_relation = where(primary_key => ids)
377
+ yielded_relation.load_records(records)
378
+ elsif (empty_scope && use_ranges != false) || use_ranges
379
+ ids = batch_relation.ids
380
+ finish = ids.last
381
+ if finish
382
+ yielded_relation = apply_finish_limit(batch_relation, finish, batch_orders)
383
+ yielded_relation = yielded_relation.except(:limit, :order)
384
+ yielded_relation.skip_query_cache!(false)
385
+ end
386
+ else
387
+ ids = batch_relation.ids
388
+ yielded_relation = where(primary_key => ids)
389
+ end
390
+
391
+ break if ids.empty?
392
+
393
+ primary_key_offset = ids.last
394
+ raise ArgumentError.new("Primary key not included in the custom select clause") unless primary_key_offset
395
+
396
+ yield yielded_relation
397
+
398
+ break if ids.length < batch_limit
399
+
400
+ if limit_value
401
+ remaining -= ids.length
402
+
403
+ if remaining == 0
404
+ # Saves a useless iteration when the limit is a multiple of the
405
+ # batch size.
406
+ break
407
+ elsif remaining < batch_limit
408
+ relation = relation.limit(remaining)
409
+ end
410
+ end
411
+
412
+ batch_orders_copy = batch_orders.dup
413
+ _last_column, last_order = batch_orders_copy.pop
414
+ operators = batch_orders_copy.map do |_column, order|
415
+ order == :desc ? :lteq : :gteq
416
+ end
417
+ operators << (last_order == :desc ? :lt : :gt)
418
+
419
+ batch_relation = batch_condition(relation, primary_key, primary_key_offset, operators)
420
+ end
421
+
422
+ nil
423
+ end
295
424
  end
296
425
  end