activerecord 6.0.3.4 → 6.1.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 (245) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +891 -695
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +3 -3
  5. data/lib/active_record.rb +7 -14
  6. data/lib/active_record/aggregations.rb +5 -5
  7. data/lib/active_record/association_relation.rb +30 -12
  8. data/lib/active_record/associations.rb +118 -11
  9. data/lib/active_record/associations/alias_tracker.rb +19 -15
  10. data/lib/active_record/associations/association.rb +44 -28
  11. data/lib/active_record/associations/association_scope.rb +19 -15
  12. data/lib/active_record/associations/belongs_to_association.rb +22 -8
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -3
  14. data/lib/active_record/associations/builder/association.rb +32 -5
  15. data/lib/active_record/associations/builder/belongs_to.rb +10 -7
  16. data/lib/active_record/associations/builder/collection_association.rb +5 -4
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +0 -1
  18. data/lib/active_record/associations/builder/has_many.rb +6 -2
  19. data/lib/active_record/associations/builder/has_one.rb +11 -14
  20. data/lib/active_record/associations/builder/singular_association.rb +1 -1
  21. data/lib/active_record/associations/collection_association.rb +19 -6
  22. data/lib/active_record/associations/collection_proxy.rb +13 -5
  23. data/lib/active_record/associations/foreign_association.rb +13 -0
  24. data/lib/active_record/associations/has_many_association.rb +24 -2
  25. data/lib/active_record/associations/has_many_through_association.rb +10 -4
  26. data/lib/active_record/associations/has_one_association.rb +15 -1
  27. data/lib/active_record/associations/join_dependency.rb +72 -50
  28. data/lib/active_record/associations/join_dependency/join_association.rb +39 -16
  29. data/lib/active_record/associations/join_dependency/join_part.rb +3 -3
  30. data/lib/active_record/associations/preloader.rb +11 -5
  31. data/lib/active_record/associations/preloader/association.rb +51 -25
  32. data/lib/active_record/associations/preloader/through_association.rb +2 -2
  33. data/lib/active_record/associations/singular_association.rb +1 -1
  34. data/lib/active_record/associations/through_association.rb +1 -1
  35. data/lib/active_record/attribute_assignment.rb +10 -8
  36. data/lib/active_record/attribute_methods.rb +64 -54
  37. data/lib/active_record/attribute_methods/before_type_cast.rb +13 -9
  38. data/lib/active_record/attribute_methods/dirty.rb +1 -11
  39. data/lib/active_record/attribute_methods/primary_key.rb +6 -2
  40. data/lib/active_record/attribute_methods/query.rb +3 -6
  41. data/lib/active_record/attribute_methods/read.rb +8 -11
  42. data/lib/active_record/attribute_methods/serialization.rb +11 -5
  43. data/lib/active_record/attribute_methods/time_zone_conversion.rb +12 -13
  44. data/lib/active_record/attribute_methods/write.rb +12 -20
  45. data/lib/active_record/attributes.rb +33 -8
  46. data/lib/active_record/autosave_association.rb +57 -40
  47. data/lib/active_record/base.rb +2 -14
  48. data/lib/active_record/callbacks.rb +152 -22
  49. data/lib/active_record/coders/yaml_column.rb +1 -1
  50. data/lib/active_record/connection_adapters.rb +50 -0
  51. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +191 -134
  52. data/lib/active_record/connection_adapters/abstract/database_limits.rb +2 -44
  53. data/lib/active_record/connection_adapters/abstract/database_statements.rb +65 -22
  54. data/lib/active_record/connection_adapters/abstract/query_cache.rb +3 -8
  55. data/lib/active_record/connection_adapters/abstract/quoting.rb +34 -34
  56. data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
  57. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -116
  58. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +116 -27
  59. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +3 -3
  60. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +228 -83
  61. data/lib/active_record/connection_adapters/abstract/transaction.rb +80 -32
  62. data/lib/active_record/connection_adapters/abstract_adapter.rb +54 -72
  63. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +133 -96
  64. data/lib/active_record/connection_adapters/column.rb +15 -1
  65. data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
  66. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +31 -0
  67. data/lib/active_record/connection_adapters/mysql/database_statements.rb +23 -25
  68. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -1
  69. data/lib/active_record/connection_adapters/mysql/quoting.rb +1 -1
  70. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +32 -6
  71. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +8 -0
  72. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +5 -2
  73. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +11 -7
  74. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +10 -1
  75. data/lib/active_record/connection_adapters/mysql2_adapter.rb +31 -12
  76. data/lib/active_record/connection_adapters/pool_config.rb +73 -0
  77. data/lib/active_record/connection_adapters/pool_manager.rb +43 -0
  78. data/lib/active_record/connection_adapters/postgresql/column.rb +24 -1
  79. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +13 -54
  80. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  81. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -5
  82. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +2 -2
  83. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +10 -2
  84. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -2
  86. data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -2
  88. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +24 -5
  89. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +11 -1
  90. data/lib/active_record/connection_adapters/postgresql/quoting.rb +4 -4
  91. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +1 -1
  92. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +5 -1
  93. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +61 -29
  94. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +8 -0
  95. data/lib/active_record/connection_adapters/postgresql_adapter.rb +73 -58
  96. data/lib/active_record/connection_adapters/schema_cache.rb +98 -15
  97. data/lib/active_record/connection_adapters/sql_type_metadata.rb +10 -0
  98. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +31 -6
  99. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +1 -1
  100. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -1
  101. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +37 -4
  102. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +49 -50
  103. data/lib/active_record/connection_handling.rb +218 -71
  104. data/lib/active_record/core.rb +245 -61
  105. data/lib/active_record/database_configurations.rb +124 -85
  106. data/lib/active_record/database_configurations/connection_url_resolver.rb +98 -0
  107. data/lib/active_record/database_configurations/database_config.rb +52 -9
  108. data/lib/active_record/database_configurations/hash_config.rb +54 -8
  109. data/lib/active_record/database_configurations/url_config.rb +15 -40
  110. data/lib/active_record/delegated_type.rb +209 -0
  111. data/lib/active_record/destroy_association_async_job.rb +36 -0
  112. data/lib/active_record/enum.rb +82 -38
  113. data/lib/active_record/errors.rb +47 -12
  114. data/lib/active_record/explain.rb +9 -4
  115. data/lib/active_record/explain_subscriber.rb +1 -1
  116. data/lib/active_record/fixture_set/file.rb +10 -17
  117. data/lib/active_record/fixture_set/model_metadata.rb +1 -2
  118. data/lib/active_record/fixture_set/render_context.rb +1 -1
  119. data/lib/active_record/fixture_set/table_row.rb +2 -2
  120. data/lib/active_record/fixtures.rb +58 -9
  121. data/lib/active_record/gem_version.rb +3 -3
  122. data/lib/active_record/inheritance.rb +40 -18
  123. data/lib/active_record/insert_all.rb +35 -6
  124. data/lib/active_record/integration.rb +3 -5
  125. data/lib/active_record/internal_metadata.rb +16 -7
  126. data/lib/active_record/legacy_yaml_adapter.rb +7 -3
  127. data/lib/active_record/locking/optimistic.rb +33 -17
  128. data/lib/active_record/locking/pessimistic.rb +6 -2
  129. data/lib/active_record/log_subscriber.rb +27 -8
  130. data/lib/active_record/middleware/database_selector.rb +4 -1
  131. data/lib/active_record/middleware/database_selector/resolver.rb +5 -0
  132. data/lib/active_record/middleware/database_selector/resolver/session.rb +3 -0
  133. data/lib/active_record/migration.rb +113 -83
  134. data/lib/active_record/migration/command_recorder.rb +47 -27
  135. data/lib/active_record/migration/compatibility.rb +68 -17
  136. data/lib/active_record/model_schema.rb +117 -13
  137. data/lib/active_record/nested_attributes.rb +2 -3
  138. data/lib/active_record/no_touching.rb +1 -1
  139. data/lib/active_record/persistence.rb +50 -45
  140. data/lib/active_record/query_cache.rb +15 -5
  141. data/lib/active_record/querying.rb +11 -6
  142. data/lib/active_record/railtie.rb +64 -44
  143. data/lib/active_record/railties/console_sandbox.rb +2 -4
  144. data/lib/active_record/railties/databases.rake +276 -99
  145. data/lib/active_record/readonly_attributes.rb +4 -0
  146. data/lib/active_record/reflection.rb +71 -57
  147. data/lib/active_record/relation.rb +96 -67
  148. data/lib/active_record/relation/batches.rb +38 -31
  149. data/lib/active_record/relation/batches/batch_enumerator.rb +25 -9
  150. data/lib/active_record/relation/calculations.rb +101 -44
  151. data/lib/active_record/relation/delegation.rb +2 -1
  152. data/lib/active_record/relation/finder_methods.rb +45 -15
  153. data/lib/active_record/relation/from_clause.rb +1 -1
  154. data/lib/active_record/relation/merger.rb +27 -25
  155. data/lib/active_record/relation/predicate_builder.rb +59 -38
  156. data/lib/active_record/relation/predicate_builder/array_handler.rb +8 -9
  157. data/lib/active_record/relation/predicate_builder/association_query_value.rb +4 -5
  158. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -6
  159. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
  160. data/lib/active_record/relation/query_methods.rb +333 -195
  161. data/lib/active_record/relation/record_fetch_warning.rb +3 -3
  162. data/lib/active_record/relation/spawn_methods.rb +8 -7
  163. data/lib/active_record/relation/where_clause.rb +104 -57
  164. data/lib/active_record/result.rb +41 -33
  165. data/lib/active_record/runtime_registry.rb +2 -2
  166. data/lib/active_record/sanitization.rb +6 -17
  167. data/lib/active_record/schema_dumper.rb +34 -4
  168. data/lib/active_record/schema_migration.rb +2 -8
  169. data/lib/active_record/scoping/named.rb +6 -17
  170. data/lib/active_record/secure_token.rb +16 -8
  171. data/lib/active_record/serialization.rb +5 -3
  172. data/lib/active_record/signed_id.rb +116 -0
  173. data/lib/active_record/statement_cache.rb +20 -4
  174. data/lib/active_record/store.rb +2 -2
  175. data/lib/active_record/suppressor.rb +2 -2
  176. data/lib/active_record/table_metadata.rb +42 -51
  177. data/lib/active_record/tasks/database_tasks.rb +140 -113
  178. data/lib/active_record/tasks/mysql_database_tasks.rb +34 -35
  179. data/lib/active_record/tasks/postgresql_database_tasks.rb +24 -26
  180. data/lib/active_record/tasks/sqlite_database_tasks.rb +13 -9
  181. data/lib/active_record/test_databases.rb +5 -4
  182. data/lib/active_record/test_fixtures.rb +37 -16
  183. data/lib/active_record/timestamp.rb +4 -6
  184. data/lib/active_record/touch_later.rb +21 -21
  185. data/lib/active_record/transactions.rb +19 -66
  186. data/lib/active_record/type.rb +8 -1
  187. data/lib/active_record/type/serialized.rb +6 -2
  188. data/lib/active_record/type/time.rb +10 -0
  189. data/lib/active_record/type_caster/connection.rb +0 -1
  190. data/lib/active_record/type_caster/map.rb +8 -5
  191. data/lib/active_record/validations.rb +1 -0
  192. data/lib/active_record/validations/numericality.rb +35 -0
  193. data/lib/active_record/validations/uniqueness.rb +24 -4
  194. data/lib/arel.rb +5 -13
  195. data/lib/arel/attributes/attribute.rb +4 -0
  196. data/lib/arel/collectors/bind.rb +5 -0
  197. data/lib/arel/collectors/composite.rb +8 -0
  198. data/lib/arel/collectors/sql_string.rb +7 -0
  199. data/lib/arel/collectors/substitute_binds.rb +7 -0
  200. data/lib/arel/nodes.rb +3 -1
  201. data/lib/arel/nodes/binary.rb +82 -8
  202. data/lib/arel/nodes/bind_param.rb +8 -0
  203. data/lib/arel/nodes/casted.rb +21 -9
  204. data/lib/arel/nodes/equality.rb +6 -9
  205. data/lib/arel/nodes/grouping.rb +3 -0
  206. data/lib/arel/nodes/homogeneous_in.rb +72 -0
  207. data/lib/arel/nodes/in.rb +8 -1
  208. data/lib/arel/nodes/infix_operation.rb +13 -1
  209. data/lib/arel/nodes/join_source.rb +1 -1
  210. data/lib/arel/nodes/node.rb +7 -6
  211. data/lib/arel/nodes/ordering.rb +27 -0
  212. data/lib/arel/nodes/sql_literal.rb +3 -0
  213. data/lib/arel/nodes/table_alias.rb +7 -3
  214. data/lib/arel/nodes/unary.rb +0 -1
  215. data/lib/arel/predications.rb +12 -18
  216. data/lib/arel/select_manager.rb +1 -2
  217. data/lib/arel/table.rb +13 -5
  218. data/lib/arel/visitors.rb +0 -7
  219. data/lib/arel/visitors/dot.rb +14 -2
  220. data/lib/arel/visitors/mysql.rb +11 -1
  221. data/lib/arel/visitors/postgresql.rb +15 -4
  222. data/lib/arel/visitors/to_sql.rb +89 -78
  223. data/lib/rails/generators/active_record/migration.rb +6 -1
  224. data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -0
  225. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +2 -0
  226. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +3 -3
  227. data/lib/rails/generators/active_record/model/model_generator.rb +39 -2
  228. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
  229. metadata +25 -26
  230. data/lib/active_record/advisory_lock_base.rb +0 -18
  231. data/lib/active_record/attribute_decorators.rb +0 -88
  232. data/lib/active_record/connection_adapters/connection_specification.rb +0 -296
  233. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -29
  234. data/lib/active_record/define_callbacks.rb +0 -22
  235. data/lib/active_record/railties/collection_cache_association_loading.rb +0 -34
  236. data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -18
  237. data/lib/active_record/relation/where_clause_factory.rb +0 -33
  238. data/lib/arel/attributes.rb +0 -22
  239. data/lib/arel/visitors/depth_first.rb +0 -203
  240. data/lib/arel/visitors/ibm_db.rb +0 -34
  241. data/lib/arel/visitors/informix.rb +0 -62
  242. data/lib/arel/visitors/mssql.rb +0 -156
  243. data/lib/arel/visitors/oracle.rb +0 -158
  244. data/lib/arel/visitors/oracle12.rb +0 -65
  245. data/lib/arel/visitors/where_sql.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 140f6f368f1117ee61cd524faca9684ae30de2683f916da86fc5d407a7745cca
