activerecord 4.2.9 → 6.1.4.1

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 (374) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +964 -1382
  3. data/MIT-LICENSE +4 -2
  4. data/README.rdoc +15 -14
  5. data/examples/performance.rb +33 -32
  6. data/examples/simple.rb +5 -4
  7. data/lib/active_record/aggregations.rb +266 -251
  8. data/lib/active_record/association_relation.rb +40 -15
  9. data/lib/active_record/associations/alias_tracker.rb +40 -43
  10. data/lib/active_record/associations/association.rb +162 -69
  11. data/lib/active_record/associations/association_scope.rb +105 -130
  12. data/lib/active_record/associations/belongs_to_association.rb +83 -65
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +13 -12
  14. data/lib/active_record/associations/builder/association.rb +57 -43
  15. data/lib/active_record/associations/builder/belongs_to.rb +74 -57
  16. data/lib/active_record/associations/builder/collection_association.rb +15 -37
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +49 -66
  18. data/lib/active_record/associations/builder/has_many.rb +13 -5
  19. data/lib/active_record/associations/builder/has_one.rb +44 -6
  20. data/lib/active_record/associations/builder/singular_association.rb +16 -10
  21. data/lib/active_record/associations/collection_association.rb +148 -287
  22. data/lib/active_record/associations/collection_proxy.rb +252 -150
  23. data/lib/active_record/associations/foreign_association.rb +23 -1
  24. data/lib/active_record/associations/has_many_association.rb +56 -98
  25. data/lib/active_record/associations/has_many_through_association.rb +68 -89
  26. data/lib/active_record/associations/has_one_association.rb +73 -47
  27. data/lib/active_record/associations/has_one_through_association.rb +20 -11
  28. data/lib/active_record/associations/join_dependency/join_association.rb +54 -81
  29. data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
  30. data/lib/active_record/associations/join_dependency/join_part.rb +14 -14
  31. data/lib/active_record/associations/join_dependency.rb +174 -169
  32. data/lib/active_record/associations/preloader/association.rb +108 -115
  33. data/lib/active_record/associations/preloader/through_association.rb +85 -65
  34. data/lib/active_record/associations/preloader.rb +97 -94
  35. data/lib/active_record/associations/singular_association.rb +18 -39
  36. data/lib/active_record/associations/through_association.rb +39 -19
  37. data/lib/active_record/associations.rb +1845 -1598
  38. data/lib/active_record/attribute_assignment.rb +59 -185
  39. data/lib/active_record/attribute_methods/before_type_cast.rb +18 -10
  40. data/lib/active_record/attribute_methods/dirty.rb +168 -148
  41. data/lib/active_record/attribute_methods/primary_key.rb +93 -83
  42. data/lib/active_record/attribute_methods/query.rb +8 -10
  43. data/lib/active_record/attribute_methods/read.rb +19 -79
  44. data/lib/active_record/attribute_methods/serialization.rb +49 -24
  45. data/lib/active_record/attribute_methods/time_zone_conversion.rb +55 -36
  46. data/lib/active_record/attribute_methods/write.rb +24 -55
  47. data/lib/active_record/attribute_methods.rb +149 -154
  48. data/lib/active_record/attributes.rb +234 -78
  49. data/lib/active_record/autosave_association.rb +133 -60
  50. data/lib/active_record/base.rb +46 -46
  51. data/lib/active_record/callbacks.rb +234 -79
  52. data/lib/active_record/coders/json.rb +3 -1
  53. data/lib/active_record/coders/yaml_column.rb +34 -13
  54. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +887 -323
  55. data/lib/active_record/connection_adapters/abstract/database_limits.rb +17 -41
  56. data/lib/active_record/connection_adapters/abstract/database_statements.rb +292 -124
  57. data/lib/active_record/connection_adapters/abstract/query_cache.rb +78 -24
  58. data/lib/active_record/connection_adapters/abstract/quoting.rb +177 -60
  59. data/lib/active_record/connection_adapters/abstract/savepoints.rb +8 -6
  60. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +157 -93
  61. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +473 -255
  62. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +79 -36
  63. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +869 -286
  64. data/lib/active_record/connection_adapters/abstract/transaction.rb +257 -91
  65. data/lib/active_record/connection_adapters/abstract_adapter.rb +483 -230
  66. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +557 -640
  67. data/lib/active_record/connection_adapters/column.rb +67 -40
  68. data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
  69. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
  70. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  71. data/lib/active_record/connection_adapters/mysql/database_statements.rb +194 -0
  72. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +71 -0
  73. data/lib/active_record/connection_adapters/mysql/quoting.rb +96 -0
  74. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +97 -0
  75. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +103 -0
  76. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +91 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +268 -0
  78. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +40 -0
  79. data/lib/active_record/connection_adapters/mysql2_adapter.rb +80 -192
  80. data/lib/active_record/connection_adapters/pool_config.rb +73 -0
  81. data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +44 -11
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +75 -160
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +49 -58
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +9 -8
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +4 -2
  89. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +8 -6
  90. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +13 -1
  91. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +14 -19
  92. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  93. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -4
  94. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +31 -20
  95. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  98. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +44 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -9
  101. data/lib/active_record/connection_adapters/postgresql/oid/{infinity.rb → oid.rb} +5 -3
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +32 -11
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +70 -34
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -5
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +58 -54
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +18 -4
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +3 -1
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +3 -1
  109. data/lib/active_record/connection_adapters/postgresql/oid.rb +25 -25
  110. data/lib/active_record/connection_adapters/postgresql/quoting.rb +145 -48
  111. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +27 -14
  112. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +80 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +178 -108
  114. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +49 -0
  115. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +496 -298
  116. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +44 -0
  117. data/lib/active_record/connection_adapters/postgresql/utils.rb +11 -8
  118. data/lib/active_record/connection_adapters/postgresql_adapter.rb +588 -375
  119. data/lib/active_record/connection_adapters/schema_cache.rb +167 -29
  120. data/lib/active_record/connection_adapters/sql_type_metadata.rb +45 -0
  121. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +144 -0
  122. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  123. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +102 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +21 -0
  125. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  126. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  127. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +170 -0
  128. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +322 -373
  129. data/lib/active_record/connection_adapters/statement_pool.rb +33 -13
  130. data/lib/active_record/connection_adapters.rb +52 -0
  131. data/lib/active_record/connection_handling.rb +314 -41
  132. data/lib/active_record/core.rb +458 -241
  133. data/lib/active_record/counter_cache.rb +70 -49
  134. data/lib/active_record/database_configurations/connection_url_resolver.rb +98 -0
  135. data/lib/active_record/database_configurations/database_config.rb +80 -0
  136. data/lib/active_record/database_configurations/hash_config.rb +96 -0
  137. data/lib/active_record/database_configurations/url_config.rb +53 -0
  138. data/lib/active_record/database_configurations.rb +272 -0
  139. data/lib/active_record/delegated_type.rb +209 -0
  140. data/lib/active_record/destroy_association_async_job.rb +36 -0
  141. data/lib/active_record/dynamic_matchers.rb +87 -106
  142. data/lib/active_record/enum.rb +211 -92
  143. data/lib/active_record/errors.rb +224 -54
  144. data/lib/active_record/explain.rb +27 -11
  145. data/lib/active_record/explain_registry.rb +4 -2
  146. data/lib/active_record/explain_subscriber.rb +10 -5
  147. data/lib/active_record/fixture_set/file.rb +33 -14
  148. data/lib/active_record/fixture_set/model_metadata.rb +32 -0
  149. data/lib/active_record/fixture_set/render_context.rb +17 -0
  150. data/lib/active_record/fixture_set/table_row.rb +152 -0
  151. data/lib/active_record/fixture_set/table_rows.rb +46 -0
  152. data/lib/active_record/fixtures.rb +275 -500
  153. data/lib/active_record/gem_version.rb +6 -4
  154. data/lib/active_record/inheritance.rb +175 -110
  155. data/lib/active_record/insert_all.rb +212 -0
  156. data/lib/active_record/integration.rb +121 -29
  157. data/lib/active_record/internal_metadata.rb +62 -0
  158. data/lib/active_record/legacy_yaml_adapter.rb +27 -5
  159. data/lib/active_record/locale/en.yml +3 -2
  160. data/lib/active_record/locking/optimistic.rb +98 -92
  161. data/lib/active_record/locking/pessimistic.rb +22 -6
  162. data/lib/active_record/log_subscriber.rb +93 -31
  163. data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
  164. data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
  165. data/lib/active_record/middleware/database_selector.rb +77 -0
  166. data/lib/active_record/migration/command_recorder.rb +185 -90
  167. data/lib/active_record/migration/compatibility.rb +295 -0
  168. data/lib/active_record/migration/join_table.rb +8 -7
  169. data/lib/active_record/migration.rb +673 -325
  170. data/lib/active_record/model_schema.rb +418 -113
  171. data/lib/active_record/nested_attributes.rb +263 -224
  172. data/lib/active_record/no_touching.rb +15 -2
  173. data/lib/active_record/null_relation.rb +24 -38
  174. data/lib/active_record/persistence.rb +572 -136
  175. data/lib/active_record/query_cache.rb +29 -23
  176. data/lib/active_record/querying.rb +50 -31
  177. data/lib/active_record/railtie.rb +170 -51
  178. data/lib/active_record/railties/console_sandbox.rb +3 -3
  179. data/lib/active_record/railties/controller_runtime.rb +34 -33
  180. data/lib/active_record/railties/databases.rake +523 -199
  181. data/lib/active_record/readonly_attributes.rb +9 -4
  182. data/lib/active_record/reflection.rb +454 -291
  183. data/lib/active_record/relation/batches/batch_enumerator.rb +85 -0
  184. data/lib/active_record/relation/batches.rb +217 -59
  185. data/lib/active_record/relation/calculations.rb +324 -249
  186. data/lib/active_record/relation/delegation.rb +76 -84
  187. data/lib/active_record/relation/finder_methods.rb +316 -242
  188. data/lib/active_record/relation/from_clause.rb +30 -0
  189. data/lib/active_record/relation/merger.rb +95 -103
  190. data/lib/active_record/relation/predicate_builder/array_handler.rb +26 -26
  191. data/lib/active_record/relation/predicate_builder/association_query_value.rb +42 -0
  192. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
  193. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +57 -0
  194. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
  195. data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
  196. data/lib/active_record/relation/predicate_builder.rb +136 -122
  197. data/lib/active_record/relation/query_attribute.rb +50 -0
  198. data/lib/active_record/relation/query_methods.rb +757 -413
  199. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  200. data/lib/active_record/relation/spawn_methods.rb +18 -20
  201. data/lib/active_record/relation/where_clause.rb +239 -0
  202. data/lib/active_record/relation.rb +554 -343
  203. data/lib/active_record/result.rb +91 -47
  204. data/lib/active_record/runtime_registry.rb +6 -4
  205. data/lib/active_record/sanitization.rb +134 -122
  206. data/lib/active_record/schema.rb +21 -24
  207. data/lib/active_record/schema_dumper.rb +141 -92
  208. data/lib/active_record/schema_migration.rb +24 -23
  209. data/lib/active_record/scoping/default.rb +96 -83
  210. data/lib/active_record/scoping/named.rb +78 -36
  211. data/lib/active_record/scoping.rb +45 -27
  212. data/lib/active_record/secure_token.rb +48 -0
  213. data/lib/active_record/serialization.rb +8 -6
  214. data/lib/active_record/signed_id.rb +116 -0
  215. data/lib/active_record/statement_cache.rb +89 -36
  216. data/lib/active_record/store.rb +128 -43
  217. data/lib/active_record/suppressor.rb +61 -0
  218. data/lib/active_record/table_metadata.rb +81 -0
  219. data/lib/active_record/tasks/database_tasks.rb +364 -130
  220. data/lib/active_record/tasks/mysql_database_tasks.rb +67 -113
  221. data/lib/active_record/tasks/postgresql_database_tasks.rb +86 -49
  222. data/lib/active_record/tasks/sqlite_database_tasks.rb +44 -19
  223. data/lib/active_record/test_databases.rb +24 -0
  224. data/lib/active_record/test_fixtures.rb +287 -0
  225. data/lib/active_record/timestamp.rb +86 -43
  226. data/lib/active_record/touch_later.rb +65 -0
  227. data/lib/active_record/transactions.rb +182 -163
  228. data/lib/active_record/translation.rb +3 -1
  229. data/lib/active_record/type/adapter_specific_registry.rb +126 -0
  230. data/lib/active_record/type/date.rb +4 -45
  231. data/lib/active_record/type/date_time.rb +4 -49
  232. data/lib/active_record/type/decimal_without_scale.rb +6 -2
  233. data/lib/active_record/type/hash_lookup_type_map.rb +5 -4
  234. data/lib/active_record/type/internal/timezone.rb +17 -0
  235. data/lib/active_record/type/json.rb +30 -0
  236. data/lib/active_record/type/serialized.rb +27 -15
  237. data/lib/active_record/type/text.rb +2 -2
  238. data/lib/active_record/type/time.rb +21 -16
  239. data/lib/active_record/type/type_map.rb +16 -19
  240. data/lib/active_record/type/unsigned_integer.rb +9 -8
  241. data/lib/active_record/type.rb +84 -23
  242. data/lib/active_record/type_caster/connection.rb +33 -0
  243. data/lib/active_record/type_caster/map.rb +23 -0
  244. data/lib/active_record/type_caster.rb +9 -0
  245. data/lib/active_record/validations/absence.rb +25 -0
  246. data/lib/active_record/validations/associated.rb +12 -4
  247. data/lib/active_record/validations/length.rb +26 -0
  248. data/lib/active_record/validations/numericality.rb +35 -0
  249. data/lib/active_record/validations/presence.rb +14 -13
  250. data/lib/active_record/validations/uniqueness.rb +63 -56
  251. data/lib/active_record/validations.rb +39 -35
  252. data/lib/active_record/version.rb +3 -1
  253. data/lib/active_record.rb +42 -29
  254. data/lib/arel/alias_predication.rb +9 -0
  255. data/lib/arel/attributes/attribute.rb +41 -0
  256. data/lib/arel/collectors/bind.rb +29 -0
  257. data/lib/arel/collectors/composite.rb +39 -0
  258. data/lib/arel/collectors/plain_string.rb +20 -0
  259. data/lib/arel/collectors/sql_string.rb +27 -0
  260. data/lib/arel/collectors/substitute_binds.rb +35 -0
  261. data/lib/arel/crud.rb +42 -0
  262. data/lib/arel/delete_manager.rb +18 -0
  263. data/lib/arel/errors.rb +9 -0
  264. data/lib/arel/expressions.rb +29 -0
  265. data/lib/arel/factory_methods.rb +49 -0
  266. data/lib/arel/insert_manager.rb +49 -0
  267. data/lib/arel/math.rb +45 -0
  268. data/lib/arel/nodes/and.rb +32 -0
  269. data/lib/arel/nodes/ascending.rb +23 -0
  270. data/lib/arel/nodes/binary.rb +126 -0
  271. data/lib/arel/nodes/bind_param.rb +44 -0
  272. data/lib/arel/nodes/case.rb +55 -0
  273. data/lib/arel/nodes/casted.rb +62 -0
  274. data/lib/arel/nodes/comment.rb +29 -0
  275. data/lib/arel/nodes/count.rb +12 -0
  276. data/lib/arel/nodes/delete_statement.rb +45 -0
  277. data/lib/arel/nodes/descending.rb +23 -0
  278. data/lib/arel/nodes/equality.rb +15 -0
  279. data/lib/arel/nodes/extract.rb +24 -0
  280. data/lib/arel/nodes/false.rb +16 -0
  281. data/lib/arel/nodes/full_outer_join.rb +8 -0
  282. data/lib/arel/nodes/function.rb +44 -0
  283. data/lib/arel/nodes/grouping.rb +11 -0
  284. data/lib/arel/nodes/homogeneous_in.rb +76 -0
  285. data/lib/arel/nodes/in.rb +15 -0
  286. data/lib/arel/nodes/infix_operation.rb +92 -0
  287. data/lib/arel/nodes/inner_join.rb +8 -0
  288. data/lib/arel/nodes/insert_statement.rb +37 -0
  289. data/lib/arel/nodes/join_source.rb +20 -0
  290. data/lib/arel/nodes/matches.rb +18 -0
  291. data/lib/arel/nodes/named_function.rb +23 -0
  292. data/lib/arel/nodes/node.rb +51 -0
  293. data/lib/arel/nodes/node_expression.rb +13 -0
  294. data/lib/arel/nodes/ordering.rb +27 -0
  295. data/lib/arel/nodes/outer_join.rb +8 -0
  296. data/lib/arel/nodes/over.rb +15 -0
  297. data/lib/arel/nodes/regexp.rb +16 -0
  298. data/lib/arel/nodes/right_outer_join.rb +8 -0
  299. data/lib/arel/nodes/select_core.rb +67 -0
  300. data/lib/arel/nodes/select_statement.rb +41 -0
  301. data/lib/arel/nodes/sql_literal.rb +19 -0
  302. data/lib/arel/nodes/string_join.rb +11 -0
  303. data/lib/arel/nodes/table_alias.rb +31 -0
  304. data/lib/arel/nodes/terminal.rb +16 -0
  305. data/lib/arel/nodes/true.rb +16 -0
  306. data/lib/arel/nodes/unary.rb +44 -0
  307. data/lib/arel/nodes/unary_operation.rb +20 -0
  308. data/lib/arel/nodes/unqualified_column.rb +22 -0
  309. data/lib/arel/nodes/update_statement.rb +41 -0
  310. data/lib/arel/nodes/values_list.rb +9 -0
  311. data/lib/arel/nodes/window.rb +126 -0
  312. data/lib/arel/nodes/with.rb +11 -0
  313. data/lib/arel/nodes.rb +70 -0
  314. data/lib/arel/order_predications.rb +13 -0
  315. data/lib/arel/predications.rb +250 -0
  316. data/lib/arel/select_manager.rb +270 -0
  317. data/lib/arel/table.rb +118 -0
  318. data/lib/arel/tree_manager.rb +72 -0
  319. data/lib/arel/update_manager.rb +34 -0
  320. data/lib/arel/visitors/dot.rb +308 -0
  321. data/lib/arel/visitors/mysql.rb +93 -0
  322. data/lib/arel/visitors/postgresql.rb +120 -0
  323. data/lib/arel/visitors/sqlite.rb +38 -0
  324. data/lib/arel/visitors/to_sql.rb +899 -0
  325. data/lib/arel/visitors/visitor.rb +45 -0
  326. data/lib/arel/visitors.rb +13 -0
  327. data/lib/arel/window_predications.rb +9 -0
  328. data/lib/arel.rb +54 -0
  329. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +26 -0
  330. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  331. data/lib/rails/generators/active_record/migration/migration_generator.rb +43 -37
  332. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +26 -0
  333. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +13 -4
  334. data/lib/rails/generators/active_record/migration.rb +35 -1
  335. data/lib/rails/generators/active_record/model/model_generator.rb +55 -22
  336. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
  337. data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
  338. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  339. data/lib/rails/generators/active_record.rb +7 -5
  340. metadata +172 -65
  341. data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
  342. data/lib/active_record/associations/preloader/collection_association.rb +0 -24
  343. data/lib/active_record/associations/preloader/has_many.rb +0 -17
  344. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  345. data/lib/active_record/associations/preloader/has_one.rb +0 -23
  346. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  347. data/lib/active_record/associations/preloader/singular_association.rb +0 -21
  348. data/lib/active_record/attribute.rb +0 -163
  349. data/lib/active_record/attribute_decorators.rb +0 -66
  350. data/lib/active_record/attribute_set/builder.rb +0 -106
  351. data/lib/active_record/attribute_set.rb +0 -81
  352. data/lib/active_record/connection_adapters/connection_specification.rb +0 -275
  353. data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -491
  354. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
  355. data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
  356. data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +0 -11
  357. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -35
  358. data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
  359. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  360. data/lib/active_record/serializers/xml_serializer.rb +0 -193
  361. data/lib/active_record/type/big_integer.rb +0 -13
  362. data/lib/active_record/type/binary.rb +0 -50
  363. data/lib/active_record/type/boolean.rb +0 -31
  364. data/lib/active_record/type/decimal.rb +0 -64
  365. data/lib/active_record/type/decorator.rb +0 -14
  366. data/lib/active_record/type/float.rb +0 -19
  367. data/lib/active_record/type/integer.rb +0 -59
  368. data/lib/active_record/type/mutable.rb +0 -16
  369. data/lib/active_record/type/numeric.rb +0 -36
  370. data/lib/active_record/type/string.rb +0 -40
  371. data/lib/active_record/type/time_value.rb +0 -38
  372. data/lib/active_record/type/value.rb +0 -110
  373. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +0 -19
  374. data/lib/rails/generators/active_record/model/templates/model.rb +0 -10
