activerecord 5.2.4.2 → 6.0.2

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 (269) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +714 -570
  3. data/MIT-LICENSE +3 -1
  4. data/README.rdoc +4 -2
  5. data/examples/performance.rb +1 -1
  6. data/lib/active_record.rb +9 -2
  7. data/lib/active_record/aggregations.rb +4 -2
  8. data/lib/active_record/association_relation.rb +15 -6
  9. data/lib/active_record/associations.rb +20 -15
  10. data/lib/active_record/associations/association.rb +61 -20
  11. data/lib/active_record/associations/association_scope.rb +4 -6
  12. data/lib/active_record/associations/belongs_to_association.rb +36 -42
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +0 -4
  14. data/lib/active_record/associations/builder/association.rb +14 -18
  15. data/lib/active_record/associations/builder/belongs_to.rb +19 -52
  16. data/lib/active_record/associations/builder/collection_association.rb +3 -13
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +17 -38
  18. data/lib/active_record/associations/builder/has_many.rb +2 -0
  19. data/lib/active_record/associations/builder/has_one.rb +35 -1
  20. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  21. data/lib/active_record/associations/collection_association.rb +12 -23
  22. data/lib/active_record/associations/collection_proxy.rb +12 -15
  23. data/lib/active_record/associations/foreign_association.rb +7 -0
  24. data/lib/active_record/associations/has_many_association.rb +2 -10
  25. data/lib/active_record/associations/has_many_through_association.rb +14 -14
  26. data/lib/active_record/associations/has_one_association.rb +28 -30
  27. data/lib/active_record/associations/has_one_through_association.rb +5 -5
  28. data/lib/active_record/associations/join_dependency.rb +28 -28
  29. data/lib/active_record/associations/join_dependency/join_association.rb +9 -10
  30. data/lib/active_record/associations/join_dependency/join_part.rb +2 -2
  31. data/lib/active_record/associations/preloader.rb +39 -31
  32. data/lib/active_record/associations/preloader/association.rb +38 -36
  33. data/lib/active_record/associations/preloader/through_association.rb +48 -39
  34. data/lib/active_record/associations/singular_association.rb +2 -16
  35. data/lib/active_record/attribute_assignment.rb +7 -10
  36. data/lib/active_record/attribute_methods.rb +28 -100
  37. data/lib/active_record/attribute_methods/before_type_cast.rb +4 -1
  38. data/lib/active_record/attribute_methods/dirty.rb +111 -40
  39. data/lib/active_record/attribute_methods/primary_key.rb +15 -22
  40. data/lib/active_record/attribute_methods/query.rb +2 -3
  41. data/lib/active_record/attribute_methods/read.rb +15 -53
  42. data/lib/active_record/attribute_methods/serialization.rb +1 -1
  43. data/lib/active_record/attribute_methods/time_zone_conversion.rb +1 -1
  44. data/lib/active_record/attribute_methods/write.rb +17 -24
  45. data/lib/active_record/attributes.rb +13 -0
  46. data/lib/active_record/autosave_association.rb +2 -2
  47. data/lib/active_record/base.rb +2 -3
  48. data/lib/active_record/callbacks.rb +5 -19
  49. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +104 -16
  50. data/lib/active_record/connection_adapters/abstract/database_limits.rb +17 -4
  51. data/lib/active_record/connection_adapters/abstract/database_statements.rb +99 -123
  52. data/lib/active_record/connection_adapters/abstract/query_cache.rb +18 -8
  53. data/lib/active_record/connection_adapters/abstract/quoting.rb +68 -17
  54. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +19 -12
  55. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +76 -48
  56. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +1 -3
  57. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +132 -53
  58. data/lib/active_record/connection_adapters/abstract/transaction.rb +96 -56
  59. data/lib/active_record/connection_adapters/abstract_adapter.rb +187 -43
  60. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +138 -195
  61. data/lib/active_record/connection_adapters/column.rb +17 -13
  62. data/lib/active_record/connection_adapters/connection_specification.rb +53 -43
  63. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +6 -10
  64. data/lib/active_record/connection_adapters/mysql/database_statements.rb +75 -13
  65. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -7
  66. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +3 -4
  67. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +40 -32
  68. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +14 -6
  69. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +129 -13
  70. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +6 -10
  71. data/lib/active_record/connection_adapters/mysql2_adapter.rb +26 -9
  72. data/lib/active_record/connection_adapters/postgresql/column.rb +17 -31
  73. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +22 -1
  74. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
  75. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
  76. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +1 -1
  77. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +1 -1
  78. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
  79. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +1 -1
  80. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +1 -1
  81. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +9 -7
  82. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +6 -3
  83. data/lib/active_record/connection_adapters/postgresql/quoting.rb +44 -7
  84. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +12 -1
  85. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -91
  86. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +55 -53
  87. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +24 -27
  88. data/lib/active_record/connection_adapters/postgresql_adapter.rb +164 -74
  89. data/lib/active_record/connection_adapters/schema_cache.rb +37 -14
  90. data/lib/active_record/connection_adapters/sql_type_metadata.rb +11 -8
  91. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
  92. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +42 -6
  93. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +42 -11
  94. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +129 -141
  95. data/lib/active_record/connection_handling.rb +155 -26
  96. data/lib/active_record/core.rb +103 -59
  97. data/lib/active_record/counter_cache.rb +4 -29
  98. data/lib/active_record/database_configurations.rb +233 -0
  99. data/lib/active_record/database_configurations/database_config.rb +37 -0
  100. data/lib/active_record/database_configurations/hash_config.rb +50 -0
  101. data/lib/active_record/database_configurations/url_config.rb +79 -0
  102. data/lib/active_record/dynamic_matchers.rb +1 -1
  103. data/lib/active_record/enum.rb +37 -7
  104. data/lib/active_record/errors.rb +15 -7
  105. data/lib/active_record/explain.rb +1 -1
  106. data/lib/active_record/fixture_set/model_metadata.rb +33 -0
  107. data/lib/active_record/fixture_set/render_context.rb +17 -0
  108. data/lib/active_record/fixture_set/table_row.rb +153 -0
  109. data/lib/active_record/fixture_set/table_rows.rb +47 -0
  110. data/lib/active_record/fixtures.rb +145 -472
  111. data/lib/active_record/gem_version.rb +4 -4
  112. data/lib/active_record/inheritance.rb +13 -3
  113. data/lib/active_record/insert_all.rb +179 -0
  114. data/lib/active_record/integration.rb +68 -16
  115. data/lib/active_record/internal_metadata.rb +10 -2
  116. data/lib/active_record/locking/optimistic.rb +5 -6
  117. data/lib/active_record/locking/pessimistic.rb +3 -3
  118. data/lib/active_record/log_subscriber.rb +7 -26
  119. data/lib/active_record/middleware/database_selector.rb +75 -0
  120. data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
  121. data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
  122. data/lib/active_record/migration.rb +100 -81
  123. data/lib/active_record/migration/command_recorder.rb +50 -6
  124. data/lib/active_record/migration/compatibility.rb +76 -49
  125. data/lib/active_record/model_schema.rb +33 -9
  126. data/lib/active_record/nested_attributes.rb +2 -2
  127. data/lib/active_record/no_touching.rb +7 -0
  128. data/lib/active_record/persistence.rb +228 -24
  129. data/lib/active_record/query_cache.rb +11 -4
  130. data/lib/active_record/querying.rb +32 -20
  131. data/lib/active_record/railtie.rb +80 -43
  132. data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
  133. data/lib/active_record/railties/controller_runtime.rb +30 -35
  134. data/lib/active_record/railties/databases.rake +199 -46
  135. data/lib/active_record/reflection.rb +32 -30
  136. data/lib/active_record/relation.rb +311 -80
  137. data/lib/active_record/relation/batches.rb +13 -10
  138. data/lib/active_record/relation/calculations.rb +53 -47
  139. data/lib/active_record/relation/delegation.rb +26 -43
  140. data/lib/active_record/relation/finder_methods.rb +23 -27
  141. data/lib/active_record/relation/merger.rb +11 -20
  142. data/lib/active_record/relation/predicate_builder.rb +4 -6
  143. data/lib/active_record/relation/predicate_builder/array_handler.rb +5 -4
  144. data/lib/active_record/relation/predicate_builder/association_query_value.rb +1 -4
  145. data/lib/active_record/relation/predicate_builder/base_handler.rb +1 -2
  146. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
  147. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +1 -4
  148. data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
  149. data/lib/active_record/relation/query_attribute.rb +13 -8
  150. data/lib/active_record/relation/query_methods.rb +213 -64
  151. data/lib/active_record/relation/spawn_methods.rb +1 -1
  152. data/lib/active_record/relation/where_clause.rb +14 -10
  153. data/lib/active_record/relation/where_clause_factory.rb +1 -2
  154. data/lib/active_record/result.rb +30 -11
  155. data/lib/active_record/sanitization.rb +32 -40
  156. data/lib/active_record/schema.rb +2 -11
  157. data/lib/active_record/schema_dumper.rb +22 -7
  158. data/lib/active_record/schema_migration.rb +5 -1
  159. data/lib/active_record/scoping.rb +8 -8
  160. data/lib/active_record/scoping/default.rb +4 -5
  161. data/lib/active_record/scoping/named.rb +20 -15
  162. data/lib/active_record/statement_cache.rb +30 -3
  163. data/lib/active_record/store.rb +87 -8
  164. data/lib/active_record/table_metadata.rb +10 -17
  165. data/lib/active_record/tasks/database_tasks.rb +194 -25
  166. data/lib/active_record/tasks/mysql_database_tasks.rb +5 -5
  167. data/lib/active_record/tasks/postgresql_database_tasks.rb +5 -7
  168. data/lib/active_record/tasks/sqlite_database_tasks.rb +2 -8
  169. data/lib/active_record/test_databases.rb +23 -0
  170. data/lib/active_record/test_fixtures.rb +225 -0
  171. data/lib/active_record/timestamp.rb +39 -25
  172. data/lib/active_record/touch_later.rb +4 -2
  173. data/lib/active_record/transactions.rb +56 -65
  174. data/lib/active_record/translation.rb +1 -1
  175. data/lib/active_record/type.rb +3 -4
  176. data/lib/active_record/type/adapter_specific_registry.rb +1 -8
  177. data/lib/active_record/type_caster/connection.rb +15 -14
  178. data/lib/active_record/type_caster/map.rb +1 -4
  179. data/lib/active_record/validations.rb +1 -0
  180. data/lib/active_record/validations/uniqueness.rb +15 -27
  181. data/lib/arel.rb +58 -0
  182. data/lib/arel/alias_predication.rb +9 -0
  183. data/lib/arel/attributes.rb +22 -0
  184. data/lib/arel/attributes/attribute.rb +37 -0
  185. data/lib/arel/collectors/bind.rb +24 -0
  186. data/lib/arel/collectors/composite.rb +31 -0
  187. data/lib/arel/collectors/plain_string.rb +20 -0
  188. data/lib/arel/collectors/sql_string.rb +20 -0
  189. data/lib/arel/collectors/substitute_binds.rb +28 -0
  190. data/lib/arel/crud.rb +42 -0
  191. data/lib/arel/delete_manager.rb +18 -0
  192. data/lib/arel/errors.rb +9 -0
  193. data/lib/arel/expressions.rb +29 -0
  194. data/lib/arel/factory_methods.rb +49 -0
  195. data/lib/arel/insert_manager.rb +49 -0
  196. data/lib/arel/math.rb +45 -0
  197. data/lib/arel/nodes.rb +68 -0
  198. data/lib/arel/nodes/and.rb +32 -0
  199. data/lib/arel/nodes/ascending.rb +23 -0
  200. data/lib/arel/nodes/binary.rb +52 -0
  201. data/lib/arel/nodes/bind_param.rb +36 -0
  202. data/lib/arel/nodes/case.rb +55 -0
  203. data/lib/arel/nodes/casted.rb +50 -0
  204. data/lib/arel/nodes/comment.rb +29 -0
  205. data/lib/arel/nodes/count.rb +12 -0
  206. data/lib/arel/nodes/delete_statement.rb +45 -0
  207. data/lib/arel/nodes/descending.rb +23 -0
  208. data/lib/arel/nodes/equality.rb +18 -0
  209. data/lib/arel/nodes/extract.rb +24 -0
  210. data/lib/arel/nodes/false.rb +16 -0
  211. data/lib/arel/nodes/full_outer_join.rb +8 -0
  212. data/lib/arel/nodes/function.rb +44 -0
  213. data/lib/arel/nodes/grouping.rb +8 -0
  214. data/lib/arel/nodes/in.rb +8 -0
  215. data/lib/arel/nodes/infix_operation.rb +80 -0
  216. data/lib/arel/nodes/inner_join.rb +8 -0
  217. data/lib/arel/nodes/insert_statement.rb +37 -0
  218. data/lib/arel/nodes/join_source.rb +20 -0
  219. data/lib/arel/nodes/matches.rb +18 -0
  220. data/lib/arel/nodes/named_function.rb +23 -0
  221. data/lib/arel/nodes/node.rb +50 -0
  222. data/lib/arel/nodes/node_expression.rb +13 -0
  223. data/lib/arel/nodes/outer_join.rb +8 -0
  224. data/lib/arel/nodes/over.rb +15 -0
  225. data/lib/arel/nodes/regexp.rb +16 -0
  226. data/lib/arel/nodes/right_outer_join.rb +8 -0
  227. data/lib/arel/nodes/select_core.rb +67 -0
  228. data/lib/arel/nodes/select_statement.rb +41 -0
  229. data/lib/arel/nodes/sql_literal.rb +16 -0
  230. data/lib/arel/nodes/string_join.rb +11 -0
  231. data/lib/arel/nodes/table_alias.rb +27 -0
  232. data/lib/arel/nodes/terminal.rb +16 -0
  233. data/lib/arel/nodes/true.rb +16 -0
  234. data/lib/arel/nodes/unary.rb +45 -0
  235. data/lib/arel/nodes/unary_operation.rb +20 -0
  236. data/lib/arel/nodes/unqualified_column.rb +22 -0
  237. data/lib/arel/nodes/update_statement.rb +41 -0
  238. data/lib/arel/nodes/values_list.rb +9 -0
  239. data/lib/arel/nodes/window.rb +126 -0
  240. data/lib/arel/nodes/with.rb +11 -0
  241. data/lib/arel/order_predications.rb +13 -0
  242. data/lib/arel/predications.rb +257 -0
  243. data/lib/arel/select_manager.rb +271 -0
  244. data/lib/arel/table.rb +110 -0
  245. data/lib/arel/tree_manager.rb +72 -0
  246. data/lib/arel/update_manager.rb +34 -0
  247. data/lib/arel/visitors.rb +20 -0
  248. data/lib/arel/visitors/depth_first.rb +204 -0
  249. data/lib/arel/visitors/dot.rb +297 -0
  250. data/lib/arel/visitors/ibm_db.rb +34 -0
  251. data/lib/arel/visitors/informix.rb +62 -0
  252. data/lib/arel/visitors/mssql.rb +157 -0
  253. data/lib/arel/visitors/mysql.rb +83 -0
  254. data/lib/arel/visitors/oracle.rb +159 -0
  255. data/lib/arel/visitors/oracle12.rb +66 -0
  256. data/lib/arel/visitors/postgresql.rb +110 -0
  257. data/lib/arel/visitors/sqlite.rb +39 -0
  258. data/lib/arel/visitors/to_sql.rb +889 -0
  259. data/lib/arel/visitors/visitor.rb +46 -0
  260. data/lib/arel/visitors/where_sql.rb +23 -0
  261. data/lib/arel/window_predications.rb +9 -0
  262. data/lib/rails/generators/active_record/migration.rb +14 -1
  263. data/lib/rails/generators/active_record/migration/migration_generator.rb +2 -5
  264. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +1 -1
  265. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +4 -2
  266. data/lib/rails/generators/active_record/model/model_generator.rb +1 -0
  267. data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
  268. metadata +110 -25
  269. data/lib/active_record/collection_cache_key.rb +0 -53
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f2ed511c798be3e1ce393344da64741731f35c02b4467f11fe143e8b91992cf
4
- data.tar.gz: 60a37dd0c0728d1d22d41ab2795ce376a5bcea4b3b1974314c98088459a7a849
3
+ metadata.gz: cd066bb5f36431ba8c01e43583145bfc944a55a7111cb2d9d53b64a1d2c68122
4
+ data.tar.gz: 567fcfaf568001b64de89198d5879feabb0e1fa623b563dee7ac0e1a74019220
5
5
  SHA512:
6
- metadata.gz: 6a53a681e41adab0127bcad09191dd9f3dcdd0da6eb939f885db6dd502efb94cb8b375f4b822cd4b02ad90153e5b1200f1bc606bc47c48a16d61f8144c4ba136
7
- data.tar.gz: 90d5f45f8a1968bb46e1479c81dc8b63b4196e5f7d4b293713abe37fd8303e1866a59cc4e76c9408b9284badd120a374704200b3f33c77e53342cdf8b9e87da9
6
+ metadata.gz: 53616ab7035f58aed77d710bf6391bb128bd8dab73e826d15f65685be872e2a0926a525b9be38b48f446684f58496b199a8721d9141051066b7644a5a41aa7c1
7
+ data.tar.gz: ea93e2b7e117fc2516173ca26ac8712af7f47e0d0b895e8bfd9063b66199f07cfb9c84ea0b12afafe0bd71b66168ca40e280e8152fabbecc82068d14c976b104
@@ -1,982 +1,1126 @@
1
- ## Rails 5.2.4.1 (December 18, 2019) ##
2
-
3
- * No changes.
1
+ ## Rails 6.0.2 (December 13, 2019) ##
4
2
 
3
+ * Share the same connection pool for primary and replica databases in the
4
+ transactional tests for the same database.
5
5
 
6
- ## Rails 5.2.4 (November 27, 2019) ##
7
-
8
- * Fix circular `autosave: true` causes invalid records to be saved.
6
+ *Edouard Chin*
9
7
 
10
- Prior to the fix, when there was a circular series of `autosave: true`
11
- associations, the callback for a `has_many` association was run while
12
- another instance of the same callback on the same association hadn't
13
- finished running. When control returned to the first instance of the
14
- callback, the instance variable had changed, and subsequent associated
15
- records weren't saved correctly. Specifically, the ID field for the
16
- `belongs_to` corresponding to the `has_many` was `nil`.
17
-
18
- Fixes #28080.
8
+ * Fix the preloader when one record is fetched using `after_initialize`
9
+ but not the entire collection.
19
10
 
20
- *Larry Reid*
11
+ *Bradley Price*
21
12
 
22
- * PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute.
13
+ * Fix collection callbacks not terminating when `:abort` is thrown.
23
14
 
24
- Fixes #36022.
15
+ *Edouard Chin*, *Ryuta Kamizono*
25
16
 
26
- *Ryuta Kamizono*
17
+ * Correctly deprecate `where.not` working as NOR for relations.
27
18
 
28
- * Fix sqlite3 collation parsing when using decimal columns.
19
+ 12a9664 deprecated where.not working as NOR, however
20
+ doing a relation query like `where.not(relation: { ... })`
21
+ wouldn't be properly deprecated and `where.not` would work as
22
+ NAND instead.
29
23
 
30
- *Martin R. Schuster*
24
+ *Edouard Chin*
31
25
 
32
- * Make ActiveRecord `ConnectionPool.connections` method thread-safe.
26
+ * Fix `db:migrate` task with multiple databases to restore the connection
27
+ to the previous database.
33
28
 
34
- Fixes #36465.
29
+ The migrate task iterates and establish a connection over each db
30
+ resulting in the last one to be used by subsequent rake tasks.
31
+ We should reestablish a connection to the connection that was
32
+ established before the migrate tasks was run
35
33
 
36
- *Jeff Doering*
34
+ *Edouard Chin*
37
35
 
38
- * Assign all attributes before calling `build` to ensure the child record is visible in
39
- `before_add` and `after_add` callbacks for `has_many :through` associations.
36
+ * Fix multi-threaded issue for `AcceptanceValidator`.
40
37
 
41
- Fixes #33249.
38
+ *Ryuta Kamizono*
42
39
 
43
- *Ryan H. Kerr*
44
40
 
41
+ ## Rails 6.0.1 (November 5, 2019) ##
45
42
 
46
- ## Rails 5.2.3 (March 27, 2019) ##
43
+ * Common Table Expressions are allowed on read-only connections.
47
44
 
48
- * Fix different `count` calculation when using `size` with manual `select` with DISTINCT.
45
+ *Chris Morris*
49
46
 
50
- Fixes #35214.
47
+ * New record instantiation respects `unscope`.
51
48
 
52
- *Juani Villarejo*
49
+ *Ryuta Kamizono*
53
50
 
54
- * Fix prepared statements caching to be enabled even when query caching is enabled.
51
+ * Fixed a case where `find_in_batches` could halt too early.
55
52
 
56
- *Ryuta Kamizono*
53
+ *Takayuki Nakata*
57
54
 
58
- * Don't allow `where` with invalid value matches to nil values.
55
+ * Autosaved associations always perform validations when a custom validation
56
+ context is used.
59
57
 
60
- Fixes #33624.
58
+ *Tekin Suleyman*
61
59
 
62
- *Ryuta Kamizono*
60
+ * `sql.active_record` notifications now include the `:connection` in
61
+ their payloads.
63
62
 
64
- * Restore an ability that class level `update` without giving ids.
63
+ *Eugene Kenny*
65
64
 
66
- Fixes #34743.
65
+ * A rollback encountered in an `after_commit` callback does not reset
66
+ previously-committed record state.
67
67
 
68
- *Ryuta Kamizono*
68
+ *Ryuta Kamizono*
69
69
 
70
- * Fix join table column quoting with SQLite.
70
+ * Fixed that join order was lost when eager-loading.
71
71
 
72
- *Gannon McGibbon*
72
+ *Ryuta Kamizono*
73
73
 
74
- * Ensure that `delete_all` on collection proxy returns affected count.
74
+ * `DESCRIBE` queries are allowed on read-only connections.
75
75
 
76
- *Ryuta Kamizono*
76
+ *Dylan Thacker-Smith*
77
77
 