4
- data.tar.gz: e4c73cf6172c0d17caaf5f4d705e4a4ebff642d0db70c3a70b67f98b8f60b5c4
3
+ metadata.gz: 4112bd4b4d7a00b26db10332f2f6452d2b6ad1fdf3f2ad50988a8229abac9192
4
+ data.tar.gz: 6fbcb021ae1942e307730ef7aaad54d1f594cd52b749d407f8aafe052bee6794
5
5
  SHA512:
6
- metadata.gz: ef0ee3b5b549012bf375144e37e33ca1061dfdd6c2b2a95b4dddf97e42260f7127b64f2d34cf48ac6a532a33b84bff16db6a5bc7263f18773db71a347c13ea01
7
- data.tar.gz: 4fc23dcaa4cd671863cf194f6d8ab9673353e4b78f647224b25a76b00d947cc740f00a89c78ecd55384c9d3146f183f5bd1d756ec3e99c5794047ab068d05d8b
6
+ metadata.gz: eb1830fe0587253ecede381d53dd0be5c8de80070559458653d4a6e149c002a03a3ba37a8723e0e869693500b55ec9956f9f18d2d556884f40d28ac9af0e34f6
7
+ data.tar.gz: d78aeab68b5c4d4d0e2d8a315b7175a0257b6e1bb6db82c1ae04551f44540d72ffd122881af9cfb93cf8c0d8d57b6310a5bd303e612e24795f54fc07b777f307
data/CHANGELOG.md CHANGED
@@ -1,1217 +1,1413 @@
1
- ## Rails 6.0.3.4 (October 07, 2020) ##
1
+ ## Rails 6.1.2 (February 09, 2021) ##
2
2
 
3
- * No changes.
3
+ * Fix timestamp type for sqlite3.
4
4
 
5
+ *Eileen M. Uchitelle*
5
6
 
6
- ## Rails 6.0.3.3 (September 09, 2020) ##
7
-
8
- * No changes.
9
-
7
+ * Make destroy async transactional.
10
8
 
11
- ## Rails 6.0.3.2 (June 17, 2020) ##
9
+ An active record rollback could occur while enqueuing a job. In this
10
+ case the job would enqueue even though the database deletion
11
+ rolledback putting things in a funky state.
12
12
 
13
- * No changes.
13
+ Now the jobs are only enqueued until after the db transaction has been committed.
14
14
 
15
+ *Cory Gwin*
15
16
 
16
- ## Rails 6.0.3.1 (May 18, 2020) ##
17
+ * Fix malformed packet error in MySQL statement for connection configuration.
17
18
 
18
- * No changes.
19
+ *robinroestenburg*
19
20
 
21
+ * Connection specification now passes the "url" key as a configuration for the
22
+ adapter if the "url" protocol is "jdbc", "http", or "https". Previously only
23
+ urls with the "jdbc" prefix were passed to the Active Record Adapter, others
24
+ are assumed to be adapter specification urls.
20
25
 
21
- ## Rails 6.0.3 (May 06, 2020) ##
26
+ Fixes #41137.
22
27
 
23
- * Recommend applications don't use the `database` kwarg in `connected_to`
28
+ *Jonathan Bracy*
24
29
 
25
- The database kwarg in `connected_to` was meant to be used for one-off scripts but is often used in requests. This is really dangerous because it re-establishes a connection every time. It's deprecated in 6.1 and will be removed in 6.2 without replacement. This change soft deprecates it in 6.0 by removing documentation.
30
+ * Fix granular connection swapping when there are multiple abstract classes.
26
31
 
27
32
  *Eileen M. Uchitelle*
28
33
 
29
- * Fix support for PostgreSQL 11+ partitioned indexes.
34
+ * Fix `find_by` with custom primary key for belongs_to association.
30
35
 
31
- *Sebastián Palma*
36
+ *Ryuta Kamizono*
32
37
 
33
- * Add support for beginless ranges, introduced in Ruby 2.7.
38
+ * Add support for `rails console --sandbox` for multiple database applications.
34
39
 
35
- *Josh Goodall*
40
+ *alpaca-tc*
36
41
 
37
- * Fix insert_all with enum values
42
+ * Fix `where` on polymorphic association with empty array.
38
43
 
39
- Fixes #38716.
44
+ *Ryuta Kamizono*
40
45
 
41
- *Joel Blum*
46
+ * Fix preventing writes for `ApplicationRecord`.
42
47
 
43
- * Regexp-escape table name for MS SQL
48
+ *Eileen M. Uchitelle*
44
49
 
45
- Add `Regexp.escape` to one method in ActiveRecord, so that table names with regular expression characters in them work as expected. Since MS SQL Server uses "[" and "]" to quote table and column names, and those characters are regular expression characters, methods like `pluck` and `select` fail in certain cases when used with the MS SQL Server adapter.
46
50
 
47
- *Larry Reid*
51
+ ## Rails 6.1.1 (January 07, 2021) ##
48
52
 
49
- * Store advisory locks on their own named connection.
53
+ * Fix fixtures loading when strict loading is enabled for the association.
50
54
 
51
- Previously advisory locks were taken out against a connection when a migration started. This works fine in single database applications but doesn't work well when migrations need to open new connections which results in the lock getting dropped.
55
+ *Alex Ghiculescu*
52
56
 
53
- In order to fix this we are storing the advisory lock on a new connection with the connection specification name `AdisoryLockBase`. The caveat is that we need to maintain at least 2 connections to a database while migrations are running in order to do this.
57
+ * Fix `where` with custom primary key for belongs_to association.
54
58
 
55
- *Eileen M. Uchitelle*, *John Crepezzi*
59
+ *Ryuta Kamizono*
56
60
 
57
- * Ensure `:reading` connections always raise if a write is attempted.
61
+ * Fix `where` with aliased associations.
58
62
 
59
- Now Rails will raise an `ActiveRecord::ReadOnlyError` if any connection on the reading handler attempts to make a write. If your reading role needs to write you should name the role something other than `:reading`.
63
+ *Ryuta Kamizono*
60
64
 
61
- *Eileen M. Uchitelle*
65
+ * Fix `composed_of` with symbol mapping.
62
66
 
63
- * Enforce fresh ETag header after a collection's contents change by adding
64
- ActiveRecord::Relation#cache_key_with_version. This method will be used by
65
- ActionController::ConditionalGet to ensure that when collection cache versioning
66
- is enabled, requests using ConditionalGet don't return the same ETag header
67
- after a collection is modified. Fixes #38078.
67
+ *Ryuta Kamizono*
68
68
 
69
- *Aaron Lipman*
69
+ * Don't skip money's type cast for pluck and calculations.
70
70
 
71
- * A database URL can now contain a querystring value that contains an equal sign. This is needed to support passing PostgresSQL `options`.
71
+ *Ryuta Kamizono*
72
72
 
73
- *Joshua Flanagan*
73
+ * Fix `where` on polymorphic association with non Active Record object.
74
74
 
75
- * Retain explicit selections on the base model after applying `includes` and `joins`.
75
+ *Ryuta Kamizono*
76
76
 
77
- Resolves #34889.
77
+ * Make sure `db:prepare` works even the schema file doesn't exist.
78
78
 
79
- *Patrick Rebsch*
79
+ *Rafael Mendonça França*
80
80
 
81
+ * Fix complicated `has_many :through` with nested where condition.
81
82
 
82
- ## Rails 6.0.2.2 (March 19, 2020) ##
83
+ *Ryuta Kamizono*
83
84
 
84
- * No changes.
85
+ * Handle STI models for `has_many dependent: :destroy_async`.
85
86
 
87
+ *Muhammad Usman*
86
88
 
87
- ## Rails 6.0.2.1 (December 18, 2019) ##
89
+ * Restore possibility of passing `false` to :polymorphic option of `belongs_to`.
88
90
 
89
- * No changes.
91
+ Previously, passing `false` would trigger the option validation logic
92
+ to throw an error saying :polymorphic would not be a valid option.
90
93
 
94
+ *glaszig*
91
95
 
92
- ## Rails 6.0.2 (December 13, 2019) ##
96
+ * Allow adding nonnamed expression indexes to be revertible.
93
97
 
94
- * Share the same connection pool for primary and replica databases in the
95
- transactional tests for the same database.
98
+ Fixes #40732.
96
99
 
97
- *Edouard Chin*
100
+ Previously, the following code would raise an error, when executed while rolling back,
101
+ and the index name should be specified explicitly. Now, the index name is inferred
102
+ automatically.
98
103
 
99
- * Fix the preloader when one record is fetched using `after_initialize`
100
- but not the entire collection.
104
+ ```ruby
105
+ add_index(:items, "to_tsvector('english', description)")
106
+ ```
101
107
 
102
- *Bradley Price*
108
+ *fatkodima*
103
109
 
104
- * Fix collection callbacks not terminating when `:abort` is thrown.
105
110
 
106
- *Edouard Chin*, *Ryuta Kamizono*
111
+ ## Rails 6.1.0 (December 09, 2020) ##
107
112
 
108
- * Correctly deprecate `where.not` working as NOR for relations.
113
+ * Only warn about negative enums if a positive form that would cause conflicts exists.
109
114
 
110
- 12a9664 deprecated where.not working as NOR, however
111
- doing a relation query like `where.not(relation: { ... })`
112
- wouldn't be properly deprecated and `where.not` would work as
113
- NAND instead.
115
+ Fixes #39065.
114
116
 
115
- *Edouard Chin*
117
+ *Alex Ghiculescu*
116
118
 
117
- * Fix `db:migrate` task with multiple databases to restore the connection
118
- to the previous database.
119
+ * Change `attribute_for_inspect` to take `filter_attributes` in consideration.
119
120
 
120
- The migrate task iterates and establish a connection over each db
121
- resulting in the last one to be used by subsequent rake tasks.
122
- We should reestablish a connection to the connection that was
123
- established before the migrate tasks was run
121
+ *Rafael Mendonça França*
124
122
 
125
- *Edouard Chin*
123
+ * Fix odd behavior of inverse_of with multiple belongs_to to same class.
126
124
 
127
- * Fix multi-threaded issue for `AcceptanceValidator`.
125
+ Fixes #35204.
128
126
 
129
- *Ryuta Kamizono*
127
+ *Tomoyuki Kai*
130
128
 
129
+ * Build predicate conditions with objects that delegate `#id` and primary key:
131
130
 
132
- ## Rails 6.0.1 (November 5, 2019) ##
131
+ ```ruby
132
+ class AdminAuthor
133
+ delegate_missing_to :@author
133
134
 
134
- * Common Table Expressions are allowed on read-only connections.
135
+ def initialize(author)
136
+ @author = author
137
+ end
138
+ end
135
139
 
136
- *Chris Morris*
140
+ Post.where(author: AdminAuthor.new(author))
141
+ ```
137
142
 
138
- * New record instantiation respects `unscope`.
143
+ *Sean Doyle*
139
144
 
140
- *Ryuta Kamizono*
145
+ * Add `connected_to_many` API.
141
146
 
142
- * Fixed a case where `find_in_batches` could halt too early.
147
+ This API allows applications to connect to multiple databases at once without switching all of them or implementing a deeply nested stack.
143
148
 
144
- *Takayuki Nakata*
149
+ Before:
145
150
 
146
- * Autosaved associations always perform validations when a custom validation
147
- context is used.
151
+ AnimalsRecord.connected_to(role: :reading) do
152
+ MealsRecord.connected_to(role: :reading) do
153
+ Dog.first # read from animals replica
154
+ Dinner.first # read from meals replica
155
+ Person.first # read from primary writer
156
+ end
157
+ end
148
158
 
149
- *Tekin Suleyman*
159
+ After:
150
160
 
151
- * `sql.active_record` notifications now include the `:connection` in
152
- their payloads.
161
+ ActiveRecord::Base.connected_to_many([AnimalsRecord, MealsRecord], role: :reading) do
162
+ Dog.first # read from animals replica
163
+ Dinner.first # read from meals replica
164
+ Person.first # read from primary writer
165
+ end
153
166
 
154
- *Eugene Kenny*
167
+ *Eileen M. Uchitelle*, *John Crepezzi*
155
168
 
156
- * A rollback encountered in an `after_commit` callback does not reset
157
- previously-committed record state.
169
+ * Add option to raise or log for `ActiveRecord::StrictLoadingViolationError`.
158
170
 
159
- *Ryuta Kamizono*
171
+ Some applications may not want to raise an error in production if using `strict_loading`. This would allow an application to set strict loading to log for the production environment while still raising in development and test environments.
160
172
 
161
- * Fixed that join order was lost when eager-loading.
173
+ Set `config.active_record.action_on_strict_loading_violation` to `:log` errors instead of raising.
162
174
 
163
- *Ryuta Kamizono*
175
+ *Eileen M. Uchitelle*
164
176
 
165
- * `DESCRIBE` queries are allowed on read-only connections.
177
+ * Allow the inverse of a `has_one` association that was previously autosaved to be loaded.
166
178
 
167
- *Dylan Thacker-Smith*
179
+ Fixes #34255.
168
180
 