@@ -1,113 +1,63 @@
1
- # -*- coding: utf-8 -*-
2
- require 'arel/collectors/bind'
1
+ # frozen_string_literal: true
3
2
 
4
3
  module ActiveRecord
5
- # = Active Record Relation
4
+ # = Active Record \Relation
6
5
  class Relation
7
6
  MULTI_VALUE_METHODS = [:includes, :eager_load, :preload, :select, :group,
8
- :order, :joins, :where, :having, :bind, :references,
9
- :extending, :unscope]
7
+ :order, :joins, :left_outer_joins, :references,
8
+ :extending, :unscope, :optimizer_hints, :annotate]
10
9
 
11
- SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :from, :reordering,
12
- :reverse_order, :distinct, :create_with, :uniq]
13
- INVALID_METHODS_FOR_DELETE_ALL = [:limit, :distinct, :offset, :group, :having]
10
+ SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :reordering, :strict_loading,
11
+ :reverse_order, :distinct, :create_with, :skip_query_cache]
14
12
 
15
- VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS
13
+ CLAUSE_METHODS = [:where, :having, :from]
14
+ INVALID_METHODS_FOR_DELETE_ALL = [:distinct, :group, :having]
16
15
 
16
+ VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS + CLAUSE_METHODS
17
+
18
+ include Enumerable
17
19
  include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation
18
20
 
19
- attr_reader :table, :klass, :loaded
21
+ attr_reader :table, :klass, :loaded, :predicate_builder
22
+ attr_accessor :skip_preloading_value
20
23
  alias :model :klass
21
24
  alias :loaded? :loaded
25
+ alias :locked? :lock_value
22
26
 
23
- def initialize(klass, table, values = {})
27
+ def initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {})
24
28
  @klass = klass
25
29
  @table = table
26
30
  @values = values
27
- @offsets = {}
28
31
  @loaded = false
32
+ @predicate_builder = predicate_builder
33
+ @delegate_to_klass = false
29
34
  end
30
35
 
31
36
  def initialize_copy(other)
32
- # This method is a hot spot, so for now, use Hash[] to dup the hash.
33
- # https://bugs.ruby-lang.org/issues/7166
34
- @values = Hash[@values]
35
- @values[:bind] = @values[:bind].dup if @values.key? :bind
37
+ @values = @values.dup
36
38
  reset
37
39
  end
38
40
 
39
- def insert(values) # :nodoc:
40
- primary_key_value = nil
41
-
42
- if primary_key && Hash === values
43
- primary_key_value = values[values.keys.find { |k|
44
- k.name == primary_key
45
- }]
46
-
47
- if !primary_key_value && connection.prefetch_primary_key?(klass.table_name)
48
- primary_key_value = connection.next_sequence_value(klass.sequence_name)
49
- values[klass.arel_table[klass.primary_key]] = primary_key_value
50
- end
51
- end
52
-
53
- im = arel.create_insert
54
- im.into @table
55
-
56
- substitutes, binds = substitute_values values
57
-
58
- if values.empty? # empty insert
59
- im.values = Arel.sql(connection.empty_insert_statement_value)
60
- else
61
- im.insert substitutes
62
- end
63
-
64
- @klass.connection.insert(
65
- im,
66
- 'SQL',
67
- primary_key,
68
- primary_key_value,
69
- nil,
70
- binds)
41
+ def arel_attribute(name) # :nodoc:
42
+ table[name]
71
43
  end
44
+ deprecate :arel_attribute
72
45
 
73
- def _update_record(values, id, id_was) # :nodoc:
74
- substitutes, binds = substitute_values values
75
-
76
- scope = @klass.unscoped
77
-
78
- if @klass.finder_needs_type_condition?
79
- scope.unscope!(where: @klass.inheritance_column)
46
+ def bind_attribute(name, value) # :nodoc:
47
+ if reflection = klass._reflect_on_association(name)
48
+ name = reflection.foreign_key
49
+ value = value.read_attribute(reflection.klass.primary_key) unless value.nil?
80
50
  end
81
51
 
82
- relation = scope.where(@klass.primary_key => (id_was || id))
83
- bvs = binds + relation.bind_values
84
- um = relation
85
- .arel
86
- .compile_update(substitutes, @klass.primary_key)
87
-
88
- @klass.connection.update(
89
- um,
90
- 'SQL',
91
- bvs,
92
- )
93
- end
94
-
95
- def substitute_values(values) # :nodoc:
96
- binds = values.map do |arel_attr, value|
97
- [@klass.columns_hash[arel_attr.name], value]
98
- end
99
-
100
- substitutes = values.each_with_index.map do |(arel_attr, _), i|
101
- [arel_attr, @klass.connection.substitute_at(binds[i][0])]
102
- end
103
-
104
- [substitutes, binds]
52
+ attr = table[name]
53
+ bind = predicate_builder.build_bind_attribute(attr.name, value)
54
+ yield attr, bind
105
55
  end