78
- * Reset scope after delete on collection association to clear stale offsets of removed records.
78
+ * Fixed that records that had been `inspect`ed could not be marshaled.
79
79
 
80
- *Gannon McGibbon*
80
+ *Eugene Kenny*
81
81
 
82
+ * The connection pool reaper thread is respawned in forked processes. This
83
+ fixes that idle connections in forked processes wouldn't be reaped.
82
84
 
83
- ## Rails 5.2.2.1 (March 11, 2019) ##
85
+ *John Hawthorn*
84
86
 
85
- * No changes.
87
+ * The memoized result of `ActiveRecord::Relation#take` is properly cleared
88
+ when `ActiveRecord::Relation#reset` or `ActiveRecord::Relation#reload`
89
+ is called.
86
90
 
91
+ *Anmol Arora*
87
92
 
88
- ## Rails 5.2.2 (December 04, 2018) ##
93
+ * Fixed the performance regression for `primary_keys` introduced MySQL 8.0.
89
94
 
90
- * Do not ignore the scoping with query methods in the scope block.
95
+ *Hiroyuki Ishii*
91
96
 
92
- *Ryuta Kamizono*
97
+ * `insert`, `insert_all`, `upsert`, and `upsert_all` now clear the query cache.
93
98
 
94
- * Allow aliased attributes to be used in `#update_columns` and `#update`.
99
+ *Eugene Kenny*
95
100
 
96
- *Gannon McGibbon*
101
+ * Call `while_preventing_writes` directly from `connected_to`.
97
102
 
98
- * Allow spaces in postgres table names.
103
+ In some cases application authors want to use the database switching middleware and make explicit calls with `connected_to`. It's possible for an app to turn off writes and not turn them back on by the time we call `connected_to(role: :writing)`.
99
104
 
100
- Fixes issue where "user post" is misinterpreted as "\"user\".\"post\"" when quoting table names with the postgres
101
- adapter.
105
+ This change allows apps to fix this by assuming if a role is writing we want to allow writes, except in the case it's explicitly turned off.
102
106
 
103
- *Gannon McGibbon*
107
+ *Eileen M. Uchitelle*
104
108
 
105
- * Cached columns_hash fields should be excluded from ResultSet#column_types
109
+ * Improve detection of ActiveRecord::StatementTimeout with mysql2 adapter in the edge case when the query is terminated during filesort.
106
110
 
107
- PR #34528 addresses the inconsistent behaviour when attribute is defined for an ignored column. The following test
108
- was passing for SQLite and MySQL, but failed for PostgreSQL:
111
+ *Kir Shatrov*
109
112
 
110
- ```ruby
111
- class DeveloperName < ActiveRecord::Type::String
112
- def deserialize(value)
113
- "Developer: #{value}"
114
- end
115
- end
116
113
 
117
- class AttributedDeveloper < ActiveRecord::Base
118
- self.table_name = "developers"
114
+ ## Rails 6.0.0 (August 16, 2019) ##
119
115
 
120
- attribute :name, DeveloperName.new
116
+ * Preserve user supplied joins order as much as possible.
121
117
 
122
- self.ignored_columns += ["name"]
123
- end
118
+ Fixes #36761, #34328, #24281, #12953.
124
119
 
125
- developer = AttributedDeveloper.create
126
- developer.update_column :name, "name"
120
+ *Ryuta Kamizono*
127
121
 
128
- loaded_developer = AttributedDeveloper.where(id: developer.id).select("*").first
129
- puts loaded_developer.name # should be "Developer: name" but it's just "name"
130
- ```
122
+ * Make the DATABASE_URL env variable only affect the primary connection. Add new env variables for multiple databases.
131
123
 
132
- *Dmitry Tsepelev*
124
+ *John Crepezzi*, *Eileen Uchitelle*
133
125
 
134
- * Values of enum are frozen, raising an error when attempting to modify them.
126
+ * Add a warning for enum elements with 'not_' prefix.
135
127
 
136
- *Emmanuel Byrd*
128
+ class Foo
129
+ enum status: [:sent, :not_sent]
130
+ end
137
131
 
138
- * `update_columns` now correctly raises `ActiveModel::MissingAttributeError`
139
- if the attribute does not exist.
132
+ *Edu Depetris*
140
133
 
141
- *Sean Griffin*
134
+ * Make currency symbols optional for money column type in PostgreSQL
142
135
 
143
- * Do not use prepared statement in queries that have a large number of binds.
136
+ *Joel Schneider*
144
137
 
145
- *Ryuta Kamizono*
146
138
 
147
- * Fix query cache to load before first request.
139
+ ## Rails 6.0.0.rc2 (July 22, 2019) ##
148
140
 
149
- *Eileen M. Uchitelle*
141
+ * Add database_exists? method to connection adapters to check if a database exists.
150
142
 
151
- * Fix collection cache key with limit and custom select to avoid ambiguous timestamp column error.
143
+ *Guilherme Mansur*
152
144
 
153
- Fixes #33056.
145
+ * PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute.
154
146
 
155
- *Federico Martinez*
147
+ Fixes #36022.
156
148
 
157
- * Fix duplicated record creation when using nested attributes with `create_with`.
149
+ *Ryuta Kamizono*
158
150
 
159
- *Darwin Wu*
151
+ * Make ActiveRecord `ConnectionPool.connections` method thread-safe.
160
152
 
161
- * Fix regression setting children record in parent `before_save` callback.
153
+ Fixes #36465.
162
154
 
163
- *Guo Xiang Tan*
155
+ *Jeff Doering*
164
156
 
165
- * Prevent leaking of user's DB credentials on `rails db:create` failure.
157
+ * Fix sqlite3 collation parsing when using decimal columns.
166
158
 
167
- *bogdanvlviv*
159
+ *Martin R. Schuster*
168
160
 
169
- * Clear mutation tracker before continuing the around callbacks.
161
+ * Fix invalid schema when primary key column has a comment.
170
162
 
171
- *Yuya Tanaka*
163
+ Fixes #29966.
172
164
 
173
- * Prevent deadlocks when waiting for connection from pool.
165
+ *Guilherme Goettems Schneider*
174
166
 
175
- *Brent Wheeldon*
167
+ * Fix table comment also being applied to the primary key column.
176
168
 
177
- * Avoid extra scoping when using `Relation#update` that was causing this method to change the current scope.
169
+ *Guilherme Goettems Schneider*
178
170
 
179
- *Ryuta Kamizono*
171
+ * Fix merging left_joins to maintain its own `join_type` context.
180
172
 
181
- * Fix numericality validator not to be affected by custom getter.
173
+ Fixes #36103.
182
174
 
183
175
  *Ryuta Kamizono*
184
176
 
185
- * Fix bulk change table ignores comment option on PostgreSQL.
186
177
 
187
- *Yoshiyuki Kinjo*
178
+ ## Rails 6.0.0.rc1 (April 24, 2019) ##
188
179
 
180
+ * Add `touch` option to `has_one` association.
189
181
 
190
- ## Rails 5.2.1.1 (November 27, 2018) ##
182
+ *Abhay Nikam*
191
183
 
192
- * No changes.
184
+ * Deprecate `where.not` working as NOR and will be changed to NAND in Rails 6.1.
193
185
 
186
+ ```ruby
187
+ all = [treasures(:diamond), treasures(:sapphire), cars(:honda), treasures(:sapphire)]
188
+ assert_equal all, PriceEstimate.all.map(&:estimate_of)
189
+ ```
194
190
 
195
- ## Rails 5.2.1 (August 07, 2018) ##
191
+ In Rails 6.0:
196
192
 
197
- * PostgreSQL: Support new relkind for partitioned tables.
193
+ ```ruby
194
+ sapphire = treasures(:sapphire)
195
+
196
+ nor = all.reject { |e|
197
+ e.estimate_of_type == sapphire.class.polymorphic_name
198
+ }.reject { |e|
199
+ e.estimate_of_id == sapphire.id
200
+ }
201
+ assert_equal [cars(:honda)], nor
202
+
203
+ without_sapphire = PriceEstimate.where.not(
204
+ estimate_of_type: sapphire.class.polymorphic_name, estimate_of_id: sapphire.id
205
+ )
206
+ assert_equal nor, without_sapphire.map(&:estimate_of)
207
+ ```
198
208
 
199
- Fixes #33008.
209
+ In Rails 6.1:
200
210
 
201
- *Yannick Schutz*
211
+ ```ruby
212
+ sapphire = treasures(:sapphire)
202
213
 
203
- * Rollback parent transaction when children fails to update.
214
+ nand = all - [sapphire]
215
+ assert_equal [treasures(:diamond), cars(:honda)], nand
204
216
 
205
- *Guillaume Malette*
217
+ without_sapphire = PriceEstimate.where.not(
218
+ estimate_of_type: sapphire.class.polymorphic_name, estimate_of_id: sapphire.id
219
+ )
220
+ assert_equal nand, without_sapphire.map(&:estimate_of)
221
+ ```
206
222
 
207
- * Fix default value for MySQL time types with specified precision.
223
+ *Ryuta Kamizono*
208
224
 
209
- *Nikolay Kondratyev*
225
+ * Fix dirty tracking after rollback.
210
226
 
211
- * Fix `touch` option to behave consistently with `Persistence#touch` method.
227
+ Fixes #15018, #30167, #33868.
212
228
 
213
229
  *Ryuta Kamizono*
214
230
 
215
- * Fix `save` in `after_create_commit` won't invoke extra `after_create_commit`.
231
+ * Add `ActiveRecord::Relation#cache_version` to support recyclable cache keys via
232
+ the versioned entries in `ActiveSupport::Cache`. This also means that
233
+ `ActiveRecord::Relation#cache_key` will now return a stable key that does not
234
+ include the max timestamp or count any more.
216
235
 
217
- Fixes #32831.
236
+ NOTE: This feature is turned off by default, and `cache_key` will still return
237
+ cache keys with timestamps until you set `ActiveRecord::Base.collection_cache_versioning = true`.
238
+ That's the setting for all new apps on Rails 6.0+
218
239
 
219
- *Ryuta Kamizono*
240
+ *Lachlan Sylvester*
220
241
 
221
- * Fix logic on disabling commit callbacks so they are not called unexpectedly when errors occur.
242
+ * Fix dirty tracking for `touch` to track saved changes.
222
243
 
223
- *Brian Durand*
244
+ Fixes #33429.
224
245
 
225
- * Fix parent record should not get saved with duplicate children records.
246
+ *Ryuta Kamzono*
226
247
 
227
- Fixes #32940.
248
+ * `change_column_comment` and `change_table_comment` are invertible only if
249
+ `to` and `from` options are specified.
228
250
 
229
- *Santosh Wadghule*
251
+ *Yoshiyuki Kinjo*
230
252
 
231
- * Fix that association's after_touch is not called with counter cache.
253
+ * Don't call commit/rollback callbacks when a record isn't saved.
232
254
 
233
- Fixes #31559.
255
+ Fixes #29747.
234
256
 
235
257
  *Ryuta Kamizono*
236
258
 