169
- * Fixed that records that had been `inspect`ed could not be marshaled.
181
+ *Steven Weber*
170
182
 
171
- *Eugene Kenny*
183
+ * Optimise the length of index names for polymorphic references by using the reference name rather than the type and id column names.
172
184
 
173
- * The connection pool reaper thread is respawned in forked processes. This
174
- fixes that idle connections in forked processes wouldn't be reaped.
185
+ Because the default behaviour when adding an index with multiple columns is to use all column names in the index name, this could frequently lead to overly long index names for polymorphic references which would fail the migration if it exceeded the database limit.
175
186
 
176
- *John Hawthorn*
187
+ This change reduces the chance of that happening by using the reference name, e.g. `index_my_table_on_my_reference`.
177
188
 
178
- * The memoized result of `ActiveRecord::Relation#take` is properly cleared
179
- when `ActiveRecord::Relation#reset` or `ActiveRecord::Relation#reload`
180
- is called.
189
+ Fixes #38655.
181
190
 
182
- *Anmol Arora*
191
+ *Luke Redpath*
183
192
 
184
- * Fixed the performance regression for `primary_keys` introduced MySQL 8.0.
193
+ * MySQL: Uniqueness validator now respects default database collation,
194
+ no longer enforce case sensitive comparison by default.
185
195
 
186
- *Hiroyuki Ishii*
196
+ *Ryuta Kamizono*
187
197
 
188
- * `insert`, `insert_all`, `upsert`, and `upsert_all` now clear the query cache.
198
+ * Remove deprecated methods from `ActiveRecord::ConnectionAdapters::DatabaseLimits`.
189
199
 
190
- *Eugene Kenny*
200
+ `column_name_length`
201
+ `table_name_length`
202
+ `columns_per_table`
203
+ `indexes_per_table`
204
+ `columns_per_multicolumn_index`
205
+ `sql_query_length`
206
+ `joins_per_query`
191
207
 
192
- * Call `while_preventing_writes` directly from `connected_to`.
208
+ *Rafael Mendonça França*
193
209
 
194
- 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)`.
210
+ * Remove deprecated `ActiveRecord::ConnectionAdapters::AbstractAdapter#supports_multi_insert?`.
195
211
 
196
- 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.
212
+ *Rafael Mendonça França*
197
213
 
198
- *Eileen M. Uchitelle*
214
+ * Remove deprecated `ActiveRecord::ConnectionAdapters::AbstractAdapter#supports_foreign_keys_in_create?`.
199
215
 
200
- * Improve detection of ActiveRecord::StatementTimeout with mysql2 adapter in the edge case when the query is terminated during filesort.
216
+ *Rafael Mendonça França*
201
217
 
202
- *Kir Shatrov*
218
+ * Remove deprecated `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#supports_ranges?`.
203
219
 
220
+ *Rafael Mendonça França*
204
221
 
205
- ## Rails 6.0.0 (August 16, 2019) ##
222
+ * Remove deprecated `ActiveRecord::Base#update_attributes` and `ActiveRecord::Base#update_attributes!`.
206
223
 
207
- * Preserve user supplied joins order as much as possible.
224
+ *Rafael Mendonça França*
208
225
 
209
- Fixes #36761, #34328, #24281, #12953.
226
+ * Remove deprecated `migrations_path` argument in `ActiveRecord::ConnectionAdapter::SchemaStatements#assume_migrated_upto_version`.
210
227
 
211
- *Ryuta Kamizono*
228
+ *Rafael Mendonça França*
212
229
 
213
- * Make the DATABASE_URL env variable only affect the primary connection. Add new env variables for multiple databases.
230
+ * Remove deprecated `config.active_record.sqlite3.represent_boolean_as_integer`.
214
231
 
215
- *John Crepezzi*, *Eileen Uchitelle*
232
+ *Rafael Mendonça França*
216
233
 
217
- * Add a warning for enum elements with 'not_' prefix.
234
+ * `relation.create` does no longer leak scope to class level querying methods
235
+ in initialization block and callbacks.
218
236
 
219
- class Foo
220
- enum status: [:sent, :not_sent]
237
+ Before:
238
+
239
+ User.where(name: "John").create do |john|
240
+ User.find_by(name: "David") # => nil
221
241
  end
222
242
 
223
- *Edu Depetris*
243
+ After:
224
244
 
225
- * Make currency symbols optional for money column type in PostgreSQL
245
+ User.where(name: "John").create do |john|
246
+ User.find_by(name: "David") # => #<User name: "David", ...>
247
+ end
226
248
 
227
- *Joel Schneider*
249
+ *Ryuta Kamizono*
228
250
 
251
+ * Named scope chain does no longer leak scope to class level querying methods.
229
252
 
230
- ## Rails 6.0.0.rc2 (July 22, 2019) ##
253
+ class User < ActiveRecord::Base
254
+ scope :david, -> { User.where(name: "David") }
255
+ end
231
256
 
232
- * Add database_exists? method to connection adapters to check if a database exists.
257
+ Before:
233
258
 
234
- *Guilherme Mansur*
259
+ User.where(name: "John").david
260
+ # SELECT * FROM users WHERE name = 'John' AND name = 'David'
235
261
 
236
- * PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute.
262
+ After:
237
263
 
238
- Fixes #36022.
264
+ User.where(name: "John").david
265
+ # SELECT * FROM users WHERE name = 'David'
239
266
 
240
267
  *Ryuta Kamizono*
241
268
 
242
- * Make ActiveRecord `ConnectionPool.connections` method thread-safe.
269
+ * Remove deprecated methods from `ActiveRecord::DatabaseConfigurations`.
243
270
 
244
- Fixes #36465.
271
+ `fetch`
272
+ `each`
273
+ `first`
274
+ `values`
275
+ `[]=`
245
276
 
246
- *Jeff Doering*
277
+ *Rafael Mendonça França*
247
278
 
248
- * Fix sqlite3 collation parsing when using decimal columns.
279
+ * `where.not` now generates NAND predicates instead of NOR.
249
280
 
250
- *Martin R. Schuster*
281
+ Before:
251
282
 
252
- * Fix invalid schema when primary key column has a comment.
283
+ User.where.not(name: "Jon", role: "admin")
284
+ # SELECT * FROM users WHERE name != 'Jon' AND role != 'admin'
253
285
 
254
- Fixes #29966.
286
+ After:
255
287
 
256
- *Guilherme Goettems Schneider*
288
+ User.where.not(name: "Jon", role: "admin")
289
+ # SELECT * FROM users WHERE NOT (name == 'Jon' AND role == 'admin')
257
290
 
258
- * Fix table comment also being applied to the primary key column.
291
+ *Rafael Mendonça França*
259
292
 
260
- *Guilherme Goettems Schneider*
293
+ * Remove deprecated `ActiveRecord::Result#to_hash` method.
261
294
 
262
- * Fix merging left_joins to maintain its own `join_type` context.
295
+ *Rafael Mendonça França*
263
296
 
264
- Fixes #36103.
297
+ * Deprecate `ActiveRecord::Base.allow_unsafe_raw_sql`.
265
298
 
266
- *Ryuta Kamizono*
299
+ *Rafael Mendonça França*
300
+
301
+ * Remove deprecated support for using unsafe raw SQL in `ActiveRecord::Relation` methods.
302
+
303
+ *Rafael Mendonça França*
267
304
 
305
+ * Allow users to silence the "Rails couldn't infer whether you are using multiple databases..."
306
+ message using `config.active_record.suppress_multiple_database_warning`.
268
307
 
269
- ## Rails 6.0.0.rc1 (April 24, 2019) ##
308
+ *Omri Gabay*
270
309
 
271
- * Add `touch` option to `has_one` association.
310
+ * Connections can be granularly switched for abstract classes when `connected_to` is called.
272
311
 
273
- *Abhay Nikam*
312
+ This change allows `connected_to` to switch a `role` and/or `shard` for a single abstract class instead of all classes globally. Applications that want to use the new feature need to set `config.active_record.legacy_connection_handling` to `false` in their application configuration.
274
313
 
275
- * Deprecate `where.not` working as NOR and will be changed to NAND in Rails 6.1.
314
+ Example usage:
315
+
316
+ Given an application we have a `User` model that inherits from `ApplicationRecord` and a `Dog` model that inherits from `AnimalsRecord`. `AnimalsRecord` and `ApplicationRecord` have writing and reading connections as well as shard `default`, `one`, and `two`.
276
317
 
277
318
  ```ruby
278
- all = [treasures(:diamond), treasures(:sapphire), cars(:honda), treasures(:sapphire)]
279
- assert_equal all, PriceEstimate.all.map(&:estimate_of)
319
+ ActiveRecord::Base.connected_to(role: :reading) do
320
+ User.first # reads from default replica
321
+ Dog.first # reads from default replica
322
+
323
+ AnimalsRecord.connected_to(role: :writing, shard: :one) do
324
+ User.first # reads from default replica
325
+ Dog.first # reads from shard one primary
326
+ end
327
+
328
+ User.first # reads from default replica
329
+ Dog.first # reads from default replica
330
+
331
+ ApplicationRecord.connected_to(role: :writing, shard: :two) do
332
+ User.first # reads from shard two primary
333
+ Dog.first # reads from default replica
334
+ end
335
+ end
280
336
  ```
281
337
 
282
- In Rails 6.0:
338
+ *Eileen M. Uchitelle*, *John Crepezzi*
339
+
340
+ * Allow double-dash comment syntax when querying read-only databases
341
+
342
+ *James Adam*
343
+
344
+ * Add `values_at` method.
345
+
346
+ Returns an array containing the values associated with the given methods.
283
347
 
284
348
  ```ruby
285
- sapphire = treasures(:sapphire)
286
-
287
- nor = all.reject { |e|
288
- e.estimate_of_type == sapphire.class.polymorphic_name
289
- }.reject { |e|
290
- e.estimate_of_id == sapphire.id
291
- }
292
- assert_equal [cars(:honda)], nor
293
-
294
- without_sapphire = PriceEstimate.where.not(
295
- estimate_of_type: sapphire.class.polymorphic_name, estimate_of_id: sapphire.id
296
- )
297
- assert_equal nor, without_sapphire.map(&:estimate_of)
349
+ topic = Topic.first
350
+ topic.values_at(:title, :author_name)
351
+ # => ["Budget", "Jason"]
298
352
  ```
299
353
 
300
- In Rails 6.1:
354
+ Similar to `Hash#values_at` but on an Active Record instance.
355
+
356
+ *Guillaume Briday*
357
+
358
+ * Fix `read_attribute_before_type_cast` to consider attribute aliases.
359
+
360
+ *Marcelo Lauxen*
361
+
362
+ * Support passing record to uniqueness validator `:conditions` callable:
301
363
 
302
364
  ```ruby
303
- sapphire = treasures(:sapphire)
365
+ class Article < ApplicationRecord
366
+ validates_uniqueness_of :title, conditions: ->(article) {
367
+ published_at = article.published_at
368
+ where(published_at: published_at.beginning_of_year..published_at.end_of_year)
369
+ }
370
+ end
371
+ ```
304
372
 
305
- nand = all - [sapphire]
306
- assert_equal [treasures(:diamond), cars(:honda)], nand
373
+ *Eliot Sykes*
307
374
 
308
- without_sapphire = PriceEstimate.where.not(
309
- estimate_of_type: sapphire.class.polymorphic_name, estimate_of_id: sapphire.id
310
- )
311
- assert_equal nand, without_sapphire.map(&:estimate_of)
375
+ * `BatchEnumerator#update_all` and `BatchEnumerator#delete_all` now return the
376
+ total number of rows affected, just like their non-batched counterparts.
377
+
378
+ ```ruby
379
+ Person.in_batches.update_all("first_name = 'Eugene'") # => 42
380
+ Person.in_batches.delete_all # => 42
312
381
  ```
313
382
 
314
- *Ryuta Kamizono*
383
+ Fixes #40287.
315
384
 
316
- * Fix dirty tracking after rollback.
385
+ *Eugene Kenny*
317
386
 
318
- Fixes #15018, #30167, #33868.
387
+ * Add support for PostgreSQL `interval` data type with conversion to
388
+ `ActiveSupport::Duration` when loading records from database and
389
+ serialization to ISO 8601 formatted duration string on save.
390
+ Add support to define a column in migrations and get it in a schema dump.
391
+ Optional column precision is supported.
319
392
 
320
- *Ryuta Kamizono*
393
+ To use this in 6.1, you need to place the next string to your model file:
321
394
 
322
- * Add `ActiveRecord::Relation#cache_version` to support recyclable cache keys via
323
- the versioned entries in `ActiveSupport::Cache`. This also means that
324
- `ActiveRecord::Relation#cache_key` will now return a stable key that does not
325
- include the max timestamp or count any more.
395
+ attribute :duration, :interval
326
396
 
327
- NOTE: This feature is turned off by default, and `cache_key` will still return
328
- cache keys with timestamps until you set `ActiveRecord::Base.collection_cache_versioning = true`.
329
- That's the setting for all new apps on Rails 6.0+
397
+ To keep old behavior until 6.2 is released:
330
398
 
331
- *Lachlan Sylvester*
399
+ attribute :duration, :string
332
400
 
333
- * Fix dirty tracking for `touch` to track saved changes.
401
+ Example:
334
402
 
335
- Fixes #33429.
403
+ create_table :events do |t|
404
+ t.string :name
405
+ t.interval :duration
406
+ end
336
407
 
337
- *Ryuta Kamzono*
408
+ class Event < ApplicationRecord
409
+ attribute :duration, :interval
410
+ end
338
411
 