106
56
 
107
57
  # Initializes new record from relation while maintaining the current
108
58
  # scope.
109
59
  #
110
- # Expects arguments in the same format as +Base.new+.
60
+ # Expects arguments in the same format as {ActiveRecord::Base.new}[rdoc-ref:Core.new].
111
61
  #
112
62
  # users = User.where(name: 'DHH')
113
63
  # user = users.new # => #<User id: nil, name: "DHH", created_at: nil, updated_at: nil>
@@ -116,39 +66,53 @@ module ActiveRecord
116
66
  #
117
67
  # user = users.new { |user| user.name = 'Oscar' }
118
68
  # user.name # => Oscar
119
- def new(*args, &block)
120
- scoping { @klass.new(*args, &block) }
69
+ def new(attributes = nil, &block)
70
+ block = current_scope_restoring_block(&block)
71
+ scoping { _new(attributes, &block) }
121
72
  end
122
-
123
73
  alias build new
124
74
 
125
75
  # Tries to create a new record with the same scoped attributes
126
76
  # defined in the relation. Returns the initialized object if validation fails.
127
77
  #
128
- # Expects arguments in the same format as +Base.create+.
78
+ # Expects arguments in the same format as
79
+ # {ActiveRecord::Base.create}[rdoc-ref:Persistence::ClassMethods#create].
129
80
  #
130
81
  # ==== Examples
82
+ #
131
83
  # users = User.where(name: 'Oscar')
132
- # users.create # #<User id: 3, name: "oscar", ...>
84
+ # users.create # => #<User id: 3, name: "Oscar", ...>
133
85
  #
134
86
  # users.create(name: 'fxn')
135
- # users.create # #<User id: 4, name: "fxn", ...>
87
+ # users.create # => #<User id: 4, name: "fxn", ...>
136
88
  #
137
89
  # users.create { |user| user.name = 'tenderlove' }
138
- # # #<User id: 5, name: "tenderlove", ...>
90
+ # # => #<User id: 5, name: "tenderlove", ...>
139
91
  #
140
92
  # users.create(name: nil) # validation on name
141
- # # #<User id: nil, name: nil, ...>
142
- def create(*args, &block)
143
- scoping { @klass.create(*args, &block) }
93
+ # # => #<User id: nil, name: nil, ...>
94
+ def create(attributes = nil, &block)
95
+ if attributes.is_a?(Array)
96
+ attributes.collect { |attr| create(attr, &block) }
97
+ else
98
+ block = current_scope_restoring_block(&block)
99
+ scoping { _create(attributes, &block) }
100
+ end
144
101
  end
145
102
 
146
- # Similar to #create, but calls +create!+ on the base class. Raises
147
- # an exception if a validation error occurs.
103
+ # Similar to #create, but calls
104
+ # {create!}[rdoc-ref:Persistence::ClassMethods#create!]
105
+ # on the base class. Raises an exception if a validation error occurs.
148
106
  #
149
- # Expects arguments in the same format as <tt>Base.create!</tt>.
150
- def create!(*args, &block)
151
- scoping { @klass.create!(*args, &block) }
107
+ # Expects arguments in the same format as
108
+ # {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!].
109
+ def create!(attributes = nil, &block)
110
+ if attributes.is_a?(Array)
111
+ attributes.collect { |attr| create!(attr, &block) }
112
+ else
113
+ block = current_scope_restoring_block(&block)
114
+ scoping { _create!(attributes, &block) }
115
+ end
152
116
  end
153
117
 
154
118
  def first_or_create(attributes = nil, &block) # :nodoc:
@@ -180,7 +144,7 @@ module ActiveRecord
180
144
  # User.create_with(last_name: 'Johansson').find_or_create_by(first_name: 'Scarlett')
181
145
  # # => #<User id: 2, first_name: "Scarlett", last_name: "Johansson">
182
146
  #
183
- # This method accepts a block, which is passed down to +create+. The last example
147
+ # This method accepts a block, which is passed down to #create. The last example
184
148
  # above can be alternatively written this way:
185
149
  #
186
150
  # # Find the first user named "Scarlett" or create a new one with a
@@ -192,34 +156,72 @@ module ActiveRecord
192
156
  #
193
157
  # This method always returns a record, but if creation was attempted and
194
158
  # failed due to validation errors it won't be persisted, you get what
195
- # +create+ returns in such situation.
159
+ # #create returns in such situation.
196
160
  #
197
- # Please note *this method is not atomic*, it runs first a SELECT, and if
161
+ # Please note <b>this method is not atomic</b>, it runs first a SELECT, and if
198
162
  # there are no results an INSERT is attempted. If there are other threads
199
163
  # or processes there is a race condition between both calls and it could
200
164
  # be the case that you end up with two similar records.
201
165
  #
202
- # Whether that is a problem or not depends on the logic of the
203
- # application, but in the particular case in which rows have a UNIQUE
204
- # constraint an exception may be raised, just retry:
205
- #
206
- # begin
207
- # CreditAccount.find_or_create_by(user_id: user.id)
208
- # rescue ActiveRecord::RecordNotUnique
209
- # retry
210
- # end
211
- #
166
+ # If this might be a problem for your application, please see #create_or_find_by.
212
167
  def find_or_create_by(attributes, &block)
213
168
  find_by(attributes) || create(attributes, &block)
214
169
  end
215
170
 
216
- # Like <tt>find_or_create_by</tt>, but calls <tt>create!</tt> so an exception
171
+ # Like #find_or_create_by, but calls
172
+ # {create!}[rdoc-ref:Persistence::ClassMethods#create!] so an exception
217
173
  # is raised if the created record is invalid.
218
174
  def find_or_create_by!(attributes, &block)
219
175
  find_by(attributes) || create!(attributes, &block)
220
176
  end
221
177
 
222
- # Like <tt>find_or_create_by</tt>, but calls <tt>new</tt> instead of <tt>create</tt>.
178
+ # Attempts to create a record with the given attributes in a table that has a unique constraint
179
+ # on one or several of its columns. If a row already exists with one or several of these
180
+ # unique constraints, the exception such an insertion would normally raise is caught,
181
+ # and the existing record with those attributes is found using #find_by!.
182
+ #
183
+ # This is similar to #find_or_create_by, but avoids the problem of stale reads between the SELECT
184
+ # and the INSERT, as that method needs to first query the table, then attempt to insert a row
185
+ # if none is found.
186
+ #
187
+ # There are several drawbacks to #create_or_find_by, though:
188
+ #
189
+ # * The underlying table must have the relevant columns defined with unique constraints.
190
+ # * A unique constraint violation may be triggered by only one, or at least less than all,
191
+ # of the given attributes. This means that the subsequent #find_by! may fail to find a
192
+ # matching record, which will then raise an <tt>ActiveRecord::RecordNotFound</tt> exception,
193
+ # rather than a record with the given attributes.
194
+ # * While we avoid the race condition between SELECT -> INSERT from #find_or_create_by,
195
+ # we actually have another race condition between INSERT -> SELECT, which can be triggered
196
+ # if a DELETE between those two statements is run by another client. But for most applications,
197
+ # that's a significantly less likely condition to hit.
198
+ # * It relies on exception handling to handle control flow, which may be marginally slower.
199
+ # * The primary key may auto-increment on each create, even if it fails. This can accelerate
200
+ # the problem of running out of integers, if the underlying table is still stuck on a primary
201
+ # key of type int (note: All Rails apps since 5.1+ have defaulted to bigint, which is not liable
202
+ # to this problem).
203
+ #
204
+ # This method will return a record if all given attributes are covered by unique constraints
205
+ # (unless the INSERT -> DELETE -> SELECT race condition is triggered), but if creation was attempted
206
+ # and failed due to validation errors it won't be persisted, you get what #create returns in
207
+ # such situation.
208
+ def create_or_find_by(attributes, &block)
209
+ transaction(requires_new: true) { create(attributes, &block) }
210
+ rescue ActiveRecord::RecordNotUnique
211
+ find_by!(attributes)
212
+ end
213
+
214
+ # Like #create_or_find_by, but calls
215
+ # {create!}[rdoc-ref:Persistence::ClassMethods#create!] so an exception
216
+ # is raised if the created record is invalid.
217
+ def create_or_find_by!(attributes, &block)
218
+ transaction(requires_new: true) { create!(attributes, &block) }
219
+ rescue ActiveRecord::RecordNotUnique
220
+ find_by!(attributes)
221
+ end
222
+
223
+ # Like #find_or_create_by, but calls {new}[rdoc-ref:Core#new]
224
+ # instead of {create}[rdoc-ref:Persistence::ClassMethods#create].
223
225
  def find_or_initialize_by(attributes, &block)