237
- * `becomes` should clear the mutation tracker which is created in `after_initialize`.
259
+ * Fix circular `autosave: true` causes invalid records to be saved.
260
+
261
+ Prior to the fix, when there was a circular series of `autosave: true`
262
+ associations, the callback for a `has_many` association was run while
263
+ another instance of the same callback on the same association hadn't
264
+ finished running. When control returned to the first instance of the
265
+ callback, the instance variable had changed, and subsequent associated
266
+ records weren't saved correctly. Specifically, the ID field for the
267
+ `belongs_to` corresponding to the `has_many` was `nil`.
238
268
 
239
- Fixes #32867.
269
+ Fixes #28080.
240
270
 
241
- *Ryuta Kamizono*
271
+ *Larry Reid*
242
272
 
243
- * Allow a belonging to parent object to be created from a new record.
273
+ * Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options.
244
274
 
245
- *Jolyon Pawlyn*
275
+ Before:
246
276
 
247
- * Fix that building record with assigning multiple has_one associations
248
- wrongly persists through record.
277
+ ```ruby
278
+ add_column :items, :attr1, :binary, size: 10 # => ArgumentError
279
+ add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError
280
+ add_column :items, :attr3, :integer, limit: 10 # => ActiveRecordError
281
+ add_column :items, :attr4, :datetime, precision: 10 # => ActiveRecordError
282
+ ```
249
283
 
250
- Fixes #32511.
284
+ After:
251
285
 
252
- *Sam DeCesare*
286
+ ```ruby
287
+ add_column :items, :attr1, :binary, size: 10 # => ArgumentError
288
+ add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError
289
+ add_column :items, :attr3, :integer, limit: 10 # => ArgumentError
290
+ add_column :items, :attr4, :datetime, precision: 10 # => ArgumentError
291
+ ```
253
292
 
254
- * Fix relation merging when one of the relations is going to skip the
255
- query cache.
293
+ *Ryuta Kamizono*
256
294
 
257
- *James Williams*
295
+ * Association loading isn't to be affected by scoping consistently
296
+ whether preloaded / eager loaded or not, with the exception of `unscoped`.
258
297
 
298
+ Before:
259
299
 
260
- ## Rails 5.2.0 (April 09, 2018) ##
300
+ ```ruby
301
+ Post.where("1=0").scoping do
302
+ Comment.find(1).post # => nil
303
+ Comment.preload(:post).find(1).post # => #<Post id: 1, ...>
304
+ Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
305
+ end
306
+ ```
261
307
 
262
- * MySQL: Support mysql2 0.5.x.
308
+ After:
263
309
 
264
- *Aaron Stone*
310
+ ```ruby
311
+ Post.where("1=0").scoping do
312
+ Comment.find(1).post # => #<Post id: 1, ...>
313
+ Comment.preload(:post).find(1).post # => #<Post id: 1, ...>
314
+ Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
315
+ end
316
+ ```
265
317
 
266
- * Apply time column precision on assignment.
318
+ Fixes #34638, #35398.
267
319
 
268
- PR #20317 changed the behavior of datetime columns so that when they
269
- have a specified precision then on assignment the value is rounded to
270
- that precision. This behavior is now applied to time columns as well.
320
+ *Ryuta Kamizono*
271
321
 
272
- Fixes #30301.
322
+ * Add `rails db:prepare` to migrate or setup a database.
273
323
 
274
- *Andrew White*
324
+ Runs `db:migrate` if the database exists or `db:setup` if it doesn't.
275
325
 
276
- * Normalize time column values for SQLite database.
326
+ *Roberto Miranda*
277
327
 
278
- For legacy reasons, time columns in SQLite are stored as full datetimes
279
- because until #24542 the quoting for time columns didn't remove the date
280
- component. To ensure that values are consistent we now normalize the
281
- date component to 2001-01-01 on reading and writing.
328
+ * Add `after_save_commit` callback as shortcut for `after_commit :hook, on: [ :create, :update ]`.
282
329
 
283
- *Andrew White*
330
+ *DHH*
284
331
 
285
- * Ensure that the date component is removed when quoting times.
332
+ * Assign all attributes before calling `build` to ensure the child record is visible in
333
+ `before_add` and `after_add` callbacks for `has_many :through` associations.
286
334
 
287
- PR #24542 altered the quoting for time columns so that the date component
288
- was removed however it only removed it when it was 2001-01-01. Now the
289
- date component is removed irrespective of what the date is.
335
+ Fixes #33249.
290
336
 
291
- *Andrew White*
337
+ *Ryan H. Kerr*
292
338
 
293
- * Fix `dependent: :destroy` issue for has_one/belongs_to relationship where
294
- the parent class was getting deleted when the child was not.
339
+ * Add `ActiveRecord::Relation#extract_associated` for extracting associated records from a relation.
295
340
 
296
- Fixes #32022.
341
+ ```
342
+ account.memberships.extract_associated(:user)
343
+ # => Returns collection of User records
344
+ ```
297
345
 
298
- *Fernando Gorodscy*
346
+ *DHH*
299
347
 
300
- * Whitelist `NULLS FIRST` and `NULLS LAST` in order clauses too.
348
+ * Add `ActiveRecord::Relation#annotate` for adding SQL comments to its queries.
301
349
 
302
- *Xavier Noria*
350
+ For example:
303
351
 
304
- * Fix that after commit callbacks on update does not triggered when optimistic locking is enabled.
352
+ ```
353
+ Post.where(id: 123).annotate("this is a comment").to_sql
354
+ # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123 /* this is a comment */
355
+ ```
305
356
 
306
- *Ryuta Kamizono*
357
+ This can be useful in instrumentation or other analysis of issued queries.
307
358
 
308
- * Fix `#columns_for_distinct` of MySQL and PostgreSQL to make
309
- `ActiveRecord::FinderMethods#limited_ids_for` use correct primary key values
310
- even if `ORDER BY` columns include other table's primary key.
359
+ *Matt Yoho*
311
360
 
312
- Fixes #28364.
361
+ * Support Optimizer Hints.
313
362
 
314
- *Takumi Kagiyama*
363
+ In most databases, a way to control the optimizer is by using optimizer hints,
364
+ which can be specified within individual statements.
315
365
 
316
- * Make `reflection.klass` raise if `polymorphic?` not to be misused.
366
+ Example (for MySQL):
317
367
 
318
- Fixes #31876.
368
+ Topic.optimizer_hints("MAX_EXECUTION_TIME(50000)", "NO_INDEX_MERGE(topics)")
369
+ # SELECT /*+ MAX_EXECUTION_TIME(50000) NO_INDEX_MERGE(topics) */ `topics`.* FROM `topics`
319
370
 
320
- *Ryuta Kamizono*
371
+ Example (for PostgreSQL with pg_hint_plan):
321
372
 
322
- * PostgreSQL: Allow pg-1.0 gem to be used with Active Record.
373
+ Topic.optimizer_hints("SeqScan(topics)", "Parallel(topics 8)")
374
+ # SELECT /*+ SeqScan(topics) Parallel(topics 8) */ "topics".* FROM "topics"
323
375
 
324
- *Lars Kanis*
376
+ See also:
325
377
 
326
- * Deprecate `expand_hash_conditions_for_aggregates` without replacement.
327
- Using a `Relation` for performing queries is the prefered API.
378
+ * https://dev.mysql.com/doc/refman/8.0/en/optimizer-hints.html
379
+ * https://pghintplan.osdn.jp/pg_hint_plan.html
380
+ * https://docs.oracle.com/en/database/oracle/oracle-database/12.2/tgsql/influencing-the-optimizer.html
381
+ * https://docs.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query?view=sql-server-2017
382
+ * https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.admin.perf.doc/doc/c0070117.html
328
383
 
329
384
  *Ryuta Kamizono*
330
385
 
331
- * Fix not expanded problem when passing an Array object as argument to the where method using `composed_of` column.
386
+ * Fix query attribute method on user-defined attribute to be aware of typecasted value.
332
387
 
333
- ```
334
- david_balance = customers(:david).balance
335
- Customer.where(balance: [david_balance]).to_sql
388
+ For example, the following code no longer return false as casted non-empty string:
336
389
 
337
- # Before: WHERE `customers`.`balance` = NULL
338
- # After : WHERE `customers`.`balance` = 50
339
390
  ```
391
+ class Post < ActiveRecord::Base
392
+ attribute :user_defined_text, :text
393
+ end
340
394
 
341
- Fixes #31723.
395
+ Post.new(user_defined_text: "false").user_defined_text? # => true
396
+ ```
342
397
 
343
- *Yutaro Kanagawa*
398
+ *Yuji Kamijima*
344
399
 
345
- * Fix `count(:all)` with eager loading and having an order other than the driving table.
400
+ * Quote empty ranges like other empty enumerables.
346
401
 
347
- Fixes #31783.
402
+ *Patrick Rebsch*
348
403
 
349
- *Ryuta Kamizono*
404
+ * Add `insert_all`/`insert_all!`/`upsert_all` methods to `ActiveRecord::Persistence`,
405
+ allowing bulk inserts akin to the bulk updates provided by `update_all` and
406
+ bulk deletes by `delete_all`.
350
407
 
351
- * Clear the transaction state when an Active Record object is duped.
408
+ Supports skipping or upserting duplicates through the `ON CONFLICT` syntax
409
+ for PostgreSQL (9.5+) and SQLite (3.24+) and `ON DUPLICATE KEY UPDATE` syntax
410
+ for MySQL.
352
411
 
353
- Fixes #31670.
412
+ *Bob Lail*
354
413
 
355
- *Yuriy Ustushenko*
414
+ * Add `rails db:seed:replant` that truncates tables of each database
415
+ for current environment and loads the seeds.
356
416
 
357
- * Support for PostgreSQL foreign tables.
417
+ *bogdanvlviv*, *DHH*
358
418
 
359
- *fatkodima*
419
+ * Add `ActiveRecord::Base.connection.truncate` for SQLite3 adapter.
360
420
 
361
- * Fix relation merger issue with `left_outer_joins`.
421
+ *bogdanvlviv*
362
422
 
363
- *Mehmet Emin İNAÇ*
423
+ * Deprecate mismatched collation comparison for uniqueness validator.
364
424
 
365
- * Don't allow destroyed object mutation after `save` or `save!` is called.
425
+ Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1.
426
+ To continue case sensitive comparison on the case insensitive column,
427
+ pass `case_sensitive: true` option explicitly to the uniqueness validator.
366
428
 
367
429
  *Ryuta Kamizono*
368
430
 
369
- * Take into account association conditions when deleting through records.
431
+ * Add `reselect` method. This is a short-hand for `unscope(:select).select(fields)`.
370
432
 
371
- Fixes #18424.
433
+ Fixes #27340.
372
434
 
373
- *Piotr Jakubowski*
435
+ *Willian Gustavo Veiga*
374
436
 
375
- * Fix nested `has_many :through` associations on unpersisted parent instances.
437
+ * Add negative scopes for all enum values.
376
438
 
377
- For example, if you have
439
+ Example:
378
440
 
379
441
  class Post < ActiveRecord::Base
380
- belongs_to :author
381
- has_many :books, through: :author
382
- has_many :subscriptions, through: :books
383
- end
384
-
385
- class Author < ActiveRecord::Base
386
- has_one :post
387
- has_many :books
388
- has_many :subscriptions, through: :books
442
+ enum status: %i[ drafted active trashed ]
389
443
  end
390
444
 
391
- class Book < ActiveRecord::Base
392
- belongs_to :author
393
- has_many :subscriptions
394
- end
395
-
396
- class Subscription < ActiveRecord::Base
397
- belongs_to :book
398
- end
399
-
400
- Before:
401
-
402
- If `post` is not persisted, then `post.subscriptions` will be empty.
445
+ Post.not_drafted # => where.not(status: :drafted)
446
+ Post.not_active # => where.not(status: :active)
447
+ Post.not_trashed # => where.not(status: :trashed)
403
448
 
404
- After:
449
+ *DHH*
405
450
 
406
- If `post` is not persisted, then `post.subscriptions` can be set and used
407
- just like it would if `post` were persisted.
451
+ * Fix different `count` calculation when using `size` with manual `select` with DISTINCT.
408
452
 
409
- Fixes #16313.
453
+ Fixes #35214.
410
454
 
411
- *Zoltan Kiss*
455
+ *Juani Villarejo*
412
456
 
413
- * Fixed inconsistency with `first(n)` when used with `limit()`.
414
- The `first(n)` finder now respects the `limit()`, making it consistent
415
- with `relation.to_a.first(n)`, and also with the behavior of `last(n)`.
416
457
 
417
- Fixes #23979.
458
+ ## Rails 6.0.0.beta3 (March 11, 2019) ##
418
459
 
419
- *Brian Christian*
460
+ * No changes.
420
461
 
421
- * Use `count(:all)` in `HasManyAssociation#count_records` to prevent invalid
422
- SQL queries for association counting.
423
462
 