339
- * `change_column_comment` and `change_table_comment` are invertible only if
340
- `to` and `from` options are specified.
412
+ Event.create!(name: 'Rock Fest', duration: 2.days)
413
+ Event.last.duration # => 2 days
414
+ Event.last.duration.iso8601 # => "P2D"
415
+ Event.new(duration: 'P1DT12H3S').duration # => 1 day, 12 hours, and 3 seconds
416
+ Event.new(duration: '1 day') # Unknown value will be ignored and NULL will be written to database
341
417
 
342
- *Yoshiyuki Kinjo*
418
+ *Andrey Novikov*
343
419
 
344
- * Don't call commit/rollback callbacks when a record isn't saved.
420
+ * Allow associations supporting the `dependent:` key to take `dependent: :destroy_async`.
345
421
 
346
- Fixes #29747.
422
+ ```ruby
423
+ class Account < ActiveRecord::Base
424
+ belongs_to :supplier, dependent: :destroy_async
425
+ end
426
+ ```
347
427
 
348
- *Ryuta Kamizono*
428
+ `:destroy_async` will enqueue a job to destroy associated records in the background.
349
429
 
350
- * Fix circular `autosave: true` causes invalid records to be saved.
430
+ *DHH*, *George Claghorn*, *Cory Gwin*, *Rafael Mendonça França*, *Adrianna Chang*
351
431
 
352
- Prior to the fix, when there was a circular series of `autosave: true`
353
- associations, the callback for a `has_many` association was run while
354
- another instance of the same callback on the same association hadn't
355
- finished running. When control returned to the first instance of the
356
- callback, the instance variable had changed, and subsequent associated
357
- records weren't saved correctly. Specifically, the ID field for the
358
- `belongs_to` corresponding to the `has_many` was `nil`.
432
+ * Add `SKIP_TEST_DATABASE` environment variable to disable modifying the test database when `rails db:create` and `rails db:drop` are called.
359
433
 
360
- Fixes #28080.
434
+ *Jason Schweier*
361
435
 
362
- *Larry Reid*
436
+ * `connects_to` can only be called on `ActiveRecord::Base` or abstract classes.
363
437
 
364
- * Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options.
438
+ Ensure that `connects_to` can only be called from `ActiveRecord::Base` or abstract classes. This protects the application from opening duplicate or too many connections.
365
439
 
366
- Before:
440
+ *Eileen M. Uchitelle*, *John Crepezzi*
367
441
 
368
- ```ruby
369
- add_column :items, :attr1, :binary, size: 10 # => ArgumentError
370
- add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError
371
- add_column :items, :attr3, :integer, limit: 10 # => ActiveRecordError
372
- add_column :items, :attr4, :datetime, precision: 10 # => ActiveRecordError
373
- ```
442
+ * All connection adapters `execute` now raises `ActiveRecord::ConnectionNotEstablished` rather than
443
+ `ActiveRecord::StatementInvalid` when they encounter a connection error.
374
444
 
375
- After:
445
+ *Jean Boussier*
446
+
447
+ * `Mysql2Adapter#quote_string` now raises `ActiveRecord::ConnectionNotEstablished` rather than
448
+ `ActiveRecord::StatementInvalid` when it can't connect to the MySQL server.
449
+
450
+ *Jean Boussier*
451
+
452
+ * Add support for check constraints that are `NOT VALID` via `validate: false` (PostgreSQL-only).
453
+
454
+ *Alex Robbin*
455
+
456
+ * Ensure the default configuration is considered primary or first for an environment
457
+
458
+ If a multiple database application provides a configuration named primary, that will be treated as default. In applications that do not have a primary entry, the default database configuration will be the first configuration for an environment.
459
+
460
+ *Eileen M. Uchitelle*
461
+
462
+ * Allow `where` references association names as joined table name aliases.
376
463
 
377
464
  ```ruby
378
- add_column :items, :attr1, :binary, size: 10 # => ArgumentError
379
- add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError
380
- add_column :items, :attr3, :integer, limit: 10 # => ArgumentError
381
- add_column :items, :attr4, :datetime, precision: 10 # => ArgumentError
465
+ class Comment < ActiveRecord::Base
466
+ enum label: [:default, :child]
467
+ has_many :children, class_name: "Comment", foreign_key: :parent_id
468
+ end
469
+
470
+ # ... FROM comments LEFT OUTER JOIN comments children ON ... WHERE children.label = 1
471
+ Comment.includes(:children).where("children.label": "child")
382
472
  ```
383
473
 
384
474
  *Ryuta Kamizono*
385
475
 
386
- * Association loading isn't to be affected by scoping consistently
387
- whether preloaded / eager loaded or not, with the exception of `unscoped`.
476
+ * Support storing demodulized class name for polymorphic type.
388
477
 
389
- Before:
478
+ Before Rails 6.1, storing demodulized class name is supported only for STI type
479
+ by `store_full_sti_class` class attribute.
480
+
481
+ Now `store_full_class_name` class attribute can handle both STI and polymorphic types.
482
+
483
+ *Ryuta Kamizono*
484
+
485
+ * Deprecate `rails db:structure:{load, dump}` tasks and extend
486
+ `rails db:schema:{load, dump}` tasks to work with either `:ruby` or `:sql` format,
487
+ depending on `config.active_record.schema_format` configuration value.
488
+
489
+ *fatkodima*
490
+
491
+ * Respect the `select` values for eager loading.
390
492
 
391
493
  ```ruby
392
- Post.where("1=0").scoping do
393
- Comment.find(1).post # => nil
394
- Comment.preload(:post).find(1).post # => #<Post id: 1, ...>
395
- Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
396
- end
494
+ post = Post.select("UPPER(title) AS title").first
495
+ post.title # => "WELCOME TO THE WEBLOG"
496
+ post.body # => ActiveModel::MissingAttributeError
497
+
498
+ # Rails 6.0 (ignore the `select` values)
499
+ post = Post.select("UPPER(title) AS title").eager_load(:comments).first
500
+ post.title # => "Welcome to the weblog"
501
+ post.body # => "Such a lovely day"
502
+
503
+ # Rails 6.1 (respect the `select` values)
504
+ post = Post.select("UPPER(title) AS title").eager_load(:comments).first
505
+ post.title # => "WELCOME TO THE WEBLOG"
506
+ post.body # => ActiveModel::MissingAttributeError
397
507
  ```
398
508
 
399
- After:
509
+ *Ryuta Kamizono*
510
+
511
+ * Allow attribute's default to be configured but keeping its own type.
400
512
 
401
513
  ```ruby
402
- Post.where("1=0").scoping do
403
- Comment.find(1).post # => #<Post id: 1, ...>
404
- Comment.preload(:post).find(1).post # => #<Post id: 1, ...>
405
- Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
514
+ class Post < ActiveRecord::Base
515
+ attribute :written_at, default: -> { Time.now.utc }
406
516
  end
407
- ```
408
517
 
409
- Fixes #34638, #35398.
518
+ # Rails 6.0
519
+ Post.type_for_attribute(:written_at) # => #<Type::Value ... precision: nil, ...>
520
+
521
+ # Rails 6.1
522
+ Post.type_for_attribute(:written_at) # => #<Type::DateTime ... precision: 6, ...>
523
+ ```
410
524
 
411
525
  *Ryuta Kamizono*
412
526
 
413
- * Add `rails db:prepare` to migrate or setup a database.
527
+ * Allow default to be configured for Enum.
414
528
 
415
- Runs `db:migrate` if the database exists or `db:setup` if it doesn't.
529
+ ```ruby
530
+ class Book < ActiveRecord::Base
531
+ enum status: [:proposed, :written, :published], _default: :published
532
+ end
416
533
 
417
- *Roberto Miranda*
534
+ Book.new.status # => "published"
535
+ ```
418
536
 
419
- * Add `after_save_commit` callback as shortcut for `after_commit :hook, on: [ :create, :update ]`.
537
+ *Ryuta Kamizono*
420
538
 
421
- *DHH*
539
+ * Deprecate YAML loading from legacy format older than Rails 5.0.
422
540
 
423
- * Assign all attributes before calling `build` to ensure the child record is visible in
424
- `before_add` and `after_add` callbacks for `has_many :through` associations.
541
+ *Ryuta Kamizono*
425
542
 
426
- Fixes #33249.
543
+ * Added the setting `ActiveRecord::Base.immutable_strings_by_default`, which
544
+ allows you to specify that all string columns should be frozen unless
545
+ otherwise specified. This will reduce memory pressure for applications which
546
+ do not generally mutate string properties of Active Record objects.
427
547
 
428
- *Ryan H. Kerr*
548
+ *Sean Griffin*, *Ryuta Kamizono*
429
549
 
430
- * Add `ActiveRecord::Relation#extract_associated` for extracting associated records from a relation.
550
+ * Deprecate `map!` and `collect!` on `ActiveRecord::Result`.
431
551
 
432
- ```
433
- account.memberships.extract_associated(:user)
434
- # => Returns collection of User records
435
- ```
552
+ *Ryuta Kamizono*
436
553
 
437
- *DHH*
554
+ * Support `relation.and` for intersection as Set theory.
438
555
 
439
- * Add `ActiveRecord::Relation#annotate` for adding SQL comments to its queries.
556
+ ```ruby
557
+ david_and_mary = Author.where(id: [david, mary])
558
+ mary_and_bob = Author.where(id: [mary, bob])
440
559
 
441
- For example:
560
+ david_and_mary.merge(mary_and_bob) # => [mary, bob]
442
561
 
443
- ```
444
- Post.where(id: 123).annotate("this is a comment").to_sql
445
- # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123 /* this is a comment */
562
+ david_and_mary.and(mary_and_bob) # => [mary]
563
+ david_and_mary.or(mary_and_bob) # => [david, mary, bob]
446
564
  ```
447
565
 
448
- This can be useful in instrumentation or other analysis of issued queries.
449
-
450
- *Matt Yoho*
566
+ *Ryuta Kamizono*
451
567
 
452
- * Support Optimizer Hints.
568
+ * Merging conditions on the same column no longer maintain both conditions,
569
+ and will be consistently replaced by the latter condition in Rails 6.2.
570
+ To migrate to Rails 6.2's behavior, use `relation.merge(other, rewhere: true)`.
453
571
 
454
- In most databases, a way to control the optimizer is by using optimizer hints,
455
- which can be specified within individual statements.
572
+ ```ruby
573
+ # Rails 6.1 (IN clause is replaced by merger side equality condition)
574
+ Author.where(id: [david.id, mary.id]).merge(Author.where(id: bob)) # => [bob]
456
575
 
457
- Example (for MySQL):
576
+ # Rails 6.1 (both conflict conditions exists, deprecated)
577
+ Author.where(id: david.id..mary.id).merge(Author.where(id: bob)) # => []
458
578
 
459
- Topic.optimizer_hints("MAX_EXECUTION_TIME(50000)", "NO_INDEX_MERGE(topics)")
460
- # SELECT /*+ MAX_EXECUTION_TIME(50000) NO_INDEX_MERGE(topics) */ `topics`.* FROM `topics`
579
+ # Rails 6.1 with rewhere to migrate to Rails 6.2's behavior
580
+ Author.where(id: david.id..mary.id).merge(Author.where(id: bob), rewhere: true) # => [bob]
461
581
 
462
- Example (for PostgreSQL with pg_hint_plan):
582
+ # Rails 6.2 (same behavior with IN clause, mergee side condition is consistently replaced)
583
+ Author.where(id: [david.id, mary.id]).merge(Author.where(id: bob)) # => [bob]
584
+ Author.where(id: david.id..mary.id).merge(Author.where(id: bob)) # => [bob]
585
+ ```
463
586
 
464
- Topic.optimizer_hints("SeqScan(topics)", "Parallel(topics 8)")
465
- # SELECT /*+ SeqScan(topics) Parallel(topics 8) */ "topics".* FROM "topics"
587
+ *Ryuta Kamizono*
466
588
 
467
- See also:
589
+ * Do not mark Postgresql MAC address and UUID attributes as changed when the assigned value only varies by case.
468
590
 
469
- * https://dev.mysql.com/doc/refman/8.0/en/optimizer-hints.html
470
- * https://pghintplan.osdn.jp/pg_hint_plan.html
471
- * https://docs.oracle.com/en/database/oracle/oracle-database/12.2/tgsql/influencing-the-optimizer.html
472
- * https://docs.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query?view=sql-server-2017
473
- * https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.admin.perf.doc/doc/c0070117.html
591
+ *Peter Fry*
474
592
 
475
- *Ryuta Kamizono*
593
+ * Resolve issue with insert_all unique_by option when used with expression index.
476
594
 
477
- * Fix query attribute method on user-defined attribute to be aware of typecasted value.
595
+ When the `:unique_by` option of `ActiveRecord::Persistence.insert_all` and
596
+ `ActiveRecord::Persistence.upsert_all` was used with the name of an expression index, an error
597
+ was raised. Adding a guard around the formatting behavior for the `:unique_by` corrects this.
478
598
 
479
- For example, the following code no longer return false as casted non-empty string:
599
+ Usage:
480
600
 
481
- ```
482
- class Post < ActiveRecord::Base
483
- attribute :user_defined_text, :text
601
+ ```ruby
602
+ create_table :books, id: :integer, force: true do |t|
603
+ t.column :name, :string
604
+ t.index "lower(name)", unique: true
484
605
  end
485
606
 
486
- Post.new(user_defined_text: "false").user_defined_text? # => true
607
+ Book.insert_all [{ name: "MyTest" }], unique_by: :index_books_on_lower_name
487
608
  ```
488
609
 
489
- *Yuji Kamijima*
610
+ Fixes #39516.
490
611
 
491
- * Quote empty ranges like other empty enumerables.
612
+ *Austen Madden*
492
613
 
493
- *Patrick Rebsch*
614
+ * Add basic support for CHECK constraints to database migrations.
494
615
 
495
- * Add `insert_all`/`insert_all!`/`upsert_all` methods to `ActiveRecord::Persistence`,
496
- allowing bulk inserts akin to the bulk updates provided by `update_all` and
497
- bulk deletes by `delete_all`.
616
+ Usage:
498
617
 