224
226
  find_by(attributes) || new(attributes, &block)
225
227
  end
@@ -232,25 +234,25 @@ module ActiveRecord
232
234
  # are needed by the next ones when eager loading is going on.
233
235
  #
234
236
  # Please see further details in the
235
- # {Active Record Query Interface guide}[http://guides.rubyonrails.org/active_record_querying.html#running-explain].
237
+ # {Active Record Query Interface guide}[https://guides.rubyonrails.org/active_record_querying.html#running-explain].
236
238
  def explain
237
- #TODO: Fix for binds.
238
239
  exec_explain(collecting_queries_for_explain { exec_queries })
239
240
  end
240
241
 
241
242
  # Converts relation objects to Array.
242
- def to_a
243
+ def to_ary
244
+ records.dup
245
+ end
246
+ alias to_a to_ary
247
+
248
+ def records # :nodoc:
243
249
  load
244
250
  @records
245
251
  end
246
252
 
247
253
  # Serializes the relation objects Array.
248
254
  def encode_with(coder)
249
- coder.represent_seq(nil, to_a)
250
- end
251
-
252
- def as_json(options = nil) #:nodoc:
253
- to_a.as_json(options)
255
+ coder.represent_seq(nil, records)
254
256
  end
255
257
 
256
258
  # Returns size of the records.
@@ -261,30 +263,133 @@ module ActiveRecord
261
263
  # Returns true if there are no records.
262
264
  def empty?
263
265
  return @records.empty? if loaded?
266
+ !exists?
267
+ end
264
268
 
265
- if limit_value == 0
266
- true
267
- else
268
- c = count(:all)
269
- c.respond_to?(:zero?) ? c.zero? : c.empty?
270
- end
269
+ # Returns true if there are no records.
270
+ def none?
271
+ return super if block_given?
272
+ empty?
271
273
  end
272
274
 
273
275
  # Returns true if there are any records.
274
276
  def any?
275
- if block_given?
276
- to_a.any? { |*block_args| yield(*block_args) }
277
- else
278
- !empty?
279
- end
277
+ return super if block_given?
278
+ !empty?
279
+ end
280
+
281
+ # Returns true if there is exactly one record.
282
+ def one?
283
+ return super if block_given?
284
+ limit_value ? records.one? : size == 1
280
285
  end
281
286
 
282
287
  # Returns true if there is more than one record.
283
288
  def many?
284
- if block_given?
285
- to_a.many? { |*block_args| yield(*block_args) }
289
+ return super if block_given?
290
+ limit_value ? records.many? : size > 1
291
+ end
292
+
293
+ # Returns a stable cache key that can be used to identify this query.
294
+ # The cache key is built with a fingerprint of the SQL query.
295
+ #
296
+ # Product.where("name like ?", "%Cosmic Encounter%").cache_key
297
+ # # => "products/query-1850ab3d302391b85b8693e941286659"
298
+ #
299
+ # If ActiveRecord::Base.collection_cache_versioning is turned off, as it was
300
+ # in Rails 6.0 and earlier, the cache key will also include a version.
301
+ #
302
+ # ActiveRecord::Base.collection_cache_versioning = false
303
+ # Product.where("name like ?", "%Cosmic Encounter%").cache_key
304
+ # # => "products/query-1850ab3d302391b85b8693e941286659-1-20150714212553907087000"
305
+ #
306
+ # You can also pass a custom timestamp column to fetch the timestamp of the
307
+ # last updated record.
308
+ #
309
+ # Product.where("name like ?", "%Game%").cache_key(:last_reviewed_at)
310
+ def cache_key(timestamp_column = "updated_at")
311
+ @cache_keys ||= {}
312
+ @cache_keys[timestamp_column] ||= klass.collection_cache_key(self, timestamp_column)
313
+ end
314
+
315
+ def compute_cache_key(timestamp_column = :updated_at) # :nodoc:
316
+ query_signature = ActiveSupport::Digest.hexdigest(to_sql)
317
+ key = "#{klass.model_name.cache_key}/query-#{query_signature}"
318
+
319
+ if collection_cache_versioning
320
+ key
321
+ else
322
+ "#{key}-#{compute_cache_version(timestamp_column)}"
323
+ end
324
+ end
325
+ private :compute_cache_key
326
+
327
+ # Returns a cache version that can be used together with the cache key to form
328
+ # a recyclable caching scheme. The cache version is built with the number of records
329
+ # matching the query, and the timestamp of the last updated record. When a new record
330
+ # comes to match the query, or any of the existing records is updated or deleted,
331
+ # the cache version changes.
332
+ #
333
+ # If the collection is loaded, the method will iterate through the records
334
+ # to generate the timestamp, otherwise it will trigger one SQL query like:
335
+ #
336
+ # SELECT COUNT(*), MAX("products"."updated_at") FROM "products" WHERE (name like '%Cosmic Encounter%')
337
+ def cache_version(timestamp_column = :updated_at)
338
+ if collection_cache_versioning
339
+ @cache_versions ||= {}
340
+ @cache_versions[timestamp_column] ||= compute_cache_version(timestamp_column)
341
+ end
342
+ end
343
+
344
+ def compute_cache_version(timestamp_column) # :nodoc:
345
+ timestamp_column = timestamp_column.to_s
346
+
347
+ if loaded? || distinct_value
348
+ size = records.size
349
+ if size > 0
350
+ timestamp = records.map { |record| record.read_attribute(timestamp_column) }.max
351
+ end
352
+ else
353
+ collection = eager_loading? ? apply_join_dependency : self
354
+
355
+ column = connection.visitor.compile(table[timestamp_column])
356
+ select_values = "COUNT(*) AS #{connection.quote_column_name("size")}, MAX(%s) AS timestamp"
357
+
358
+ if collection.has_limit_or_offset?
359
+ query = collection.select("#{column} AS collection_cache_key_timestamp")
360
+ subquery_alias = "subquery_for_cache_key"
361
+ subquery_column = "#{subquery_alias}.collection_cache_key_timestamp"
362
+ arel = query.build_subquery(subquery_alias, select_values % subquery_column)
363
+ else
364
+ query = collection.unscope(:order)
365
+ query.select_values = [select_values % column]
366
+ arel = query.arel
367
+ end
368
+
369
+ size, timestamp = connection.select_rows(arel, nil).first
370
+
371
+ if size
372
+ column_type = klass.type_for_attribute(timestamp_column)
373
+ timestamp = column_type.deserialize(timestamp)
374
+ else
375
+ size = 0
376
+ end
377
+ end
378
+
379
+ if timestamp
380
+ "#{size}-#{timestamp.utc.to_s(cache_timestamp_format)}"
381
+ else
382
+ "#{size}"
383
+ end
384
+ end
385
+ private :compute_cache_version
386
+
387
+ # Returns a cache key along with the version.
388
+ def cache_key_with_version
389
+ if version = cache_version
390
+ "#{cache_key}-#{version}"
286
391
  else
287
- limit_value ? to_a.many? : size > 1
392
+ cache_key
288
393
  end
289
394
  end
290
395
 
@@ -298,17 +403,22 @@ module ActiveRecord
298
403
  # Please check unscoped if you want to remove all previous scopes (including
299
404
  # the default_scope) during the execution of a block.
300
405
  def scoping
301
- previous, klass.current_scope = klass.current_scope, self
302
- yield
406
+ already_in_scope? ? yield : _scoping(self) { yield }
407
+ end
408
+
409
+ def _exec_scope(*args, &block) # :nodoc:
410
+ @delegate_to_klass = true
411
+ _scoping(nil) { instance_exec(*args, &block) || self }
303
412
  ensure
304
- klass.current_scope = previous
413
+ @delegate_to_klass = false
305
414
  end
306
415
 
307
416
  # Updates all records in the current relation with details given. This method constructs a single SQL UPDATE
308
417
  # statement and sends it straight to the database. It does not instantiate the involved models and it does not