424
- *Klas Eskilson*
463
+ ## Rails 6.0.0.beta2 (February 25, 2019) ##
425
464
 
426
- * Fix to invoke callbacks when using `update_attribute`.
465
+ * Fix prepared statements caching to be enabled even when query caching is enabled.
427
466
 
428
- *Mike Busch*
467
+ *Ryuta Kamizono*
429
468
 
430
- * Fix `count(:all)` to correctly work `distinct` with custom SELECT list.
469
+ * Ensure `update_all` series cares about optimistic locking.
431
470
 
432
471
  *Ryuta Kamizono*
433
472
 
434
- * Using subselect for `delete_all` with `limit` or `offset`.
473
+ * Don't allow `where` with non numeric string matches to 0 values.
435
474
 
436
475
  *Ryuta Kamizono*
437
476
 
438
- * Undefine attribute methods on descendants when resetting column
439
- information.
477
+ * Introduce `ActiveRecord::Relation#destroy_by` and `ActiveRecord::Relation#delete_by`.
440
478
 
441
- *Chris Salzberg*
479
+ `destroy_by` allows relation to find all the records matching the condition and perform
480
+ `destroy_all` on the matched records.
442
481
 
443
- * Log database query callers.
482
+ Example:
444
483
 
445
- Add `verbose_query_logs` configuration option to display the caller
446
- of database queries in the log to facilitate N+1 query resolution
447
- and other debugging.
484
+ Person.destroy_by(name: 'David')
485
+ Person.destroy_by(name: 'David', rating: 4)
448
486
 
449
- Enabled in development only for new and upgraded applications. Not
450
- recommended for use in the production environment since it relies
451
- on Ruby's `Kernel#caller_locations` which is fairly slow.
487
+ david = Person.find_by(name: 'David')
488
+ david.posts.destroy_by(id: [1, 2, 3])
452
489
 
453
- *Olivier Lacan*
490
+ `delete_by` allows relation to find all the records matching the condition and perform
491
+ `delete_all` on the matched records.
454
492
 
455
- * Fix conflicts `counter_cache` with `touch: true` by optimistic locking.
493
+ Example:
456
494
 
457
- ```
458
- # create_table :posts do |t|
459
- # t.integer :comments_count, default: 0
460
- # t.integer :lock_version
461
- # t.timestamps
462
- # end
463
- class Post < ApplicationRecord
464
- end
495
+ Person.delete_by(name: 'David')
496
+ Person.delete_by(name: 'David', rating: 4)
465
497
 
466
- # create_table :comments do |t|
467
- # t.belongs_to :post
468
- # end
469
- class Comment < ApplicationRecord
470
- belongs_to :post, touch: true, counter_cache: true
471
- end
472
- ```
498
+ david = Person.find_by(name: 'David')
499
+ david.posts.delete_by(id: [1, 2, 3])
473
500
 
474
- Before:
475
- ```
476
- post = Post.create!
477
- # => begin transaction
478
- INSERT INTO "posts" ("created_at", "updated_at", "lock_version")
479
- VALUES ("2017-12-11 21:27:11.387397", "2017-12-11 21:27:11.387397", 0)
480
- commit transaction
501
+ *Abhay Nikam*
481
502
 
482
- comment = Comment.create!(post: post)
483
- # => begin transaction
484
- INSERT INTO "comments" ("post_id") VALUES (1)
503
+ * Don't allow `where` with invalid value matches to nil values.
485
504
 
486
- UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1,
487
- "lock_version" = COALESCE("lock_version", 0) + 1 WHERE "posts"."id" = 1
505
+ Fixes #33624.
488
506
 
489
- UPDATE "posts" SET "updated_at" = '2017-12-11 21:27:11.398330',
490
- "lock_version" = 1 WHERE "posts"."id" = 1 AND "posts"."lock_version" = 0
491
- rollback transaction
492
- # => ActiveRecord::StaleObjectError: Attempted to touch a stale object: Post.
507
+ *Ryuta Kamizono*
493
508
 
494
- Comment.take.destroy!
495
- # => begin transaction
496
- DELETE FROM "comments" WHERE "comments"."id" = 1
509
+ * SQLite3: Implement `add_foreign_key` and `remove_foreign_key`.
497
510
 
498
- UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) - 1,
499
- "lock_version" = COALESCE("lock_version", 0) + 1 WHERE "posts"."id" = 1
511
+ *Ryuta Kamizono*
500
512
 
501
- UPDATE "posts" SET "updated_at" = '2017-12-11 21:42:47.785901',
502
- "lock_version" = 1 WHERE "posts"."id" = 1 AND "posts"."lock_version" = 0
503
- rollback transaction
504
- # => ActiveRecord::StaleObjectError: Attempted to touch a stale object: Post.
505
- ```
513
+ * Deprecate using class level querying methods if the receiver scope
514
+ regarded as leaked. Use `klass.unscoped` to avoid the leaking scope.
506
515
 
507
- After:
508
- ```
509
- post = Post.create!
510
- # => begin transaction
511
- INSERT INTO "posts" ("created_at", "updated_at", "lock_version")
512
- VALUES ("2017-12-11 21:27:11.387397", "2017-12-11 21:27:11.387397", 0)
513
- commit transaction
516
+ *Ryuta Kamizono*
517
+
518
+ * Allow applications to automatically switch connections.
514
519
 
515
- comment = Comment.create!(post: post)
516
- # => begin transaction
517
- INSERT INTO "comments" ("post_id") VALUES (1)
520
+ Adds a middleware and configuration options that can be used in your
521
+ application to automatically switch between the writing and reading
522
+ database connections.
518
523
 
519
- UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1,
520
- "lock_version" = COALESCE("lock_version", 0) + 1,
521
- "updated_at" = '2017-12-11 21:37:09.802642' WHERE "posts"."id" = 1
522
- commit transaction
524
+ `GET` and `HEAD` requests will read from the replica unless there was
525
+ a write in the last 2 seconds, otherwise they will read from the primary.
526
+ Non-get requests will always write to the primary. The middleware accepts
527
+ an argument for a Resolver class and an Operations class where you are able
528
+ to change how the auto-switcher works to be most beneficial for your
529
+ application.
523
530
 
524
- comment.destroy!
525
- # => begin transaction
526
- DELETE FROM "comments" WHERE "comments"."id" = 1
531
+ To use the middleware in your application you can use the following
532
+ configuration options:
527
533
 
528
- UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) - 1,
529
- "lock_version" = COALESCE("lock_version", 0) + 1,
530
- "updated_at" = '2017-12-11 21:39:02.685520' WHERE "posts"."id" = 1
531
- commit transaction
534
+ ```
535
+ config.active_record.database_selector = { delay: 2.seconds }
536
+ config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
537
+ config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
532
538
  ```
533
539
 
534
- Fixes #31199.
540
+ To change the database selection strategy, pass a custom class to the
541
+ configuration options:
535
542
 
536
- *bogdanvlviv*
543
+ ```
544
+ config.active_record.database_selector = { delay: 10.seconds }
545
+ config.active_record.database_resolver = MyResolver
546
+ config.active_record.database_resolver_context = MyResolver::MyCookies
547
+ ```
537
548
 
538
- * Add support for PostgreSQL operator classes to `add_index`.
549
+ *Eileen M. Uchitelle*
539
550
 
540
- Example:
551
+ * MySQL: Support `:size` option to change text and blob size.
541
552
 
542
- add_index :users, :name, using: :gist, opclass: { name: :gist_trgm_ops }
553
+ *Ryuta Kamizono*
543
554
 
544
- *Greg Navis*
555
+ * Make `t.timestamps` with precision by default.
545
556
 
546
- * Don't allow scopes to be defined which conflict with instance methods on `Relation`.
557
+ *Ryuta Kamizono*
547
558
 
548
- Fixes #31120.
549
559
 
550
- *kinnrot*
560
+ ## Rails 6.0.0.beta1 (January 18, 2019) ##
551
561
 
552
- * Add new error class `QueryCanceled` which will be raised
553
- when canceling statement due to user request.
562
+ * Remove deprecated `#set_state` from the transaction object.
554
563
 
555
- *Ryuta Kamizono*
564
+ *Rafael Mendonça França*
556
565
 
557
- * Add `#up_only` to database migrations for code that is only relevant when
558
- migrating up, e.g. populating a new column.
566
+ * Remove deprecated `#supports_statement_cache?` from the database adapters.
559
567
 
560
- *Rich Daley*
568
+ *Rafael Mendonça França*
561
569
 
562
- * Require raw SQL fragments to be explicitly marked when used in
563
- relation query methods.
570
+ * Remove deprecated `#insert_fixtures` from the database adapters.
564
571
 
565
- Before:
566
- ```
567
- Article.order("LENGTH(title)")
568
- ```
572
+ *Rafael Mendonça França*
569
573
 
570
- After:
571
- ```
572
- Article.order(Arel.sql("LENGTH(title)"))
573
- ```
574
+ * Remove deprecated `ActiveRecord::ConnectionAdapters::SQLite3Adapter#valid_alter_table_type?`.
574
575
 
575
- This prevents SQL injection if applications use the [strongly
576
- discouraged] form `Article.order(params[:my_order])`, under the
577
- mistaken belief that only column names will be accepted.
576
+ *Rafael Mendonça França*
578
577
 
579
- Raw SQL strings will now cause a deprecation warning, which will
580
- become an UnknownAttributeReference error in Rails 6.0. Applications
581
- can opt in to the future behavior by setting `allow_unsafe_raw_sql`
582
- to `:disabled`.
578
+ * Do not allow passing the column name to `sum` when a block is passed.
583
579
 