499
- Supports skipping or upserting duplicates through the `ON CONFLICT` syntax
500
- for PostgreSQL (9.5+) and SQLite (3.24+) and `ON DUPLICATE KEY UPDATE` syntax
501
- for MySQL.
618
+ ```ruby
619
+ add_check_constraint :products, "price > 0", name: "price_check"
620
+ remove_check_constraint :products, name: "price_check"
621
+ ```
502
622
 
503
- *Bob Lail*
623
+ *fatkodima*
504
624
 
505
- * Add `rails db:seed:replant` that truncates tables of each database
506
- for current environment and loads the seeds.
625
+ * Add `ActiveRecord::Base.strict_loading_by_default` and `ActiveRecord::Base.strict_loading_by_default=`
626
+ to enable/disable strict_loading mode by default for a model. The configuration's value is
627
+ inheritable by subclasses, but they can override that value and it will not impact parent class.
507
628
 
508
- *bogdanvlviv*, *DHH*
629
+ Usage:
509
630
 
510
- * Add `ActiveRecord::Base.connection.truncate` for SQLite3 adapter.
631
+ ```ruby
632
+ class Developer < ApplicationRecord
633
+ self.strict_loading_by_default = true
511
634
 
512
- *bogdanvlviv*
635
+ has_many :projects
636
+ end
513
637
 
514
- * Deprecate mismatched collation comparison for uniqueness validator.
638
+ dev = Developer.first
639
+ dev.projects.first
640
+ # => ActiveRecord::StrictLoadingViolationError Exception: Developer is marked as strict_loading and Project cannot be lazily loaded.
641
+ ```
515
642
 
516
- Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1.
517
- To continue case sensitive comparison on the case insensitive column,
518
- pass `case_sensitive: true` option explicitly to the uniqueness validator.
643
+ *bogdanvlviv*
644
+
645
+ * Deprecate passing an Active Record object to `quote`/`type_cast` directly.
519
646
 
520
647
  *Ryuta Kamizono*
521
648
 
522
- * Add `reselect` method. This is a short-hand for `unscope(:select).select(fields)`.
649
+ * Default engine `ENGINE=InnoDB` is no longer dumped to make schema more agnostic.
523
650
 
524
- Fixes #27340.
651
+ Before:
525
652
 
526
- *Willian Gustavo Veiga*
653
+ ```ruby
654
+ create_table "accounts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
655
+ end
656
+ ```
527
657
 
528
- * Add negative scopes for all enum values.
658
+ After:
529
659
 
530
- Example:
660
+ ```ruby
661
+ create_table "accounts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
662
+ end
663
+ ```
531
664
 
532
- class Post < ActiveRecord::Base
533
- enum status: %i[ drafted active trashed ]
534
- end
665
+ *Ryuta Kamizono*
535
666
 
536
- Post.not_drafted # => where.not(status: :drafted)
537
- Post.not_active # => where.not(status: :active)
538
- Post.not_trashed # => where.not(status: :trashed)
667
+ * Added delegated type as an alternative to single-table inheritance for representing class hierarchies.
668
+ See ActiveRecord::DelegatedType for the full description.
539
669
 
540
670
  *DHH*
541
671
 
542
- * Fix different `count` calculation when using `size` with manual `select` with DISTINCT.
672
+ * Deprecate aggregations with group by duplicated fields.
543
673
 
544
- Fixes #35214.
674
+ To migrate to Rails 6.2's behavior, use `uniq!(:group)` to deduplicate group fields.
545
675
 
546
- *Juani Villarejo*
676
+ ```ruby
677
+ accounts = Account.group(:firm_id)
678
+
679
+ # duplicated group fields, deprecated.
680
+ accounts.merge(accounts.where.not(credit_limit: nil)).sum(:credit_limit)
681
+ # => {
682
+ # [1, 1] => 50,
683
+ # [2, 2] => 60
684
+ # }
685
+
686
+ # use `uniq!(:group)` to deduplicate group fields.
687
+ accounts.merge(accounts.where.not(credit_limit: nil)).uniq!(:group).sum(:credit_limit)
688
+ # => {
689
+ # 1 => 50,
690
+ # 2 => 60
691
+ # }
692
+ ```
547
693
 
694
+ *Ryuta Kamizono*
548
695
 
549
- ## Rails 6.0.0.beta3 (March 11, 2019) ##
696
+ * Deprecate duplicated query annotations.
550
697
 
551
- * No changes.
698
+ To migrate to Rails 6.2's behavior, use `uniq!(:annotate)` to deduplicate query annotations.
552
699
 
700
+ ```ruby
701
+ accounts = Account.where(id: [1, 2]).annotate("david and mary")
553
702
 
554
- ## Rails 6.0.0.beta2 (February 25, 2019) ##
703
+ # duplicated annotations, deprecated.
704
+ accounts.merge(accounts.rewhere(id: 3))
705
+ # SELECT accounts.* FROM accounts WHERE accounts.id = 3 /* david and mary */ /* david and mary */
555
706
 
556
- * Fix prepared statements caching to be enabled even when query caching is enabled.
707
+ # use `uniq!(:annotate)` to deduplicate annotations.
708
+ accounts.merge(accounts.rewhere(id: 3)).uniq!(:annotate)
709
+ # SELECT accounts.* FROM accounts WHERE accounts.id = 3 /* david and mary */
710
+ ```
557
711
 
558
712
  *Ryuta Kamizono*
559
713
 
560
- * Ensure `update_all` series cares about optimistic locking.
714
+ * Resolve conflict between counter cache and optimistic locking.
561
715
 
562
- *Ryuta Kamizono*
716
+ Bump an Active Record instance's lock version after updating its counter
717
+ cache. This avoids raising an unnecessary `ActiveRecord::StaleObjectError`
718
+ upon subsequent transactions by maintaining parity with the corresponding
719
+ database record's `lock_version` column.
563
720
 
564
- * Don't allow `where` with non numeric string matches to 0 values.
721
+ Fixes #16449.
565
722
 
566
- *Ryuta Kamizono*
723
+ *Aaron Lipman*
567
724
 
568
- * Introduce `ActiveRecord::Relation#destroy_by` and `ActiveRecord::Relation#delete_by`.
725
+ * Support merging option `:rewhere` to allow mergee side condition to be replaced exactly.
569
726
 
570
- `destroy_by` allows relation to find all the records matching the condition and perform
571
- `destroy_all` on the matched records.
727
+ ```ruby
728
+ david_and_mary = Author.where(id: david.id..mary.id)
572
729
 
573
- Example:
730
+ # both conflict conditions exists
731
+ david_and_mary.merge(Author.where(id: bob)) # => []
574
732
 
575
- Person.destroy_by(name: 'David')
576
- Person.destroy_by(name: 'David', rating: 4)
733
+ # mergee side condition is replaced by rewhere
734
+ david_and_mary.merge(Author.rewhere(id: bob)) # => [bob]
577
735
 
578
- david = Person.find_by(name: 'David')
579
- david.posts.destroy_by(id: [1, 2, 3])
736
+ # mergee side condition is replaced by rewhere option
737
+ david_and_mary.merge(Author.where(id: bob), rewhere: true) # => [bob]
738
+ ```
580
739
 
581
- `delete_by` allows relation to find all the records matching the condition and perform
582
- `delete_all` on the matched records.
740
+ *Ryuta Kamizono*
583
741
 
584
- Example:
742
+ * Add support for finding records based on signed ids, which are tamper-proof, verified ids that can be
743
+ set to expire and scoped with a purpose. This is particularly useful for things like password reset
744
+ or email verification, where you want the bearer of the signed id to be able to interact with the
745
+ underlying record, but usually only within a certain time period.
585
746
 
586
- Person.delete_by(name: 'David')
587
- Person.delete_by(name: 'David', rating: 4)
747
+ ```ruby
748
+ signed_id = User.first.signed_id expires_in: 15.minutes, purpose: :password_reset
588
749
 
589
- david = Person.find_by(name: 'David')
590
- david.posts.delete_by(id: [1, 2, 3])
750
+ User.find_signed signed_id # => nil, since the purpose does not match
591
751
 
592
- *Abhay Nikam*
752
+ travel 16.minutes
753
+ User.find_signed signed_id, purpose: :password_reset # => nil, since the signed id has expired
593
754
 
594
- * Don't allow `where` with invalid value matches to nil values.
755
+ travel_back
756
+ User.find_signed signed_id, purpose: :password_reset # => User.first
595
757
 
596
- Fixes #33624.
758
+ User.find_signed! "bad data" # => ActiveSupport::MessageVerifier::InvalidSignature
759
+ ```
597
760
 
598
- *Ryuta Kamizono*
761
+ *DHH*
599
762
 
600
- * SQLite3: Implement `add_foreign_key` and `remove_foreign_key`.
763
+ * Support `ALGORITHM = INSTANT` DDL option for index operations on MySQL.
601
764
 
602
765
  *Ryuta Kamizono*
603
766
 
604
- * Deprecate using class level querying methods if the receiver scope
605
- regarded as leaked. Use `klass.unscoped` to avoid the leaking scope.
767
+ * Fix index creation to preserve index comment in bulk change table on MySQL.
606
768
 
607
769
  *Ryuta Kamizono*
608
770
 
609
- * Allow applications to automatically switch connections.
771
+ * Allow `unscope` to be aware of table name qualified values.
610
772
 
611
- Adds a middleware and configuration options that can be used in your
612
- application to automatically switch between the writing and reading
613
- database connections.
773
+ It is possible to unscope only the column in the specified table.
614
774
 
615
- `GET` and `HEAD` requests will read from the replica unless there was
616
- a write in the last 2 seconds, otherwise they will read from the primary.
617
- Non-get requests will always write to the primary. The middleware accepts
618
- an argument for a Resolver class and an Operations class where you are able
619
- to change how the auto-switcher works to be most beneficial for your
620
- application.
775
+ ```ruby
776
+ posts = Post.joins(:comments).group(:"posts.hidden")
777
+ posts = posts.where("posts.hidden": false, "comments.hidden": false)
621
778
 
622
- To use the middleware in your application you can use the following
623
- configuration options:
779
+ posts.count
780
+ # => { false => 10 }
624
781
 
625
- ```
626
- config.active_record.database_selector = { delay: 2.seconds }
627
- config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
628
- config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
782
+ # unscope both hidden columns
783
+ posts.unscope(where: :hidden).count
784
+ # => { false => 11, true => 1 }
785
+
786
+ # unscope only comments.hidden column
787
+ posts.unscope(where: :"comments.hidden").count
788
+ # => { false => 11 }
629
789
  ```
630
790
 
631
- To change the database selection strategy, pass a custom class to the
632
- configuration options:
791
+ *Ryuta Kamizono*, *Slava Korolev*
633
792
 
793
+ * Fix `rewhere` to truly overwrite collided where clause by new where clause.
794
+
795
+ ```ruby
796
+ steve = Person.find_by(name: "Steve")
797
+ david = Author.find_by(name: "David")
798
+
799
+ relation = Essay.where(writer: steve)
800
+
801
+ # Before
802
+ relation.rewhere(writer: david).to_a # => []
803
+
804
+ # After
805
+ relation.rewhere(writer: david).to_a # => [david]
634
806
  ```
635
- config.active_record.database_selector = { delay: 10.seconds }
636
- config.active_record.database_resolver = MyResolver
637
- config.active_record.database_resolver_context = MyResolver::MyCookies
807
+
808
+ *Ryuta Kamizono*
809
+
810
+ * Inspect time attributes with subsec and time zone offset.
811
+
812
+ ```ruby
813
+ p Knot.create
814
+ => #<Knot id: 1, created_at: "2016-05-05 01:29:47.116928000 +0000">
638
815
  ```
639
816
 
640
- *Eileen M. Uchitelle*
817
+ *akinomaeni*, *Jonathan Hefner*
641
818
 
642
- * MySQL: Support `:size` option to change text and blob size.
819
+ * Deprecate passing a column to `type_cast`.
643
820
 
644
821
  *Ryuta Kamizono*
645
822
 
646
- * Make `t.timestamps` with precision by default.
823
+ * Deprecate `in_clause_length` and `allowed_index_name_length` in `DatabaseLimits`.
647
824
 
648
825
  *Ryuta Kamizono*
649
826
 
827
+ * Support bulk insert/upsert on relation to preserve scope values.
650
828
 
651
- ## Rails 6.0.0.beta1 (January 18, 2019) ##
829
+ *Josef Šimánek*, *Ryuta Kamizono*
652
830
 
653
- * Remove deprecated `#set_state` from the transaction object.
831
+ * Preserve column comment value on changing column name on MySQL.
654
832
 
655
- *Rafael Mendonça França*
833
+ *Islam Taha*
656
834
 
657
- * Remove deprecated `#supports_statement_cache?` from the database adapters.
835
+ * Add support for `if_exists` option for removing an index.
658
836
 
659
- *Rafael Mendonça França*
837
+ The `remove_index` method can take an `if_exists` option. If this is set to true an error won't be raised if the index doesn't exist.
660
838
 
661
- * Remove deprecated `#insert_fixtures` from the database adapters.
839
+ *Eileen M. Uchitelle*
662
840
 
663
- *Rafael Mendonça França*
841
+ * Remove ibm_db, informix, mssql, oracle, and oracle12 Arel visitors which are not used in the code base.
664
842
 
665
- * Remove deprecated `ActiveRecord::ConnectionAdapters::SQLite3Adapter#valid_alter_table_type?`.
843
+ *Ryuta Kamizono*
666
844
 
667
- *Rafael Mendonça França*
845
+ * Prevent `build_association` from `touching` a parent record if the record isn't persisted for `has_one` associations.
668
846
 