309
- # trigger Active Record callbacks or validations. Values passed to `update_all` will not go through
310
- # ActiveRecord's type-casting behavior. It should receive only values that can be passed as-is to the SQL
311
- # database.
418
+ # trigger Active Record callbacks or validations. However, values passed to #update_all will still go through
419
+ # Active Record's normal type casting and serialization. Returns the number of rows affected.
420
+ #
421
+ # Note: As Active Record callbacks are not triggered, this method will not automatically update +updated_at+/+updated_on+ columns.
312
422
  #
313
423
  # ==== Parameters
314
424
  #
@@ -324,185 +434,190 @@ module ActiveRecord
324
434
  #
325
435
  # # Update all books that match conditions, but limit it to 5 ordered by date
326
436
  # Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(author: 'David')
437
+ #
438
+ # # Update all invoices and set the number column to its id value.
439
+ # Invoice.update_all('number = id')
327
440
  def update_all(updates)
328
441
  raise ArgumentError, "Empty list of attributes to change" if updates.blank?
329
442
 
330
- stmt = Arel::UpdateManager.new(arel.engine)
443
+ arel = eager_loading? ? apply_join_dependency.arel : build_arel
444
+ arel.source.left = table
331
445
 
332
- stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))
333
- stmt.table(table)
446
+ stmt = Arel::UpdateManager.new
447
+ stmt.table(arel.source)
334
448
  stmt.key = table[primary_key]
335
-
336
- if joins_values.any?
337
- @klass.connection.join_to_update(stmt, arel)
449
+ stmt.take(arel.limit)
450
+ stmt.offset(arel.offset)
451
+ stmt.order(*arel.orders)
452
+ stmt.wheres = arel.constraints
453
+
454
+ if updates.is_a?(Hash)
455
+ if klass.locking_enabled? &&
456
+ !updates.key?(klass.locking_column) &&
457
+ !updates.key?(klass.locking_column.to_sym)
458
+ attr = table[klass.locking_column]
459
+ updates[attr.name] = _increment_attribute(attr)
460
+ end
461
+ stmt.set _substitute_values(updates)
338
462
  else
339
- stmt.take(arel.limit)
340
- stmt.order(*arel.orders)
341
- stmt.wheres = arel.constraints
463
+ stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
342
464
  end
343
465
 
344
- bvs = arel.bind_values + bind_values
345
- @klass.connection.update stmt, 'SQL', bvs
466
+ klass.connection.update(stmt, "#{klass} Update All").tap { reset }
467
+ end
468
+
469
+ def update(id = :all, attributes) # :nodoc:
470
+ if id == :all
471
+ each { |record| record.update(attributes) }
472
+ else
473
+ klass.update(id, attributes)
474
+ end
346
475
  end
347
476
 
348
- # Updates an object (or multiple objects) and saves it to the database, if validations pass.
349
- # The resulting object is returned whether the object was saved successfully to the database or not.
477
+ # Updates the counters of the records in the current relation.
350
478
  #
351
479
  # ==== Parameters
352
480
  #
353
- # * +id+ - This should be the id or an array of ids to be updated.
354
- # * +attributes+ - This should be a hash of attributes or an array of hashes.
481
+ # * +counter+ - A Hash containing the names of the fields to update as keys and the amount to update as values.
482
+ # * <tt>:touch</tt> option - Touch the timestamp columns when updating.
483
+ # * If attributes names are passed, they are updated along with update_at/on attributes.
355
484
  #
356
485
  # ==== Examples
357
486
  #
358
- # # Updates one record
359
- # Person.update(15, user_name: 'Samuel', group: 'expert')
360
- #
361
- # # Updates multiple records
362
- # people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
363
- # Person.update(people.keys, people.values)
364
- def update(id, attributes)
365
- if id.is_a?(Array)
366
- id.map.with_index { |one_id, idx| update(one_id, attributes[idx]) }
367
- else
368
- object = find(id)
369
- object.update(attributes)
370
- object
487
+ # # For Posts by a given author increment the comment_count by 1.
488
+ # Post.where(author_id: author.id).update_counters(comment_count: 1)
489
+ def update_counters(counters)
490
+ touch = counters.delete(:touch)
491
+
492
+ updates = {}
493
+ counters.each do |counter_name, value|
494
+ attr = table[counter_name]
495
+ updates[attr.name] = _increment_attribute(attr, value)
496
+ end
497
+
498
+ if touch
499
+ names = touch if touch != true
500
+ names = Array.wrap(names)
501
+ options = names.extract_options!
502
+ touch_updates = klass.touch_attributes_with_time(*names, **options)
503
+ updates.merge!(touch_updates) unless touch_updates.empty?
371
504
  end
505
+
506
+ update_all updates
372
507
  end
373
508
 
374
- # Destroys the records matching +conditions+ by instantiating each
375
- # record and calling its +destroy+ method. Each object's callbacks are
376
- # executed (including <tt>:dependent</tt> association options). Returns the
377
- # collection of objects that were destroyed; each will be frozen, to
378
- # reflect that no changes should be made (since they can't be persisted).
509
+ # Touches all records in the current relation, setting the +updated_at+/+updated_on+ attributes to the current time or the time specified.
510
+ # It does not instantiate the involved models, and it does not trigger Active Record callbacks or validations.
511
+ # This method can be passed attribute names and an optional time argument.
512
+ # If attribute names are passed, they are updated along with +updated_at+/+updated_on+ attributes.
513
+ # If no time argument is passed, the current time is used as default.
379
514
  #
380
- # Note: Instantiation, callback execution, and deletion of each
381
- # record can be time consuming when you're removing many records at
382
- # once. It generates at least one SQL +DELETE+ query per record (or
383
- # possibly more, to enforce your callbacks). If you want to delete many
384
- # rows quickly, without concern for their associations or callbacks, use
385
- # +delete_all+ instead.
515
+ # === Examples
386
516
  #
387
- # ==== Parameters
517
+ # # Touch all records
518
+ # Person.all.touch_all
519
+ # # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670'"
388
520
  #
389
- # * +conditions+ - A string, array, or hash that specifies which records
390
- # to destroy. If omitted, all records are destroyed. See the
391
- # Conditions section in the introduction to ActiveRecord::Base for
392
- # more information.
521
+ # # Touch multiple records with a custom attribute
522
+ # Person.all.touch_all(:created_at)
523
+ # # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670', \"created_at\" = '2018-01-04 22:55:23.132670'"
393
524
  #
394
- # ==== Examples
525
+ # # Touch multiple records with a specified time
526
+ # Person.all.touch_all(time: Time.new(2020, 5, 16, 0, 0, 0))
527
+ # # => "UPDATE \"people\" SET \"updated_at\" = '2020-05-16 00:00:00'"
395
528
  #
396
- # Person.destroy_all("last_login < '2004-04-04'")
397
- # Person.destroy_all(status: "inactive")
398
- # Person.where(age: 0..18).destroy_all
399
- def destroy_all(conditions = nil)
400
- if conditions
401
- where(conditions).destroy_all
402
- else
403
- to_a.each {|object| object.destroy }.tap { reset }
404
- end
529
+ # # Touch records with scope
530
+ # Person.where(name: 'David').touch_all
531
+ # # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670' WHERE \"people\".\"name\" = 'David'"
532
+ def touch_all(*names, time: nil)
533
+ update_all klass.touch_attributes_with_time(*names, time: time)
405
534
  end
406
535
 
407
- # Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
408
- # therefore all callbacks and filters are fired off before the object is deleted. This method is
409
- # less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run.
410
- #
411
- # This essentially finds the object (or multiple objects) with the given id, creates a new object
412
- # from the attributes, and then calls destroy on it.
413
- #
414
- # ==== Parameters
536
+ # Destroys the records by instantiating each
537
+ # record and calling its {#destroy}[rdoc-ref:Persistence#destroy] method.
538
+ # Each object's callbacks are executed (including <tt>:dependent</tt> association options).
539
+ # Returns the collection of objects that were destroyed; each will be frozen, to
540
+ # reflect that no changes should be made (since they can't be persisted).
415
541
  #
416
- # * +id+ - Can be either an Integer or an Array of Integers.
542
+ # Note: Instantiation, callback execution, and deletion of each
543
+ # record can be time consuming when you're removing many records at
544
+ # once. It generates at least one SQL +DELETE+ query per record (or
545
+ # possibly more, to enforce your callbacks). If you want to delete many
546
+ # rows quickly, without concern for their associations or callbacks, use
547
+ # #delete_all instead.
417
548
  #
418
549
  # ==== Examples
419
550
  #