584
- Common and judged-safe string values (such as simple column
585
- references) are unaffected:
586
- ```
587
- Article.order("title DESC")
588
- ```
580
+ *Rafael Mendonça França*
589
581
 
590
- *Ben Toews*
582
+ * Do not allow passing the column name to `count` when a block is passed.
591
583
 
592
- * `update_all` will now pass its values to `Type#cast` before passing them to
593
- `Type#serialize`. This means that `update_all(foo: 'true')` will properly
594
- persist a boolean.
584
+ *Rafael Mendonça França*
595
585
 
596
- *Sean Griffin*
586
+ * Remove delegation of missing methods in a relation to arel.
597
587
 
598
- * Add new error class `StatementTimeout` which will be raised
599
- when statement timeout exceeded.
588
+ *Rafael Mendonça França*
600
589
 
601
- *Ryuta Kamizono*
590
+ * Remove delegation of missing methods in a relation to private methods of the class.
602
591
 
603
- * Fix `bin/rails db:migrate` with specified `VERSION`.
604
- `bin/rails db:migrate` with empty VERSION behaves as without `VERSION`.
605
- Check a format of `VERSION`: Allow a migration version number
606
- or name of a migration file. Raise error if format of `VERSION` is invalid.
607
- Raise error if target migration doesn't exist.
592
+ *Rafael Mendonça França*
608
593
 
609
- *bogdanvlviv*
594
+ * Deprecate `config.active_record.sqlite3.represent_boolean_as_integer`.
610
595
 
611
- * Fixed a bug where column orders for an index weren't written to
612
- `db/schema.rb` when using the sqlite adapter.
596
+ *Rafael Mendonça França*
613
597
 
614
- Fixes #30902.
598
+ * Change `SQLite3Adapter` to always represent boolean values as integers.
615
599
 
616
- *Paul Kuruvilla*
600
+ *Rafael Mendonça França*
617
601
 
618
- * Remove deprecated method `#sanitize_conditions`.
602
+ * Remove ability to specify a timestamp name for `#cache_key`.
619
603
 
620
604
  *Rafael Mendonça França*
621
605
 
622
- * Remove deprecated method `#scope_chain`.
606
+ * Remove deprecated `ActiveRecord::Migrator.migrations_path=`.
623
607
 
624
608
  *Rafael Mendonça França*
625
609
 
626
- * Remove deprecated configuration `.error_on_ignored_order_or_limit`.
610
+ * Remove deprecated `expand_hash_conditions_for_aggregates`.
627
611
 
628
612
  *Rafael Mendonça França*
629
613
 
630
- * Remove deprecated arguments from `#verify!`.
614
+ * Set polymorphic type column to NULL on `dependent: :nullify` strategy.
631
615
 
632
- *Rafael Mendonça França*
616
+ On polymorphic associations both the foreign key and the foreign type columns will be set to NULL.
633
617
 
634
- * Remove deprecated argument `name` from `#indexes`.
618
+ *Laerti Papa*
635
619
 
636
- *Rafael Mendonça França*
620
+ * Allow permitted instance of `ActionController::Parameters` as argument of `ActiveRecord::Relation#exists?`.
637
621
 
638
- * Remove deprecated method `ActiveRecord::Migrator.schema_migrations_table_name`.
622
+ *Gannon McGibbon*
639
623
 
640
- *Rafael Mendonça França*
624
+ * Add support for endless ranges introduces in Ruby 2.6.
641
625
 
642
- * Remove deprecated method `supports_primary_key?`.
626
+ *Greg Navis*
643
627
 
644
- *Rafael Mendonça França*
628
+ * Deprecate passing `migrations_paths` to `connection.assume_migrated_upto_version`.
645
629
 
646
- * Remove deprecated method `supports_migrations?`.
630
+ *Ryuta Kamizono*
647
631
 
648
- *Rafael Mendonça França*
632
+ * MySQL: `ROW_FORMAT=DYNAMIC` create table option by default.
649
633
 
650
- * Remove deprecated methods `initialize_schema_migrations_table` and `initialize_internal_metadata_table`.
634
+ Since MySQL 5.7.9, the `innodb_default_row_format` option defines the default row
635
+ format for InnoDB tables. The default setting is `DYNAMIC`.
636
+ The row format is required for indexing on `varchar(255)` with `utf8mb4` columns.
651
637
 
652
- *Rafael Mendonça França*
638
+ *Ryuta Kamizono*
653
639
 
654
- * Raises when calling `lock!` in a dirty record.
640
+ * Fix join table column quoting with SQLite.
655
641
 
656
- *Rafael Mendonça França*
642
+ *Gannon McGibbon*
657
643
 
658
- * Remove deprecated support to passing a class to `:class_name` on associations.
644
+ * Allow disabling scopes generated by `ActiveRecord.enum`.
659
645
 
660
- *Rafael Mendonça França*
646
+ *Alfred Dominic*
661
647
 
662
- * Remove deprecated argument `default` from `index_name_exists?`.
648
+ * Ensure that `delete_all` on collection proxy returns affected count.
663
649
 
664
- *Rafael Mendonça França*
650
+ *Ryuta Kamizono*
665
651
 
666
- * Remove deprecated support to `quoted_id` when typecasting an Active Record object.
652
+ * Reset scope after delete on collection association to clear stale offsets of removed records.
667
653
 
668
- *Rafael Mendonça França*
654
+ *Gannon McGibbon*
669
655
 
670
- * Fix `bin/rails db:setup` and `bin/rails db:test:prepare` create wrong
671
- ar_internal_metadata's data for a test database.
656
+ * Add the ability to prevent writes to a database for the duration of a block.
672
657
 
673
- Before:
674
- ```
675
- $ RAILS_ENV=test rails dbconsole
676
- > SELECT * FROM ar_internal_metadata;
677
- key|value|created_at|updated_at
678
- environment|development|2017-09-11 23:14:10.815679|2017-09-11 23:14:10.815679
679
- ```
658
+ Allows the application to prevent writes to a database. This can be useful when
659
+ you're building out multiple databases and want to make sure you're not sending
660
+ writes when you want a read.
680
661
 
681
- After:
682
- ```
683
- $ RAILS_ENV=test rails dbconsole
684
- > SELECT * FROM ar_internal_metadata;
685
- key|value|created_at|updated_at
686
- environment|test|2017-09-11 23:14:10.815679|2017-09-11 23:14:10.815679
687
- ```
662
+ If `while_preventing_writes` is called and the query is considered a write
663
+ query the database will raise an exception regardless of whether the database
664
+ user is able to write.
688
665
 
689
- Fixes #26731.
666
+ This is not meant to be a catch-all for write queries but rather a way to enforce
667
+ read-only queries without opening a second connection. One purpose of this is to
668
+ catch accidental writes, not all writes.
690
669
 
691
- *bogdanvlviv*
670
+ *Eileen M. Uchitelle*
692
671
 
693
- * Fix longer sequence name detection for serial columns.
672
+ * Allow aliased attributes to be used in `#update_columns` and `#update`.
694
673
 
695
- Fixes #28332.
674
+ *Gannon McGibbon*
696
675
 
697
- *Ryuta Kamizono*
676
+ * Allow spaces in postgres table names.
698
677
 
699
- * MySQL: Don't lose `auto_increment: true` in the `db/schema.rb`.
678
+ Fixes issue where "user post" is misinterpreted as "\"user\".\"post\"" when quoting table names with the postgres adapter.
700
679
 
701
- Fixes #30894.
680
+ *Gannon McGibbon*
702
681
 
703
- *Ryuta Kamizono*
682
+ * Cached `columns_hash` fields should be excluded from `ResultSet#column_types`.
704
683
 
705
- * Fix `COUNT(DISTINCT ...)` for `GROUP BY` with `ORDER BY` and `LIMIT`.
684
+ PR #34528 addresses the inconsistent behaviour when attribute is defined for an ignored column. The following test
685
+ was passing for SQLite and MySQL, but failed for PostgreSQL:
706
686
 
707
- Fixes #30886.
687
+ ```ruby
688
+ class DeveloperName < ActiveRecord::Type::String
689
+ def deserialize(value)
690
+ "Developer: #{value}"
691
+ end
692
+ end
708
693
 
709
- *Ryuta Kamizono*
694
+ class AttributedDeveloper < ActiveRecord::Base
695
+ self.table_name = "developers"
710
696
 
711
- * PostgreSQL `tsrange` now preserves subsecond precision.
697
+ attribute :name, DeveloperName.new
712
698
 
713
- PostgreSQL 9.1+ introduced range types, and Rails added support for using
714
- this datatype in Active Record. However, the serialization of
715
- `PostgreSQL::OID::Range` was incomplete, because it did not properly
716
- cast the bounds that make up the range. This led to subseconds being
717
- dropped in SQL commands:
699
+ self.ignored_columns += ["name"]
700
+ end
718
701
 
719
- Before:
702
+ developer = AttributedDeveloper.create
703
+ developer.update_column :name, "name"
720
704
 
721
- connection.type_cast(tsrange.serialize(range_value))
722
- # => "[2010-01-01 13:30:00 UTC,2011-02-02 19:30:00 UTC)"
705
+ loaded_developer = AttributedDeveloper.where(id: developer.id).select("*").first
706
+ puts loaded_developer.name # should be "Developer: name" but it's just "name"
707
+ ```
723
708
 
724
- Now:
709
+ *Dmitry Tsepelev*
725
710
 
726
- connection.type_cast(tsrange.serialize(range_value))
727
- # => "[2010-01-01 13:30:00.670277,2011-02-02 19:30:00.745125)"
711
+ * Make the implicit order column configurable.
728
712
 
729
- *Thomas Cannon*
713
+ When calling ordered finder methods such as `first` or `last` without an
714
+ explicit order clause, ActiveRecord sorts records by primary key. This can
715
+ result in unpredictable and surprising behaviour when the primary key is
716
+ not an auto-incrementing integer, for example when it's a UUID. This change
717
+ makes it possible to override the column used for implicit ordering such
718
+ that `first` and `last` will return more predictable results.
730
719
 
731
- * Passing a `Set` to `Relation#where` now behaves the same as passing an
732
- array.
720
+ Example:
733
721
 
734
- *Sean Griffin*
722
+ class Project < ActiveRecord::Base
723
+ self.implicit_order_column = "created_at"
724
+ end
735
725
 
736
- * Use given algorithm while removing index from database.
726
+ *Tekin Suleyman*
737
727
 
738
- Fixes #24190.
728
+ * Bump minimum PostgreSQL version to 9.3.
739
729
 
740
- *Mehmet Emin İNAÇ*
730
+ *Yasuo Honda*
741
731
 
742
- * Update payload names for `sql.active_record` instrumentation to be
743
- more descriptive.
732
+ * Values of enum are frozen, raising an error when attempting to modify them.
744
733
 
745
- Fixes #30586.
734
+ *Emmanuel Byrd*
746
735
 
747
- *Jeremy Green*
736
+ * Move `ActiveRecord::StatementInvalid` SQL to error property and include binds as separate error property.
748
737
 
749
- * Add new error class `LockWaitTimeout` which will be raised
750
- when lock wait timeout exceeded.
738
+ `ActiveRecord::ConnectionAdapters::AbstractAdapter#translate_exception_class` now requires `binds` to be passed as the last argument.
751
739
 