669
- * Do not allow passing the column name to `sum` when a block is passed.
847
+ Fixes #38219.
670
848
 
671
- *Rafael Mendonça França*
849
+ *Josh Brody*
672
850
 
673
- * Do not allow passing the column name to `count` when a block is passed.
851
+ * Add support for `if_not_exists` option for adding index.
674
852
 
675
- *Rafael Mendonça França*
853
+ The `add_index` method respects `if_not_exists` option. If it is set to true
854
+ index won't be added.
676
855
 
677
- * Remove delegation of missing methods in a relation to arel.
856
+ Usage:
678
857
 
679
- *Rafael Mendonça França*
858
+ ```ruby
859
+ add_index :users, :account_id, if_not_exists: true
860
+ ```
680
861
 
681
- * Remove delegation of missing methods in a relation to private methods of the class.
862
+ The `if_not_exists` option passed to `create_table` also gets propagated to indexes
863
+ created within that migration so that if table and its indexes exist then there is no
864
+ attempt to create them again.
682
865
 
683
- *Rafael Mendonça França*
866
+ *Prathamesh Sonpatki*
684
867
 
685
- * Deprecate `config.active_record.sqlite3.represent_boolean_as_integer`.
868
+ * Add `ActiveRecord::Base#previously_new_record?` to show if a record was new before the last save.
686
869
 
687
- *Rafael Mendonça França*
870
+ *Tom Ward*
688
871
 
689
- * Change `SQLite3Adapter` to always represent boolean values as integers.
872
+ * Support descending order for `find_each`, `find_in_batches`, and `in_batches`.
690
873
 
691
- *Rafael Mendonça França*
874
+ Batch processing methods allow you to work with the records in batches, greatly reducing memory consumption, but records are always batched from oldest id to newest.
692
875
 
693
- * Remove ability to specify a timestamp name for `#cache_key`.
876
+ This change allows reversing the order, batching from newest to oldest. This is useful when you need to process newer batches of records first.
694
877
 
695
- *Rafael Mendonça França*
878
+ Pass `order: :desc` to yield batches in descending order. The default remains `order: :asc`.
696
879
 
697
- * Remove deprecated `ActiveRecord::Migrator.migrations_path=`.
880
+ ```ruby
881
+ Person.find_each(order: :desc) do |person|
882
+ person.party_all_night!
883
+ end
884
+ ```
698
885
 
699
- *Rafael Mendonça França*
886
+ *Alexey Vasiliev*
700
887
 
701
- * Remove deprecated `expand_hash_conditions_for_aggregates`.
888
+ * Fix `insert_all` with enum values.
702
889
 
703
- *Rafael Mendonça França*
890
+ Fixes #38716.
704
891
 
705
- * Set polymorphic type column to NULL on `dependent: :nullify` strategy.
892
+ *Joel Blum*
706
893
 
707
- On polymorphic associations both the foreign key and the foreign type columns will be set to NULL.
894
+ * Add support for `db:rollback:name` for multiple database applications.
708
895
 
709
- *Laerti Papa*
896
+ Multiple database applications will now raise if `db:rollback` is call and recommend using the `db:rollback:[NAME]` to rollback migrations.
710
897
 
711
- * Allow permitted instance of `ActionController::Parameters` as argument of `ActiveRecord::Relation#exists?`.
898
+ *Eileen M. Uchitelle*
712
899
 
713
- *Gannon McGibbon*
900
+ * `Relation#pick` now uses already loaded results instead of making another query.
714
901
 
715
- * Add support for endless ranges introduces in Ruby 2.6.
902
+ *Eugene Kenny*
716
903
 
717
- *Greg Navis*
904
+ * Deprecate using `return`, `break` or `throw` to exit a transaction block after writes.
718
905
 
719
- * Deprecate passing `migrations_paths` to `connection.assume_migrated_upto_version`.
906
+ *Dylan Thacker-Smith*
720
907
 
721
- *Ryuta Kamizono*
908
+ * Dump the schema or structure of a database when calling `db:migrate:name`.
722
909
 
723
- * MySQL: `ROW_FORMAT=DYNAMIC` create table option by default.
910
+ In previous versions of Rails, `rails db:migrate` would dump the schema of the database. In Rails 6, that holds true (`rails db:migrate` dumps all databases' schemas), but `rails db:migrate:name` does not share that behavior.
724
911
 
725
- Since MySQL 5.7.9, the `innodb_default_row_format` option defines the default row
726
- format for InnoDB tables. The default setting is `DYNAMIC`.
727
- The row format is required for indexing on `varchar(255)` with `utf8mb4` columns.
912
+ Going forward, calls to `rails db:migrate:name` will dump the schema (or structure) of the database being migrated.
728
913
 
729
- *Ryuta Kamizono*
914
+ *Kyle Thompson*
730
915
 
731
- * Fix join table column quoting with SQLite.
916
+ * Reset the `ActiveRecord::Base` connection after `rails db:migrate:name`.
732
917
 
733
- *Gannon McGibbon*
918
+ When `rails db:migrate` has finished, it ensures the `ActiveRecord::Base` connection is reset to its original configuration. Going forward, `rails db:migrate:name` will have the same behavior.
734
919
 
735
- * Allow disabling scopes generated by `ActiveRecord.enum`.
920
+ *Kyle Thompson*
736
921
 
737
- *Alfred Dominic*
922
+ * Disallow calling `connected_to` on subclasses of `ActiveRecord::Base`.
738
923
 
739
- * Ensure that `delete_all` on collection proxy returns affected count.
924
+ Behavior has not changed here but the previous API could be misleading to people who thought it would switch connections for only that class. `connected_to` switches the context from which we are getting connections, not the connections themselves.
740
925
 
741
- *Ryuta Kamizono*
926
+ *Eileen M. Uchitelle*, *John Crepezzi*
742
927
 
743
- * Reset scope after delete on collection association to clear stale offsets of removed records.
928
+ * Add support for horizontal sharding to `connects_to` and `connected_to`.
744
929
 
745
- *Gannon McGibbon*
930
+ Applications can now connect to multiple shards and switch between their shards in an application. Note that the shard swapping is still a manual process as this change does not include an API for automatic shard swapping.
746
931
 
747
- * Add the ability to prevent writes to a database for the duration of a block.
932
+ Usage:
748
933
 
749
- Allows the application to prevent writes to a database. This can be useful when
750
- you're building out multiple databases and want to make sure you're not sending
751
- writes when you want a read.
934
+ Given the following configuration:
752
935
 
753
- If `while_preventing_writes` is called and the query is considered a write
754
- query the database will raise an exception regardless of whether the database
755
- user is able to write.
936
+ ```yaml
937
+ # config/database.yml
938
+ production:
939
+ primary:
940
+ database: my_database
941
+ primary_shard_one:
942
+ database: my_database_shard_one
943
+ ```
756
944
 
757
- This is not meant to be a catch-all for write queries but rather a way to enforce
758
- read-only queries without opening a second connection. One purpose of this is to
759
- catch accidental writes, not all writes.
945
+ Connect to multiple shards:
760
946
 
761
- *Eileen M. Uchitelle*
947
+ ```ruby
948
+ class ApplicationRecord < ActiveRecord::Base
949
+ self.abstract_class = true
762
950
 
763
- * Allow aliased attributes to be used in `#update_columns` and `#update`.
951
+ connects_to shards: {
952
+ default: { writing: :primary },
953
+ shard_one: { writing: :primary_shard_one }
954
+ }
955
+ ```
764
956
 
765
- *Gannon McGibbon*
957
+ Swap between shards in your controller / model code:
958
+
959
+ ```ruby
960
+ ActiveRecord::Base.connected_to(shard: :shard_one) do
961
+ # Read from shard one
962
+ end
963
+ ```
766
964
 
767
- * Allow spaces in postgres table names.
965
+ The horizontal sharding API also supports read replicas. See guides for more details.
768
966
 
769
- Fixes issue where "user post" is misinterpreted as "\"user\".\"post\"" when quoting table names with the postgres adapter.
967
+ *Eileen M. Uchitelle*, *John Crepezzi*
770
968
 
771
- *Gannon McGibbon*
969
+ * Deprecate `spec_name` in favor of `name` on database configurations.
772
970
 
773
- * Cached `columns_hash` fields should be excluded from `ResultSet#column_types`.
971
+ The accessors for `spec_name` on `configs_for` and `DatabaseConfig` are deprecated. Please use `name` instead.
774
972
 
775
- PR #34528 addresses the inconsistent behaviour when attribute is defined for an ignored column. The following test
776
- was passing for SQLite and MySQL, but failed for PostgreSQL:
973
+ Deprecated behavior:
777
974
 
778
975
  ```ruby
779
- class DeveloperName < ActiveRecord::Type::String
780
- def deserialize(value)
781
- "Developer: #{value}"
782
- end
783
- end
976
+ db_config = ActiveRecord::Base.configs_for(env_name: "development", spec_name: "primary")
977
+ db_config.spec_name
978
+ ```
784
979
 
785
- class AttributedDeveloper < ActiveRecord::Base
786
- self.table_name = "developers"
980
+ New behavior:
787
981
 
788
- attribute :name, DeveloperName.new
982
+ ```ruby
983
+ db_config = ActiveRecord::Base.configs_for(env_name: "development", name: "primary")
984
+ db_config.name
985
+ ```
789
986
 
790
- self.ignored_columns += ["name"]
791
- end
987
+ *Eileen M. Uchitelle*
792
988
 
793
- developer = AttributedDeveloper.create
794
- developer.update_column :name, "name"
989
+ * Add additional database-specific rake tasks for multi-database users.
795
990
 
796
- loaded_developer = AttributedDeveloper.where(id: developer.id).select("*").first
797
- puts loaded_developer.name # should be "Developer: name" but it's just "name"
991
+ Previously, `rails db:create`, `rails db:drop`, and `rails db:migrate` were the only rails tasks that could operate on a single
992
+ database. For example:
993
+
994
+ ```
995
+ rails db:create
996
+ rails db:create:primary
997
+ rails db:create:animals
998
+ rails db:drop
999
+ rails db:drop:primary
1000
+ rails db:drop:animals
1001
+ rails db:migrate
1002
+ rails db:migrate:primary
1003
+ rails db:migrate:animals
798
1004
  ```
799
1005
 
800
- *Dmitry Tsepelev*
1006
+ With these changes, `rails db:schema:dump`, `rails db:schema:load`, `rails db:structure:dump`, `rails db:structure:load` and
1007
+ `rails db:test:prepare` can additionally operate on a single database. For example:
801
1008
 
802
- * Make the implicit order column configurable.
1009
+ ```
1010
+ rails db:schema:dump
1011
+ rails db:schema:dump:primary
1012
+ rails db:schema:dump:animals
1013
+ rails db:schema:load
1014
+ rails db:schema:load:primary
1015
+ rails db:schema:load:animals
1016
+ rails db:structure:dump
1017
+ rails db:structure:dump:primary
1018
+ rails db:structure:dump:animals
1019
+ rails db:structure:load
1020
+ rails db:structure:load:primary
1021
+ rails db:structure:load:animals
1022
+ rails db:test:prepare
1023
+ rails db:test:prepare:primary
1024
+ rails db:test:prepare:animals
1025
+ ```
803
1026
 
804
- When calling ordered finder methods such as `first` or `last` without an
805
- explicit order clause, ActiveRecord sorts records by primary key. This can
806
- result in unpredictable and surprising behaviour when the primary key is
807
- not an auto-incrementing integer, for example when it's a UUID. This change
808
- makes it possible to override the column used for implicit ordering such
809
- that `first` and `last` will return more predictable results.
1027
+ *Kyle Thompson*
810
1028
 
811
- Example:
1029
+ * Add support for `strict_loading` mode on association declarations.
812
1030
 
813
- class Project < ActiveRecord::Base
814
- self.implicit_order_column = "created_at"
815
- end
1031
+ Raise an error if attempting to load a record from an association that has been marked as `strict_loading` unless it was explicitly eager loaded.
1032
+
1033
+ Usage:
1034
+
1035
+ ```ruby
1036
+ class Developer < ApplicationRecord
1037
+ has_many :projects, strict_loading: true
1038
+ end
1039
+
1040
+ dev = Developer.first
1041
+ dev.projects.first
1042
+ # => ActiveRecord::StrictLoadingViolationError: The projects association is marked as strict_loading and cannot be lazily loaded.
1043
+ ```
1044
+
1045
+ *Kevin Deisz*
816
1046
 
817
- *Tekin Suleyman*
1047
+ * Add support for `strict_loading` mode to prevent lazy loading of records.
818
1048
 
819
- * Bump minimum PostgreSQL version to 9.3.
1049
+ Raise an error if a parent record is marked as `strict_loading` and attempts to lazily load its associations. This is useful for finding places you may want to preload an association and avoid additional queries.
820
1050
 
821
- *Yasuo Honda*
1051
+ Usage:
822
1052
 
823
- * Values of enum are frozen, raising an error when attempting to modify them.
1053
+ ```ruby
1054
+ dev = Developer.strict_loading.first
1055
+ dev.audit_logs.to_a
1056
+ # => ActiveRecord::StrictLoadingViolationError: Developer is marked as strict_loading and AuditLog cannot be lazily loaded.
1057
+ ```
824
1058
 
825
- *Emmanuel Byrd*
1059
+ *Eileen M. Uchitelle*, *Aaron Patterson*
826
1060
 
827
- * Move `ActiveRecord::StatementInvalid` SQL to error property and include binds as separate error property.
1061
+ * Add support for PostgreSQL 11+ partitioned indexes when using `upsert_all`.
828
1062
 
829
- `ActiveRecord::ConnectionAdapters::AbstractAdapter#translate_exception_class` now requires `binds` to be passed as the last argument.
1063
+ *Sebastián Palma*
830
1064
 
831
- `ActiveRecord::ConnectionAdapters::AbstractAdapter#translate_exception` now requires `message`, `sql`, and `binds` to be passed as keyword arguments.
1065
+ * Adds support for `if_not_exists` to `add_column` and `if_exists` to `remove_column`.
832
1066
 
833
- Subclasses of `ActiveRecord::StatementInvalid` must now provide `sql:` and `binds:` arguments to `super`.
1067
+ Applications can set their migrations to ignore exceptions raised when adding a column that already exists or when removing a column that does not exist.
834
1068
 
835
- Example:
1069
+ Example Usage:
836
1070
 
1071
+ ```ruby
1072
+ class AddColumnTitle < ActiveRecord::Migration[6.1]
1073
+ def change
1074
+ add_column :posts, :title, :string, if_not_exists: true
1075
+ end
1076
+ end
837
1077
  ```
838
- class MySubclassedError < ActiveRecord::StatementInvalid
839
- def initialize(message, sql:, binds:)
840
- super(message, sql: sql, binds: binds)
1078
+
1079
+ ```ruby
1080
+ class RemoveColumnTitle < ActiveRecord::Migration[6.1]
1081
+ def change
1082
+ remove_column :posts, :title, if_exists: true
841
1083
  end
842
1084
  end
843
1085
  ```
844
1086
 
845
- *Gannon McGibbon*
1087
+ *Eileen M. Uchitelle*
846
1088
 
847
- * Add an `:if_not_exists` option to `create_table`.
1089
+ * Regexp-escape table name for MS SQL Server.
848
1090
 
849
- Example:
1091
+ Add `Regexp.escape` to one method in ActiveRecord, so that table names with regular expression characters in them work as expected. Since MS SQL Server uses "[" and "]" to quote table and column names, and those characters are regular expression characters, methods like `pluck` and `select` fail in certain cases when used with the MS SQL Server adapter.
850
1092
 
851
- create_table :posts, if_not_exists: true do |t|
852
- t.string :title
853
- end
1093
+ *Larry Reid*
854
1094
 
855
- That would execute:
1095
+ * Store advisory locks on their own named connection.
856
1096
 
857
- CREATE TABLE IF NOT EXISTS posts (
858
- ...
859
- )
1097
+ Previously advisory locks were taken out against a connection when a migration started. This works fine in single database applications but doesn't work well when migrations need to open new connections which results in the lock getting dropped.
860
1098
 
861
- If the table already exists, `if_not_exists: false` (the default) raises an
862
- exception whereas `if_not_exists: true` does nothing.
1099
+ In order to fix this we are storing the advisory lock on a new connection with the connection specification name `AdvisoryLockBase`. The caveat is that we need to maintain at least 2 connections to a database while migrations are running in order to do this.
863
1100
 
864
- *fatkodima*, *Stefan Kanev*
1101
+ *Eileen M. Uchitelle*, *John Crepezzi*
865
1102
 
866
- * Defining an Enum as a Hash with blank key, or as an Array with a blank value, now raises an `ArgumentError`.
1103
+ * Allow schema cache path to be defined in the database configuration file.
867
1104
 
868
- *Christophe Maximin*
1105
+ For example:
869
1106
 
870
- * Adds support for multiple databases to `rails db:schema:cache:dump` and `rails db:schema:cache:clear`.
1107
+ ```yaml
1108
+ development:
1109
+ adapter: postgresql
1110
+ database: blog_development
1111
+ pool: 5
1112
+ schema_cache_path: tmp/schema/main.yml
1113
+ ```
871
1114
 
872
- *Gannon McGibbon*
1115
+ *Katrina Owen*
873
1116
 
874
- * `update_columns` now correctly raises `ActiveModel::MissingAttributeError`
875
- if the attribute does not exist.
1117
+ * Deprecate `#remove_connection` in favor of `#remove_connection_pool` when called on the handler.
876
1118
 
877
- *Sean Griffin*
1119
+ `#remove_connection` is deprecated in order to support returning a `DatabaseConfig` object instead of a `Hash`. Use `#remove_connection_pool`, `#remove_connection` will be removed in 6.2.
878
1120
 
879
- * Add support for hash and URL configs in database hash of `ActiveRecord::Base.connected_to`.
1121
+ *Eileen M. Uchitelle*, *John Crepezzi*
880
1122
 
881
- ````
882
- User.connected_to(database: { writing: "postgres://foo" }) do
883
- User.create!(name: "Gannon")
884
- end
1123
+ * Deprecate `#default_hash` and it's alias `#[]` on database configurations.
885
1124
 
886
- config = { "adapter" => "sqlite3", "database" => "db/readonly.sqlite3" }
887
- User.connected_to(database: { reading: config }) do
888
- User.count
889
- end
890
- ````
1125
+ Applications should use `configs_for`. `#default_hash` and `#[]` will be removed in 6.2.
891
1126
 
892
- *Gannon McGibbon*
1127
+ *Eileen M. Uchitelle*, *John Crepezzi*
893
1128
 
894
- * Support default expression for MySQL.
1129
+ * Add scale support to `ActiveRecord::Validations::NumericalityValidator`.
895
1130
 
896
- MySQL 8.0.13 and higher supports default value to be a function or expression.
1131
+ *Gannon McGibbon*
897
1132
 
898
- https://dev.mysql.com/doc/refman/8.0/en/create-table.html
1133
+ * Find orphans by looking for missing relations through chaining `where.missing`:
899
1134
 
900
- *Ryuta Kamizono*
1135
+ Before:
901
1136
 
902
- * Support expression indexes for MySQL.
1137
+ ```ruby
1138
+ Post.left_joins(:author).where(authors: { id: nil })
1139
+ ```
903
1140
 
904
- MySQL 8.0.13 and higher supports functional key parts that index
905
- expression values rather than column or column prefix values.
1141
+ After:
906
1142
 
907
- https://dev.mysql.com/doc/refman/8.0/en/create-index.html
1143
+ ```ruby
1144
+ Post.where.missing(:author)
1145
+ ```
908
1146
 
909
- *Ryuta Kamizono*
1147
+ *Tom Rossi*
910
1148
 
911
- * Fix collection cache key with limit and custom select to avoid ambiguous timestamp column error.
1149
+ * Ensure `:reading` connections always raise if a write is attempted.
912
1150
 
913
- Fixes #33056.
1151
+ Now Rails will raise an `ActiveRecord::ReadOnlyError` if any connection on the reading handler attempts to make a write. If your reading role needs to write you should name the role something other than `:reading`.
914
1152
 
915
- *Federico Martinez*
1153
+ *Eileen M. Uchitelle*
916
1154
 
917
- * Add basic API for connection switching to support multiple databases.
1155
+ * Deprecate `"primary"` as the `connection_specification_name` for `ActiveRecord::Base`.
918
1156
 
919
- 1) Adds a `connects_to` method for models to connect to multiple databases. Example:
1157
+ `"primary"` has been deprecated as the `connection_specification_name` for `ActiveRecord::Base` in favor of using `"ActiveRecord::Base"`. This change affects calls to `ActiveRecord::Base.connection_handler.retrieve_connection` and `ActiveRecord::Base.connection_handler.remove_connection`. If you're calling these methods with `"primary"`, please switch to `"ActiveRecord::Base"`.
920
1158
 