420
- # # Destroy a single object
421
- # Todo.destroy(1)
422
- #
423
- # # Destroy multiple objects
424
- # todos = [1,2,3]
425
- # Todo.destroy(todos)
426
- def destroy(id)
427
- if id.is_a?(Array)
428
- id.map { |one_id| destroy(one_id) }
429
- else
430
- find(id).destroy
431
- end
551
+ # Person.where(age: 0..18).destroy_all
552
+ def destroy_all
553
+ records.each(&:destroy).tap { reset }
432
554
  end
433
555
 
434
- # Deletes the records matching +conditions+ without instantiating the records
435
- # first, and hence not calling the +destroy+ method nor invoking callbacks. This
436
- # is a single SQL DELETE statement that goes straight to the database, much more
437
- # efficient than +destroy_all+. Be careful with relations though, in particular
556
+ # Deletes the records without instantiating the records
557
+ # first, and hence not calling the {#destroy}[rdoc-ref:Persistence#destroy]
558
+ # method nor invoking callbacks.
559
+ # This is a single SQL DELETE statement that goes straight to the database, much more
560
+ # efficient than #destroy_all. Be careful with relations though, in particular
438
561
  # <tt>:dependent</tt> rules defined on associations are not honored. Returns the
439
562
  # number of rows affected.
440
563
  #
441
- # Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')")
442
- # Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else'])
443
564
  # Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all
444
565
  #
445
566
  # Both calls delete the affected posts all at once with a single DELETE statement.
446
567
  # If you need to destroy dependent associations or call your <tt>before_*</tt> or
447
- # +after_destroy+ callbacks, use the +destroy_all+ method instead.
568
+ # +after_destroy+ callbacks, use the #destroy_all method instead.
448
569
  #
449
- # If an invalid method is supplied, +delete_all+ raises an ActiveRecord error:
570
+ # If an invalid method is supplied, #delete_all raises an ActiveRecordError:
450
571
  #
451
- # Post.limit(100).delete_all
452
- # # => ActiveRecord::ActiveRecordError: delete_all doesn't support limit
453
- def delete_all(conditions = nil)
454
- invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select { |method|
455
- if MULTI_VALUE_METHODS.include?(method)
456
- send("#{method}_values").any?
457
- else
458
- send("#{method}_value")
459
- end
460
- }
572
+ # Post.distinct.delete_all
573
+ # # => ActiveRecord::ActiveRecordError: delete_all doesn't support distinct
574
+ def delete_all
575
+ invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select do |method|
576
+ value = @values[method]
577
+ method == :distinct ? value : value&.any?
578
+ end
461
579
  if invalid_methods.any?
462
580
  raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
463
581
  end
464
582
 
465
- if conditions
466
- where(conditions).delete_all
467
- else
468
- stmt = Arel::DeleteManager.new(arel.engine)
469
- stmt.from(table)
470
-
471
- if joins_values.any?
472
- @klass.connection.join_to_delete(stmt, arel, table[primary_key])
473
- else
474
- stmt.wheres = arel.constraints
475
- end
583
+ arel = eager_loading? ? apply_join_dependency.arel : build_arel
584
+ arel.source.left = table
476
585
 
477
- bvs = arel.bind_values + bind_values
478
- affected = @klass.connection.delete(stmt, 'SQL', bvs)
586
+ stmt = Arel::DeleteManager.new
587
+ stmt.from(arel.source)
588
+ stmt.key = table[primary_key]
589
+ stmt.take(arel.limit)
590
+ stmt.offset(arel.offset)
591
+ stmt.order(*arel.orders)
592
+ stmt.wheres = arel.constraints
479
593
 
480
- reset
481
- affected
482
- end
594
+ klass.connection.delete(stmt, "#{klass} Destroy").tap { reset }
483
595
  end
484
596
 
485
- # Deletes the row with a primary key matching the +id+ argument, using a
486
- # SQL +DELETE+ statement, and returns the number of rows deleted. Active
487
- # Record objects are not instantiated, so the object's callbacks are not
488
- # executed, including any <tt>:dependent</tt> association options.
489
- #
490
- # You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
597
+ # Finds and destroys all records matching the specified conditions.
598
+ # This is short-hand for <tt>relation.where(condition).destroy_all</tt>.
599
+ # Returns the collection of objects that were destroyed.
491
600
  #
492
- # Note: Although it is often much faster than the alternative,
493
- # <tt>#destroy</tt>, skipping callbacks might bypass business logic in
494
- # your application that ensures referential integrity or performs other
495
- # essential jobs.
601
+ # If no record is found, returns empty array.
496
602
  #
497
- # ==== Examples
603
+ # Person.destroy_by(id: 13)
604
+ # Person.destroy_by(name: 'Spartacus', rating: 4)
605
+ # Person.destroy_by("published_at < ?", 2.weeks.ago)
606
+ def destroy_by(*args)
607
+ where(*args).destroy_all
608
+ end
609
+
610
+ # Finds and deletes all records matching the specified conditions.
611
+ # This is short-hand for <tt>relation.where(condition).delete_all</tt>.
612
+ # Returns the number of rows affected.
498
613
  #
499
- # # Delete a single row
500
- # Todo.delete(1)
614
+ # If no record is found, returns <tt>0</tt> as zero rows were affected.
501
615
  #
502
- # # Delete multiple rows
503
- # Todo.delete([2,3,4])
504
- def delete(id_or_array)
505
- where(primary_key => id_or_array).delete_all
616
+ # Person.delete_by(id: 13)
617
+ # Person.delete_by(name: 'Spartacus', rating: 4)
618
+ # Person.delete_by("published_at < ?", 2.weeks.ago)
619
+ def delete_by(*args)
620
+ where(*args).delete_all
506
621
  end
507
622
 
508
623
  # Causes the records to be loaded from the database if they have not
@@ -511,8 +626,11 @@ module ActiveRecord
511
626
  # return value is the relation itself, not the records.
512
627
  #
513
628
  # Post.where(published: true).load # => #<ActiveRecord::Relation>
514
- def load
515
- exec_queries unless loaded?
629
+ def load(&block)
630
+ unless loaded?
631
+ @records = exec_queries(&block)
632
+ @loaded = true
633
+ end
516
634
 
517
635
  self
518
636
  end
@@ -524,10 +642,11 @@ module ActiveRecord
524
642
  end
525
643
 
526
644
  def reset
527
- @last = @to_sql = @order_clause = @scope_for_create = @arel = @loaded = nil
528
- @should_eager_load = @join_dependency = nil
529
- @records = []
530
- @offsets = {}
645
+ @delegate_to_klass = false
646
+ @to_sql = @arel = @loaded = @should_eager_load = nil
647
+ @offsets = @take = nil
648
+ @cache_keys = nil
649
+ @records = [].freeze
531
650
  self
532
651
  end
533
652
 
@@ -537,47 +656,30 @@ module ActiveRecord
537
656
  # # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
538
657
  def to_sql
539
658
  @to_sql ||= begin
540
- relation = self
541
- connection = klass.connection
542
- visitor = connection.visitor
543
-
544
- if eager_loading?
545
- find_with_associations { |rel| relation = rel }
546
- end
547
-
548
- arel = relation.arel
549
- binds = (arel.bind_values + relation.bind_values).dup
550
- binds.map! { |bv| connection.quote(*bv.reverse) }
551
- collect = visitor.accept(arel.ast, Arel::Collectors::Bind.new)
552
- collect.substitute_binds(binds).join
553
- end
659
+ if eager_loading?
660
+ apply_join_dependency do |relation, join_dependency|
661
+ relation = join_dependency.apply_column_aliases(relation)
662
+ relation.to_sql
663
+ end
664
+ else
665
+ conn = klass.connection
666
+ conn.unprepared_statement { conn.to_sql(arel) }
667
+ end
668
+ end
554
669
  end
555
670
 
556
671
  # Returns a hash of where conditions.
557
672
  #
558
673
  # User.where(name: 'Oscar').where_values_hash
559
674
  # # => {name: "Oscar"}
560
- def where_values_hash(relation_table_name = table_name)
561
- equalities = where_values.grep(Arel::Nodes::Equality).find_all { |node|
562
- node.left.relation.name == relation_table_name
563
- }
564
-
565
- binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }]
566
-
567
- Hash[equalities.map { |where|
568
- name = where.left.name
569
- [name, binds.fetch(name.to_s) {
570
- case where.right
571
- when Array then where.right.map(&:val)
572
- when Arel::Nodes::Casted
573
- where.right.val
574
- end
575
- }]
576
- }]
675
+ def where_values_hash(relation_table_name = klass.table_name)
676
+ where_clause.to_h(relation_table_name)
577
677
  end