752
- *Gabriel Courtemanche*
740
+ `ActiveRecord::ConnectionAdapters::AbstractAdapter#translate_exception` now requires `message`, `sql`, and `binds` to be passed as keyword arguments.
753
741
 
754
- * Remove deprecated `#migration_keys`.
742
+ Subclasses of `ActiveRecord::StatementInvalid` must now provide `sql:` and `binds:` arguments to `super`.
755
743
 
756
- *Ryuta Kamizono*
744
+ Example:
757
745
 
758
- * Automatically guess the inverse associations for STI.
746
+ ```
747
+ class MySubclassedError < ActiveRecord::StatementInvalid
748
+ def initialize(message, sql:, binds:)
749
+ super(message, sql: sql, binds: binds)
750
+ end
751
+ end
752
+ ```
759
753
 
760
- *Yuichiro Kaneko*
754
+ *Gannon McGibbon*
755
+
756
+ * Add an `:if_not_exists` option to `create_table`.
757
+
758
+ Example:
761
759
 
762
- * Ensure `sum` honors `distinct` on `has_many :through` associations.
760
+ create_table :posts, if_not_exists: true do |t|
761
+ t.string :title
762
+ end
763
763
 
764
- Fixes #16791.
764
+ That would execute:
765
765
 
766
- *Aaron Wortham*
766
+ CREATE TABLE IF NOT EXISTS posts (
767
+ ...
768
+ )
767
769
 
768
- * Add `binary` fixture helper method.
770
+ If the table already exists, `if_not_exists: false` (the default) raises an
771
+ exception whereas `if_not_exists: true` does nothing.
769
772
 
770
- *Atsushi Yoshida*
773
+ *fatkodima*, *Stefan Kanev*
771
774
 
772
- * When using `Relation#or`, extract the common conditions and put them before the OR condition.
775
+ * Defining an Enum as a Hash with blank key, or as an Array with a blank value, now raises an `ArgumentError`.
773
776
 
774
- *Maxime Handfield Lapointe*
777
+ *Christophe Maximin*
775
778
 
776
- * `Relation#or` now accepts two relations who have different values for
777
- `references` only, as `references` can be implicitly called by `where`.
779
+ * Adds support for multiple databases to `rails db:schema:cache:dump` and `rails db:schema:cache:clear`.
778
780
 
779
- Fixes #29411.
781
+ *Gannon McGibbon*
782
+
783
+ * `update_columns` now correctly raises `ActiveModel::MissingAttributeError`
784
+ if the attribute does not exist.
780
785
 
781
786
  *Sean Griffin*
782
787
 
783
- * `ApplicationRecord` is no longer generated when generating models. If you
784
- need to generate it, it can be created with `rails g application_record`.
788
+ * Add support for hash and URL configs in database hash of `ActiveRecord::Base.connected_to`.
789
+
790
+ ````
791
+ User.connected_to(database: { writing: "postgres://foo" }) do
792
+ User.create!(name: "Gannon")
793
+ end
794
+
795
+ config = { "adapter" => "sqlite3", "database" => "db/readonly.sqlite3" }
796
+ User.connected_to(database: { reading: config }) do
797
+ User.count
798
+ end
799
+ ````
800
+
801
+ *Gannon McGibbon*
785
802
 
786
- *Lisa Ugray*
803
+ * Support default expression for MySQL.
787
804
 
788
- * Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT` to keep the existing select list.
805
+ MySQL 8.0.13 and higher supports default value to be a function or expression.
806
+
807
+ https://dev.mysql.com/doc/refman/8.0/en/create-table.html
789
808
 
790
809
  *Ryuta Kamizono*
791
810
 
792
- * When a `has_one` association is destroyed by `dependent: destroy`,
793
- `destroyed_by_association` will now be set to the reflection, matching the
794
- behaviour of `has_many` associations.
811
+ * Support expression indexes for MySQL.
795
812
 
796
- *Lisa Ugray*
813
+ MySQL 8.0.13 and higher supports functional key parts that index
814
+ expression values rather than column or column prefix values.
797
815
 
798
- * Fix `unscoped(where: [columns])` removing the wrong bind values.
816
+ https://dev.mysql.com/doc/refman/8.0/en/create-index.html
799
817
 
800
- When the `where` is called on a relation after a `or`, unscoping the column of that later `where` removed
801
- bind values used by the `or` instead. (possibly other cases too)
818
+ *Ryuta Kamizono*
819
+
820
+ * Fix collection cache key with limit and custom select to avoid ambiguous timestamp column error.
821
+
822
+ Fixes #33056.
823
+
824
+ *Federico Martinez*
825
+
826
+ * Add basic API for connection switching to support multiple databases.
827
+
828
+ 1) Adds a `connects_to` method for models to connect to multiple databases. Example:
802
829
 
803
830
  ```
804
- Post.where(id: 1).or(Post.where(id: 2)).where(foo: 3).unscope(where: :foo).to_sql
805
- # Currently:
806
- # SELECT "posts".* FROM "posts" WHERE ("posts"."id" = 2 OR "posts"."id" = 3)
807
- # With fix:
808
- # SELECT "posts".* FROM "posts" WHERE ("posts"."id" = 1 OR "posts"."id" = 2)
831
+ class AnimalsModel < ApplicationRecord
832
+ self.abstract_class = true
833
+
834
+ connects_to database: { writing: :animals_primary, reading: :animals_replica }
835
+ end
836
+
837
+ class Dog < AnimalsModel
838
+ # connected to both the animals_primary db for writing and the animals_replica for reading
839
+ end
809
840
  ```
810
841
 
811
- *Maxime Handfield Lapointe*
842
+ 2) Adds a `connected_to` block method for switching connection roles or connecting to
843
+ a database that the model didn't connect to. Connecting to the database in this block is
844
+ useful when you have another defined connection, for example `slow_replica` that you don't
845
+ want to connect to by default but need in the console, or a specific code block.
812
846
 
813
- * Values constructed using multi-parameter assignment will now use the
814
- post-type-cast value for rendering in single-field form inputs.
847
+ ```
848
+ ActiveRecord::Base.connected_to(role: :reading) do
849
+ Dog.first # finds dog from replica connected to AnimalsBase
850
+ Book.first # doesn't have a reading connection, will raise an error
851
+ end
852
+ ```
815
853
 
816
- *Sean Griffin*
854
+ ```
855
+ ActiveRecord::Base.connected_to(database: :slow_replica) do
856
+ SlowReplicaModel.first # if the db config has a slow_replica configuration this will be used to do the lookup, otherwise this will throw an exception
857
+ end
858
+ ```
817
859
 
818
- * `Relation#joins` is no longer affected by the target model's
819
- `current_scope`, with the exception of `unscoped`.
860
+ *Eileen M. Uchitelle*
820
861
 
821
- Fixes #29338.
862
+ * Enum raises on invalid definition values
822
863
 
823
- *Sean Griffin*
864
+ When defining a Hash enum it can be easy to use `[]` instead of `{}`. This
865
+ commit checks that only valid definition values are provided, those can
866
+ be a Hash, an array of Symbols or an array of Strings. Otherwise it
867
+ raises an `ArgumentError`.
824
868
 
825
- * Change sqlite3 boolean serialization to use 1 and 0.
869
+ Fixes #33961
826
870
 
827
- SQLite natively recognizes 1 and 0 as true and false, but does not natively
828
- recognize 't' and 'f' as was previously serialized.
871
+ *Alberto Almagro*
829
872
 
830
- This change in serialization requires a migration of stored boolean data
831
- for SQLite databases, so it's implemented behind a configuration flag
832
- whose default false value is deprecated.
873
+ * Reloading associations now clears the Query Cache like `Persistence#reload` does.
833
874
 
834
- *Lisa Ugray*
875
+ ```
876
+ class Post < ActiveRecord::Base
877
+ has_one :category
878
+ belongs_to :author
879
+ has_many :comments
880
+ end
835
881
 
836
- * Skip query caching when working with batches of records (`find_each`, `find_in_batches`,
837
- `in_batches`).
882
+ # Each of the following will now clear the query cache.
883
+ post.reload_category
884
+ post.reload_author
885
+ post.comments.reload
886
+ ```
838
887
 
839
- Previously, records would be fetched in batches, but all records would be retained in memory
840
- until the end of the request or job.
888
+ *Christophe Maximin*
841
889
 
842
- *Eugene Kenny*
890
+ * Added `index` option for `change_table` migration helpers.
891
+ With this change you can create indexes while adding new
892
+ columns into the existing tables.
843
893
 
844
- * Prevent errors raised by `sql.active_record` notification subscribers from being converted into
845
- `ActiveRecord::StatementInvalid` exceptions.
894
+ Example:
846
895
 
847
- *Dennis Taylor*
896
+ change_table(:languages) do |t|
897
+ t.string :country_code, index: true
898
+ end
848
899
 
849
- * Fix eager loading/preloading association with scope including joins.
900
+ *Mehmet Emin İNAÇ*
901
+
902
+ * Fix `transaction` reverting for migrations.
903
+
904
+ Before: Commands inside a `transaction` in a reverted migration ran uninverted.
905
+ Now: This change fixes that by reverting commands inside `transaction` block.
906
+
907
+ *fatkodima*, *David Verhasselt*
850
908
 
851
- Fixes #28324.
909
+ * Raise an error instead of scanning the filesystem root when `fixture_path` is blank.
910
+
911
+ *Gannon McGibbon*, *Max Albrecht*
912
+
913
+ * Allow `ActiveRecord::Base.configurations=` to be set with a symbolized hash.
914
+
915
+ *Gannon McGibbon*
916
+
917
+ * Don't update counter cache unless the record is actually saved.
918
+
919
+ Fixes #31493, #33113, #33117.
852
920
 
853
921
  *Ryuta Kamizono*
854
922
 
855
- * Fix transactions to apply state to child transactions.
923
+ * Deprecate `ActiveRecord::Result#to_hash` in favor of `ActiveRecord::Result#to_a`.
856
924
 
857
- Previously, if you had a nested transaction and the outer transaction was rolledback, the record from the
858
- inner transaction would still be marked as persisted.
925
+ *Gannon McGibbon*, *Kevin Cheng*
859
926
 
860
- This change fixes that by applying the state of the parent transaction to the child transaction when the
861
- parent transaction is rolledback. This will correctly mark records from the inner transaction as not persisted.
927
+ * SQLite3 adapter supports expression indexes.
862
928
 
863
- *Eileen M. Uchitelle*, *Aaron Patterson*
929
+ ```
930
+ create_table :users do |t|
931
+ t.string :email
932
+ end
864
933
 
865
- * Deprecate `set_state` method in `TransactionState`.
934
+ add_index :users, 'lower(email)', name: 'index_users_on_email', unique: true
935
+ ```
866
936
 
867
- Deprecated the `set_state` method in favor of setting the state via specific methods. If you need to mark the
868
- state of the transaction you can now use `rollback!`, `commit!` or `nullify!` instead of
869
- `set_state(:rolledback)`, `set_state(:committed)`, or `set_state(nil)`.
937
+ *Gray Kemmey*
870
938
 