921
- ```
922
- class AnimalsModel < ApplicationRecord
923
- self.abstract_class = true
924
-
925
- connects_to database: { writing: :animals_primary, reading: :animals_replica }
926
- end
1159
+ *Eileen M. Uchitelle*, *John Crepezzi*
927
1160
 
928
- class Dog < AnimalsModel
929
- # connected to both the animals_primary db for writing and the animals_replica for reading
930
- end
931
- ```
1161
+ * Add `ActiveRecord::Validations::NumericalityValidator` with
1162
+ support for casting floats using a database columns' precision value.
932
1163
 
933
- 2) Adds a `connected_to` block method for switching connection roles or connecting to
934
- a database that the model didn't connect to. Connecting to the database in this block is
935
- useful when you have another defined connection, for example `slow_replica` that you don't
936
- want to connect to by default but need in the console, or a specific code block.
1164
+ *Gannon McGibbon*
937
1165
 
938
- ```
939
- ActiveRecord::Base.connected_to(role: :reading) do
940
- Dog.first # finds dog from replica connected to AnimalsBase
941
- Book.first # doesn't have a reading connection, will raise an error
942
- end
943
- ```
1166
+ * Enforce fresh ETag header after a collection's contents change by adding
1167
+ ActiveRecord::Relation#cache_key_with_version. This method will be used by
1168
+ ActionController::ConditionalGet to ensure that when collection cache versioning
1169
+ is enabled, requests using ConditionalGet don't return the same ETag header
1170
+ after a collection is modified.
944
1171
 
945
- ```
946
- ActiveRecord::Base.connected_to(database: :slow_replica) do
947
- 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
948
- end
949
- ```
1172
+ Fixes #38078.
950
1173
 
951
- *Eileen M. Uchitelle*
1174
+ *Aaron Lipman*
952
1175
 
953
- * Enum raises on invalid definition values
1176
+ * Skip test database when running `db:create` or `db:drop` in development
1177
+ with `DATABASE_URL` set.
954
1178
 
955
- When defining a Hash enum it can be easy to use `[]` instead of `{}`. This
956
- commit checks that only valid definition values are provided, those can
957
- be a Hash, an array of Symbols or an array of Strings. Otherwise it
958
- raises an `ArgumentError`.
1179
+ *Brian Buchalter*
959
1180
 
960
- Fixes #33961
1181
+ * Don't allow mutations on the database configurations hash.
961
1182
 
962
- *Alberto Almagro*
1183
+ Freeze the configurations hash to disallow directly changing it. If applications need to change the hash, for example to create databases for parallelization, they should use the `DatabaseConfig` object directly.
963
1184
 
964
- * Reloading associations now clears the Query Cache like `Persistence#reload` does.
1185
+ Before:
965
1186
 
1187
+ ```ruby
1188
+ @db_config = ActiveRecord::Base.configurations.configs_for(env_name: "test", spec_name: "primary")
1189
+ @db_config.configuration_hash.merge!(idle_timeout: "0.02")
966
1190
  ```
967
- class Post < ActiveRecord::Base
968
- has_one :category
969
- belongs_to :author
970
- has_many :comments
971
- end
972
1191
 
973
- # Each of the following will now clear the query cache.
974
- post.reload_category
975
- post.reload_author
976
- post.comments.reload
1192
+ After:
1193
+
1194
+ ```ruby
1195
+ @db_config = ActiveRecord::Base.configurations.configs_for(env_name: "test", spec_name: "primary")
1196
+ config = @db_config.configuration_hash.merge(idle_timeout: "0.02")
1197
+ db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(@db_config.env_name, @db_config.spec_name, config)
977
1198
  ```
978
1199
 
979
- *Christophe Maximin*
1200
+ *Eileen M. Uchitelle*, *John Crepezzi*
980
1201
 
981
- * Added `index` option for `change_table` migration helpers.
982
- With this change you can create indexes while adding new
983
- columns into the existing tables.
1202
+ * Remove `:connection_id` from the `sql.active_record` notification.
984
1203
 
985
- Example:
1204
+ *Aaron Patterson*, *Rafael Mendonça França*
986
1205
 
987
- change_table(:languages) do |t|
988
- t.string :country_code, index: true
989
- end
1206
+ * The `:name` key will no longer be returned as part of `DatabaseConfig#configuration_hash`. Please use `DatabaseConfig#owner_name` instead.
990
1207
 
991
- *Mehmet Emin İNAÇ*
1208
+ *Eileen M. Uchitelle*, *John Crepezzi*
992
1209
 
993
- * Fix `transaction` reverting for migrations.
1210
+ * ActiveRecord's `belongs_to_required_by_default` flag can now be set per model.
994
1211
 
995
- Before: Commands inside a `transaction` in a reverted migration ran uninverted.
996
- Now: This change fixes that by reverting commands inside `transaction` block.
1212
+ You can now opt-out/opt-in specific models from having their associations required
1213
+ by default.
997
1214
 
998
- *fatkodima*, *David Verhasselt*
1215
+ This change is meant to ease the process of migrating all your models to have
1216
+ their association required.
999
1217
 
1000
- * Raise an error instead of scanning the filesystem root when `fixture_path` is blank.
1218
+ *Edouard Chin*
1001
1219
 
1002
- *Gannon McGibbon*, *Max Albrecht*
1220
+ * The `connection_config` method has been deprecated, please use `connection_db_config` instead which will return a `DatabaseConfigurations::DatabaseConfig` instead of a `Hash`.
1003
1221
 
1004
- * Allow `ActiveRecord::Base.configurations=` to be set with a symbolized hash.
1222
+ *Eileen M. Uchitelle*, *John Crepezzi*
1005
1223
 
1006
- *Gannon McGibbon*
1224
+ * Retain explicit selections on the base model after applying `includes` and `joins`.
1007
1225
 
1008
- * Don't update counter cache unless the record is actually saved.
1226
+ Resolves #34889.
1009
1227
 
1010
- Fixes #31493, #33113, #33117.
1228
+ *Patrick Rebsch*
1011
1229
 
1012
- *Ryuta Kamizono*
1230
+ * The `database` kwarg is deprecated without replacement because it can't be used for sharding and creates an issue if it's used during a request. Applications that need to create new connections should use `connects_to` instead.
1013
1231
 
1014
- * Deprecate `ActiveRecord::Result#to_hash` in favor of `ActiveRecord::Result#to_a`.
1232
+ *Eileen M. Uchitelle*, *John Crepezzi*
1015
1233
 
1016
- *Gannon McGibbon*, *Kevin Cheng*
1234
+ * Allow attributes to be fetched from Arel node groupings.
1017
1235
 
1018
- * SQLite3 adapter supports expression indexes.
1236
+ *Jeff Emminger*, *Gannon McGibbon*
1019
1237
 