578
678
 
579
679
  def scope_for_create
580
- @scope_for_create ||= where_values_hash.merge(create_with_value)
680
+ hash = where_clause.to_h(klass.table_name, equality_only: true)
681
+ create_with_value.each { |k, v| hash[k.to_s] = v } unless create_with_value.empty?
682
+ hash
581
683
  end
582
684
 
583
685
  # Returns true if relation needs eager loading.
@@ -595,88 +697,197 @@ module ActiveRecord
595
697
  includes_values & joins_values
596
698
  end
597
699
 
598
- # +uniq+ and +uniq!+ are silently deprecated. +uniq_value+ delegates to +distinct_value+
599
- # to maintain backwards compatibility. Use +distinct_value+ instead.
600
- def uniq_value
601
- distinct_value
602
- end
603
-
604
700
  # Compares two relations for equality.
605
701
  def ==(other)
606
702
  case other
607
703
  when Associations::CollectionProxy, AssociationRelation
608
- self == other.to_a
704
+ self == other.records
609
705
  when Relation
610
706
  other.to_sql == to_sql
611
707
  when Array
612
- to_a == other
708
+ records == other
613
709
  end
614
710
  end
615
711
 
616
712
  def pretty_print(q)
617
- q.pp(self.to_a)
713
+ q.pp(records)
618
714
  end
619
715
 
620
716
  # Returns true if relation is blank.
621
717
  def blank?
622
- to_a.blank?
718
+ records.blank?
623
719
  end
624
720
 
625
721
  def values
626
- Hash[@values]
722
+ @values.dup
627
723
  end
628
724
 
629
725
  def inspect
630
- entries = to_a.take([limit_value, 11].compact.min).map!(&:inspect)
631
- entries[10] = '...' if entries.size == 11
726
+ subject = loaded? ? records : annotate("loading for inspect")
727
+ entries = subject.take([limit_value, 11].compact.min).map!(&:inspect)
728
+
729
+ entries[10] = "..." if entries.size == 11
632
730
 
633
731
  "#<#{self.class.name} [#{entries.join(', ')}]>"
634
732
  end
635
733
 
636
- private
734
+ def empty_scope? # :nodoc:
735
+ @values == klass.unscoped.values
736
+ end
637
737
 
638
- def exec_queries
639
- @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel, arel.bind_values + bind_values)
738
+ def has_limit_or_offset? # :nodoc:
739
+ limit_value || offset_value
740
+ end
741
+
742
+ def alias_tracker(joins = [], aliases = nil) # :nodoc:
743
+ ActiveRecord::Associations::AliasTracker.create(connection, table.name, joins, aliases)
744
+ end
745
+
746
+ class StrictLoadingScope # :nodoc:
747
+ def self.empty_scope?
748
+ true
749
+ end
640
750
 
751
+ def self.strict_loading_value
752
+ true
753
+ end
754
+ end
755
+
756
+ def preload_associations(records) # :nodoc:
641
757
  preload = preload_values
642
- preload += includes_values unless eager_loading?
643
- preloader = build_preloader
758
+ preload += includes_values unless eager_loading?
759
+ preloader = nil
760
+ scope = strict_loading_value ? StrictLoadingScope : nil
644
761
  preload.each do |associations|
645
- preloader.preload @records, associations
762
+ preloader ||= build_preloader
763
+ preloader.preload records, associations, scope
764
+ end
765
+ end
766
+
767
+ protected
768
+ def load_records(records)
769
+ @records = records.freeze
770
+ @loaded = true
646
771
  end
647
772
 
648
- @records.each { |record| record.readonly! } if readonly_value
773
+ def null_relation? # :nodoc:
774
+ is_a?(NullRelation)
775
+ end
649
776
 
650
- @loaded = true
651
- @records
652
- end
777
+ private
778
+ def already_in_scope?
779
+ @delegate_to_klass && klass.current_scope(true)
780
+ end
653
781
 
654
- def build_preloader
655
- ActiveRecord::Associations::Preloader.new
656
- end
782
+ def current_scope_restoring_block(&block)
783
+ current_scope = klass.current_scope(true)
784
+ -> record do
785
+ klass.current_scope = current_scope
786
+ yield record if block_given?
787
+ end
788
+ end
789
+
790
+ def _new(attributes, &block)
791
+ klass.new(attributes, &block)
792
+ end
793
+
794
+ def _create(attributes, &block)
795
+ klass.create(attributes, &block)
796
+ end
797
+
798
+ def _create!(attributes, &block)
799
+ klass.create!(attributes, &block)
800
+ end
801
+
802
+ def _scoping(scope)
803
+ previous, klass.current_scope = klass.current_scope(true), scope
804
+ yield
805
+ ensure
806
+ klass.current_scope = previous
807
+ end
808
+
809
+ def _substitute_values(values)
810
+ values.map do |name, value|
811
+ attr = table[name]
812
+ unless Arel.arel_node?(value)
813
+ type = klass.type_for_attribute(attr.name)
814
+ value = predicate_builder.build_bind_attribute(attr.name, type.cast(value))
815
+ end
816
+ [attr, value]
817
+ end
818
+ end
819
+
820
+ def _increment_attribute(attribute, value = 1)
821
+ bind = predicate_builder.build_bind_attribute(attribute.name, value.abs)
822
+ expr = table.coalesce(Arel::Nodes::UnqualifiedColumn.new(attribute), 0)
823
+ expr = value < 0 ? expr - bind : expr + bind
824
+ expr.expr
825
+ end
826
+
827
+ def exec_queries(&block)
828
+ skip_query_cache_if_necessary do
829
+ records =
830
+ if where_clause.contradiction?
831
+ []
832
+ elsif eager_loading?
833
+ apply_join_dependency do |relation, join_dependency|
834
+ if relation.null_relation?
835
+ []
836
+ else
837
+ relation = join_dependency.apply_column_aliases(relation)
838
+ rows = connection.select_all(relation.arel, "SQL")
839
+ join_dependency.instantiate(rows, strict_loading_value, &block)
840
+ end.freeze
841
+ end
842
+ else
843
+ klass.find_by_sql(arel, &block).freeze
844
+ end
845
+
846
+ preload_associations(records) unless skip_preloading_value
847
+
848
+ records.each(&:readonly!) if readonly_value
849
+ records.each(&:strict_loading!) if strict_loading_value
850
+
851
+ records
852
+ end
853
+ end
657
854
 
658
- def references_eager_loaded_tables?
659
- joined_tables = arel.join_sources.map do |join|
660
- if join.is_a?(Arel::Nodes::StringJoin)
661
- tables_in_string(join.left)
855
+ def skip_query_cache_if_necessary
856
+ if skip_query_cache_value
857
+ uncached do
858
+ yield
859
+ end
662
860
  else
663
- [join.left.table_name, join.left.table_alias]
861
+ yield
664
862
  end
665
863
  end
666
864
 
667
- joined_tables += [table.name, table.table_alias]
865
+ def build_preloader
866
+ ActiveRecord::Associations::Preloader.new
867
+ end
868
+
869
+ def references_eager_loaded_tables?
870
+ joined_tables = build_joins([]).flat_map do |join|
871
+ if join.is_a?(Arel::Nodes::StringJoin)
872
+ tables_in_string(join.left)
873
+ else
874
+ join.left.name
875
+ end
876
+ end
877
+
878
+ joined_tables << table.name
668
879
 
669
- # always convert table names to downcase as in Oracle quoted table names are in uppercase
670
- joined_tables = joined_tables.flatten.compact.map { |t| t.downcase }.uniq
880
+ # always convert table names to downcase as in Oracle quoted table names are in uppercase
881
+ joined_tables.map!(&:downcase)
671
882
 
672
- (references_values - joined_tables).any?
673
- end
883
+ !(references_values.map(&:to_s) - joined_tables).empty?
884
+ end
674
885
 
675
- def tables_in_string(string)
676
- return [] if string.blank?
677
- # always convert table names to downcase as in Oracle quoted table names are in uppercase
678
- # ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries
679
- string.scan(/([a-zA-Z_][.\w]+).?\./).flatten.map{ |s| s.downcase }.uniq - ['raw_sql_']
680
- end
886
+ def tables_in_string(string)
887
+ return [] if string.blank?
888
+ # always convert table names to downcase as in Oracle quoted table names are in uppercase
889
+ # ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries
890
+ string.scan(/[a-zA-Z_][.\w]+(?=.?\.)/).map!(&:downcase) - ["raw_sql_"]
891
+ end
681
892
  end
682
893
  end