871
- *Eileen M. Uchitelle*, *Aaron Patterson*
939
+ * Allow subclasses to redefine autosave callbacks for associated records.
940
+
941
+ Fixes #33305.
942
+
943
+ *Andrey Subbota*
944
+
945
+ * Bump minimum MySQL version to 5.5.8.
946
+
947
+ *Yasuo Honda*
948
+
949
+ * Use MySQL utf8mb4 character set by default.
950
+
951
+ `utf8mb4` character set with 4-Byte encoding supports supplementary characters including emoji.
952
+ The previous default 3-Byte encoding character set `utf8` is not enough to support them.
953
+
954
+ *Yasuo Honda*
955
+
956
+ * Fix duplicated record creation when using nested attributes with `create_with`.
957
+
958
+ *Darwin Wu*
959
+
960
+ * Configuration item `config.filter_parameters` could also filter out
961
+ sensitive values of database columns when calling `#inspect`.
962
+ We also added `ActiveRecord::Base::filter_attributes`/`=` in order to
963
+ specify sensitive attributes to specific model.
872
964
 
873
- * Deprecate delegating to `arel` in `Relation`.
965
+ ```
966
+ Rails.application.config.filter_parameters += [:credit_card_number, /phone/]
967
+ Account.last.inspect # => #<Account id: 123, name: "DHH", credit_card_number: [FILTERED], telephone_number: [FILTERED] ...>
968
+ SecureAccount.filter_attributes += [:name]
969
+ SecureAccount.last.inspect # => #<SecureAccount id: 42, name: [FILTERED], credit_card_number: [FILTERED] ...>
970
+ ```
971
+
972
+ *Zhang Kang*, *Yoshiyuki Kinjo*
973
+
974
+ * Deprecate `column_name_length`, `table_name_length`, `columns_per_table`,
975
+ `indexes_per_table`, `columns_per_multicolumn_index`, `sql_query_length`,
976
+ and `joins_per_query` methods in `DatabaseLimits`.
874
977
 
875
978
  *Ryuta Kamizono*
876
979
 
877
- * Query cache was unavailable when entering the `ActiveRecord::Base.cache` block
878
- without being connected.
980
+ * `ActiveRecord::Base.configurations` now returns an object.
879
981
 
880
- *Tsukasa Oishi*
982
+ `ActiveRecord::Base.configurations` used to return a hash, but this
983
+ is an inflexible data model. In order to improve multiple-database
984
+ handling in Rails, we've changed this to return an object. Some methods
985
+ are provided to make the object behave hash-like in order to ease the
986
+ transition process. Since most applications don't manipulate the hash
987
+ we've decided to add backwards-compatible functionality that will throw
988
+ a deprecation warning if used, however calling `ActiveRecord::Base.configurations`
989
+ will use the new version internally and externally.
881
990
 
882
- * Previously, when building records using a `has_many :through` association,
883
- if the child records were deleted before the parent was saved, they would
884
- still be persisted. Now, if child records are deleted before the parent is saved
885
- on a `has_many :through` association, the child records will not be persisted.
991
+ For example, the following `database.yml`:
886
992
 
887
- *Tobias Kraze*
993
+ ```
994
+ development:
995
+ adapter: sqlite3
996
+ database: db/development.sqlite3
997
+ ```
888
998
 
889
- * Merging two relations representing nested joins no longer transforms the joins of
890
- the merged relation into LEFT OUTER JOIN.
999
+ Used to become a hash:
891
1000
 
892
- Example:
1001
+ ```
1002
+ { "development" => { "adapter" => "sqlite3", "database" => "db/development.sqlite3" } }
1003
+ ```
1004
+
1005
+ Is now converted into the following object:
1006
+
1007
+ ```
1008
+ #<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800 @configurations=[
1009
+ #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
1010
+ @spec_name="primary", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>
1011
+ ]
1012
+ ```
893
1013
 
1014
+ Iterating over the database configurations has also changed. Instead of
1015
+ calling hash methods on the `configurations` hash directly, a new method `configs_for` has
1016
+ been provided that allows you to select the correct configuration. `env_name` and
1017
+ `spec_name` arguments are optional. For example, these return an array of
1018
+ database config objects for the requested environment and a single database config object
1019
+ will be returned for the requested environment and specification name respectively.
1020
+
1021
+ ```
1022
+ ActiveRecord::Base.configurations.configs_for(env_name: "development")
1023
+ ActiveRecord::Base.configurations.configs_for(env_name: "development", spec_name: "primary")
894
1024
  ```
895
- Author.joins(:posts).merge(Post.joins(:comments))
896
- # Before the change:
897
- #=> SELECT ... FROM authors INNER JOIN posts ON ... LEFT OUTER JOIN comments ON...
898
1025
 
899
- # After the change:
900
- #=> SELECT ... FROM authors INNER JOIN posts ON ... INNER JOIN comments ON...
1026
+ *Eileen M. Uchitelle*, *Aaron Patterson*
1027
+
1028
+ * Add database configuration to disable advisory locks.
1029
+
1030
+ ```
1031
+ production:
1032
+ adapter: postgresql
1033
+ advisory_locks: false
901
1034
  ```
902
1035
 
903
- *Maxime Handfield Lapointe*
1036
+ *Guo Xiang*
904
1037
 
905
- * `ActiveRecord::Persistence#touch` does not work well when optimistic locking enabled and
906
- `locking_column`, without default value, is null in the database.
1038
+ * SQLite3 adapter `alter_table` method restores foreign keys.
907
1039
 
908
- *bogdanvlviv*
1040
+ *Yasuo Honda*
909
1041
 
910
- * Fix destroying existing object does not work well when optimistic locking enabled and
911
- `locking_column` is null in the database.
1042
+ * Allow `:to_table` option to `invert_remove_foreign_key`.
912
1043
 
913
- *bogdanvlviv*
1044
+ Example:
914
1045
 
915
- * Use bulk INSERT to insert fixtures for better performance.
1046
+ remove_foreign_key :accounts, to_table: :owners
916
1047
 
917
- *Kir Shatrov*
1048
+ *Nikolay Epifanov*, *Rich Chen*
918
1049
 
919
- * Prevent creation of bind param if casted value is nil.
1050
+ * Add environment & load_config dependency to `bin/rake db:seed` to enable
1051
+ seed load in environments without Rails and custom DB configuration
920
1052
 
921
- *Ryuta Kamizono*
1053
+ *Tobias Bielohlawek*
922
1054
 
923
- * Deprecate passing arguments and block at the same time to `count` and `sum` in `ActiveRecord::Calculations`.
1055
+ * Fix default value for mysql time types with specified precision.
1056
+
1057
+ *Nikolay Kondratyev*
1058
+
1059
+ * Fix `touch` option to behave consistently with `Persistence#touch` method.
924
1060
 
925
1061
  *Ryuta Kamizono*
926
1062
 
927
- * Loading model schema from database is now thread-safe.
1063
+ * Migrations raise when duplicate column definition.
928
1064
 
929
- Fixes #28589.
1065
+ Fixes #33024.
930
1066
 
931
- *Vikrant Chaudhary*, *David Abdemoulaie*
1067
+ *Federico Martinez*
932
1068
 
933
- * Add `ActiveRecord::Base#cache_version` to support recyclable cache keys via the new versioned entries
934
- in `ActiveSupport::Cache`. This also means that `ActiveRecord::Base#cache_key` will now return a stable key
935
- that does not include a timestamp any more.
1069
+ * Bump minimum SQLite version to 3.8
936
1070
 
937
- NOTE: This feature is turned off by default, and `#cache_key` will still return cache keys with timestamps
938
- until you set `ActiveRecord::Base.cache_versioning = true`. That's the setting for all new apps on Rails 5.2+
1071
+ *Yasuo Honda*
939
1072
 
940
- *DHH*
1073
+ * Fix parent record should not get saved with duplicate children records.
941
1074
 
942
- * Respect `SchemaDumper.ignore_tables` in rake tasks for databases structure dump.
1075
+ Fixes #32940.
943
1076
 
944
- *Rusty Geldmacher*, *Guillermo Iguaran*
1077
+ *Santosh Wadghule*
945
1078
 
946
- * Add type caster to `RuntimeReflection#alias_name`.
1079
+ * Fix logic on disabling commit callbacks so they are not called unexpectedly when errors occur.
947
1080
 
948
- Fixes #28959.
1081
+ *Brian Durand*
949
1082
 
950
- *Jon Moss*
1083
+ * Ensure `Associations::CollectionAssociation#size` and `Associations::CollectionAssociation#empty?`
1084
+ use loaded association ids if present.
951
1085
 
952
- * Deprecate `supports_statement_cache?`.
1086
+ *Graham Turner*
953
1087
 
954
- *Ryuta Kamizono*
1088
+ * Add support to preload associations of polymorphic associations when not all the records have the requested associations.
955
1089
 
956
- * Raise error `UnknownMigrationVersionError` on the movement of migrations
957
- when the current migration does not exist.
1090
+ *Dana Sherson*
958
1091
 
959
- *bogdanvlviv*
1092
+ * Add `touch_all` method to `ActiveRecord::Relation`.
960
1093
 
961
- * Fix `bin/rails db:forward` first migration.
1094
+ Example:
962
1095
 
963
- *bogdanvlviv*
1096
+ Person.where(name: "David").touch_all(time: Time.new(2020, 5, 16, 0, 0, 0))
964
1097
 
965
- * Support Descending Indexes for MySQL.
1098
+ *fatkodima*, *duggiefresh*
966
1099
 
967
- MySQL 8.0.1 and higher supports descending indexes: `DESC` in an index definition is no longer ignored.
968
- See https://dev.mysql.com/doc/refman/8.0/en/descending-indexes.html.
1100
+ * Add `ActiveRecord::Base.base_class?` predicate.
969
1101
 
970
- *Ryuta Kamizono*
1102
+ *Bogdan Gusiev*
971
1103
 
972
- * Fix inconsistency with changed attributes when overriding Active Record attribute reader.
1104
+ * Add custom prefix/suffix options to `ActiveRecord::Store.store_accessor`.
973
1105
 
974
- *bogdanvlviv*
1106
+ *Tan Huynh*, *Yukio Mizuta*
1107
+
1108
+ * Rails 6 requires Ruby 2.5.0 or newer.
1109
+
1110
+ *Jeremy Daer*, *Kasper Timm Hansen*
975
1111
 
976
- * When calling the dynamic fixture accessor method with no arguments, it now returns all fixtures of this type.
977
- Previously this method always returned an empty array.
1112
+ * Deprecate `update_attributes`/`!` in favor of `update`/`!`.
978
1113
 
979
- *Kevin McPhillips*
1114
+ *Eddie Lebow*
1115
+
1116
+ * Add `ActiveRecord::Base.create_or_find_by`/`!` to deal with the SELECT/INSERT race condition in
1117
+ `ActiveRecord::Base.find_or_create_by`/`!` by leaning on unique constraints in the database.
1118
+
1119
+ *DHH*
1120
+
1121
+ * Add `Relation#pick` as short-hand for single-value plucks.
1122
+
1123
+ *DHH*
980
1124
 
981
1125
 
982
- Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/activerecord/CHANGELOG.md) for previous changes.
1126
+ Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activerecord/CHANGELOG.md) for previous changes.