activerecord 5.2.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (244) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +937 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +217 -0
  5. data/examples/performance.rb +185 -0
  6. data/examples/simple.rb +15 -0
  7. data/lib/active_record.rb +188 -0
  8. data/lib/active_record/aggregations.rb +283 -0
  9. data/lib/active_record/association_relation.rb +40 -0
  10. data/lib/active_record/associations.rb +1860 -0
  11. data/lib/active_record/associations/alias_tracker.rb +81 -0
  12. data/lib/active_record/associations/association.rb +299 -0
  13. data/lib/active_record/associations/association_scope.rb +168 -0
  14. data/lib/active_record/associations/belongs_to_association.rb +130 -0
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +40 -0
  16. data/lib/active_record/associations/builder/association.rb +140 -0
  17. data/lib/active_record/associations/builder/belongs_to.rb +163 -0
  18. data/lib/active_record/associations/builder/collection_association.rb +82 -0
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +135 -0
  20. data/lib/active_record/associations/builder/has_many.rb +17 -0
  21. data/lib/active_record/associations/builder/has_one.rb +30 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +42 -0
  23. data/lib/active_record/associations/collection_association.rb +513 -0
  24. data/lib/active_record/associations/collection_proxy.rb +1131 -0
  25. data/lib/active_record/associations/foreign_association.rb +13 -0
  26. data/lib/active_record/associations/has_many_association.rb +144 -0
  27. data/lib/active_record/associations/has_many_through_association.rb +227 -0
  28. data/lib/active_record/associations/has_one_association.rb +120 -0
  29. data/lib/active_record/associations/has_one_through_association.rb +45 -0
  30. data/lib/active_record/associations/join_dependency.rb +262 -0
  31. data/lib/active_record/associations/join_dependency/join_association.rb +60 -0
  32. data/lib/active_record/associations/join_dependency/join_base.rb +23 -0
  33. data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
  34. data/lib/active_record/associations/preloader.rb +193 -0
  35. data/lib/active_record/associations/preloader/association.rb +131 -0
  36. data/lib/active_record/associations/preloader/through_association.rb +107 -0
  37. data/lib/active_record/associations/singular_association.rb +73 -0
  38. data/lib/active_record/associations/through_association.rb +121 -0
  39. data/lib/active_record/attribute_assignment.rb +88 -0
  40. data/lib/active_record/attribute_decorators.rb +90 -0
  41. data/lib/active_record/attribute_methods.rb +492 -0
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +78 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +150 -0
  44. data/lib/active_record/attribute_methods/primary_key.rb +143 -0
  45. data/lib/active_record/attribute_methods/query.rb +42 -0
  46. data/lib/active_record/attribute_methods/read.rb +85 -0
  47. data/lib/active_record/attribute_methods/serialization.rb +90 -0
  48. data/lib/active_record/attribute_methods/time_zone_conversion.rb +91 -0
  49. data/lib/active_record/attribute_methods/write.rb +68 -0
  50. data/lib/active_record/attributes.rb +266 -0
  51. data/lib/active_record/autosave_association.rb +498 -0
  52. data/lib/active_record/base.rb +329 -0
  53. data/lib/active_record/callbacks.rb +353 -0
  54. data/lib/active_record/coders/json.rb +15 -0
  55. data/lib/active_record/coders/yaml_column.rb +50 -0
  56. data/lib/active_record/collection_cache_key.rb +53 -0
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +1068 -0
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +72 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +540 -0
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +145 -0
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +200 -0
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +685 -0
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1396 -0
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +628 -0
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +887 -0
  70. data/lib/active_record/connection_adapters/column.rb +91 -0
  71. data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
  72. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
  73. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
  80. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
  81. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
  82. data/lib/active_record/connection_adapters/mysql2_adapter.rb +129 -0
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
  85. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +111 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  109. data/lib/active_record/connection_adapters/postgresql/quoting.rb +168 -0
  110. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
  115. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
  116. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  117. data/lib/active_record/connection_adapters/postgresql_adapter.rb +863 -0
  118. data/lib/active_record/connection_adapters/schema_cache.rb +118 -0
  119. data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
  120. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  121. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  125. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +573 -0
  127. data/lib/active_record/connection_adapters/statement_pool.rb +61 -0
  128. data/lib/active_record/connection_handling.rb +145 -0
  129. data/lib/active_record/core.rb +559 -0
  130. data/lib/active_record/counter_cache.rb +218 -0
  131. data/lib/active_record/define_callbacks.rb +22 -0
  132. data/lib/active_record/dynamic_matchers.rb +122 -0
  133. data/lib/active_record/enum.rb +244 -0
  134. data/lib/active_record/errors.rb +380 -0
  135. data/lib/active_record/explain.rb +50 -0
  136. data/lib/active_record/explain_registry.rb +32 -0
  137. data/lib/active_record/explain_subscriber.rb +34 -0
  138. data/lib/active_record/fixture_set/file.rb +82 -0
  139. data/lib/active_record/fixtures.rb +1065 -0
  140. data/lib/active_record/gem_version.rb +17 -0
  141. data/lib/active_record/inheritance.rb +283 -0
  142. data/lib/active_record/integration.rb +155 -0
  143. data/lib/active_record/internal_metadata.rb +45 -0
  144. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  145. data/lib/active_record/locale/en.yml +48 -0
  146. data/lib/active_record/locking/optimistic.rb +198 -0
  147. data/lib/active_record/locking/pessimistic.rb +89 -0
  148. data/lib/active_record/log_subscriber.rb +137 -0
  149. data/lib/active_record/migration.rb +1378 -0
  150. data/lib/active_record/migration/command_recorder.rb +240 -0
  151. data/lib/active_record/migration/compatibility.rb +217 -0
  152. data/lib/active_record/migration/join_table.rb +17 -0
  153. data/lib/active_record/model_schema.rb +521 -0
  154. data/lib/active_record/nested_attributes.rb +600 -0
  155. data/lib/active_record/no_touching.rb +58 -0
  156. data/lib/active_record/null_relation.rb +68 -0
  157. data/lib/active_record/persistence.rb +763 -0
  158. data/lib/active_record/query_cache.rb +45 -0
  159. data/lib/active_record/querying.rb +70 -0
  160. data/lib/active_record/railtie.rb +226 -0
  161. data/lib/active_record/railties/console_sandbox.rb +7 -0
  162. data/lib/active_record/railties/controller_runtime.rb +56 -0
  163. data/lib/active_record/railties/databases.rake +377 -0
  164. data/lib/active_record/readonly_attributes.rb +24 -0
  165. data/lib/active_record/reflection.rb +1044 -0
  166. data/lib/active_record/relation.rb +629 -0
  167. data/lib/active_record/relation/batches.rb +287 -0
  168. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  169. data/lib/active_record/relation/calculations.rb +417 -0
  170. data/lib/active_record/relation/delegation.rb +147 -0
  171. data/lib/active_record/relation/finder_methods.rb +565 -0
  172. data/lib/active_record/relation/from_clause.rb +26 -0
  173. data/lib/active_record/relation/merger.rb +193 -0
  174. data/lib/active_record/relation/predicate_builder.rb +152 -0
  175. data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
  176. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  177. data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
  178. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
  179. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  180. data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
  181. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  182. data/lib/active_record/relation/query_attribute.rb +45 -0
  183. data/lib/active_record/relation/query_methods.rb +1231 -0
  184. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  185. data/lib/active_record/relation/spawn_methods.rb +77 -0
  186. data/lib/active_record/relation/where_clause.rb +186 -0
  187. data/lib/active_record/relation/where_clause_factory.rb +34 -0
  188. data/lib/active_record/result.rb +149 -0
  189. data/lib/active_record/runtime_registry.rb +24 -0
  190. data/lib/active_record/sanitization.rb +222 -0
  191. data/lib/active_record/schema.rb +70 -0
  192. data/lib/active_record/schema_dumper.rb +255 -0
  193. data/lib/active_record/schema_migration.rb +56 -0
  194. data/lib/active_record/scoping.rb +106 -0
  195. data/lib/active_record/scoping/default.rb +152 -0
  196. data/lib/active_record/scoping/named.rb +213 -0
  197. data/lib/active_record/secure_token.rb +40 -0
  198. data/lib/active_record/serialization.rb +22 -0
  199. data/lib/active_record/statement_cache.rb +121 -0
  200. data/lib/active_record/store.rb +211 -0
  201. data/lib/active_record/suppressor.rb +61 -0
  202. data/lib/active_record/table_metadata.rb +82 -0
  203. data/lib/active_record/tasks/database_tasks.rb +337 -0
  204. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  205. data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
  206. data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
  207. data/lib/active_record/timestamp.rb +153 -0
  208. data/lib/active_record/touch_later.rb +64 -0
  209. data/lib/active_record/transactions.rb +502 -0
  210. data/lib/active_record/translation.rb +24 -0
  211. data/lib/active_record/type.rb +79 -0
  212. data/lib/active_record/type/adapter_specific_registry.rb +136 -0
  213. data/lib/active_record/type/date.rb +9 -0
  214. data/lib/active_record/type/date_time.rb +9 -0
  215. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  216. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  217. data/lib/active_record/type/internal/timezone.rb +17 -0
  218. data/lib/active_record/type/json.rb +30 -0
  219. data/lib/active_record/type/serialized.rb +71 -0
  220. data/lib/active_record/type/text.rb +11 -0
  221. data/lib/active_record/type/time.rb +21 -0
  222. data/lib/active_record/type/type_map.rb +62 -0
  223. data/lib/active_record/type/unsigned_integer.rb +17 -0
  224. data/lib/active_record/type_caster.rb +9 -0
  225. data/lib/active_record/type_caster/connection.rb +33 -0
  226. data/lib/active_record/type_caster/map.rb +23 -0
  227. data/lib/active_record/validations.rb +93 -0
  228. data/lib/active_record/validations/absence.rb +25 -0
  229. data/lib/active_record/validations/associated.rb +60 -0
  230. data/lib/active_record/validations/length.rb +26 -0
  231. data/lib/active_record/validations/presence.rb +68 -0
  232. data/lib/active_record/validations/uniqueness.rb +238 -0
  233. data/lib/active_record/version.rb +10 -0
  234. data/lib/rails/generators/active_record.rb +19 -0
  235. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  236. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  237. data/lib/rails/generators/active_record/migration.rb +35 -0
  238. data/lib/rails/generators/active_record/migration/migration_generator.rb +78 -0
  239. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  240. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +46 -0
  241. data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
  242. data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
  243. data/lib/rails/generators/active_record/model/templates/module.rb.tt +7 -0
  244. metadata +333 -0