1020
- ```
1021
- create_table :users do |t|
1022
- t.string :email
1023
- end
1238
+ * A database URL can now contain a querystring value that contains an equal sign. This is needed to support passing PostgreSQL `options`.
1024
1239
 
1025
- add_index :users, 'lower(email)', name: 'index_users_on_email', unique: true
1026
- ```
1240
+ *Joshua Flanagan*
1027
1241
 
1028
- *Gray Kemmey*
1242
+ * Calling methods like `establish_connection` with a `Hash` which is invalid (eg: no `adapter`) will now raise an error the same way as connections defined in `config/database.yml`.
1029
1243
 
1030
- * Allow subclasses to redefine autosave callbacks for associated records.
1244
+ *John Crepezzi*
1031
1245
 
1032
- Fixes #33305.
1246
+ * Specifying `implicit_order_column` now subsorts the records by primary key if available to ensure deterministic results.
1033
1247
 
1034
- *Andrey Subbota*
1248
+ *Paweł Urbanek*
1035
1249
 
1036
- * Bump minimum MySQL version to 5.5.8.
1250
+ * `where(attr => [])` now loads an empty result without making a query.
1037
1251
 
1038
- *Yasuo Honda*
1252
+ *John Hawthorn*
1039
1253
 
1040
- * Use MySQL utf8mb4 character set by default.
1254
+ * Fixed the performance regression for `primary_keys` introduced MySQL 8.0.
1041
1255
 
1042
- `utf8mb4` character set with 4-Byte encoding supports supplementary characters including emoji.
1043
- The previous default 3-Byte encoding character set `utf8` is not enough to support them.
1256
+ *Hiroyuki Ishii*
1044
1257
 
1045
- *Yasuo Honda*
1258
+ * Add support for `belongs_to` to `has_many` inversing.
1046
1259
 
1047
- * Fix duplicated record creation when using nested attributes with `create_with`.
1260
+ *Gannon McGibbon*
1048
1261
 
1049
- *Darwin Wu*
1262
+ * Allow length configuration for `has_secure_token` method. The minimum length
1263
+ is set at 24 characters.
1050
1264
 
1051
- * Configuration item `config.filter_parameters` could also filter out
1052
- sensitive values of database columns when calling `#inspect`.
1053
- We also added `ActiveRecord::Base::filter_attributes`/`=` in order to
1054
- specify sensitive attributes to specific model.
1265
+ Before:
1055
1266
 
1267
+ ```ruby
1268
+ has_secure_token :auth_token
1056
1269
  ```
1057
- Rails.application.config.filter_parameters += [:credit_card_number, /phone/]
1058
- Account.last.inspect # => #<Account id: 123, name: "DHH", credit_card_number: [FILTERED], telephone_number: [FILTERED] ...>
1059
- SecureAccount.filter_attributes += [:name]
1060
- SecureAccount.last.inspect # => #<SecureAccount id: 42, name: [FILTERED], credit_card_number: [FILTERED] ...>
1270
+
1271
+ After:
1272
+
1273
+ ```ruby
1274
+ has_secure_token :default_token # 24 characters
1275
+ has_secure_token :auth_token, length: 36 # 36 characters
1276
+ has_secure_token :invalid_token, length: 12 # => ActiveRecord::SecureToken::MinimumLengthError
1061
1277
  ```
1062
1278
 
1063
- *Zhang Kang*, *Yoshiyuki Kinjo*
1279
+ *Bernardo de Araujo*
1064
1280
 
1065
- * Deprecate `column_name_length`, `table_name_length`, `columns_per_table`,
1066
- `indexes_per_table`, `columns_per_multicolumn_index`, `sql_query_length`,
1067
- and `joins_per_query` methods in `DatabaseLimits`.
1281
+ * Deprecate `DatabaseConfigurations#to_h`. These connection hashes are still available via `ActiveRecord::Base.configurations.configs_for`.
1068
1282
 
1069
- *Ryuta Kamizono*
1283
+ *Eileen Uchitelle*, *John Crepezzi*
1070
1284
 
1071
- * `ActiveRecord::Base.configurations` now returns an object.
1285
+ * Add `DatabaseConfig#configuration_hash` to return database configuration hashes with symbol keys, and use all symbol-key configuration hashes internally. Deprecate `DatabaseConfig#config` which returns a String-keyed `Hash` with the same values.
1072
1286
 
1073
- `ActiveRecord::Base.configurations` used to return a hash, but this
1074
- is an inflexible data model. In order to improve multiple-database
1075
- handling in Rails, we've changed this to return an object. Some methods
1076
- are provided to make the object behave hash-like in order to ease the
1077
- transition process. Since most applications don't manipulate the hash
1078
- we've decided to add backwards-compatible functionality that will throw
1079
- a deprecation warning if used, however calling `ActiveRecord::Base.configurations`
1080
- will use the new version internally and externally.
1287
+ *John Crepezzi*, *Eileen Uchitelle*
1081
1288
 
1082
- For example, the following `database.yml`:
1289
+ * Allow column names to be passed to `remove_index` positionally along with other options.
1083
1290
 
1084
- ```
1085
- development:
1086
- adapter: sqlite3
1087
- database: db/development.sqlite3
1088
- ```
1291
+ Passing other options can be necessary to make `remove_index` correctly reversible.
1089
1292
 
1090
- Used to become a hash:
1293
+ Before:
1091
1294
 
1092
- ```
1093
- { "development" => { "adapter" => "sqlite3", "database" => "db/development.sqlite3" } }
1094
- ```
1295
+ add_index :reports, :report_id # => works
1296
+ add_index :reports, :report_id, unique: true # => works
1297
+ remove_index :reports, :report_id # => works
1298
+ remove_index :reports, :report_id, unique: true # => ArgumentError
1095
1299
 
1096
- Is now converted into the following object:
1300
+ After:
1097
1301
 
1098
- ```
1099
- #<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800 @configurations=[
1100
- #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
1101
- @spec_name="primary", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>
1102
- ]
1103
- ```
1302
+ remove_index :reports, :report_id, unique: true # => works
1104
1303
 
1105
- Iterating over the database configurations has also changed. Instead of
1106
- calling hash methods on the `configurations` hash directly, a new method `configs_for` has
1107
- been provided that allows you to select the correct configuration. `env_name` and
1108
- `spec_name` arguments are optional. For example, these return an array of
1109
- database config objects for the requested environment and a single database config object
1110
- will be returned for the requested environment and specification name respectively.
1304
+ *Eugene Kenny*
1111
1305
 
1112
- ```
1113
- ActiveRecord::Base.configurations.configs_for(env_name: "development")
1114
- ActiveRecord::Base.configurations.configs_for(env_name: "development", spec_name: "primary")
1115
- ```
1306
+ * Allow bulk `ALTER` statements to drop and recreate indexes with the same name.
1116
1307
 
1117
- *Eileen M. Uchitelle*, *Aaron Patterson*
1308
+ *Eugene Kenny*
1118
1309
 
1119
- * Add database configuration to disable advisory locks.
1310
+ * `insert`, `insert_all`, `upsert`, and `upsert_all` now clear the query cache.
1120
1311
 
1121
- ```
1122
- production:
1123
- adapter: postgresql
1124
- advisory_locks: false
1125
- ```
1312
+ *Eugene Kenny*
1126
1313
 
1127
- *Guo Xiang*
1314
+ * Call `while_preventing_writes` directly from `connected_to`.
1128
1315
 
1129
- * SQLite3 adapter `alter_table` method restores foreign keys.
1316
+ 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)`.
1130
1317
 
1131
- *Yasuo Honda*
1318
+ 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.
1132
1319
 
1133
- * Allow `:to_table` option to `invert_remove_foreign_key`.
1320
+ *Eileen M. Uchitelle*
1134
1321
 
1135
- Example:
1322
+ * Improve detection of ActiveRecord::StatementTimeout with mysql2 adapter in the edge case when the query is terminated during filesort.
1323
+
1324
+ *Kir Shatrov*
1136
1325
 
1137
- remove_foreign_key :accounts, to_table: :owners
1326
+ * Stop trying to read yaml file fixtures when loading Active Record fixtures.
1138
1327
 
1139
- *Nikolay Epifanov*, *Rich Chen*
1328
+ *Gannon McGibbon*
1140
1329
 
1141
- * Add environment & load_config dependency to `bin/rake db:seed` to enable
1142
- seed load in environments without Rails and custom DB configuration
1330
+ * Deprecate `.reorder(nil)` with `.first` / `.first!` taking non-deterministic result.
1143
1331
 
1144
- *Tobias Bielohlawek*
1332
+ To continue taking non-deterministic result, use `.take` / `.take!` instead.
1145
1333
 
1146
- * Fix default value for mysql time types with specified precision.
1334
+ *Ryuta Kamizono*
1147
1335
 
1148
- *Nikolay Kondratyev*
1336
+ * Preserve user supplied joins order as much as possible.
1149
1337
 
1150
- * Fix `touch` option to behave consistently with `Persistence#touch` method.
1338
+ Fixes #36761, #34328, #24281, #12953.
1151
1339
 
1152
1340
  *Ryuta Kamizono*
1153
1341
 
1154
- * Migrations raise when duplicate column definition.
1342
+ * Allow `matches_regex` and `does_not_match_regexp` on the MySQL Arel visitor.
1343
+
1344
+ *James Pearson*
1345
+
1346
+ * Allow specifying fixtures to be ignored by setting `ignore` in YAML file's '_fixture' section.
1155
1347
 
1156
- Fixes #33024.
1348
+ *Tongfei Gao*
1157
1349
 
1158
- *Federico Martinez*
1350
+ * Make the DATABASE_URL env variable only affect the primary connection. Add new env variables for multiple databases.
1159
1351
 
1160
- * Bump minimum SQLite version to 3.8
1352
+ *John Crepezzi*, *Eileen Uchitelle*
1161
1353
 
1162
- *Yasuo Honda*
1354
+ * Add a warning for enum elements with 'not_' prefix.
1163
1355
 
1164
- * Fix parent record should not get saved with duplicate children records.
1356
+ class Foo
1357
+ enum status: [:sent, :not_sent]
1358
+ end
1165
1359
 
1166
- Fixes #32940.
1360
+ *Edu Depetris*
1167
1361
 
1168
- *Santosh Wadghule*
1362
+ * Make currency symbols optional for money column type in PostgreSQL.
1169
1363
 
1170
- * Fix logic on disabling commit callbacks so they are not called unexpectedly when errors occur.
1364
+ *Joel Schneider*
1171
1365
 
1172
- *Brian Durand*
1366
+ * Add support for beginless ranges, introduced in Ruby 2.7.
1173
1367
 
1174
- * Ensure `Associations::CollectionAssociation#size` and `Associations::CollectionAssociation#empty?`
1175
- use loaded association ids if present.
1368
+ *Josh Goodall*
1176
1369
 
1177
- *Graham Turner*
1370
+ * Add `database_exists?` method to connection adapters to check if a database exists.
1178
1371
 
1179
- * Add support to preload associations of polymorphic associations when not all the records have the requested associations.
1372
+ *Guilherme Mansur*
1180
1373
 
1181
- *Dana Sherson*
1374
+ * Loading the schema for a model that has no `table_name` raises a `TableNotSpecified` error.
1182
1375
 
1183
- * Add `touch_all` method to `ActiveRecord::Relation`.
1376
+ *Guilherme Mansur*, *Eugene Kenny*
1184
1377
 
1185
- Example:
1378
+ * PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute.
1186
1379
 
1187
- Person.where(name: "David").touch_all(time: Time.new(2020, 5, 16, 0, 0, 0))
1380
+ Fixes #36022.
1188
1381
 
1189
- *fatkodima*, *duggiefresh*
1382
+ *Ryuta Kamizono*
1190
1383
 
1191
- * Add `ActiveRecord::Base.base_class?` predicate.
1384
+ * Make ActiveRecord `ConnectionPool.connections` method thread-safe.
1385
+
1386
+ Fixes #36465.
1192
1387
 
1193
- *Bogdan Gusiev*
1388
+ *Jeff Doering*
1194
1389
 
1195
- * Add custom prefix/suffix options to `ActiveRecord::Store.store_accessor`.
1390
+ * Add support for multiple databases to `rails db:abort_if_pending_migrations`.
1196
1391
 
1197
- *Tan Huynh*, *Yukio Mizuta*
1392
+ *Mark Lee*
1198
1393
 
1199
- * Rails 6 requires Ruby 2.5.0 or newer.
1394
+ * Fix sqlite3 collation parsing when using decimal columns.
1395
+
1396
+ *Martin R. Schuster*
1200
1397
 
1201
- *Jeremy Daer*, *Kasper Timm Hansen*
1398
+ * Fix invalid schema when primary key column has a comment.
1202
1399
 
1203
- * Deprecate `update_attributes`/`!` in favor of `update`/`!`.
1400
+ Fixes #29966.
1204
1401
 
1205
- *Eddie Lebow*
1402
+ *Guilherme Goettems Schneider*
1206
1403
 
1207
- * Add `ActiveRecord::Base.create_or_find_by`/`!` to deal with the SELECT/INSERT race condition in
1208
- `ActiveRecord::Base.find_or_create_by`/`!` by leaning on unique constraints in the database.
1404
+ * Fix table comment also being applied to the primary key column.
1209
1405
 
1210
- *DHH*
1406
+ *Guilherme Goettems Schneider*
1211
1407
 
1212
- * Add `Relation#pick` as short-hand for single-value plucks.
1408
+ * Allow generated `create_table` migrations to include or skip timestamps.
1213
1409
 
1214
- *DHH*
1410
+ *Michael Duchemin*
1215
1411
 
1216
1412
 
1217
- Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activerecord/CHANGELOG.md) for previous changes.
1413
+ Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/activerecord/CHANGELOG.md) for previous changes.