@@ -0,0 +1,287 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/relation/batches/batch_enumerator"
4
+
5
+ module ActiveRecord
6
+ module Batches
7
+ ORDER_IGNORE_MESSAGE = "Scoped order is ignored, it's forced to be batch order."
8
+
9
+ # Looping through a collection of records from the database
10
+ # (using the Scoping::Named::ClassMethods.all method, for example)
11
+ # is very inefficient since it will try to instantiate all the objects at once.
12
+ #
13
+ # In that case, batch processing methods allow you to work
14
+ # with the records in batches, thereby greatly reducing memory consumption.
15
+ #
16
+ # The #find_each method uses #find_in_batches with a batch size of 1000 (or as
17
+ # specified by the +:batch_size+ option).
18
+ #
19
+ # Person.find_each do |person|
20
+ # person.do_awesome_stuff
21
+ # end
22
+ #
23
+ # Person.where("age > 21").find_each do |person|
24
+ # person.party_all_night!
25
+ # end
26
+ #
27
+ # If you do not provide a block to #find_each, it will return an Enumerator
28
+ # for chaining with other methods:
29
+ #
30
+ # Person.find_each.with_index do |person, index|
31
+ # person.award_trophy(index + 1)
32
+ # end
33
+ #
34
+ # ==== Options
35
+ # * <tt>:batch_size</tt> - Specifies the size of the batch. Defaults to 1000.
36
+ # * <tt>:start</tt> - Specifies the primary key value to start from, inclusive of the value.
37
+ # * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
38
+ # * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
39
+ # an order is present in the relation.
40
+ #
41
+ # Limits are honored, and if present there is no requirement for the batch
42
+ # size: it can be less than, equal to, or greater than the limit.
43
+ #
44
+ # The options +start+ and +finish+ are especially useful if you want
45
+ # multiple workers dealing with the same processing queue. You can make
46
+ # worker 1 handle all the records between id 1 and 9999 and worker 2
47
+ # handle from 10000 and beyond by setting the +:start+ and +:finish+
48
+ # option on each worker.
49
+ #
50
+ # # In worker 1, let's process until 9999 records.
51
+ # Person.find_each(finish: 9_999) do |person|
52
+ # person.party_all_night!
53
+ # end
54
+ #
55
+ # # In worker 2, let's process from record 10_000 and onwards.
56
+ # Person.find_each(start: 10_000) do |person|
57
+ # person.party_all_night!
58
+ # end
59
+ #
60
+ # NOTE: It's not possible to set the order. That is automatically set to
61
+ # ascending on the primary key ("id ASC") to make the batch ordering
62
+ # work. This also means that this method only works when the primary key is
63
+ # orderable (e.g. an integer or string).
64
+ #
65
+ # NOTE: By its nature, batch processing is subject to race conditions if
66
+ # other processes are modifying the database.
67
+ def find_each(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil)
68
+ if block_given?
69
+ find_in_batches(start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore) do |records|
70
+ records.each { |record| yield record }
71
+ end
72
+ else
73
+ enum_for(:find_each, start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore) do
74
+ relation = self
75
+ apply_limits(relation, start, finish).size
76
+ end
77
+ end
78
+ end
79
+
80
+ # Yields each batch of records that was found by the find options as
81
+ # an array.
82
+ #
83
+ # Person.where("age > 21").find_in_batches do |group|
84
+ # sleep(50) # Make sure it doesn't get too crowded in there!
85
+ # group.each { |person| person.party_all_night! }
86
+ # end
87
+ #
88
+ # If you do not provide a block to #find_in_batches, it will return an Enumerator
89
+ # for chaining with other methods:
90
+ #
91
+ # Person.find_in_batches.with_index do |group, batch|
92
+ # puts "Processing group ##{batch}"
93
+ # group.each(&:recover_from_last_night!)
94
+ # end
95
+ #
96
+ # To be yielded each record one by one, use #find_each instead.
97
+ #
98
+ # ==== Options
99
+ # * <tt>:batch_size</tt> - Specifies the size of the batch. Defaults to 1000.
100
+ # * <tt>:start</tt> - Specifies the primary key value to start from, inclusive of the value.
101
+ # * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
102
+ # * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
103
+ # an order is present in the relation.
104
+ #
105
+ # Limits are honored, and if present there is no requirement for the batch
106
+ # size: it can be less than, equal to, or greater than the limit.
107
+ #
108
+ # The options +start+ and +finish+ are especially useful if you want
109
+ # multiple workers dealing with the same processing queue. You can make
110
+ # worker 1 handle all the records between id 1 and 9999 and worker 2
111
+ # handle from 10000 and beyond by setting the +:start+ and +:finish+
112
+ # option on each worker.
113
+ #
114
+ # # Let's process from record 10_000 on.
115
+ # Person.find_in_batches(start: 10_000) do |group|
116
+ # group.each { |person| person.party_all_night! }
117
+ # end
118
+ #
119
+ # NOTE: It's not possible to set the order. That is automatically set to
120
+ # ascending on the primary key ("id ASC") to make the batch ordering
121
+ # work. This also means that this method only works when the primary key is
122
+ # orderable (e.g. an integer or string).
123
+ #
124
+ # NOTE: By its nature, batch processing is subject to race conditions if
125
+ # other processes are modifying the database.
126
+ def find_in_batches(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil)
127
+ relation = self
128
+ unless block_given?
129
+ return to_enum(:find_in_batches, start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore) do
130
+ total = apply_limits(relation, start, finish).size
131
+ (total - 1).div(batch_size) + 1
132
+ end
133
+ end
134
+
135
+ in_batches(of: batch_size, start: start, finish: finish, load: true, error_on_ignore: error_on_ignore) do |batch|
136
+ yield batch.to_a
137
+ end
138
+ end
139
+
140
+ # Yields ActiveRecord::Relation objects to work with a batch of records.
141
+ #
142
+ # Person.where("age > 21").in_batches do |relation|
143
+ # relation.delete_all
144
+ # sleep(10) # Throttle the delete queries
145
+ # end
146
+ #
147
+ # If you do not provide a block to #in_batches, it will return a
148
+ # BatchEnumerator which is enumerable.
149
+ #
150
+ # Person.in_batches.each_with_index do |relation, batch_index|
151
+ # puts "Processing relation ##{batch_index}"
152
+ # relation.delete_all
153
+ # end
154
+ #
155
+ # Examples of calling methods on the returned BatchEnumerator object:
156
+ #
157
+ # Person.in_batches.delete_all
158
+ # Person.in_batches.update_all(awesome: true)
159
+ # Person.in_batches.each_record(&:party_all_night!)
160
+ #
161
+ # ==== Options
162
+ # * <tt>:of</tt> - Specifies the size of the batch. Defaults to 1000.
163
+ # * <tt>:load</tt> - Specifies if the relation should be loaded. Defaults to false.
164
+ # * <tt>:start</tt> - Specifies the primary key value to start from, inclusive of the value.
165
+ # * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
166
+ # * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
167
+ # an order is present in the relation.
168
+ #
169
+ # Limits are honored, and if present there is no requirement for the batch
170
+ # size, it can be less than, equal, or greater than the limit.
171
+ #
172
+ # The options +start+ and +finish+ are especially useful if you want
173
+ # multiple workers dealing with the same processing queue. You can make
174
+ # worker 1 handle all the records between id 1 and 9999 and worker 2
175
+ # handle from 10000 and beyond by setting the +:start+ and +:finish+
176
+ # option on each worker.
177
+ #
178
+ # # Let's process from record 10_000 on.
179
+ # Person.in_batches(start: 10_000).update_all(awesome: true)
180
+ #
181
+ # An example of calling where query method on the relation:
182
+ #
183
+ # Person.in_batches.each do |relation|
184
+ # relation.update_all('age = age + 1')
185
+ # relation.where('age > 21').update_all(should_party: true)
186
+ # relation.where('age <= 21').delete_all
187
+ # end
188
+ #
189
+ # NOTE: If you are going to iterate through each record, you should call
190
+ # #each_record on the yielded BatchEnumerator:
191
+ #
192
+ # Person.in_batches.each_record(&:party_all_night!)
193
+ #
194
+ # NOTE: It's not possible to set the order. That is automatically set to
195
+ # ascending on the primary key ("id ASC") to make the batch ordering
196
+ # consistent. Therefore the primary key must be orderable, e.g. an integer
197
+ # or a string.
198
+ #
199
+ # NOTE: By its nature, batch processing is subject to race conditions if
200
+ # other processes are modifying the database.
201
+ def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
202
+ relation = self
203
+ unless block_given?
204
+ return BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
205
+ end
206
+
207
+ if arel.orders.present?
208
+ act_on_ignored_order(error_on_ignore)
209
+ end
210
+
211
+ batch_limit = of
212
+ if limit_value
213
+ remaining = limit_value
214
+ batch_limit = remaining if remaining < batch_limit
215
+ end
216
+
217
+ relation = relation.reorder(batch_order).limit(batch_limit)
218
+ relation = apply_limits(relation, start, finish)
219
+ relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching
220
+ batch_relation = relation
221
+
222
+ loop do
223
+ if load
224
+ records = batch_relation.records
225
+ ids = records.map(&:id)
226
+ yielded_relation = where(primary_key => ids)
227
+ yielded_relation.load_records(records)
228
+ else
229
+ ids = batch_relation.pluck(primary_key)
230
+ yielded_relation = where(primary_key => ids)
231
+ end
232
+
233
+ break if ids.empty?
234
+
235
+ primary_key_offset = ids.last
236
+ raise ArgumentError.new("Primary key not included in the custom select clause") unless primary_key_offset
237
+
238
+ yield yielded_relation
239
+
240
+ break if ids.length < batch_limit
241
+
242
+ if limit_value
243
+ remaining -= ids.length
244
+
245
+ if remaining == 0
246
+ # Saves a useless iteration when the limit is a multiple of the
247
+ # batch size.
248
+ break
249
+ elsif remaining < batch_limit
250
+ relation = relation.limit(remaining)
251
+ end
252
+ end
253
+
254
+ attr = Relation::QueryAttribute.new(primary_key, primary_key_offset, klass.type_for_attribute(primary_key))
255
+ batch_relation = relation.where(arel_attribute(primary_key).gt(Arel::Nodes::BindParam.new(attr)))
256
+ end
257
+ end
258
+
259
+ private
260
+
261
+ def apply_limits(relation, start, finish)
262
+ if start
263
+ attr = Relation::QueryAttribute.new(primary_key, start, klass.type_for_attribute(primary_key))
264
+ relation = relation.where(arel_attribute(primary_key).gteq(Arel::Nodes::BindParam.new(attr)))
265
+ end
266
+ if finish
267
+ attr = Relation::QueryAttribute.new(primary_key, finish, klass.type_for_attribute(primary_key))
268
+ relation = relation.where(arel_attribute(primary_key).lteq(Arel::Nodes::BindParam.new(attr)))
269
+ end
270
+ relation
271
+ end
272
+
273
+ def batch_order
274
+ arel_attribute(primary_key).asc
275
+ end
276
+
277
+ def act_on_ignored_order(error_on_ignore)
278
+ raise_error = (error_on_ignore.nil? ? klass.error_on_ignored_order : error_on_ignore)
279
+
280
+ if raise_error
281
+ raise ArgumentError.new(ORDER_IGNORE_MESSAGE)
282
+ elsif logger
283
+ logger.warn(ORDER_IGNORE_MESSAGE)
284
+ end
285
+ end
286
+ end
287
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Batches
5
+ class BatchEnumerator
6
+ include Enumerable
7
+
8
+ def initialize(of: 1000, start: nil, finish: nil, relation:) #:nodoc:
9
+ @of = of
10
+ @relation = relation
11
+ @start = start
12
+ @finish = finish
13
+ end
14
+
15
+ # Looping through a collection of records from the database (using the
16
+ # +all+ method, for example) is very inefficient since it will try to
17
+ # instantiate all the objects at once.
18
+ #
19
+ # In that case, batch processing methods allow you to work with the
20
+ # records in batches, thereby greatly reducing memory consumption.
21
+ #
22
+ # Person.in_batches.each_record do |person|
23
+ # person.do_awesome_stuff
24
+ # end
25
+ #
26
+ # Person.where("age > 21").in_batches(of: 10).each_record do |person|
27
+ # person.party_all_night!
28
+ # end
29
+ #
30
+ # If you do not provide a block to #each_record, it will return an Enumerator
31
+ # for chaining with other methods:
32
+ #
33
+ # Person.in_batches.each_record.with_index do |person, index|
34
+ # person.award_trophy(index + 1)
35
+ # end
36
+ def each_record
37
+ return to_enum(:each_record) unless block_given?
38
+
39
+ @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: true).each do |relation|
40
+ relation.records.each { |record| yield record }
41
+ end
42
+ end
43
+
44
+ # Delegates #delete_all, #update_all, #destroy_all methods to each batch.
45
+ #
46
+ # People.in_batches.delete_all
47
+ # People.where('age < 10').in_batches.destroy_all
48
+ # People.in_batches.update_all('age = age + 1')
49
+ [:delete_all, :update_all, :destroy_all].each do |method|
50
+ define_method(method) do |*args, &block|
51
+ @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: false).each do |relation|
52
+ relation.send(method, *args, &block)
53
+ end
54
+ end
55
+ end
56
+
57
+ # Yields an ActiveRecord::Relation object for each batch of records.
58
+ #
59
+ # Person.in_batches.each do |relation|
60
+ # relation.update_all(awesome: true)
61
+ # end
62
+ def each
63
+ enum = @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: false)
64
+ return enum.each { |relation| yield relation } if block_given?
65
+ enum
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,417 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Calculations
5
+ # Count the records.
6
+ #
7
+ # Person.count
8
+ # # => the total count of all people
9
+ #
10
+ # Person.count(:age)
11
+ # # => returns the total count of all people whose age is present in database
12
+ #
13
+ # Person.count(:all)
14
+ # # => performs a COUNT(*) (:all is an alias for '*')
15
+ #
16
+ # Person.distinct.count(:age)
17
+ # # => counts the number of different age values
18
+ #
19
+ # If #count is used with {Relation#group}[rdoc-ref:QueryMethods#group],
20
+ # it returns a Hash whose keys represent the aggregated column,
21
+ # and the values are the respective amounts:
22
+ #
23
+ # Person.group(:city).count
24
+ # # => { 'Rome' => 5, 'Paris' => 3 }
25
+ #
26
+ # If #count is used with {Relation#group}[rdoc-ref:QueryMethods#group] for multiple columns, it returns a Hash whose
27
+ # keys are an array containing the individual values of each column and the value
28
+ # of each key would be the #count.
29
+ #
30
+ # Article.group(:status, :category).count
31
+ # # => {["draft", "business"]=>10, ["draft", "technology"]=>4,
32
+ # ["published", "business"]=>0, ["published", "technology"]=>2}
33
+ #
34
+ # If #count is used with {Relation#select}[rdoc-ref:QueryMethods#select], it will count the selected columns:
35
+ #
36
+ # Person.select(:age).count
37
+ # # => counts the number of different age values
38
+ #
39
+ # Note: not all valid {Relation#select}[rdoc-ref:QueryMethods#select] expressions are valid #count expressions. The specifics differ
40
+ # between databases. In invalid cases, an error from the database is thrown.
41
+ def count(column_name = nil)
42
+ if block_given?
43
+ unless column_name.nil?
44
+ ActiveSupport::Deprecation.warn \
45
+ "When `count' is called with a block, it ignores other arguments. " \
46
+ "This behavior is now deprecated and will result in an ArgumentError in Rails 6.0."
47
+ end
48
+
49
+ return super()
50
+ end
51
+
52
+ calculate(:count, column_name)
53
+ end
54
+
55
+ # Calculates the average value on a given column. Returns +nil+ if there's
56
+ # no row. See #calculate for examples with options.
57
+ #
58
+ # Person.average(:age) # => 35.8
59
+ def average(column_name)
60
+ calculate(:average, column_name)
61
+ end
62
+
63
+ # Calculates the minimum value on a given column. The value is returned
64
+ # with the same data type of the column, or +nil+ if there's no row. See
65
+ # #calculate for examples with options.
66
+ #
67
+ # Person.minimum(:age) # => 7
68
+ def minimum(column_name)
69
+ calculate(:minimum, column_name)
70
+ end
71
+
72
+ # Calculates the maximum value on a given column. The value is returned
73
+ # with the same data type of the column, or +nil+ if there's no row. See
74
+ # #calculate for examples with options.
75
+ #
76
+ # Person.maximum(:age) # => 93
77
+ def maximum(column_name)
78
+ calculate(:maximum, column_name)
79
+ end
80
+
81
+ # Calculates the sum of values on a given column. The value is returned
82
+ # with the same data type of the column, +0+ if there's no row. See
83
+ # #calculate for examples with options.
84
+ #
85
+ # Person.sum(:age) # => 4562
86
+ def sum(column_name = nil)
87
+ if block_given?
88
+ unless column_name.nil?
89
+ ActiveSupport::Deprecation.warn \
90
+ "When `sum' is called with a block, it ignores other arguments. " \
91
+ "This behavior is now deprecated and will result in an ArgumentError in Rails 6.0."
92
+ end
93
+
94
+ return super()
95
+ end
96
+
97
+ calculate(:sum, column_name)
98
+ end
99
+
100
+ # This calculates aggregate values in the given column. Methods for #count, #sum, #average,
101
+ # #minimum, and #maximum have been added as shortcuts.
102
+ #
103
+ # Person.calculate(:count, :all) # The same as Person.count
104
+ # Person.average(:age) # SELECT AVG(age) FROM people...
105
+ #
106
+ # # Selects the minimum age for any family without any minors
107
+ # Person.group(:last_name).having("min(age) > 17").minimum(:age)
108
+ #
109
+ # Person.sum("2 * age")
110
+ #
111
+ # There are two basic forms of output:
112
+ #
113
+ # * Single aggregate value: The single value is type cast to Integer for COUNT, Float
114
+ # for AVG, and the given column's type for everything else.
115
+ #
116
+ # * Grouped values: This returns an ordered hash of the values and groups them. It
117
+ # takes either a column name, or the name of a belongs_to association.
118
+ #
119
+ # values = Person.group('last_name').maximum(:age)
120
+ # puts values["Drake"]
121
+ # # => 43
122
+ #
123
+ # drake = Family.find_by(last_name: 'Drake')
124
+ # values = Person.group(:family).maximum(:age) # Person belongs_to :family
125
+ # puts values[drake]
126
+ # # => 43
127
+ #
128
+ # values.each do |family, max_age|
129
+ # ...
130
+ # end
131
+ def calculate(operation, column_name)
132
+ if has_include?(column_name)
133
+ relation = apply_join_dependency
134
+
135
+ if operation.to_s.downcase == "count"
136
+ relation.distinct!
137
+ # PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
138
+ if (column_name == :all || column_name.nil?) && select_values.empty?
139
+ relation.order_values = []
140
+ end
141
+ end
142
+
143
+ relation.calculate(operation, column_name)
144
+ else
145
+ perform_calculation(operation, column_name)
146
+ end
147
+ end
148
+
149
+ # Use #pluck as a shortcut to select one or more attributes without
150
+ # loading a bunch of records just to grab the attributes you want.
151
+ #
152
+ # Person.pluck(:name)
153
+ #
154
+ # instead of
155
+ #
156
+ # Person.all.map(&:name)
157
+ #
158
+ # Pluck returns an Array of attribute values type-casted to match
159
+ # the plucked column names, if they can be deduced. Plucking an SQL fragment
160
+ # returns String values by default.
161
+ #
162
+ # Person.pluck(:name)
163
+ # # SELECT people.name FROM people
164
+ # # => ['David', 'Jeremy', 'Jose']
165
+ #
166
+ # Person.pluck(:id, :name)
167
+ # # SELECT people.id, people.name FROM people
168
+ # # => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
169
+ #
170
+ # Person.distinct.pluck(:role)
171
+ # # SELECT DISTINCT role FROM people
172
+ # # => ['admin', 'member', 'guest']
173
+ #
174
+ # Person.where(age: 21).limit(5).pluck(:id)
175
+ # # SELECT people.id FROM people WHERE people.age = 21 LIMIT 5
176
+ # # => [2, 3]
177
+ #
178
+ # Person.pluck('DATEDIFF(updated_at, created_at)')
179
+ # # SELECT DATEDIFF(updated_at, created_at) FROM people
180
+ # # => ['0', '27761', '173']
181
+ #
182
+ # See also #ids.
183
+ #
184
+ def pluck(*column_names)
185
+ if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty?
186
+ return records.pluck(*column_names)
187
+ end
188
+
189
+ if has_include?(column_names.first)
190
+ relation = apply_join_dependency
191
+ relation.pluck(*column_names)
192
+ else
193
+ klass.enforce_raw_sql_whitelist(column_names)
194
+ relation = spawn
195
+ relation.select_values = column_names
196
+ result = skip_query_cache_if_necessary { klass.connection.select_all(relation.arel, nil) }
197
+ result.cast_values(klass.attribute_types)
198
+ end
199
+ end
200
+
201
+ # Pluck all the ID's for the relation using the table's primary key
202
+ #
203
+ # Person.ids # SELECT people.id FROM people
204
+ # Person.joins(:companies).ids # SELECT people.id FROM people INNER JOIN companies ON companies.person_id = people.id
205
+ def ids
206
+ pluck primary_key
207
+ end
208
+
209
+ private
210
+ def has_include?(column_name)
211
+ eager_loading? || (includes_values.present? && column_name && column_name != :all)
212
+ end
213
+
214
+ def perform_calculation(operation, column_name)
215
+ operation = operation.to_s.downcase
216
+
217
+ # If #count is used with #distinct (i.e. `relation.distinct.count`) it is
218
+ # considered distinct.
219
+ distinct = distinct_value
220
+
221
+ if operation == "count"
222
+ column_name ||= select_for_count
223
+ if column_name == :all
224
+ if !distinct
225
+ distinct = distinct_select?(select_for_count) if group_values.empty?
226
+ elsif group_values.any? || select_values.empty? && order_values.empty?
227
+ column_name = primary_key
228
+ end
229
+ elsif distinct_select?(column_name)
230
+ distinct = nil
231
+ end
232
+ end
233
+
234
+ if group_values.any?
235
+ execute_grouped_calculation(operation, column_name, distinct)
236
+ else
237
+ execute_simple_calculation(operation, column_name, distinct)
238
+ end
239
+ end
240
+
241
+ def distinct_select?(column_name)
242
+ column_name.is_a?(::String) && /\bDISTINCT[\s(]/i.match?(column_name)
243
+ end
244
+
245
+ def aggregate_column(column_name)
246
+ return column_name if Arel::Expressions === column_name
247
+
248
+ if @klass.has_attribute?(column_name) || @klass.attribute_alias?(column_name)
249
+ @klass.arel_attribute(column_name)
250
+ else
251
+ Arel.sql(column_name == :all ? "*" : column_name.to_s)
252
+ end
253
+ end
254
+
255
+ def operation_over_aggregate_column(column, operation, distinct)
256
+ operation == "count" ? column.count(distinct) : column.send(operation)
257
+ end
258
+
259
+ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
260
+ column_alias = column_name
261
+
262
+ if operation == "count" && (column_name == :all && distinct || has_limit_or_offset?)
263
+ # Shortcut when limit is zero.
264
+ return 0 if limit_value == 0
265
+
266
+ query_builder = build_count_subquery(spawn, column_name, distinct)
267
+ else
268
+ # PostgreSQL doesn't like ORDER BY when there are no GROUP BY
269
+ relation = unscope(:order).distinct!(false)
270
+
271
+ column = aggregate_column(column_name)
272
+
273
+ select_value = operation_over_aggregate_column(column, operation, distinct)
274
+ if operation == "sum" && distinct
275
+ select_value.distinct = true
276
+ end
277
+
278
+ column_alias = select_value.alias
279
+ column_alias ||= @klass.connection.column_name_for_operation(operation, select_value)
280
+ relation.select_values = [select_value]
281
+
282
+ query_builder = relation.arel
283
+ end
284
+
285
+ result = skip_query_cache_if_necessary { @klass.connection.select_all(query_builder, nil) }
286
+ row = result.first
287
+ value = row && row.values.first
288
+ type = result.column_types.fetch(column_alias) do
289
+ type_for(column_name)
290
+ end
291
+
292
+ type_cast_calculated_value(value, type, operation)
293
+ end
294
+
295
+ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
296
+ group_attrs = group_values
297
+
298
+ if group_attrs.first.respond_to?(:to_sym)
299
+ association = @klass._reflect_on_association(group_attrs.first)
300
+ associated = group_attrs.size == 1 && association && association.belongs_to? # only count belongs_to associations
301
+ group_fields = Array(associated ? association.foreign_key : group_attrs)
302
+ else
303
+ group_fields = group_attrs
304
+ end
305
+ group_fields = arel_columns(group_fields)
306
+
307
+ group_aliases = group_fields.map { |field| column_alias_for(field) }
308
+ group_columns = group_aliases.zip(group_fields)
309
+
310
+ if operation == "count" && column_name == :all
311
+ aggregate_alias = "count_all"
312
+ else
313
+ aggregate_alias = column_alias_for([operation, column_name].join(" "))
314
+ end
315
+
316
+ select_values = [
317
+ operation_over_aggregate_column(
318
+ aggregate_column(column_name),
319
+ operation,
320
+ distinct).as(aggregate_alias)
321
+ ]
322
+ select_values += self.select_values unless having_clause.empty?
323
+
324
+ select_values.concat group_columns.map { |aliaz, field|
325
+ if field.respond_to?(:as)
326
+ field.as(aliaz)
327
+ else
328
+ "#{field} AS #{aliaz}"
329
+ end
330
+ }
331
+
332
+ relation = except(:group).distinct!(false)
333
+ relation.group_values = group_fields
334
+ relation.select_values = select_values
335
+
336
+ calculated_data = skip_query_cache_if_necessary { @klass.connection.select_all(relation.arel, nil) }
337
+
338
+ if association
339
+ key_ids = calculated_data.collect { |row| row[group_aliases.first] }
340
+ key_records = association.klass.base_class.where(association.klass.base_class.primary_key => key_ids)
341
+ key_records = Hash[key_records.map { |r| [r.id, r] }]
342
+ end
343
+
344
+ Hash[calculated_data.map do |row|
345
+ key = group_columns.map { |aliaz, col_name|
346
+ type = type_for(col_name) do
347
+ calculated_data.column_types.fetch(aliaz, Type.default_value)
348
+ end
349
+ type_cast_calculated_value(row[aliaz], type)
350
+ }
351
+ key = key.first if key.size == 1
352
+ key = key_records[key] if associated
353
+
354
+ type = calculated_data.column_types.fetch(aggregate_alias) { type_for(column_name) }
355
+ [key, type_cast_calculated_value(row[aggregate_alias], type, operation)]
356
+ end]
357
+ end
358
+
359
+ # Converts the given keys to the value that the database adapter returns as
360
+ # a usable column name:
361
+ #
362
+ # column_alias_for("users.id") # => "users_id"
363
+ # column_alias_for("sum(id)") # => "sum_id"
364
+ # column_alias_for("count(distinct users.id)") # => "count_distinct_users_id"
365
+ # column_alias_for("count(*)") # => "count_all"
366
+ def column_alias_for(keys)
367
+ if keys.respond_to? :name
368
+ keys = "#{keys.relation.name}.#{keys.name}"
369
+ end
370
+
371
+ table_name = keys.to_s.downcase
372
+ table_name.gsub!(/\*/, "all")
373
+ table_name.gsub!(/\W+/, " ")
374
+ table_name.strip!
375
+ table_name.gsub!(/ +/, "_")
376
+
377
+ @klass.connection.table_alias_for(table_name)
378
+ end
379
+
380
+ def type_for(field, &block)
381
+ field_name = field.respond_to?(:name) ? field.name.to_s : field.to_s.split(".").last
382
+ @klass.type_for_attribute(field_name, &block)
383
+ end
384
+
385
+ def type_cast_calculated_value(value, type, operation = nil)
386
+ case operation
387
+ when "count" then value.to_i
388
+ when "sum" then type.deserialize(value || 0)
389
+ when "average" then value && value.respond_to?(:to_d) ? value.to_d : value
390
+ else type.deserialize(value)
391
+ end
392
+ end
393
+
394
+ def select_for_count
395
+ if select_values.present?
396
+ return select_values.first if select_values.one?
397
+ select_values.join(", ")
398
+ else
399
+ :all
400
+ end
401
+ end
402
+
403
+ def build_count_subquery(relation, column_name, distinct)
404
+ if column_name == :all
405
+ relation.select_values = [ Arel.sql(FinderMethods::ONE_AS_ONE) ] unless distinct
406
+ else
407
+ column_alias = Arel.sql("count_column")
408
+ relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
409
+ end
410
+
411
+ subquery = relation.arel.as(Arel.sql("subquery_for_count"))
412
+ select_value = operation_over_aggregate_column(column_alias || Arel.star, "count", false)
413
+
414
+ Arel::SelectManager.new(subquery).project(select_value)
415
+ end
416
+ end
417
+ end