activerecord 6.1.4.1 → 7.0.0.rc2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1050 -981
- data/MIT-LICENSE +1 -1
- data/README.rdoc +1 -1
- data/lib/active_record/aggregations.rb +1 -1
- data/lib/active_record/association_relation.rb +0 -10
- data/lib/active_record/associations/association.rb +33 -17
- data/lib/active_record/associations/association_scope.rb +1 -3
- data/lib/active_record/associations/belongs_to_association.rb +15 -4
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
- data/lib/active_record/associations/builder/association.rb +8 -2
- data/lib/active_record/associations/builder/belongs_to.rb +19 -6
- data/lib/active_record/associations/builder/collection_association.rb +10 -3
- data/lib/active_record/associations/builder/has_many.rb +3 -2
- data/lib/active_record/associations/builder/has_one.rb +2 -1
- data/lib/active_record/associations/builder/singular_association.rb +2 -2
- data/lib/active_record/associations/collection_association.rb +34 -27
- data/lib/active_record/associations/collection_proxy.rb +8 -3
- data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
- data/lib/active_record/associations/has_many_association.rb +1 -1
- data/lib/active_record/associations/has_many_through_association.rb +2 -1
- data/lib/active_record/associations/has_one_association.rb +10 -7
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/preloader/association.rb +187 -55
- data/lib/active_record/associations/preloader/batch.rb +48 -0
- data/lib/active_record/associations/preloader/branch.rb +147 -0
- data/lib/active_record/associations/preloader/through_association.rb +49 -13
- data/lib/active_record/associations/preloader.rb +39 -113
- data/lib/active_record/associations/singular_association.rb +8 -2
- data/lib/active_record/associations/through_association.rb +3 -3
- data/lib/active_record/associations.rb +90 -82
- data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
- data/lib/active_record/attribute_assignment.rb +1 -1
- data/lib/active_record/attribute_methods/before_type_cast.rb +7 -2
- data/lib/active_record/attribute_methods/dirty.rb +49 -16
- data/lib/active_record/attribute_methods/primary_key.rb +2 -2
- data/lib/active_record/attribute_methods/query.rb +2 -2
- data/lib/active_record/attribute_methods/read.rb +7 -5
- data/lib/active_record/attribute_methods/serialization.rb +66 -12
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +4 -3
- data/lib/active_record/attribute_methods/write.rb +7 -10
- data/lib/active_record/attribute_methods.rb +13 -14
- data/lib/active_record/attributes.rb +24 -35
- data/lib/active_record/autosave_association.rb +6 -21
- data/lib/active_record/base.rb +19 -1
- data/lib/active_record/callbacks.rb +2 -2
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +292 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +209 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +76 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +47 -561
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +0 -17
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +46 -22
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +24 -12
- data/lib/active_record/connection_adapters/abstract/quoting.rb +42 -72
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +4 -17
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +34 -9
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +69 -18
- data/lib/active_record/connection_adapters/abstract/transaction.rb +15 -22
- data/lib/active_record/connection_adapters/abstract_adapter.rb +149 -74
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +97 -81
- data/lib/active_record/connection_adapters/column.rb +4 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +35 -23
- data/lib/active_record/connection_adapters/mysql/quoting.rb +35 -21
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +4 -1
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +12 -6
- data/lib/active_record/connection_adapters/pool_config.rb +7 -7
- data/lib/active_record/connection_adapters/postgresql/column.rb +17 -1
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +19 -12
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
- data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +50 -50
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +32 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +21 -1
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +22 -1
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +25 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +27 -16
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +205 -105
- data/lib/active_record/connection_adapters/schema_cache.rb +29 -4
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +25 -19
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +15 -16
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +4 -2
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +61 -30
- data/lib/active_record/connection_adapters.rb +6 -5
- data/lib/active_record/connection_handling.rb +47 -53
- data/lib/active_record/core.rb +122 -132
- data/lib/active_record/database_configurations/connection_url_resolver.rb +2 -1
- data/lib/active_record/database_configurations/database_config.rb +12 -9
- data/lib/active_record/database_configurations/hash_config.rb +63 -5
- data/lib/active_record/database_configurations/url_config.rb +2 -2
- data/lib/active_record/database_configurations.rb +16 -32
- data/lib/active_record/delegated_type.rb +52 -11
- data/lib/active_record/destroy_association_async_job.rb +1 -1
- data/lib/active_record/disable_joins_association_relation.rb +39 -0
- data/lib/active_record/dynamic_matchers.rb +1 -1
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +98 -0
- data/lib/active_record/encryption/cipher.rb +53 -0
- data/lib/active_record/encryption/config.rb +44 -0
- data/lib/active_record/encryption/configurable.rb +61 -0
- data/lib/active_record/encryption/context.rb +35 -0
- data/lib/active_record/encryption/contexts.rb +72 -0
- data/lib/active_record/encryption/derived_secret_key_provider.rb +12 -0
- data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
- data/lib/active_record/encryption/encryptable_record.rb +208 -0
- data/lib/active_record/encryption/encrypted_attribute_type.rb +140 -0
- data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
- data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
- data/lib/active_record/encryption/encryptor.rb +155 -0
- data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
- data/lib/active_record/encryption/errors.rb +15 -0
- data/lib/active_record/encryption/extended_deterministic_queries.rb +160 -0
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
- data/lib/active_record/encryption/key.rb +28 -0
- data/lib/active_record/encryption/key_generator.rb +42 -0
- data/lib/active_record/encryption/key_provider.rb +46 -0
- data/lib/active_record/encryption/message.rb +33 -0
- data/lib/active_record/encryption/message_serializer.rb +90 -0
- data/lib/active_record/encryption/null_encryptor.rb +21 -0
- data/lib/active_record/encryption/properties.rb +76 -0
- data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
- data/lib/active_record/encryption/scheme.rb +99 -0
- data/lib/active_record/encryption.rb +55 -0
- data/lib/active_record/enum.rb +49 -42
- data/lib/active_record/errors.rb +67 -4
- data/lib/active_record/explain_registry.rb +11 -6
- data/lib/active_record/fixture_set/file.rb +15 -1
- data/lib/active_record/fixture_set/table_row.rb +41 -6
- data/lib/active_record/fixture_set/table_rows.rb +4 -4
- data/lib/active_record/fixtures.rb +17 -20
- data/lib/active_record/future_result.rb +139 -0
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +55 -17
- data/lib/active_record/insert_all.rb +80 -14
- data/lib/active_record/integration.rb +4 -3
- data/lib/active_record/internal_metadata.rb +3 -5
- data/lib/active_record/legacy_yaml_adapter.rb +2 -39
- data/lib/active_record/locking/optimistic.rb +10 -9
- data/lib/active_record/locking/pessimistic.rb +9 -3
- data/lib/active_record/log_subscriber.rb +14 -3
- data/lib/active_record/middleware/database_selector/resolver.rb +6 -10
- data/lib/active_record/middleware/database_selector.rb +8 -3
- data/lib/active_record/middleware/shard_selector.rb +60 -0
- data/lib/active_record/migration/command_recorder.rb +4 -4
- data/lib/active_record/migration/compatibility.rb +83 -1
- data/lib/active_record/migration/join_table.rb +1 -1
- data/lib/active_record/migration.rb +109 -79
- data/lib/active_record/model_schema.rb +45 -58
- data/lib/active_record/nested_attributes.rb +13 -12
- data/lib/active_record/no_touching.rb +3 -3
- data/lib/active_record/null_relation.rb +2 -6
- data/lib/active_record/persistence.rb +219 -52
- data/lib/active_record/query_cache.rb +2 -2
- data/lib/active_record/query_logs.rb +138 -0
- data/lib/active_record/querying.rb +15 -5
- data/lib/active_record/railtie.rb +127 -17
- data/lib/active_record/railties/controller_runtime.rb +1 -1
- data/lib/active_record/railties/databases.rake +66 -129
- data/lib/active_record/readonly_attributes.rb +11 -0
- data/lib/active_record/reflection.rb +67 -50
- data/lib/active_record/relation/batches/batch_enumerator.rb +19 -5
- data/lib/active_record/relation/batches.rb +3 -3
- data/lib/active_record/relation/calculations.rb +40 -36
- data/lib/active_record/relation/delegation.rb +6 -6
- data/lib/active_record/relation/finder_methods.rb +31 -35
- data/lib/active_record/relation/merger.rb +20 -13
- data/lib/active_record/relation/predicate_builder.rb +1 -6
- data/lib/active_record/relation/query_attribute.rb +5 -11
- data/lib/active_record/relation/query_methods.rb +235 -61
- data/lib/active_record/relation/record_fetch_warning.rb +7 -9
- data/lib/active_record/relation/spawn_methods.rb +2 -2
- data/lib/active_record/relation/where_clause.rb +10 -19
- data/lib/active_record/relation.rb +171 -84
- data/lib/active_record/result.rb +17 -7
- data/lib/active_record/runtime_registry.rb +9 -13
- data/lib/active_record/sanitization.rb +11 -7
- data/lib/active_record/schema_dumper.rb +10 -3
- data/lib/active_record/schema_migration.rb +0 -4
- data/lib/active_record/scoping/default.rb +61 -12
- data/lib/active_record/scoping/named.rb +3 -11
- data/lib/active_record/scoping.rb +64 -34
- data/lib/active_record/serialization.rb +1 -1
- data/lib/active_record/signed_id.rb +1 -1
- data/lib/active_record/suppressor.rb +11 -15
- data/lib/active_record/tasks/database_tasks.rb +116 -58
- data/lib/active_record/tasks/mysql_database_tasks.rb +1 -1
- data/lib/active_record/tasks/postgresql_database_tasks.rb +19 -12
- data/lib/active_record/test_databases.rb +1 -1
- data/lib/active_record/test_fixtures.rb +4 -4
- data/lib/active_record/timestamp.rb +3 -4
- data/lib/active_record/transactions.rb +9 -14
- data/lib/active_record/translation.rb +2 -2
- data/lib/active_record/type/adapter_specific_registry.rb +32 -7
- data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
- data/lib/active_record/type/internal/timezone.rb +2 -2
- data/lib/active_record/type/serialized.rb +1 -1
- data/lib/active_record/type/type_map.rb +17 -20
- data/lib/active_record/type.rb +1 -2
- data/lib/active_record/validations/associated.rb +1 -1
- data/lib/active_record/validations/uniqueness.rb +1 -1
- data/lib/active_record.rb +204 -28
- data/lib/arel/attributes/attribute.rb +0 -8
- data/lib/arel/crud.rb +28 -22
- data/lib/arel/delete_manager.rb +18 -4
- data/lib/arel/filter_predications.rb +9 -0
- data/lib/arel/insert_manager.rb +2 -3
- data/lib/arel/nodes/casted.rb +1 -1
- data/lib/arel/nodes/delete_statement.rb +12 -13
- data/lib/arel/nodes/filter.rb +10 -0
- data/lib/arel/nodes/function.rb +1 -0
- data/lib/arel/nodes/insert_statement.rb +2 -2
- data/lib/arel/nodes/select_core.rb +2 -2
- data/lib/arel/nodes/select_statement.rb +2 -2
- data/lib/arel/nodes/update_statement.rb +8 -3
- data/lib/arel/nodes.rb +1 -0
- data/lib/arel/predications.rb +11 -3
- data/lib/arel/select_manager.rb +10 -4
- data/lib/arel/table.rb +0 -1
- data/lib/arel/tree_manager.rb +0 -12
- data/lib/arel/update_manager.rb +18 -4
- data/lib/arel/visitors/dot.rb +80 -90
- data/lib/arel/visitors/mysql.rb +8 -2
- data/lib/arel/visitors/postgresql.rb +0 -10
- data/lib/arel/visitors/to_sql.rb +58 -2
- data/lib/arel.rb +2 -1
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
- metadata +55 -12
data/CHANGELOG.md
CHANGED
@@ -1,1596 +1,1665 @@
|
|
1
|
-
## Rails
|
1
|
+
## Rails 7.0.0.rc2 (December 14, 2021) ##
|
2
2
|
|
3
|
-
*
|
4
|
-
|
5
|
-
|
6
|
-
## Rails 6.1.4 (June 24, 2021) ##
|
7
|
-
|
8
|
-
* Do not try to rollback transactions that failed due to a `ActiveRecord::TransactionRollbackError`.
|
9
|
-
|
10
|
-
*Jamie McCarthy*
|
11
|
-
|
12
|
-
* Raise an error if `pool_config` is `nil` in `set_pool_config`.
|
13
|
-
|
14
|
-
*Eileen M. Uchitelle*
|
15
|
-
|
16
|
-
* Fix compatibility with `psych >= 4`.
|
17
|
-
|
18
|
-
Starting in Psych 4.0.0 `YAML.load` behaves like `YAML.safe_load`. To preserve compatibility
|
19
|
-
Active Record's schema cache loader and `YAMLColumn` now uses `YAML.unsafe_load` if available.
|
20
|
-
|
21
|
-
*Jean Boussier*
|
22
|
-
|
23
|
-
* Support using replicas when using `rails dbconsole`.
|
24
|
-
|
25
|
-
*Christopher Thornton*
|
26
|
-
|
27
|
-
* Restore connection pools after transactional tests.
|
28
|
-
|
29
|
-
*Eugene Kenny*
|
30
|
-
|
31
|
-
* Change `upsert_all` to fails cleanly for MySQL when `:unique_by` is used.
|
3
|
+
* Remove deprecated `ActiveRecord::DatabaseConfigurations::DatabaseConfig#spec_name`.
|
32
4
|
|
33
|
-
*
|
34
|
-
|
35
|
-
* Fix user-defined `self.default_scope` to respect table alias.
|
5
|
+
*Rafael Mendonça França*
|
36
6
|
|
37
|
-
|
7
|
+
* Remove deprecated `ActiveRecord::Connection#in_clause_length`.
|
38
8
|
|
39
|
-
*
|
9
|
+
*Rafael Mendonça França*
|
40
10
|
|
41
|
-
|
11
|
+
* Remove deprecated `ActiveRecord::Connection#allowed_index_name_length`.
|
42
12
|
|
43
|
-
*
|
44
|
-
`quoted_node` so that PostgreSQL arrays are quoted properly.
|
13
|
+
*Rafael Mendonça França*
|
45
14
|
|
46
|
-
|
15
|
+
* Remove deprecated `ActiveRecord::Base#remove_connection`.
|
47
16
|
|
48
|
-
*
|
17
|
+
*Rafael Mendonça França*
|
49
18
|
|
50
|
-
|
19
|
+
* Load STI Models in fixtures
|
51
20
|
|
52
|
-
|
21
|
+
Data from Fixtures now loads based on the specific class for models with
|
22
|
+
Single Table Inheritance. This affects enums defined in subclasses, previously
|
23
|
+
the value of these fields was not parsed and remained `nil`
|
53
24
|
|
54
|
-
*
|
25
|
+
*Andres Howard*
|
55
26
|
|
56
|
-
*
|
27
|
+
* `#authenticate` returns false when the password is blank instead of raising an error.
|
57
28
|
|
58
|
-
*
|
29
|
+
*Muhammad Muhammad Ibrahim*
|
59
30
|
|
60
|
-
*
|
31
|
+
* Fix `ActiveRecord::QueryMethods#in_order_of` behavior for integer enums.
|
61
32
|
|
62
|
-
|
63
|
-
relation set `strict_loading` to false the false wasn't considered when
|
64
|
-
deciding whether to raise/warn about strict loading.
|
33
|
+
`ActiveRecord::QueryMethods#in_order_of` didn't work as expected for enums stored as integers in the database when passing an array of strings or symbols as the order argument. This unexpected behavior occurred because the string or symbol values were not casted to match the integers in the database.
|
65
34
|
|
66
|
-
|
67
|
-
class Dog < ActiveRecord::Base
|
68
|
-
self.strict_loading_by_default = true
|
35
|
+
The following example now works as expected:
|
69
36
|
|
70
|
-
|
37
|
+
```ruby
|
38
|
+
class Book < ApplicationRecord
|
39
|
+
enum status: [:proposed, :written, :published]
|
71
40
|
end
|
72
|
-
```
|
73
|
-
|
74
|
-
In the example, `dog.treats` would still raise even though
|
75
|
-
`strict_loading` was set to false. This is a bug effecting more than
|
76
|
-
Active Storage which is why I made this PR superceeding #41461. We need
|
77
|
-
to fix this for all applications since the behavior is a little
|
78
|
-
surprising. I took the test from ##41461 and the code suggestion from #41453
|
79
|
-
with some additions.
|
80
|
-
|
81
|
-
*Eileen M. Uchitelle*, *Radamés Roriz*
|
82
|
-
|
83
|
-
* Fix numericality validator without precision.
|
84
|
-
|
85
|
-
*Ryuta Kamizono*
|
86
41
|
|
87
|
-
|
88
|
-
|
89
|
-
*Ryuta Kamizono*
|
90
|
-
|
91
|
-
* Fix `CREATE INDEX` statement generation for PostgreSQL.
|
42
|
+
Book.in_order_of(:status, %w[written published proposed])
|
43
|
+
```
|
92
44
|
|
93
|
-
*
|
45
|
+
*Alexandre Ruban*
|
94
46
|
|
95
|
-
*
|
47
|
+
* Ignore persisted in-memory records when merging target lists.
|
96
48
|
|
97
|
-
*
|
49
|
+
*Kevin Sjöberg*
|
98
50
|
|
99
|
-
*
|
51
|
+
* Add a new option `:update_only` to `upsert_all` to configure the list of columns to update in case of conflict.
|
100
52
|
|
101
|
-
|
53
|
+
Before, you could only customize the update SQL sentence via `:on_duplicate`. There is now a new option `:update_only` that lets you provide a list of columns to update in case of conflict:
|
102
54
|
|
55
|
+
```ruby
|
56
|
+
Commodity.upsert_all(
|
57
|
+
[
|
58
|
+
{ id: 2, name: "Copper", price: 4.84 },
|
59
|
+
{ id: 4, name: "Gold", price: 1380.87 },
|
60
|
+
{ id: 6, name: "Aluminium", price: 0.35 }
|
61
|
+
],
|
62
|
+
update_only: [:price] # Only prices will be updated
|
63
|
+
)
|
64
|
+
```
|
103
65
|
|
104
|
-
|
66
|
+
*Jorge Manrubia*
|
105
67
|
|
106
|
-
*
|
68
|
+
* Remove deprecated `ActiveRecord::Result#map!` and `ActiveRecord::Result#collect!`.
|
107
69
|
|
70
|
+
*Rafael Mendonça França*
|
108
71
|
|
109
|
-
|
72
|
+
* Remove deprecated `ActiveRecord::Base.configurations.to_h`.
|
110
73
|
|
111
|
-
*
|
74
|
+
*Rafael Mendonça França*
|
112
75
|
|
76
|
+
* Remove deprecated `ActiveRecord::Base.configurations.default_hash`.
|
113
77
|
|
114
|
-
|
78
|
+
*Rafael Mendonça França*
|
115
79
|
|
116
|
-
*
|
117
|
-
to the connection session.
|
80
|
+
* Remove deprecated `ActiveRecord::Base.arel_attribute`.
|
118
81
|
|
119
82
|
*Rafael Mendonça França*
|
120
83
|
|
121
|
-
*
|
122
|
-
are enabled.
|
84
|
+
* Remove deprecated `ActiveRecord::Base.connection_config`.
|
123
85
|
|
124
86
|
*Rafael Mendonça França*
|
125
87
|
|
126
|
-
*
|
127
|
-
an `IN` clause.
|
128
|
-
|
129
|
-
*Ryuta Kamizono*
|
88
|
+
* Filter attributes in SQL logs
|
130
89
|
|
131
|
-
|
132
|
-
with a having clause
|
90
|
+
Previously, SQL queries in logs containing `ActiveRecord::Base.filter_attributes` were not filtered.
|
133
91
|
|
134
|
-
|
135
|
-
references an aliased select value would generate an error when
|
136
|
-
#include? was called, due to an optimisation that would generate
|
137
|
-
call #exists? on the relation instead, which effectively alters
|
138
|
-
the select values of the query (and thus removes the aliased select
|
139
|
-
values), but leaves the having clause intact. Because the having
|
140
|
-
clause is then referencing an aliased column that is no longer
|
141
|
-
present in the simplified query, an ActiveRecord::InvalidStatement
|
142
|
-
error was raised.
|
92
|
+
Now, the filter attributes will be masked `[FILTERED]` in the logs when `prepared_statement` is enabled.
|
143
93
|
|
144
|
-
|
94
|
+
```
|
95
|
+
# Before:
|
96
|
+
Foo Load (0.2ms) SELECT "foos".* FROM "foos" WHERE "foos"."passw" = ? LIMIT ? [["passw", "hello"], ["LIMIT", 1]]
|
145
97
|
|
146
|
-
|
147
|
-
|
148
|
-
.joins(:posts)
|
149
|
-
.group(:id)
|
150
|
-
.having('total_posts > 2')
|
151
|
-
.include?(Author.first)
|
98
|
+
# After:
|
99
|
+
Foo Load (0.5ms) SELECT "foos".* FROM "foos" WHERE "foos"."passw" = ? LIMIT ? [["passw", "[FILTERED]"], ["LIMIT", 1]]
|
152
100
|
```
|
153
101
|
|
154
|
-
|
155
|
-
simplified #exists? query, which simply checks for the presence of
|
156
|
-
a having clause.
|
102
|
+
*Aishwarya Subramanian*
|
157
103
|
|
158
|
-
|
104
|
+
* Remove deprecated `Tasks::DatabaseTasks.spec`.
|
159
105
|
|
160
|
-
*
|
106
|
+
*Rafael Mendonça França*
|
161
107
|
|
162
|
-
*
|
163
|
-
without Rails knowledge (e.g., if app gets kill -9d during long-running query or due to Rack::Timeout), app won't end
|
164
|
-
up in perpetual crash state for being inconsistent with Postgres.
|
108
|
+
* Remove deprecated `Tasks::DatabaseTasks.current_config`.
|
165
109
|
|
166
|
-
*
|
110
|
+
*Rafael Mendonça França*
|
167
111
|
|
112
|
+
* Deprecate `Tasks::DatabaseTasks.schema_file_type`.
|
168
113
|
|
169
|
-
|
114
|
+
*Rafael Mendonça França*
|
170
115
|
|
171
|
-
*
|
116
|
+
* Remove deprecated `Tasks::DatabaseTasks.dump_filename`.
|
172
117
|
|
173
|
-
|
174
|
-
for validating the money format in the PostgreSQL adapter. This patch
|
175
|
-
fixes the regexp.
|
118
|
+
*Rafael Mendonça França*
|
176
119
|
|
177
|
-
|
120
|
+
* Remove deprecated `Tasks::DatabaseTasks.schema_file`.
|
178
121
|
|
179
|
-
|
122
|
+
*Rafael Mendonça França*
|
180
123
|
|
181
|
-
|
124
|
+
* Remove deprecated `environment` and `name` arguments from `Tasks::DatabaseTasks.schema_up_to_date?`.
|
182
125
|
|
126
|
+
*Rafael Mendonça França*
|
183
127
|
|
184
|
-
|
128
|
+
* Merging conditions on the same column no longer maintain both conditions,
|
129
|
+
and will be consistently replaced by the latter condition.
|
185
130
|
|
186
|
-
|
131
|
+
```ruby
|
132
|
+
# Rails 6.1 (IN clause is replaced by merger side equality condition)
|
133
|
+
Author.where(id: [david.id, mary.id]).merge(Author.where(id: bob)) # => [bob]
|
134
|
+
# Rails 6.1 (both conflict conditions exists, deprecated)
|
135
|
+
Author.where(id: david.id..mary.id).merge(Author.where(id: bob)) # => []
|
136
|
+
# Rails 6.1 with rewhere to migrate to Rails 7.0's behavior
|
137
|
+
Author.where(id: david.id..mary.id).merge(Author.where(id: bob), rewhere: true) # => [bob]
|
138
|
+
# Rails 7.0 (same behavior with IN clause, mergee side condition is consistently replaced)
|
139
|
+
Author.where(id: [david.id, mary.id]).merge(Author.where(id: bob)) # => [bob]
|
140
|
+
Author.where(id: david.id..mary.id).merge(Author.where(id: bob)) # => [bob]
|
187
141
|
|
188
|
-
*
|
142
|
+
*Rafael Mendonça França*
|
189
143
|
|
190
|
-
*
|
144
|
+
* Remove deprecated support to `Model.reorder(nil).first` to search using non-deterministic order.
|
191
145
|
|
192
|
-
|
193
|
-
case the job would enqueue even though the database deletion
|
194
|
-
rolledback putting things in a funky state.
|
146
|
+
*Rafael Mendonça França*
|
195
147
|
|
196
|
-
|
148
|
+
* Remove deprecated rake tasks:
|
197
149
|
|
198
|
-
*
|
150
|
+
* `db:schema:load_if_ruby`
|
151
|
+
* `db:structure:dump`
|
152
|
+
* `db:structure:load`
|
153
|
+
* `db:structure:load_if_sql`
|
154
|
+
* `db:structure:dump:#{name}`
|
155
|
+
* `db:structure:load:#{name}`
|
156
|
+
* `db:test:load_structure`
|
157
|
+
* `db:test:load_structure:#{name}`
|
199
158
|
|
200
|
-
*
|
159
|
+
*Rafael Mendonça França*
|
201
160
|
|
202
|
-
|
161
|
+
* Remove deprecated `DatabaseConfig#config` method.
|
203
162
|
|
204
|
-
*
|
205
|
-
adapter if the "url" protocol is "jdbc", "http", or "https". Previously only
|
206
|
-
urls with the "jdbc" prefix were passed to the Active Record Adapter, others
|
207
|
-
are assumed to be adapter specification urls.
|
163
|
+
*Rafael Mendonça França*
|
208
164
|
|
209
|
-
|
165
|
+
* Rollback transactions when the block returns earlier than expected.
|
210
166
|
|
211
|
-
|
167
|
+
Before this change, when a transaction block returned early, the transaction would be committed.
|
212
168
|
|
213
|
-
|
169
|
+
The problem is that timeouts triggered inside the transaction block was also making the incomplete transaction
|
170
|
+
to be committed, so in order to avoid this mistake, the transaction block is rolled back.
|
214
171
|
|
215
|
-
*
|
172
|
+
*Rafael Mendonça França*
|
216
173
|
|
217
|
-
*
|
174
|
+
* Add middleware for automatic shard swapping.
|
218
175
|
|
219
|
-
|
176
|
+
Provides a basic middleware to perform automatic shard swapping. Applications will provide a resolver which will determine for an individual request which shard should be used. Example:
|
220
177
|
|
221
|
-
|
178
|
+
```ruby
|
179
|
+
config.active_record.shard_resolver = ->(request) {
|
180
|
+
subdomain = request.subdomain
|
181
|
+
tenant = Tenant.find_by_subdomain!(subdomain)
|
182
|
+
tenant.shard
|
183
|
+
}
|
184
|
+
```
|
222
185
|
|
223
|
-
|
186
|
+
See guides for more details.
|
224
187
|
|
225
|
-
*
|
188
|
+
*Eileen M. Uchitelle*, *John Crepezzi*
|
226
189
|
|
227
|
-
|
190
|
+
* Remove deprecated support to pass a column to `type_cast`.
|
228
191
|
|
229
|
-
*
|
192
|
+
*Rafael Mendonça França*
|
230
193
|
|
231
|
-
|
194
|
+
* Remove deprecated support to type cast to database values `ActiveRecord::Base` objects.
|
232
195
|
|
196
|
+
*Rafael Mendonça França*
|
233
197
|
|
234
|
-
|
198
|
+
* Remove deprecated support to quote `ActiveRecord::Base` objects.
|
235
199
|
|
236
|
-
*
|
200
|
+
*Rafael Mendonça França*
|
237
201
|
|
238
|
-
|
202
|
+
* Remove deprecacated support to resolve connection using `"primary"` as connection specification name.
|
239
203
|
|
240
|
-
*
|
204
|
+
*Rafael Mendonça França*
|
241
205
|
|
242
|
-
|
206
|
+
* Remove deprecation warning when using `:interval` column is used in PostgreSQL database.
|
243
207
|
|
244
|
-
|
208
|
+
Now, interval columns will return `ActiveSupport::Duration` objects instead of strings.
|
245
209
|
|
246
|
-
|
210
|
+
To keep the old behavior, you can add this line to your model:
|
247
211
|
|
248
|
-
|
212
|
+
```ruby
|
213
|
+
attribute :column, :string
|
214
|
+
```
|
249
215
|
|
250
|
-
*
|
216
|
+
*Rafael Mendonça França*
|
251
217
|
|
252
|
-
*
|
218
|
+
* Remove deprecated support to YAML load `ActiveRecord::Base` instance in the Rails 4.2 and 4.1 formats.
|
253
219
|
|
254
|
-
*
|
220
|
+
*Rafael Mendonça França*
|
255
221
|
|
256
|
-
*
|
222
|
+
* Remove deprecated option `:spec_name` in the `configs_for` method.
|
257
223
|
|
258
|
-
*
|
224
|
+
*Rafael Mendonça França*
|
259
225
|
|
260
|
-
*
|
226
|
+
* Remove deprecated `ActiveRecord::Base.allow_unsafe_raw_sql`.
|
261
227
|
|
262
228
|
*Rafael Mendonça França*
|
263
229
|
|
264
|
-
* Fix
|
230
|
+
* Fix regression bug that caused ignoring additional conditions for preloading has_many-through relations.
|
265
231
|
|
266
|
-
|
232
|
+
Fixes #43132
|
267
233
|
|
268
|
-
*
|
234
|
+
*Alexander Pauly*
|
269
235
|
|
270
|
-
|
236
|
+
* Fix `has_many` inversing recursion on models with recursive associations.
|
271
237
|
|
272
|
-
*
|
273
|
-
|
274
|
-
Previously, passing `false` would trigger the option validation logic
|
275
|
-
to throw an error saying :polymorphic would not be a valid option.
|
238
|
+
*Gannon McGibbon*
|
276
239
|
|
277
|
-
|
240
|
+
* Add `accepts_nested_attributes_for` support for `delegated_type`
|
278
241
|
|
279
|
-
|
242
|
+
```ruby
|
243
|
+
class Entry < ApplicationRecord
|
244
|
+
delegated_type :entryable, types: %w[ Message Comment ]
|
245
|
+
accepts_nested_attributes_for :entryable
|
246
|
+
end
|
280
247
|
|
281
|
-
|
248
|
+
entry = Entry.create(entryable_type: 'Message', entryable_attributes: { content: 'Hello world' })
|
249
|
+
# => #<Entry:0x00>
|
250
|
+
# id: 1
|
251
|
+
# entryable_id: 1,
|
252
|
+
# entryable_type: 'Message'
|
253
|
+
# ...>
|
254
|
+
|
255
|
+
entry.entryable
|
256
|
+
# => #<Message:0x01>
|
257
|
+
# id: 1
|
258
|
+
# content: 'Hello world'
|
259
|
+
# ...>
|
260
|
+
```
|
282
261
|
|
283
|
-
Previously
|
284
|
-
and the index name should be specified explicitly. Now, the index name is inferred
|
285
|
-
automatically.
|
262
|
+
Previously it would raise an error:
|
286
263
|
|
287
264
|
```ruby
|
288
|
-
|
265
|
+
Entry.create(entryable_type: 'Message', entryable_attributes: { content: 'Hello world' })
|
266
|
+
# ArgumentError: Cannot build association `entryable'. Are you trying to build a polymorphic one-to-one association?
|
289
267
|
```
|
290
268
|
|
291
|
-
*
|
292
|
-
|
293
|
-
|
294
|
-
## Rails 6.1.0 (December 09, 2020) ##
|
295
|
-
|
296
|
-
* Only warn about negative enums if a positive form that would cause conflicts exists.
|
297
|
-
|
298
|
-
Fixes #39065.
|
299
|
-
|
300
|
-
*Alex Ghiculescu*
|
301
|
-
|
302
|
-
* Change `attribute_for_inspect` to take `filter_attributes` in consideration.
|
269
|
+
*Sjors Baltus*
|
303
270
|
|
304
|
-
|
271
|
+
* Use subquery for DELETE with GROUP_BY and HAVING clauses.
|
305
272
|
|
306
|
-
|
273
|
+
Prior to this change, deletes with GROUP_BY and HAVING were returning an error.
|
307
274
|
|
308
|
-
|
275
|
+
After this change, GROUP_BY and HAVING are valid clauses in DELETE queries, generating the following query:
|
309
276
|
|
310
|
-
|
277
|
+
```sql
|
278
|
+
DELETE FROM "posts" WHERE "posts"."id" IN (
|
279
|
+
SELECT "posts"."id" FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" GROUP BY "posts"."id" HAVING (count(comments.id) >= 2))
|
280
|
+
) [["flagged", "t"]]
|
281
|
+
```
|
311
282
|
|
312
|
-
*
|
283
|
+
*Ignacio Chiazzo Cardarello*
|
313
284
|
|
314
|
-
|
315
|
-
class AdminAuthor
|
316
|
-
delegate_missing_to :@author
|
285
|
+
* Use subquery for UPDATE with GROUP_BY and HAVING clauses.
|
317
286
|
|
318
|
-
|
319
|
-
@author = author
|
320
|
-
end
|
321
|
-
end
|
287
|
+
Prior to this change, updates with GROUP_BY and HAVING were being ignored, generating a SQL like this:
|
322
288
|
|
323
|
-
|
289
|
+
```sql
|
290
|
+
UPDATE "posts" SET "flagged" = ? WHERE "posts"."id" IN (
|
291
|
+
SELECT "posts"."id" FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
|
292
|
+
) [["flagged", "t"]]
|
324
293
|
```
|
325
294
|
|
326
|
-
|
295
|
+
After this change, GROUP_BY and HAVING clauses are used as a subquery in updates, like this:
|
327
296
|
|
328
|
-
|
297
|
+
```sql
|
298
|
+
UPDATE "posts" SET "flagged" = ? WHERE "posts"."id" IN (
|
299
|
+
SELECT "posts"."id" FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
|
300
|
+
GROUP BY posts.id HAVING (count(comments.id) >= 2)
|
301
|
+
) [["flagged", "t"]]
|
302
|
+
```
|
329
303
|
|
330
|
-
|
304
|
+
*Ignacio Chiazzo Cardarello*
|
331
305
|
|
332
|
-
|
306
|
+
* Add support for setting the filename of the schema or structure dump in the database config.
|
333
307
|
|
334
|
-
|
335
|
-
MealsRecord.connected_to(role: :reading) do
|
336
|
-
Dog.first # read from animals replica
|
337
|
-
Dinner.first # read from meals replica
|
338
|
-
Person.first # read from primary writer
|
339
|
-
end
|
340
|
-
end
|
308
|
+
Applications may now set their the filename or path of the schema / structure dump file in their database configuration.
|
341
309
|
|
342
|
-
After:
|
343
310
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
311
|
+
```yaml
|
312
|
+
production:
|
313
|
+
primary:
|
314
|
+
database: my_db
|
315
|
+
schema_dump: my_schema_dump_filename.rb
|
316
|
+
animals:
|
317
|
+
database: animals_db
|
318
|
+
schema_dump: false
|
319
|
+
```
|
349
320
|
|
350
|
-
|
321
|
+
The filename set in `schema_dump` will be used by the application. If set to `false` the schema will not be dumped. The database tasks are responsible for adding the database directory to the filename. If a full path is provided, the Rails tasks will use that instead of `ActiveRecord::DatabaseTasks.db_dir`.
|
351
322
|
|
352
|
-
*
|
323
|
+
*Eileen M. Uchitelle*, *Ryan Kerr*
|
353
324
|
|
354
|
-
|
325
|
+
* Add `ActiveRecord::Base.prohibit_shard_swapping` to prevent attempts to change the shard within a block.
|
355
326
|
|
356
|
-
|
327
|
+
*John Crepezzi*, *Eileen M. Uchitelle*
|
357
328
|
|
358
|
-
|
329
|
+
* Filter unchanged attributes with default function from insert query when `partial_inserts` is disabled.
|
359
330
|
|
360
|
-
*
|
331
|
+
*Akshay Birajdar*, *Jacopo Beschi*
|
361
332
|
|
362
|
-
|
333
|
+
* Add support for FILTER clause (SQL:2003) to Arel.
|
363
334
|
|
364
|
-
|
335
|
+
Currently supported by PostgreSQL 9.4+ and SQLite 3.30+.
|
365
336
|
|
366
|
-
*
|
337
|
+
*Andrey Novikov*
|
367
338
|
|
368
|
-
|
339
|
+
* Automatically set timestamps on record creation during bulk insert/upsert
|
369
340
|
|
370
|
-
|
341
|
+
Prior to this change, only updates during an upsert operation (e.g. `upsert_all`) would touch timestamps (`updated_{at,on}`). Now, record creations also touch timestamp columns (`{created,updated}_{at,on}`).
|
371
342
|
|
372
|
-
|
343
|
+
This behaviour is controlled by the `<model>.record_timestamps` config, matching the behaviour of `create`, `update`, etc. It can also be overridden by using the `record_timestamps:` keyword argument.
|
373
344
|
|
374
|
-
|
345
|
+
Note that this means `upsert_all` on models with `record_timestamps = false` will no longer touch `updated_{at,on}` automatically.
|
375
346
|
|
376
|
-
*
|
377
|
-
no longer enforce case sensitive comparison by default.
|
347
|
+
*Sam Bostock*
|
378
348
|
|
379
|
-
|
349
|
+
* Don't require `role` when passing `shard` to `connected_to`.
|
380
350
|
|
381
|
-
|
351
|
+
`connected_to` can now be called with a `shard` only. Note that `role` is still inherited if `connected_to` calls are nested.
|
382
352
|
|
383
|
-
|
384
|
-
`table_name_length`
|
385
|
-
`columns_per_table`
|
386
|
-
`indexes_per_table`
|
387
|
-
`columns_per_multicolumn_index`
|
388
|
-
`sql_query_length`
|
389
|
-
`joins_per_query`
|
353
|
+
*Eileen M. Uchitelle*
|
390
354
|
|
391
|
-
|
355
|
+
* Add option to lazily load the schema cache on the connection.
|
392
356
|
|
393
|
-
|
357
|
+
Previously, the only way to load the schema cache in Active Record was through the Railtie on boot. This option provides the ability to load the schema cache on the connection after it's been established. Loading the cache lazily on the connection can be beneficial for Rails applications that use multiple databases because it will load the cache at the time the connection is established. Currently Railties doesn't have access to the connections before boot.
|
394
358
|
|
395
|
-
|
359
|
+
To use the cache, set `config.active_record.lazily_load_schema_cache = true` in your application configuration. In addition a `schema_cache_path` should be set in your database configuration if you don't want to use the default "db/schema_cache.yml" path.
|
396
360
|
|
397
|
-
*
|
361
|
+
*Eileen M. Uchitelle*
|
398
362
|
|
399
|
-
|
363
|
+
* Allow automatic `inverse_of` detection for associations with scopes.
|
400
364
|
|
401
|
-
|
365
|
+
Automatic `inverse_of` detection now works for associations with scopes. For
|
366
|
+
example, the `comments` association here now automatically detects
|
367
|
+
`inverse_of: :post`, so we don't need to pass that option:
|
402
368
|
|
403
|
-
|
369
|
+
```ruby
|
370
|
+
class Post < ActiveRecord::Base
|
371
|
+
has_many :comments, -> { visible }
|
372
|
+
end
|
404
373
|
|
405
|
-
|
374
|
+
class Comment < ActiveRecord::Base
|
375
|
+
belongs_to :post
|
376
|
+
end
|
377
|
+
```
|
406
378
|
|
407
|
-
|
379
|
+
Note that the automatic detection still won't work if the inverse
|
380
|
+
association has a scope. In this example a scope on the `post` association
|
381
|
+
would still prevent Rails from finding the inverse for the `comments`
|
382
|
+
association.
|
408
383
|
|
409
|
-
|
384
|
+
This will be the default for new apps in Rails 7. To opt in:
|
410
385
|
|
411
|
-
|
386
|
+
```ruby
|
387
|
+
config.active_record.automatic_scope_inversing = true
|
388
|
+
```
|
412
389
|
|
413
|
-
*
|
390
|
+
*Daniel Colson*, *Chris Bloom*
|
414
391
|
|
415
|
-
|
392
|
+
* Accept optional transaction args to `ActiveRecord::Locking::Pessimistic#with_lock`
|
416
393
|
|
417
|
-
|
418
|
-
|
394
|
+
`#with_lock` now accepts transaction options like `requires_new:`,
|
395
|
+
`isolation:`, and `joinable:`
|
419
396
|
|
420
|
-
|
397
|
+
* Adds support for deferrable foreign key constraints in PostgreSQL.
|
421
398
|
|
422
|
-
|
423
|
-
|
424
|
-
|
399
|
+
By default, foreign key constraints in PostgreSQL are checked after each statement. This works for most use cases,
|
400
|
+
but becomes a major limitation when creating related records before the parent record is inserted into the database.
|
401
|
+
One example of this is looking up / creating a person via one or more unique alias.
|
425
402
|
|
426
|
-
|
403
|
+
```ruby
|
404
|
+
Person.transaction do
|
405
|
+
alias = Alias
|
406
|
+
.create_with(user_id: SecureRandom.uuid)
|
407
|
+
.create_or_find_by(name: "DHH")
|
408
|
+
|
409
|
+
person = Person
|
410
|
+
.create_with(name: "David Heinemeier Hansson")
|
411
|
+
.create_or_find_by(id: alias.user_id)
|
412
|
+
end
|
413
|
+
```
|
427
414
|
|
428
|
-
|
429
|
-
User.find_by(name: "David") # => #<User name: "David", ...>
|
430
|
-
end
|
415
|
+
Using the default behavior, the transaction would fail when executing the first `INSERT` statement.
|
431
416
|
|
432
|
-
|
417
|
+
By passing the `:deferrable` option to the `add_foreign_key` statement in migrations, it's possible to defer this
|
418
|
+
check.
|
433
419
|
|
434
|
-
|
420
|
+
```ruby
|
421
|
+
add_foreign_key :aliases, :person, deferrable: true
|
422
|
+
```
|
435
423
|
|
436
|
-
|
437
|
-
|
438
|
-
|
424
|
+
Passing `deferrable: true` doesn't change the default behavior, but allows manually deferring the check using
|
425
|
+
`SET CONSTRAINTS ALL DEFERRED` within a transaction. This will cause the foreign keys to be checked after the
|
426
|
+
transaction.
|
439
427
|
|
440
|
-
|
428
|
+
It's also possible to adjust the default behavior from an immediate check (after the statement), to a deferred check
|
429
|
+
(after the transaction):
|
441
430
|
|
442
|
-
|
443
|
-
|
431
|
+
```ruby
|
432
|
+
add_foreign_key :aliases, :person, deferrable: :deferred
|
433
|
+
```
|
444
434
|
|
445
|
-
|
435
|
+
*Benedikt Deicke*
|
446
436
|
|
447
|
-
|
448
|
-
# SELECT * FROM users WHERE name = 'David'
|
437
|
+
* Allow configuring Postgres password through the socket URL.
|
449
438
|
|
450
|
-
|
439
|
+
For example:
|
440
|
+
```ruby
|
441
|
+
ActiveRecord::DatabaseConfigurations::UrlConfig.new(
|
442
|
+
:production, :production, 'postgres:///?user=user&password=secret&dbname=app', {}
|
443
|
+
).configuration_hash
|
444
|
+
```
|
451
445
|
|
452
|
-
|
446
|
+
will now return,
|
453
447
|
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
`values`
|
458
|
-
`[]=`
|
448
|
+
```ruby
|
449
|
+
{ :user=>"user", :password=>"secret", :dbname=>"app", :adapter=>"postgresql" }
|
450
|
+
```
|
459
451
|
|
460
|
-
*
|
452
|
+
*Abeid Ahmed*
|
461
453
|
|
462
|
-
*
|
454
|
+
* PostgreSQL: support custom enum types
|
463
455
|
|
464
|
-
|
456
|
+
In migrations, use `create_enum` to add a new enum type, and `t.enum` to add a column.
|
465
457
|
|
466
|
-
|
467
|
-
|
458
|
+
```ruby
|
459
|
+
def up
|
460
|
+
create_enum :mood, ["happy", "sad"]
|
468
461
|
|
469
|
-
|
462
|
+
change_table :cats do |t|
|
463
|
+
t.enum :current_mood, enum_type: "mood", default: "happy", null: false
|
464
|
+
end
|
465
|
+
end
|
466
|
+
```
|
470
467
|
|
471
|
-
|
472
|
-
|
468
|
+
Enums will be presented correctly in `schema.rb`. Note that this is only supported by
|
469
|
+
the PostgreSQL adapter.
|
473
470
|
|
474
|
-
*
|
471
|
+
*Alex Ghiculescu*
|
475
472
|
|
476
|
-
*
|
473
|
+
* Avoid COMMENT statements in PostgreSQL structure dumps
|
477
474
|
|
478
|
-
|
475
|
+
COMMENT statements are now omitted from the output of `db:structure:dump` when using PostgreSQL >= 11.
|
476
|
+
This allows loading the dump without a pgsql superuser account.
|
479
477
|
|
480
|
-
|
478
|
+
Fixes #36816, #43107.
|
481
479
|
|
482
|
-
*
|
480
|
+
*Janosch Müller*
|
483
481
|
|
484
|
-
*
|
482
|
+
* Add support for generated columns in PostgreSQL adapter
|
485
483
|
|
486
|
-
|
484
|
+
Generated columns are supported since version 12.0 of PostgreSQL. This adds
|
485
|
+
support of those to the PostgreSQL adapter.
|
487
486
|
|
488
|
-
|
489
|
-
|
487
|
+
```ruby
|
488
|
+
create_table :users do |t|
|
489
|
+
t.string :name
|
490
|
+
t.virtual :name_upcased, type: :string, as: 'upper(name)', stored: true
|
491
|
+
end
|
492
|
+
```
|
490
493
|
|
491
|
-
*
|
494
|
+
*Michał Begejowicz*
|
492
495
|
|
493
|
-
* Connections can be granularly switched for abstract classes when `connected_to` is called.
|
494
496
|
|
495
|
-
|
497
|
+
## Rails 7.0.0.alpha2 (September 15, 2021) ##
|
496
498
|
|
497
|
-
|
499
|
+
* No changes.
|
498
500
|
|
499
|
-
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`.
|
500
501
|
|
501
|
-
|
502
|
-
ActiveRecord::Base.connected_to(role: :reading) do
|
503
|
-
User.first # reads from default replica
|
504
|
-
Dog.first # reads from default replica
|
502
|
+
## Rails 7.0.0.alpha1 (September 15, 2021) ##
|
505
503
|
|
506
|
-
|
507
|
-
User.first # reads from default replica
|
508
|
-
Dog.first # reads from shard one primary
|
509
|
-
end
|
504
|
+
* Remove warning when overwriting existing scopes
|
510
505
|
|
511
|
-
|
512
|
-
Dog.first # reads from default replica
|
506
|
+
Removes the following unnecessary warning message that appeared when overwriting existing scopes
|
513
507
|
|
514
|
-
|
515
|
-
|
516
|
-
Dog.first # reads from default replica
|
517
|
-
end
|
518
|
-
end
|
508
|
+
```
|
509
|
+
Creating scope :my_scope_name. Overwriting existing method "MyClass.my_scope_name" when overwriting existing scopes
|
519
510
|
```
|
520
511
|
|
521
|
-
|
522
|
-
|
523
|
-
* Allow double-dash comment syntax when querying read-only databases
|
524
|
-
|
525
|
-
*James Adam*
|
512
|
+
*Weston Ganger*
|
526
513
|
|
527
|
-
*
|
514
|
+
* Use full precision for `updated_at` in `insert_all`/`upsert_all`
|
528
515
|
|
529
|
-
|
516
|
+
`CURRENT_TIMESTAMP` provides differing precision depending on the database,
|
517
|
+
and not all databases support explicitly specifying additional precision.
|
530
518
|
|
531
|
-
|
532
|
-
|
533
|
-
topic.values_at(:title, :author_name)
|
534
|
-
# => ["Budget", "Jason"]
|
535
|
-
```
|
519
|
+
Instead, we delegate to the new `connection.high_precision_current_timestamp`
|
520
|
+
for the SQL to produce a high precision timestamp on the current database.
|
536
521
|
|
537
|
-
|
522
|
+
Fixes #42992
|
538
523
|
|
539
|
-
*
|
524
|
+
*Sam Bostock*
|
540
525
|
|
541
|
-
*
|
526
|
+
* Add ssl support for postgresql database tasks
|
542
527
|
|
543
|
-
|
528
|
+
Add `PGSSLMODE`, `PGSSLCERT`, `PGSSLKEY` and `PGSSLROOTCERT` to pg_env from database config
|
529
|
+
when running postgresql database tasks.
|
544
530
|
|
545
|
-
|
531
|
+
```yaml
|
532
|
+
# config/database.yml
|
546
533
|
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
}
|
553
|
-
end
|
534
|
+
production:
|
535
|
+
sslmode: verify-full
|
536
|
+
sslcert: client.crt
|
537
|
+
sslkey: client.key
|
538
|
+
sslrootcert: ca.crt
|
554
539
|
```
|
555
540
|
|
556
|
-
|
541
|
+
Environment variables
|
557
542
|
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
Person.in_batches.delete_all # => 42
|
543
|
+
```
|
544
|
+
PGSSLMODE=verify-full
|
545
|
+
PGSSLCERT=client.crt
|
546
|
+
PGSSLKEY=client.key
|
547
|
+
PGSSLROOTCERT=ca.crt
|
564
548
|
```
|
565
549
|
|
566
|
-
Fixes #
|
567
|
-
|
568
|
-
*Eugene Kenny*
|
569
|
-
|
570
|
-
* Add support for PostgreSQL `interval` data type with conversion to
|
571
|
-
`ActiveSupport::Duration` when loading records from database and
|
572
|
-
serialization to ISO 8601 formatted duration string on save.
|
573
|
-
Add support to define a column in migrations and get it in a schema dump.
|
574
|
-
Optional column precision is supported.
|
550
|
+
Fixes #42994
|
575
551
|
|
576
|
-
|
552
|
+
*Michael Bayucot*
|
577
553
|
|
578
|
-
|
554
|
+
* Avoid scoping update callbacks in `ActiveRecord::Relation#update!`.
|
579
555
|
|
580
|
-
|
556
|
+
Making it consistent with how scoping is applied only to the query in `ActiveRecord::Relation#update`
|
557
|
+
and not also to the callbacks from the update itself.
|
581
558
|
|
582
|
-
|
559
|
+
*Dylan Thacker-Smith*
|
583
560
|
|
584
|
-
|
561
|
+
* Fix 2 cases that inferred polymorphic class from the association's `foreign_type`
|
562
|
+
using `String#constantize` instead of the model's `polymorphic_class_for`.
|
585
563
|
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
end
|
564
|
+
When updating a polymorphic association, the old `foreign_type` was not inferred correctly when:
|
565
|
+
1. `touch`ing the previously associated record
|
566
|
+
2. updating the previously associated record's `counter_cache`
|
590
567
|
|
591
|
-
|
592
|
-
attribute :duration, :interval
|
593
|
-
end
|
568
|
+
*Jimmy Bourassa*
|
594
569
|
|
595
|
-
|
596
|
-
Event.last.duration # => 2 days
|
597
|
-
Event.last.duration.iso8601 # => "P2D"
|
598
|
-
Event.new(duration: 'P1DT12H3S').duration # => 1 day, 12 hours, and 3 seconds
|
599
|
-
Event.new(duration: '1 day') # Unknown value will be ignored and NULL will be written to database
|
570
|
+
* Add config option for ignoring tables when dumping the schema cache.
|
600
571
|
|
601
|
-
|
572
|
+
Applications can now be configured to ignore certain tables when dumping the schema cache.
|
602
573
|
|
603
|
-
|
574
|
+
The configuration option can table an array of tables:
|
604
575
|
|
605
576
|
```ruby
|
606
|
-
|
607
|
-
belongs_to :supplier, dependent: :destroy_async
|
608
|
-
end
|
577
|
+
config.active_record.schema_cache_ignored_tables = ["ignored_table", "another_ignored_table"]
|
609
578
|
```
|
610
579
|
|
611
|
-
|
612
|
-
|
613
|
-
*DHH*, *George Claghorn*, *Cory Gwin*, *Rafael Mendonça França*, *Adrianna Chang*
|
614
|
-
|
615
|
-
* Add `SKIP_TEST_DATABASE` environment variable to disable modifying the test database when `rails db:create` and `rails db:drop` are called.
|
580
|
+
Or a regex:
|
616
581
|
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
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.
|
582
|
+
```ruby
|
583
|
+
config.active_record.schema_cache_ignored_tables = [/^_/]
|
584
|
+
```
|
622
585
|
|
623
|
-
*Eileen M. Uchitelle
|
586
|
+
*Eileen M. Uchitelle*
|
624
587
|
|
625
|
-
*
|
626
|
-
`ActiveRecord::StatementInvalid` when they encounter a connection error.
|
588
|
+
* Make schema cache methods return consistent results.
|
627
589
|
|
628
|
-
|
590
|
+
Previously the schema cache methods `primary_keys`, `columns`, `columns_hash`, and `indexes`
|
591
|
+
would behave differently than one another when a table didn't exist and differently across
|
592
|
+
database adapters. This change unifies the behavior so each method behaves the same regardless
|
593
|
+
of adapter.
|
629
594
|
|
630
|
-
|
631
|
-
`ActiveRecord::StatementInvalid` when it can't connect to the MySQL server.
|
595
|
+
The behavior now is:
|
632
596
|
|
633
|
-
|
597
|
+
`columns`: (unchanged) raises a db error if the table does not exist.
|
598
|
+
`columns_hash`: (unchanged) raises a db error if the table does not exist.
|
599
|
+
`primary_keys`: (unchanged) returns `nil` if the table does not exist.
|
600
|
+
`indexes`: (changed for mysql2) returns `[]` if the table does not exist.
|
634
601
|
|
635
|
-
*
|
602
|
+
*Eileen M. Uchitelle*
|
636
603
|
|
637
|
-
|
604
|
+
* Reestablish connection to previous database after after running `db:schema:load:name`
|
638
605
|
|
639
|
-
|
606
|
+
After running `db:schema:load:name` the previous connection is restored.
|
640
607
|
|
641
|
-
|
608
|
+
*Jacopo Beschi*
|
642
609
|
|
643
|
-
|
610
|
+
* Add database config option `database_tasks`
|
644
611
|
|
645
|
-
|
612
|
+
If you would like to connect to an external database without any database
|
613
|
+
management tasks such as schema management, migrations, seeds, etc. you can set
|
614
|
+
the per database config option `database_tasks: false`
|
646
615
|
|
647
|
-
```
|
648
|
-
|
649
|
-
enum label: [:default, :child]
|
650
|
-
has_many :children, class_name: "Comment", foreign_key: :parent_id
|
651
|
-
end
|
616
|
+
```yaml
|
617
|
+
# config/database.yml
|
652
618
|
|
653
|
-
|
654
|
-
|
619
|
+
production:
|
620
|
+
primary:
|
621
|
+
database: my_database
|
622
|
+
adapter: mysql2
|
623
|
+
animals:
|
624
|
+
database: my_animals_database
|
625
|
+
adapter: mysql2
|
626
|
+
database_tasks: false
|
655
627
|
```
|
656
628
|
|
657
|
-
*
|
658
|
-
|
659
|
-
* Support storing demodulized class name for polymorphic type.
|
629
|
+
*Weston Ganger*
|
660
630
|
|
661
|
-
|
662
|
-
by `store_full_sti_class` class attribute.
|
631
|
+
* Fix `ActiveRecord::InternalMetadata` to not be broken by `config.active_record.record_timestamps = false`
|
663
632
|
|
664
|
-
|
665
|
-
|
666
|
-
*Ryuta Kamizono*
|
633
|
+
Since the model always create the timestamp columns, it has to set them, otherwise it breaks
|
634
|
+
various DB management tasks.
|
667
635
|
|
668
|
-
|
669
|
-
`rails db:schema:{load, dump}` tasks to work with either `:ruby` or `:sql` format,
|
670
|
-
depending on `config.active_record.schema_format` configuration value.
|
636
|
+
Fixes #42983
|
671
637
|
|
672
|
-
|
638
|
+
* Add `ActiveRecord::QueryLogs`.
|
673
639
|
|
674
|
-
|
640
|
+
Configurable tags can be automatically added to all SQL queries generated by Active Record.
|
675
641
|
|
676
642
|
```ruby
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
post.title # => "Welcome to the weblog"
|
684
|
-
post.body # => "Such a lovely day"
|
685
|
-
|
686
|
-
# Rails 6.1 (respect the `select` values)
|
687
|
-
post = Post.select("UPPER(title) AS title").eager_load(:comments).first
|
688
|
-
post.title # => "WELCOME TO THE WEBLOG"
|
689
|
-
post.body # => ActiveModel::MissingAttributeError
|
643
|
+
# config/application.rb
|
644
|
+
module MyApp
|
645
|
+
class Application < Rails::Application
|
646
|
+
config.active_record.query_log_tags_enabled = true
|
647
|
+
end
|
648
|
+
end
|
690
649
|
```
|
691
650
|
|
692
|
-
|
693
|
-
|
694
|
-
* Allow attribute's default to be configured but keeping its own type.
|
651
|
+
By default the application, controller and action details are added to the query tags:
|
695
652
|
|
696
653
|
```ruby
|
697
|
-
class
|
698
|
-
|
654
|
+
class BooksController < ApplicationController
|
655
|
+
def index
|
656
|
+
@books = Book.all
|
657
|
+
end
|
699
658
|
end
|
700
|
-
|
701
|
-
# Rails 6.0
|
702
|
-
Post.type_for_attribute(:written_at) # => #<Type::Value ... precision: nil, ...>
|
703
|
-
|
704
|
-
# Rails 6.1
|
705
|
-
Post.type_for_attribute(:written_at) # => #<Type::DateTime ... precision: 6, ...>
|
706
659
|
```
|
707
660
|
|
708
|
-
|
661
|
+
```ruby
|
662
|
+
GET /books
|
663
|
+
# SELECT * FROM books /*application:MyApp;controller:books;action:index*/
|
664
|
+
```
|
709
665
|
|
710
|
-
|
666
|
+
Custom tags containing static values and Procs can be defined in the application configuration:
|
711
667
|
|
712
668
|
```ruby
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
669
|
+
config.active_record.query_log_tags = [
|
670
|
+
:application,
|
671
|
+
:controller,
|
672
|
+
:action,
|
673
|
+
{
|
674
|
+
custom_static: "foo",
|
675
|
+
custom_dynamic: -> { Time.now }
|
676
|
+
}
|
677
|
+
]
|
718
678
|
```
|
719
679
|
|
720
|
-
*
|
680
|
+
*Keeran Raj Hawoldar*, *Eileen M. Uchitelle*, *Kasper Timm Hansen*
|
721
681
|
|
722
|
-
*
|
682
|
+
* Added support for multiple databases to `rails db:setup` and `rails db:reset`.
|
723
683
|
|
724
|
-
*
|
684
|
+
*Ryan Hall*
|
725
685
|
|
726
|
-
*
|
727
|
-
allows you to specify that all string columns should be frozen unless
|
728
|
-
otherwise specified. This will reduce memory pressure for applications which
|
729
|
-
do not generally mutate string properties of Active Record objects.
|
686
|
+
* Add `ActiveRecord::Relation#structurally_compatible?`.
|
730
687
|
|
731
|
-
|
688
|
+
Adds a query method by which a user can tell if the relation that they're
|
689
|
+
about to use for `#or` or `#and` is structurally compatible with the
|
690
|
+
receiver.
|
732
691
|
|
733
|
-
*
|
692
|
+
*Kevin Newton*
|
734
693
|
|
735
|
-
|
694
|
+
* Add `ActiveRecord::QueryMethods#in_order_of`.
|
736
695
|
|
737
|
-
|
696
|
+
This allows you to specify an explicit order that you'd like records
|
697
|
+
returned in based on a SQL expression. By default, this will be accomplished
|
698
|
+
using a case statement, as in:
|
738
699
|
|
739
700
|
```ruby
|
740
|
-
|
741
|
-
mary_and_bob = Author.where(id: [mary, bob])
|
742
|
-
|
743
|
-
david_and_mary.merge(mary_and_bob) # => [mary, bob]
|
744
|
-
|
745
|
-
david_and_mary.and(mary_and_bob) # => [mary]
|
746
|
-
david_and_mary.or(mary_and_bob) # => [david, mary, bob]
|
701
|
+
Post.in_order_of(:id, [3, 5, 1])
|
747
702
|
```
|
748
703
|
|
749
|
-
|
750
|
-
|
751
|
-
* Merging conditions on the same column no longer maintain both conditions,
|
752
|
-
and will be consistently replaced by the latter condition in Rails 6.2.
|
753
|
-
To migrate to Rails 6.2's behavior, use `relation.merge(other, rewhere: true)`.
|
754
|
-
|
755
|
-
```ruby
|
756
|
-
# Rails 6.1 (IN clause is replaced by merger side equality condition)
|
757
|
-
Author.where(id: [david.id, mary.id]).merge(Author.where(id: bob)) # => [bob]
|
704
|
+
will generate the SQL:
|
758
705
|
|
759
|
-
|
760
|
-
|
706
|
+
```sql
|
707
|
+
SELECT "posts".* FROM "posts" ORDER BY CASE "posts"."id" WHEN 3 THEN 1 WHEN 5 THEN 2 WHEN 1 THEN 3 ELSE 4 END ASC
|
708
|
+
```
|
761
709
|
|
762
|
-
|
763
|
-
|
710
|
+
However, because this functionality is built into MySQL in the form of the
|
711
|
+
`FIELD` function, that connection adapter will generate the following SQL
|
712
|
+
instead:
|
764
713
|
|
765
|
-
|
766
|
-
|
767
|
-
Author.where(id: david.id..mary.id).merge(Author.where(id: bob)) # => [bob]
|
714
|
+
```sql
|
715
|
+
SELECT "posts".* FROM "posts" ORDER BY FIELD("posts"."id", 1, 5, 3) DESC
|
768
716
|
```
|
769
717
|
|
770
|
-
*
|
718
|
+
*Kevin Newton*
|
771
719
|
|
772
|
-
*
|
720
|
+
* Fix `eager_loading?` when ordering with `Symbol`.
|
773
721
|
|
774
|
-
|
722
|
+
`eager_loading?` is triggered correctly when using `order` with symbols.
|
775
723
|
|
776
|
-
|
724
|
+
```ruby
|
725
|
+
scope = Post.includes(:comments).order(:"comments.label")
|
726
|
+
=> true
|
727
|
+
```
|
777
728
|
|
778
|
-
|
779
|
-
`ActiveRecord::Persistence.upsert_all` was used with the name of an expression index, an error
|
780
|
-
was raised. Adding a guard around the formatting behavior for the `:unique_by` corrects this.
|
729
|
+
*Jacopo Beschi*
|
781
730
|
|
782
|
-
|
731
|
+
* Two change tracking methods are added for `belongs_to` associations.
|
783
732
|
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
t.index "lower(name)", unique: true
|
788
|
-
end
|
733
|
+
The `association_changed?` method (assuming an association named `:association`) returns true
|
734
|
+
if a different associated object has been assigned and the foreign key will be updated in the
|
735
|
+
next save.
|
789
736
|
|
790
|
-
|
791
|
-
|
737
|
+
The `association_previously_changed?` method returns true if the previous save updated the
|
738
|
+
association to reference a different associated object.
|
792
739
|
|
793
|
-
|
740
|
+
*George Claghorn*
|
794
741
|
|
795
|
-
|
742
|
+
* Add option to disable schema dump per-database.
|
796
743
|
|
797
|
-
|
744
|
+
Dumping the schema is on by default for all databases in an application. To turn it off for a
|
745
|
+
specific database, use the `schema_dump` option:
|
798
746
|
|
799
|
-
|
747
|
+
```yaml
|
748
|
+
# config/database.yml
|
800
749
|
|
801
|
-
|
802
|
-
|
803
|
-
remove_check_constraint :products, name: "price_check"
|
750
|
+
production:
|
751
|
+
schema_dump: false
|
804
752
|
```
|
805
753
|
|
806
|
-
*
|
754
|
+
*Luis Vasconcellos*, *Eileen M. Uchitelle*
|
807
755
|
|
808
|
-
*
|
809
|
-
to enable/disable strict_loading mode by default for a model. The configuration's value is
|
810
|
-
inheritable by subclasses, but they can override that value and it will not impact parent class.
|
756
|
+
* Fix `eager_loading?` when ordering with `Hash` syntax.
|
811
757
|
|
812
|
-
|
758
|
+
`eager_loading?` is triggered correctly when using `order` with hash syntax
|
759
|
+
on an outer table.
|
813
760
|
|
814
761
|
```ruby
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
has_many :projects
|
819
|
-
end
|
820
|
-
|
821
|
-
dev = Developer.first
|
822
|
-
dev.projects.first
|
823
|
-
# => ActiveRecord::StrictLoadingViolationError Exception: Developer is marked as strict_loading and Project cannot be lazily loaded.
|
762
|
+
Post.includes(:comments).order({ "comments.label": :ASC }).eager_loading?
|
763
|
+
# => true
|
824
764
|
```
|
825
765
|
|
826
|
-
*
|
766
|
+
*Jacopo Beschi*
|
827
767
|
|
828
|
-
*
|
768
|
+
* Move the forcing of clear text encoding to the `ActiveRecord::Encryption::Encryptor`.
|
829
769
|
|
830
|
-
|
770
|
+
Fixes #42699.
|
831
771
|
|
832
|
-
*
|
833
|
-
|
834
|
-
Before:
|
772
|
+
*J Smith*
|
835
773
|
|
836
|
-
|
837
|
-
create_table "accounts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
|
838
|
-
end
|
839
|
-
```
|
774
|
+
* `partial_inserts` is now disabled by default in new apps.
|
840
775
|
|
841
|
-
|
776
|
+
This will be the default for new apps in Rails 7. To opt in:
|
842
777
|
|
843
778
|
```ruby
|
844
|
-
|
845
|
-
end
|
779
|
+
config.active_record.partial_inserts = true
|
846
780
|
```
|
847
781
|
|
848
|
-
|
782
|
+
If a migration removes the default value of a column, this option
|
783
|
+
would cause old processes to no longer be able to create new records.
|
849
784
|
|
850
|
-
|
851
|
-
|
785
|
+
If you need to remove a column, you should first use `ignored_columns`
|
786
|
+
to stop using it.
|
852
787
|
|
853
|
-
*
|
788
|
+
*Jean Boussier*
|
854
789
|
|
855
|
-
*
|
790
|
+
* Rails can now verify foreign keys after loading fixtures in tests.
|
856
791
|
|
857
|
-
|
792
|
+
This will be the default for new apps in Rails 7. To opt in:
|
858
793
|
|
859
794
|
```ruby
|
860
|
-
|
861
|
-
|
862
|
-
# duplicated group fields, deprecated.
|
863
|
-
accounts.merge(accounts.where.not(credit_limit: nil)).sum(:credit_limit)
|
864
|
-
# => {
|
865
|
-
# [1, 1] => 50,
|
866
|
-
# [2, 2] => 60
|
867
|
-
# }
|
868
|
-
|
869
|
-
# use `uniq!(:group)` to deduplicate group fields.
|
870
|
-
accounts.merge(accounts.where.not(credit_limit: nil)).uniq!(:group).sum(:credit_limit)
|
871
|
-
# => {
|
872
|
-
# 1 => 50,
|
873
|
-
# 2 => 60
|
874
|
-
# }
|
795
|
+
config.active_record.verify_foreign_keys_for_fixtures = true
|
875
796
|
```
|
876
797
|
|
877
|
-
|
798
|
+
Tests will not run if there is a foreign key constraint violation in your fixture data.
|
878
799
|
|
879
|
-
|
800
|
+
The feature is supported by SQLite and PostgreSQL, other adapters can also add support for it.
|
880
801
|
|
881
|
-
|
802
|
+
*Alex Ghiculescu*
|
882
803
|
|
883
|
-
|
884
|
-
accounts = Account.where(id: [1, 2]).annotate("david and mary")
|
804
|
+
* Clear cached `has_one` association after setting `belongs_to` association to `nil`.
|
885
805
|
|
886
|
-
|
887
|
-
|
888
|
-
# SELECT accounts.* FROM accounts WHERE accounts.id = 3 /* david and mary */ /* david and mary */
|
806
|
+
After setting a `belongs_to` relation to `nil` and updating an unrelated attribute on the owner,
|
807
|
+
the owner should still return `nil` on the `has_one` relation.
|
889
808
|
|
890
|
-
#
|
891
|
-
accounts.merge(accounts.rewhere(id: 3)).uniq!(:annotate)
|
892
|
-
# SELECT accounts.* FROM accounts WHERE accounts.id = 3 /* david and mary */
|
893
|
-
```
|
809
|
+
Fixes #42597.
|
894
810
|
|
895
|
-
*
|
811
|
+
*Michiel de Mare*
|
896
812
|
|
897
|
-
*
|
813
|
+
* OpenSSL constants are now used for Digest computations.
|
898
814
|
|
899
|
-
|
900
|
-
cache. This avoids raising an unnecessary `ActiveRecord::StaleObjectError`
|
901
|
-
upon subsequent transactions by maintaining parity with the corresponding
|
902
|
-
database record's `lock_version` column.
|
815
|
+
*Dirkjan Bussink*
|
903
816
|
|
904
|
-
|
817
|
+
* Adds support for `if_not_exists` to `add_foreign_key` and `if_exists` to `remove_foreign_key`.
|
905
818
|
|
906
|
-
|
819
|
+
Applications can set their migrations to ignore exceptions raised when adding a foreign key
|
820
|
+
that already exists or when removing a foreign key that does not exist.
|
907
821
|
|
908
|
-
|
822
|
+
Example Usage:
|
909
823
|
|
910
824
|
```ruby
|
911
|
-
|
825
|
+
class AddAuthorsForeignKeyToArticles < ActiveRecord::Migration[7.0]
|
826
|
+
def change
|
827
|
+
add_foreign_key :articles, :authors, if_not_exists: true
|
828
|
+
end
|
829
|
+
end
|
830
|
+
```
|
912
831
|
|
913
|
-
|
914
|
-
|
832
|
+
```ruby
|
833
|
+
class RemoveAuthorsForeignKeyFromArticles < ActiveRecord::Migration[7.0]
|
834
|
+
def change
|
835
|
+
remove_foreign_key :articles, :authors, if_exists: true
|
836
|
+
end
|
837
|
+
end
|
838
|
+
```
|
915
839
|
|
916
|
-
|
917
|
-
david_and_mary.merge(Author.rewhere(id: bob)) # => [bob]
|
840
|
+
*Roberto Miranda*
|
918
841
|
|
919
|
-
|
920
|
-
david_and_mary.merge(Author.where(id: bob), rewhere: true) # => [bob]
|
921
|
-
```
|
842
|
+
* Prevent polluting ENV during postgresql structure dump/load.
|
922
843
|
|
923
|
-
|
844
|
+
Some configuration parameters were provided to pg_dump / psql via
|
845
|
+
environment variables which persisted beyond the command being run, and may
|
846
|
+
have caused subsequent commands and connections to fail. Tasks running
|
847
|
+
across multiple postgresql databases like `rails db:test:prepare` may have
|
848
|
+
been affected.
|
924
849
|
|
925
|
-
*
|
926
|
-
set to expire and scoped with a purpose. This is particularly useful for things like password reset
|
927
|
-
or email verification, where you want the bearer of the signed id to be able to interact with the
|
928
|
-
underlying record, but usually only within a certain time period.
|
850
|
+
*Samuel Cochran*
|
929
851
|
|
930
|
-
|
931
|
-
signed_id = User.first.signed_id expires_in: 15.minutes, purpose: :password_reset
|
852
|
+
* Set precision 6 by default for `datetime` columns.
|
932
853
|
|
933
|
-
|
854
|
+
By default, datetime columns will have microseconds precision instead of seconds precision.
|
934
855
|
|
935
|
-
|
936
|
-
User.find_signed signed_id, purpose: :password_reset # => nil, since the signed id has expired
|
856
|
+
*Roberto Miranda*
|
937
857
|
|
938
|
-
|
939
|
-
User.find_signed signed_id, purpose: :password_reset # => User.first
|
858
|
+
* Allow preloading of associations with instance dependent scopes.
|
940
859
|
|
941
|
-
|
942
|
-
```
|
860
|
+
*John Hawthorn*, *John Crepezzi*, *Adam Hess*, *Eileen M. Uchitelle*, *Dinah Shi*
|
943
861
|
|
944
|
-
|
862
|
+
* Do not try to rollback transactions that failed due to a `ActiveRecord::TransactionRollbackError`.
|
945
863
|
|
946
|
-
*
|
864
|
+
*Jamie McCarthy*
|
947
865
|
|
948
|
-
|
866
|
+
* Active Record Encryption will now encode values as UTF-8 when using deterministic
|
867
|
+
encryption. The encoding is part of the encrypted payload, so different encodings for
|
868
|
+
different values result in different ciphertexts. This can break unique constraints and
|
869
|
+
queries.
|
949
870
|
|
950
|
-
|
871
|
+
The new behavior is configurable via `active_record.encryption.forced_encoding_for_deterministic_encryption`
|
872
|
+
that is `Encoding::UTF_8` by default. It can be disabled by setting it to `nil`.
|
951
873
|
|
952
|
-
*
|
874
|
+
*Jorge Manrubia*
|
953
875
|
|
954
|
-
*
|
876
|
+
* The MySQL adapter now cast numbers and booleans bind parameters to string for safety reasons.
|
955
877
|
|
956
|
-
|
878
|
+
When comparing a string and a number in a query, MySQL converts the string to a number. So for
|
879
|
+
instance `"foo" = 0`, will implicitly cast `"foo"` to `0` and will evaluate to `TRUE` which can
|
880
|
+
lead to security vulnerabilities.
|
957
881
|
|
958
|
-
|
959
|
-
|
960
|
-
posts = posts.where("posts.hidden": false, "comments.hidden": false)
|
882
|
+
Active Record already protect against that vulnerability when it knows the type of the column
|
883
|
+
being compared, however until now it was still vulnerable when using bind parameters:
|
961
884
|
|
962
|
-
|
963
|
-
|
885
|
+
```ruby
|
886
|
+
User.where("login_token = ?", 0).first
|
887
|
+
```
|
964
888
|
|
965
|
-
|
966
|
-
posts.unscope(where: :hidden).count
|
967
|
-
# => { false => 11, true => 1 }
|
889
|
+
Would perform:
|
968
890
|
|
969
|
-
|
970
|
-
|
971
|
-
# => { false => 11 }
|
891
|
+
```sql
|
892
|
+
SELECT * FROM `users` WHERE `login_token` = 0 LIMIT 1;
|
972
893
|
```
|
973
894
|
|
974
|
-
|
895
|
+
Now it will perform:
|
975
896
|
|
976
|
-
|
897
|
+
```sql
|
898
|
+
SELECT * FROM `users` WHERE `login_token` = '0' LIMIT 1;
|
899
|
+
```
|
977
900
|
|
978
|
-
|
979
|
-
steve = Person.find_by(name: "Steve")
|
980
|
-
david = Author.find_by(name: "David")
|
901
|
+
*Jean Boussier*
|
981
902
|
|
982
|
-
|
903
|
+
* Fixture configurations (`_fixture`) are now strictly validated.
|
983
904
|
|
984
|
-
|
985
|
-
|
905
|
+
If an error will be raised if that entry contains unknown keys while previously it
|
906
|
+
would silently have no effects.
|
986
907
|
|
987
|
-
|
988
|
-
relation.rewhere(writer: david).to_a # => [david]
|
989
|
-
```
|
908
|
+
*Jean Boussier*
|
990
909
|
|
991
|
-
|
910
|
+
* Add `ActiveRecord::Base.update!` that works like `ActiveRecord::Base.update` but raises exceptions.
|
992
911
|
|
993
|
-
|
912
|
+
This allows for the same behavior as the instance method `#update!` at a class level.
|
994
913
|
|
995
914
|
```ruby
|
996
|
-
|
997
|
-
=> #<Knot id: 1, created_at: "2016-05-05 01:29:47.116928000 +0000">
|
915
|
+
Person.update!(:all, state: "confirmed")
|
998
916
|
```
|
999
917
|
|
1000
|
-
*
|
918
|
+
*Dorian Marié*
|
1001
919
|
|
1002
|
-
*
|
920
|
+
* Add `ActiveRecord::Base#attributes_for_database`.
|
1003
921
|
|
1004
|
-
|
922
|
+
Returns attributes with values for assignment to the database.
|
1005
923
|
|
1006
|
-
*
|
924
|
+
*Chris Salzberg*
|
1007
925
|
|
1008
|
-
|
926
|
+
* Use an empty query to check if the PostgreSQL connection is still active.
|
1009
927
|
|
1010
|
-
|
928
|
+
An empty query is faster than `SELECT 1`.
|
1011
929
|
|
1012
|
-
*
|
930
|
+
*Heinrich Lee Yu*
|
1013
931
|
|
1014
|
-
*
|
932
|
+
* Add `ActiveRecord::Base#previously_persisted?`.
|
1015
933
|
|
1016
|
-
|
934
|
+
Returns `true` if the object has been previously persisted but now it has been deleted.
|
1017
935
|
|
1018
|
-
*
|
936
|
+
* Deprecate `partial_writes` in favor of `partial_inserts` and `partial_updates`.
|
1019
937
|
|
1020
|
-
|
938
|
+
This allows to have a different behavior on update and create.
|
1021
939
|
|
1022
|
-
*
|
940
|
+
*Jean Boussier*
|
1023
941
|
|
1024
|
-
*
|
942
|
+
* Fix compatibility with `psych >= 4`.
|
1025
943
|
|
1026
|
-
|
944
|
+
Starting in Psych 4.0.0 `YAML.load` behaves like `YAML.safe_load`. To preserve compatibility,
|
945
|
+
Active Record's schema cache loader and `YAMLColumn` now uses `YAML.unsafe_load` if available.
|
1027
946
|
|
1028
|
-
*
|
947
|
+
*Jean Boussier*
|
1029
948
|
|
1030
|
-
|
949
|
+
* `ActiveRecord::Base.logger` is now a `class_attribute`.
|
1031
950
|
|
1032
|
-
|
951
|
+
This means it can no longer be accessed directly through `@@logger`, and that setting `logger =`
|
952
|
+
on a subclass won't change the parent's logger.
|
1033
953
|
|
1034
|
-
*
|
954
|
+
*Jean Boussier*
|
1035
955
|
|
1036
|
-
|
1037
|
-
index won't be added.
|
956
|
+
* Add `.asc.nulls_first` for all databases. Unfortunately MySQL still doesn't like `nulls_last`.
|
1038
957
|
|
1039
|
-
|
958
|
+
*Keenan Brock*
|
1040
959
|
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
960
|
+
* Improve performance of `one?` and `many?` by limiting the generated count query to 2 results.
|
961
|
+
|
962
|
+
*Gonzalo Riestra*
|
963
|
+
|
964
|
+
* Don't check type when using `if_not_exists` on `add_column`.
|
965
|
+
|
966
|
+
Previously, if a migration called `add_column` with the `if_not_exists` option set to true
|
967
|
+
the `column_exists?` check would look for a column with the same name and type as the migration.
|
968
|
+
|
969
|
+
Recently it was discovered that the type passed to the migration is not always the same type
|
970
|
+
as the column after migration. For example a column set to `:mediumblob` in the migration will
|
971
|
+
be casted to `binary` when calling `column.type`. Since there is no straightforward way to cast
|
972
|
+
the type to the database type without running the migration, we opted to drop the type check from
|
973
|
+
`add_column`. This means that migrations adding a duplicate column with a different type will no
|
974
|
+
longer raise an error.
|
1044
975
|
|
1045
|
-
|
1046
|
-
created within that migration so that if table and its indexes exist then there is no
|
1047
|
-
attempt to create them again.
|
976
|
+
*Eileen M. Uchitelle*
|
1048
977
|
|
1049
|
-
|
978
|
+
* Log a warning message when running SQLite in production.
|
1050
979
|
|
1051
|
-
|
980
|
+
Using SQLite in production ENV is generally discouraged. SQLite is also the default adapter
|
981
|
+
in a new Rails application.
|
982
|
+
For the above reasons log a warning message when running SQLite in production.
|
1052
983
|
|
1053
|
-
|
984
|
+
The warning can be disabled by setting `config.active_record.sqlite3_production_warning=false`.
|
1054
985
|
|
1055
|
-
*
|
986
|
+
*Jacopo Beschi*
|
1056
987
|
|
1057
|
-
|
988
|
+
* Add option to disable joins for `has_one` associations.
|
1058
989
|
|
1059
|
-
|
990
|
+
In a multiple database application, associations can't join across
|
991
|
+
databases. When set, this option instructs Rails to generate 2 or
|
992
|
+
more queries rather than generating joins for `has_one` associations.
|
1060
993
|
|
1061
|
-
|
994
|
+
Set the option on a has one through association:
|
1062
995
|
|
1063
996
|
```ruby
|
1064
|
-
Person
|
1065
|
-
|
997
|
+
class Person
|
998
|
+
has_one :dog
|
999
|
+
has_one :veterinarian, through: :dog, disable_joins: true
|
1066
1000
|
end
|
1067
1001
|
```
|
1068
1002
|
|
1069
|
-
|
1003
|
+
Then instead of generating join SQL, two queries are used for `@person.veterinarian`:
|
1070
1004
|
|
1071
|
-
|
1005
|
+
```
|
1006
|
+
SELECT "dogs"."id" FROM "dogs" WHERE "dogs"."person_id" = ? [["person_id", 1]]
|
1007
|
+
SELECT "veterinarians".* FROM "veterinarians" WHERE "veterinarians"."dog_id" = ? [["dog_id", 1]]
|
1008
|
+
```
|
1072
1009
|
|
1073
|
-
|
1010
|
+
*Sarah Vessels*, *Eileen M. Uchitelle*
|
1074
1011
|
|
1075
|
-
|
1012
|
+
* `Arel::Visitors::Dot` now renders a complete set of properties when visiting
|
1013
|
+
`Arel::Nodes::SelectCore`, `SelectStatement`, `InsertStatement`, `UpdateStatement`, and
|
1014
|
+
`DeleteStatement`, which fixes #42026. Previously, some properties were omitted.
|
1076
1015
|
|
1077
|
-
*
|
1016
|
+
*Mike Dalessio*
|
1078
1017
|
|
1079
|
-
|
1018
|
+
* `Arel::Visitors::Dot` now supports `Arel::Nodes::Bin`, `Case`, `CurrentRow`, `Distinct`,
|
1019
|
+
`DistinctOn`, `Else`, `Except`, `InfixOperation`, `Intersect`, `Lock`, `NotRegexp`, `Quoted`,
|
1020
|
+
`Regexp`, `UnaryOperation`, `Union`, `UnionAll`, `When`, and `With`. Previously, these node
|
1021
|
+
types caused an exception to be raised by `Arel::Visitors::Dot#accept`.
|
1080
1022
|
|
1081
|
-
*
|
1023
|
+
*Mike Dalessio*
|
1082
1024
|
|
1083
|
-
* `
|
1025
|
+
* Optimize `remove_columns` to use a single SQL statement.
|
1084
1026
|
|
1085
|
-
|
1027
|
+
```ruby
|
1028
|
+
remove_columns :my_table, :col_one, :col_two
|
1029
|
+
```
|
1086
1030
|
|
1087
|
-
|
1031
|
+
Now results in the following SQL:
|
1088
1032
|
|
1089
|
-
|
1033
|
+
```sql
|
1034
|
+
ALTER TABLE "my_table" DROP COLUMN "col_one", DROP COLUMN "col_two"
|
1035
|
+
```
|
1090
1036
|
|
1091
|
-
*
|
1037
|
+
*Jon Dufresne*
|
1092
1038
|
|
1093
|
-
|
1039
|
+
* Ensure `has_one` autosave association callbacks get called once.
|
1094
1040
|
|
1095
|
-
|
1041
|
+
Change the `has_one` autosave callback to be non cyclic as well.
|
1042
|
+
By doing this the autosave callback are made more consistent for
|
1043
|
+
all 3 cases: `has_many`, `has_one`, and `belongs_to`.
|
1096
1044
|
|
1097
|
-
*
|
1045
|
+
*Petrik de Heus*
|
1098
1046
|
|
1099
|
-
*
|
1047
|
+
* Add option to disable joins for associations.
|
1100
1048
|
|
1101
|
-
|
1049
|
+
In a multiple database application, associations can't join across
|
1050
|
+
databases. When set, this option instructs Rails to generate 2 or
|
1051
|
+
more queries rather than generating joins for associations.
|
1102
1052
|
|
1103
|
-
|
1053
|
+
Set the option on a has many through association:
|
1104
1054
|
|
1105
|
-
|
1055
|
+
```ruby
|
1056
|
+
class Dog
|
1057
|
+
has_many :treats, through: :humans, disable_joins: true
|
1058
|
+
has_many :humans
|
1059
|
+
end
|
1060
|
+
```
|
1106
1061
|
|
1107
|
-
|
1062
|
+
Then instead of generating join SQL, two queries are used for `@dog.treats`:
|
1108
1063
|
|
1109
|
-
|
1064
|
+
```
|
1065
|
+
SELECT "humans"."id" FROM "humans" WHERE "humans"."dog_id" = ? [["dog_id", 1]]
|
1066
|
+
SELECT "treats".* FROM "treats" WHERE "treats"."human_id" IN (?, ?, ?) [["human_id", 1], ["human_id", 2], ["human_id", 3]]
|
1067
|
+
```
|
1110
1068
|
|
1111
|
-
*
|
1069
|
+
*Eileen M. Uchitelle*, *Aaron Patterson*, *Lee Quarella*
|
1112
1070
|
|
1113
|
-
|
1071
|
+
* Add setting for enumerating column names in SELECT statements.
|
1114
1072
|
|
1115
|
-
|
1073
|
+
Adding a column to a PostgreSQL database, for example, while the application is running can
|
1074
|
+
change the result of wildcard `SELECT *` queries, which invalidates the result
|
1075
|
+
of cached prepared statements and raises a `PreparedStatementCacheExpired` error.
|
1116
1076
|
|
1117
|
-
|
1077
|
+
When enabled, Active Record will avoid wildcards and always include column names
|
1078
|
+
in `SELECT` queries, which will return consistent results and avoid prepared
|
1079
|
+
statement errors.
|
1118
1080
|
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
primary_shard_one:
|
1125
|
-
database: my_database_shard_one
|
1081
|
+
Before:
|
1082
|
+
|
1083
|
+
```ruby
|
1084
|
+
Book.limit(5)
|
1085
|
+
# SELECT * FROM books LIMIT 5
|
1126
1086
|
```
|
1127
1087
|
|
1128
|
-
|
1088
|
+
After:
|
1129
1089
|
|
1130
1090
|
```ruby
|
1131
|
-
|
1132
|
-
|
1091
|
+
# config/application.rb
|
1092
|
+
module MyApp
|
1093
|
+
class Application < Rails::Application
|
1094
|
+
config.active_record.enumerate_columns_in_select_statements = true
|
1095
|
+
end
|
1096
|
+
end
|
1133
1097
|
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1098
|
+
# or, configure per-model
|
1099
|
+
class Book < ApplicationRecord
|
1100
|
+
self.enumerate_columns_in_select_statements = true
|
1101
|
+
end
|
1138
1102
|
```
|
1139
1103
|
|
1140
|
-
Swap between shards in your controller / model code:
|
1141
|
-
|
1142
1104
|
```ruby
|
1143
|
-
|
1144
|
-
|
1145
|
-
end
|
1105
|
+
Book.limit(5)
|
1106
|
+
# SELECT id, author_id, name, format, status, language, etc FROM books LIMIT 5
|
1146
1107
|
```
|
1147
1108
|
|
1148
|
-
|
1109
|
+
*Matt Duszynski*
|
1149
1110
|
|
1150
|
-
|
1111
|
+
* Allow passing SQL as `on_duplicate` value to `#upsert_all` to make it possible to use raw SQL to update columns on conflict:
|
1151
1112
|
|
1152
|
-
|
1113
|
+
```ruby
|
1114
|
+
Book.upsert_all(
|
1115
|
+
[{ id: 1, status: 1 }, { id: 2, status: 1 }],
|
1116
|
+
on_duplicate: Arel.sql("status = GREATEST(books.status, EXCLUDED.status)")
|
1117
|
+
)
|
1118
|
+
```
|
1153
1119
|
|
1154
|
-
|
1120
|
+
*Vladimir Dementyev*
|
1155
1121
|
|
1156
|
-
|
1122
|
+
* Allow passing SQL as `returning` statement to `#upsert_all`:
|
1157
1123
|
|
1158
1124
|
```ruby
|
1159
|
-
|
1160
|
-
|
1125
|
+
Article.insert_all(
|
1126
|
+
[
|
1127
|
+
{ title: "Article 1", slug: "article-1", published: false },
|
1128
|
+
{ title: "Article 2", slug: "article-2", published: false }
|
1129
|
+
],
|
1130
|
+
returning: Arel.sql("id, (xmax = '0') as inserted, name as new_name")
|
1131
|
+
)
|
1161
1132
|
```
|
1162
1133
|
|
1163
|
-
|
1134
|
+
*Vladimir Dementyev*
|
1164
1135
|
|
1165
|
-
|
1166
|
-
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "development", name: "primary")
|
1167
|
-
db_config.name
|
1168
|
-
```
|
1136
|
+
* Deprecate `legacy_connection_handling`.
|
1169
1137
|
|
1170
1138
|
*Eileen M. Uchitelle*
|
1171
1139
|
|
1172
|
-
* Add
|
1140
|
+
* Add attribute encryption support.
|
1173
1141
|
|
1174
|
-
|
1175
|
-
|
1142
|
+
Encrypted attributes are declared at the model level. These
|
1143
|
+
are regular Active Record attributes backed by a column with
|
1144
|
+
the same name. The system will transparently encrypt these
|
1145
|
+
attributes before saving them into the database and will
|
1146
|
+
decrypt them when retrieving their values.
|
1176
1147
|
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
rails db:drop:animals
|
1184
|
-
rails db:migrate
|
1185
|
-
rails db:migrate:primary
|
1186
|
-
rails db:migrate:animals
|
1148
|
+
|
1149
|
+
```ruby
|
1150
|
+
class Person < ApplicationRecord
|
1151
|
+
encrypts :name
|
1152
|
+
encrypts :email_address, deterministic: true
|
1153
|
+
end
|
1187
1154
|
```
|
1188
1155
|
|
1189
|
-
|
1190
|
-
|
1156
|
+
You can learn more in the [Active Record Encryption
|
1157
|
+
guide](https://edgeguides.rubyonrails.org/active_record_encryption.html).
|
1191
1158
|
|
1192
|
-
|
1193
|
-
rails db:schema:dump
|
1194
|
-
rails db:schema:dump:primary
|
1195
|
-
rails db:schema:dump:animals
|
1196
|
-
rails db:schema:load
|
1197
|
-
rails db:schema:load:primary
|
1198
|
-
rails db:schema:load:animals
|
1199
|
-
rails db:structure:dump
|
1200
|
-
rails db:structure:dump:primary
|
1201
|
-
rails db:structure:dump:animals
|
1202
|
-
rails db:structure:load
|
1203
|
-
rails db:structure:load:primary
|
1204
|
-
rails db:structure:load:animals
|
1205
|
-
rails db:test:prepare
|
1206
|
-
rails db:test:prepare:primary
|
1207
|
-
rails db:test:prepare:animals
|
1208
|
-
```
|
1159
|
+
*Jorge Manrubia*
|
1209
1160
|
|
1210
|
-
|
1161
|
+
* Changed Arel predications `contains` and `overlaps` to use
|
1162
|
+
`quoted_node` so that PostgreSQL arrays are quoted properly.
|
1211
1163
|
|
1212
|
-
*
|
1164
|
+
*Bradley Priest*
|
1213
1165
|
|
1214
|
-
|
1166
|
+
* Add mode argument to record level `strict_loading!`.
|
1215
1167
|
|
1216
|
-
|
1168
|
+
This argument can be used when enabling strict loading for a single record
|
1169
|
+
to specify that we only want to raise on n plus one queries.
|
1217
1170
|
|
1218
1171
|
```ruby
|
1219
|
-
|
1220
|
-
has_many :projects, strict_loading: true
|
1221
|
-
end
|
1172
|
+
developer.strict_loading!(mode: :n_plus_one_only)
|
1222
1173
|
|
1223
|
-
|
1224
|
-
|
1225
|
-
# => ActiveRecord::StrictLoadingViolationError: The projects association is marked as strict_loading and cannot be lazily loaded.
|
1174
|
+
developer.projects.to_a # Does not raise
|
1175
|
+
developer.projects.first.client # Raises StrictLoadingViolationError
|
1226
1176
|
```
|
1227
1177
|
|
1228
|
-
|
1178
|
+
Previously, enabling strict loading would cause any lazily loaded
|
1179
|
+
association to raise an error. Using `n_plus_one_only` mode allows us to
|
1180
|
+
lazily load belongs_to, has_many, and other associations that are fetched
|
1181
|
+
through a single query.
|
1229
1182
|
|
1230
|
-
*
|
1183
|
+
*Dinah Shi*
|
1231
1184
|
|
1232
|
-
|
1185
|
+
* Fix Float::INFINITY assignment to datetime column with postgresql adapter.
|
1233
1186
|
|
1234
|
-
|
1187
|
+
Before:
|
1235
1188
|
|
1236
1189
|
```ruby
|
1237
|
-
|
1238
|
-
|
1239
|
-
# => ActiveRecord::StrictLoadingViolationError: Developer is marked as strict_loading and AuditLog cannot be lazily loaded.
|
1240
|
-
```
|
1190
|
+
# With this config
|
1191
|
+
ActiveRecord::Base.time_zone_aware_attributes = true
|
1241
1192
|
|
1242
|
-
|
1193
|
+
# and the following schema:
|
1194
|
+
create_table "postgresql_infinities" do |t|
|
1195
|
+
t.datetime "datetime"
|
1196
|
+
end
|
1243
1197
|
|
1244
|
-
|
1198
|
+
# This test fails
|
1199
|
+
record = PostgresqlInfinity.create!(datetime: Float::INFINITY)
|
1200
|
+
assert_equal Float::INFINITY, record.datetime # record.datetime gets nil
|
1201
|
+
```
|
1245
1202
|
|
1246
|
-
|
1203
|
+
After this commit, `record.datetime` gets `Float::INFINITY` as expected.
|
1247
1204
|
|
1248
|
-
*
|
1205
|
+
*Shunichi Ikegami*
|
1249
1206
|
|
1250
|
-
|
1207
|
+
* Type cast enum values by the original attribute type.
|
1251
1208
|
|
1252
|
-
|
1209
|
+
The notable thing about this change is that unknown labels will no longer match 0 on MySQL.
|
1253
1210
|
|
1254
1211
|
```ruby
|
1255
|
-
class
|
1256
|
-
|
1257
|
-
add_column :posts, :title, :string, if_not_exists: true
|
1258
|
-
end
|
1212
|
+
class Book < ActiveRecord::Base
|
1213
|
+
enum :status, { proposed: 0, written: 1, published: 2 }
|
1259
1214
|
end
|
1260
1215
|
```
|
1261
1216
|
|
1217
|
+
Before:
|
1218
|
+
|
1262
1219
|
```ruby
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1220
|
+
# SELECT `books`.* FROM `books` WHERE `books`.`status` = 'prohibited' LIMIT 1
|
1221
|
+
Book.find_by(status: :prohibited)
|
1222
|
+
# => #<Book id: 1, status: "proposed", ...> (for mysql2 adapter)
|
1223
|
+
# => ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type integer: "prohibited" (for postgresql adapter)
|
1224
|
+
# => nil (for sqlite3 adapter)
|
1268
1225
|
```
|
1269
1226
|
|
1270
|
-
|
1271
|
-
|
1272
|
-
* Regexp-escape table name for MS SQL Server.
|
1273
|
-
|
1274
|
-
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.
|
1275
|
-
|
1276
|
-
*Larry Reid*
|
1277
|
-
|
1278
|
-
* Store advisory locks on their own named connection.
|
1227
|
+
After:
|
1279
1228
|
|
1280
|
-
|
1229
|
+
```ruby
|
1230
|
+
# SELECT `books`.* FROM `books` WHERE `books`.`status` IS NULL LIMIT 1
|
1231
|
+
Book.find_by(status: :prohibited)
|
1232
|
+
# => nil (for all adapters)
|
1233
|
+
```
|
1281
1234
|
|
1282
|
-
|
1235
|
+
*Ryuta Kamizono*
|
1283
1236
|
|
1284
|
-
|
1237
|
+
* Fixtures for `has_many :through` associations now load timestamps on join tables.
|
1285
1238
|
|
1286
|
-
|
1239
|
+
Given this fixture:
|
1287
1240
|
|
1288
|
-
|
1241
|
+
```yml
|
1242
|
+
### monkeys.yml
|
1243
|
+
george:
|
1244
|
+
name: George the Monkey
|
1245
|
+
fruits: apple
|
1289
1246
|
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
database: blog_development
|
1294
|
-
pool: 5
|
1295
|
-
schema_cache_path: tmp/schema/main.yml
|
1247
|
+
### fruits.yml
|
1248
|
+
apple:
|
1249
|
+
name: apple
|
1296
1250
|
```
|
1297
1251
|
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1252
|
+
If the join table (`fruit_monkeys`) contains `created_at` or `updated_at` columns,
|
1253
|
+
these will now be populated when loading the fixture. Previously, fixture loading
|
1254
|
+
would crash if these columns were required, and leave them as null otherwise.
|
1301
1255
|
|
1302
|
-
|
1303
|
-
|
1304
|
-
*Eileen M. Uchitelle*, *John Crepezzi*
|
1305
|
-
|
1306
|
-
* Deprecate `#default_hash` and it's alias `#[]` on database configurations.
|
1256
|
+
*Alex Ghiculescu*
|
1307
1257
|
|
1308
|
-
|
1258
|
+
* Allow applications to configure the thread pool for async queries.
|
1309
1259
|
|
1310
|
-
|
1260
|
+
Some applications may want one thread pool per database whereas others want to use
|
1261
|
+
a single global thread pool for all queries. By default, Rails will set `async_query_executor`
|
1262
|
+
to `nil` which will not initialize any executor. If `load_async` is called and no executor
|
1263
|
+
has been configured, the query will be executed in the foreground.
|
1311
1264
|
|
1312
|
-
|
1265
|
+
To create one thread pool for all database connections to use applications can set
|
1266
|
+
`config.active_record.async_query_executor` to `:global_thread_pool` and optionally define
|
1267
|
+
`config.active_record.global_executor_concurrency`. This defaults to 4. For applications that want
|
1268
|
+
to have a thread pool for each database connection, `config.active_record.async_query_executor` can
|
1269
|
+
be set to `:multi_thread_pool`. The configuration for each thread pool is set in the database
|
1270
|
+
configuration.
|
1313
1271
|
|
1314
|
-
*
|
1272
|
+
*Eileen M. Uchitelle*
|
1315
1273
|
|
1316
|
-
*
|
1274
|
+
* Allow new syntax for `enum` to avoid leading `_` from reserved options.
|
1317
1275
|
|
1318
1276
|
Before:
|
1319
1277
|
|
1320
1278
|
```ruby
|
1321
|
-
|
1279
|
+
class Book < ActiveRecord::Base
|
1280
|
+
enum status: [ :proposed, :written ], _prefix: true, _scopes: false
|
1281
|
+
enum cover: [ :hard, :soft ], _suffix: true, _default: :hard
|
1282
|
+
end
|
1322
1283
|
```
|
1323
1284
|
|
1324
1285
|
After:
|
1325
1286
|
|
1326
1287
|
```ruby
|
1327
|
-
|
1288
|
+
class Book < ActiveRecord::Base
|
1289
|
+
enum :status, [ :proposed, :written ], prefix: true, scopes: false
|
1290
|
+
enum :cover, [ :hard, :soft ], suffix: true, default: :hard
|
1291
|
+
end
|
1328
1292
|
```
|
1329
1293
|
|
1330
|
-
*
|
1331
|
-
|
1332
|
-
* Ensure `:reading` connections always raise if a write is attempted.
|
1333
|
-
|
1334
|
-
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`.
|
1335
|
-
|
1336
|
-
*Eileen M. Uchitelle*
|
1337
|
-
|
1338
|
-
* Deprecate `"primary"` as the `connection_specification_name` for `ActiveRecord::Base`.
|
1339
|
-
|
1340
|
-
`"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"`.
|
1294
|
+
*Ryuta Kamizono*
|
1341
1295
|
|
1342
|
-
|
1296
|
+
* Add `ActiveRecord::Relation#load_async`.
|
1343
1297
|
|
1344
|
-
|
1345
|
-
support for casting floats using a database columns' precision value.
|
1298
|
+
This method schedules the query to be performed asynchronously from a thread pool.
|
1346
1299
|
|
1347
|
-
|
1300
|
+
If the result is accessed before a background thread had the opportunity to perform
|
1301
|
+
the query, it will be performed in the foreground.
|
1348
1302
|
|
1349
|
-
|
1350
|
-
|
1351
|
-
ActionController::ConditionalGet to ensure that when collection cache versioning
|
1352
|
-
is enabled, requests using ConditionalGet don't return the same ETag header
|
1353
|
-
after a collection is modified.
|
1303
|
+
This is useful for queries that can be performed long enough before their result will be
|
1304
|
+
needed, or for controllers which need to perform several independent queries.
|
1354
1305
|
|
1355
|
-
|
1306
|
+
```ruby
|
1307
|
+
def index
|
1308
|
+
@categories = Category.some_complex_scope.load_async
|
1309
|
+
@posts = Post.some_complex_scope.load_async
|
1310
|
+
end
|
1311
|
+
```
|
1356
1312
|
|
1357
|
-
|
1313
|
+
Active Record logs will also include timing info for the duration of how long
|
1314
|
+
the main thread had to wait to access the result. This timing is useful to know
|
1315
|
+
whether or not it's worth to load the query asynchronously.
|
1358
1316
|
|
1359
|
-
|
1360
|
-
|
1317
|
+
```
|
1318
|
+
DEBUG -- : Category Load (62.1ms) SELECT * FROM `categories` LIMIT 50
|
1319
|
+
DEBUG -- : ASYNC Post Load (64ms) (db time 126.1ms) SELECT * FROM `posts` LIMIT 100
|
1320
|
+
```
|
1361
1321
|
|
1362
|
-
|
1322
|
+
The duration in the first set of parens is how long the main thread was blocked
|
1323
|
+
waiting for the results, and the second set of parens with "db time" is how long
|
1324
|
+
the entire query took to execute.
|
1363
1325
|
|
1364
|
-
*
|
1326
|
+
*Jean Boussier*
|
1365
1327
|
|
1366
|
-
|
1328
|
+
* Implemented `ActiveRecord::Relation#excluding` method.
|
1367
1329
|
|
1368
|
-
|
1330
|
+
This method excludes the specified record (or collection of records) from
|
1331
|
+
the resulting relation:
|
1369
1332
|
|
1370
1333
|
```ruby
|
1371
|
-
|
1372
|
-
|
1334
|
+
Post.excluding(post)
|
1335
|
+
Post.excluding(post_one, post_two)
|
1373
1336
|
```
|
1374
1337
|
|
1375
|
-
|
1338
|
+
Also works on associations:
|
1376
1339
|
|
1377
1340
|
```ruby
|
1378
|
-
|
1379
|
-
|
1380
|
-
db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(@db_config.env_name, @db_config.spec_name, config)
|
1341
|
+
post.comments.excluding(comment)
|
1342
|
+
post.comments.excluding(comment_one, comment_two)
|
1381
1343
|
```
|
1382
1344
|
|
1383
|
-
|
1384
|
-
|
1385
|
-
* Remove `:connection_id` from the `sql.active_record` notification.
|
1345
|
+
This is short-hand for `Post.where.not(id: post.id)` (for a single record)
|
1346
|
+
and `Post.where.not(id: [post_one.id, post_two.id])` (for a collection).
|
1386
1347
|
|
1387
|
-
*
|
1348
|
+
*Glen Crawford*
|
1388
1349
|
|
1389
|
-
*
|
1350
|
+
* Skip optimised #exist? query when #include? is called on a relation
|
1351
|
+
with a having clause.
|
1390
1352
|
|
1391
|
-
|
1353
|
+
Relations that have aliased select values AND a having clause that
|
1354
|
+
references an aliased select value would generate an error when
|
1355
|
+
#include? was called, due to an optimisation that would generate
|
1356
|
+
call #exists? on the relation instead, which effectively alters
|
1357
|
+
the select values of the query (and thus removes the aliased select
|
1358
|
+
values), but leaves the having clause intact. Because the having
|
1359
|
+
clause is then referencing an aliased column that is no longer
|
1360
|
+
present in the simplified query, an ActiveRecord::InvalidStatement
|
1361
|
+
error was raised.
|
1392
1362
|
|
1393
|
-
|
1363
|
+
A sample query affected by this problem:
|
1394
1364
|
|
1395
|
-
|
1396
|
-
|
1365
|
+
```ruby
|
1366
|
+
Author.select('COUNT(*) as total_posts', 'authors.*')
|
1367
|
+
.joins(:posts)
|
1368
|
+
.group(:id)
|
1369
|
+
.having('total_posts > 2')
|
1370
|
+
.include?(Author.first)
|
1371
|
+
```
|
1397
1372
|
|
1398
|
-
This change
|
1399
|
-
|
1373
|
+
This change adds an addition check to the condition that skips the
|
1374
|
+
simplified #exists? query, which simply checks for the presence of
|
1375
|
+
a having clause.
|
1400
1376
|
|
1401
|
-
|
1377
|
+
Fixes #41417.
|
1402
1378
|
|
1403
|
-
*
|
1379
|
+
*Michael Smart*
|
1404
1380
|
|
1405
|
-
|
1381
|
+
* Increment postgres prepared statement counter before making a prepared statement, so if the statement is aborted
|
1382
|
+
without Rails knowledge (e.g., if app gets killed during long-running query or due to Rack::Timeout), app won't end
|
1383
|
+
up in perpetual crash state for being inconsistent with PostgreSQL.
|
1406
1384
|
|
1407
|
-
*
|
1385
|
+
*wbharding*, *Martin Tepper*
|
1408
1386
|
|
1409
|
-
|
1387
|
+
* Add ability to apply `scoping` to `all_queries`.
|
1410
1388
|
|
1411
|
-
|
1389
|
+
Some applications may want to use the `scoping` method but previously it only
|
1390
|
+
worked on certain types of queries. This change allows the `scoping` method to apply
|
1391
|
+
to all queries for a model in a block.
|
1412
1392
|
|
1413
|
-
|
1393
|
+
```ruby
|
1394
|
+
Post.where(blog_id: post.blog_id).scoping(all_queries: true) do
|
1395
|
+
post.update(title: "a post title") # adds `posts.blog_id = 1` to the query
|
1396
|
+
end
|
1397
|
+
```
|
1414
1398
|
|
1415
|
-
*Eileen M. Uchitelle
|
1399
|
+
*Eileen M. Uchitelle*
|
1416
1400
|
|
1417
|
-
*
|
1401
|
+
* `ActiveRecord::Calculations.calculate` called with `:average`
|
1402
|
+
(aliased as `ActiveRecord::Calculations.average`) will now use column-based
|
1403
|
+
type casting. This means that floating-point number columns will now be
|
1404
|
+
aggregated as `Float` and decimal columns will be aggregated as `BigDecimal`.
|
1418
1405
|
|
1419
|
-
|
1406
|
+
Integers are handled as a special case returning `BigDecimal` always
|
1407
|
+
(this was the case before already).
|
1420
1408
|
|
1421
|
-
|
1409
|
+
```ruby
|
1410
|
+
# With the following schema:
|
1411
|
+
create_table "measurements" do |t|
|
1412
|
+
t.float "temperature"
|
1413
|
+
end
|
1422
1414
|
|
1423
|
-
|
1415
|
+
# Before:
|
1416
|
+
Measurement.average(:temperature).class
|
1417
|
+
# => BigDecimal
|
1424
1418
|
|
1425
|
-
|
1419
|
+
# After:
|
1420
|
+
Measurement.average(:temperature).class
|
1421
|
+
# => Float
|
1422
|
+
```
|
1426
1423
|
|
1427
|
-
|
1424
|
+
Before this change, Rails just called `to_d` on average aggregates from the
|
1425
|
+
database adapter. This is not the case anymore. If you relied on that kind
|
1426
|
+
of magic, you now need to register your own `ActiveRecord::Type`
|
1427
|
+
(see `ActiveRecord::Attributes::ClassMethods` for documentation).
|
1428
1428
|
|
1429
|
-
*
|
1429
|
+
*Josua Schmid*
|
1430
1430
|
|
1431
|
-
|
1431
|
+
* PostgreSQL: introduce `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type`.
|
1432
1432
|
|
1433
|
-
|
1433
|
+
This setting controls what native type Active Record should use when you call `datetime` in
|
1434
|
+
a migration or schema. It takes a symbol which must correspond to one of the configured
|
1435
|
+
`NATIVE_DATABASE_TYPES`. The default is `:timestamp`, meaning `t.datetime` in a migration
|
1436
|
+
will create a "timestamp without time zone" column. To use "timestamp with time zone",
|
1437
|
+
change this to `:timestamptz` in an initializer.
|
1434
1438
|
|
1435
|
-
|
1439
|
+
You should run `bin/rails db:migrate` to rebuild your schema.rb if you change this.
|
1436
1440
|
|
1437
|
-
*
|
1441
|
+
*Alex Ghiculescu*
|
1438
1442
|
|
1439
|
-
|
1443
|
+
* PostgreSQL: handle `timestamp with time zone` columns correctly in `schema.rb`.
|
1440
1444
|
|
1441
|
-
|
1445
|
+
Previously they dumped as `t.datetime :column_name`, now they dump as `t.timestamptz :column_name`,
|
1446
|
+
and are created as `timestamptz` columns when the schema is loaded.
|
1442
1447
|
|
1443
|
-
*
|
1448
|
+
*Alex Ghiculescu*
|
1444
1449
|
|
1445
|
-
*
|
1446
|
-
|
1450
|
+
* Removing trailing whitespace when matching columns in
|
1451
|
+
`ActiveRecord::Sanitization.disallow_raw_sql!`.
|
1447
1452
|
|
1448
|
-
|
1453
|
+
*Gannon McGibbon*, *Adrian Hirt*
|
1449
1454
|
|
1450
|
-
|
1451
|
-
has_secure_token :auth_token
|
1452
|
-
```
|
1455
|
+
* Expose a way for applications to set a `primary_abstract_class`.
|
1453
1456
|
|
1454
|
-
|
1457
|
+
Multiple database applications that use a primary abstract class that is not
|
1458
|
+
named `ApplicationRecord` can now set a specific class to be the `primary_abstract_class`.
|
1455
1459
|
|
1456
1460
|
```ruby
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1461
|
+
class PrimaryApplicationRecord
|
1462
|
+
self.primary_abstract_class
|
1463
|
+
end
|
1460
1464
|
```
|
1461
1465
|
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1466
|
+
When an application boots it automatically connects to the primary or first database in the
|
1467
|
+
database configuration file. In a multiple database application that then call `connects_to`
|
1468
|
+
needs to know that the default connection is the same as the `ApplicationRecord` connection.
|
1469
|
+
However, some applications have a differently named `ApplicationRecord`. This prevents Active
|
1470
|
+
Record from opening duplicate connections to the same database.
|
1465
1471
|
|
1466
|
-
*Eileen Uchitelle*, *John Crepezzi*
|
1472
|
+
*Eileen M. Uchitelle*, *John Crepezzi*
|
1467
1473
|
|
1468
|
-
*
|
1474
|
+
* Support hash config for `structure_dump_flags` and `structure_load_flags` flags.
|
1475
|
+
Now that Active Record supports multiple databases configuration,
|
1476
|
+
we need a way to pass specific flags for dump/load databases since
|
1477
|
+
the options are not the same for different adapters.
|
1478
|
+
We can use in the original way:
|
1469
1479
|
|
1470
|
-
|
1480
|
+
```ruby
|
1481
|
+
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = ['--no-defaults', '--skip-add-drop-table']
|
1482
|
+
# or
|
1483
|
+
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = '--no-defaults --skip-add-drop-table'
|
1484
|
+
```
|
1471
1485
|
|
1472
|
-
|
1486
|
+
And also use it passing a hash, with one or more keys, where the key
|
1487
|
+
is the adapter
|
1473
1488
|
|
1474
|
-
|
1489
|
+
```ruby
|
1490
|
+
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = {
|
1491
|
+
mysql2: ['--no-defaults', '--skip-add-drop-table'],
|
1492
|
+
postgres: '--no-tablespaces'
|
1493
|
+
}
|
1494
|
+
```
|
1475
1495
|
|
1476
|
-
|
1496
|
+
*Gustavo Gonzalez*
|
1477
1497
|
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1498
|
+
* Connection specification now passes the "url" key as a configuration for the
|
1499
|
+
adapter if the "url" protocol is "jdbc", "http", or "https". Previously only
|
1500
|
+
urls with the "jdbc" prefix were passed to the Active Record Adapter, others
|
1501
|
+
are assumed to be adapter specification urls.
|
1482
1502
|
|
1483
|
-
|
1503
|
+
Fixes #41137.
|
1484
1504
|
|
1485
|
-
|
1505
|
+
*Jonathan Bracy*
|
1486
1506
|
|
1487
|
-
|
1507
|
+
* Allow to opt-out of `strict_loading` mode on a per-record base.
|
1488
1508
|
|
1489
|
-
|
1509
|
+
This is useful when strict loading is enabled application wide or on a
|
1510
|
+
model level.
|
1490
1511
|
|
1491
|
-
|
1512
|
+
```ruby
|
1513
|
+
class User < ApplicationRecord
|
1514
|
+
has_many :bookmarks
|
1515
|
+
has_many :articles, strict_loading: true
|
1516
|
+
end
|
1492
1517
|
|
1493
|
-
|
1518
|
+
user = User.first
|
1519
|
+
user.articles # => ActiveRecord::StrictLoadingViolationError
|
1520
|
+
user.bookmarks # => #<ActiveRecord::Associations::CollectionProxy>
|
1494
1521
|
|
1495
|
-
|
1522
|
+
user.strict_loading!(true) # => true
|
1523
|
+
user.bookmarks # => ActiveRecord::StrictLoadingViolationError
|
1496
1524
|
|
1497
|
-
|
1525
|
+
user.strict_loading!(false) # => false
|
1526
|
+
user.bookmarks # => #<ActiveRecord::Associations::CollectionProxy>
|
1527
|
+
user.articles.strict_loading!(false) # => #<ActiveRecord::Associations::CollectionProxy>
|
1528
|
+
```
|
1498
1529
|
|
1499
|
-
|
1530
|
+
*Ayrton De Craene*
|
1500
1531
|
|
1501
|
-
|
1532
|
+
* Add `FinderMethods#sole` and `#find_sole_by` to find and assert the
|
1533
|
+
presence of exactly one record.
|
1502
1534
|
|
1503
|
-
|
1535
|
+
Used when you need a single row, but also want to assert that there aren't
|
1536
|
+
multiple rows matching the condition; especially for when database
|
1537
|
+
constraints aren't enough or are impractical.
|
1504
1538
|
|
1505
|
-
|
1539
|
+
```ruby
|
1540
|
+
Product.where(["price = %?", price]).sole
|
1541
|
+
# => ActiveRecord::RecordNotFound (if no Product with given price)
|
1542
|
+
# => #<Product ...> (if one Product with given price)
|
1543
|
+
# => ActiveRecord::SoleRecordExceeded (if more than one Product with given price)
|
1506
1544
|
|
1507
|
-
|
1545
|
+
user.api_keys.find_sole_by(key: key)
|
1546
|
+
# as above
|
1547
|
+
```
|
1508
1548
|
|
1509
|
-
*
|
1549
|
+
*Asherah Connor*
|
1510
1550
|
|
1511
|
-
|
1551
|
+
* Makes `ActiveRecord::AttributeMethods::Query` respect the getter overrides defined in the model.
|
1512
1552
|
|
1513
|
-
|
1553
|
+
Before:
|
1514
1554
|
|
1515
|
-
|
1555
|
+
```ruby
|
1556
|
+
class User
|
1557
|
+
def admin
|
1558
|
+
false # Overriding the getter to always return false
|
1559
|
+
end
|
1560
|
+
end
|
1516
1561
|
|
1517
|
-
|
1562
|
+
user = User.first
|
1563
|
+
user.update(admin: true)
|
1518
1564
|
|
1519
|
-
|
1565
|
+
user.admin # false (as expected, due to the getter overwrite)
|
1566
|
+
user.admin? # true (not expected, returned the DB column value)
|
1567
|
+
```
|
1520
1568
|
|
1521
|
-
|
1569
|
+
After this commit, `user.admin?` above returns false, as expected.
|
1522
1570
|
|
1523
|
-
|
1571
|
+
Fixes #40771.
|
1524
1572
|
|
1525
|
-
*
|
1573
|
+
*Felipe*
|
1526
1574
|
|
1527
|
-
|
1575
|
+
* Allow delegated_type to be specified primary_key and foreign_key.
|
1528
1576
|
|
1529
|
-
|
1577
|
+
Since delegated_type assumes that the foreign_key ends with `_id`,
|
1578
|
+
`singular_id` defined by it does not work when the foreign_key does
|
1579
|
+
not end with `id`. This change fixes it by taking into account
|
1580
|
+
`primary_key` and `foreign_key` in the options.
|
1530
1581
|
|
1531
|
-
*
|
1582
|
+
*Ryota Egusa*
|
1532
1583
|
|
1533
|
-
*
|
1584
|
+
* Expose an `invert_where` method that will invert all scope conditions.
|
1534
1585
|
|
1535
|
-
|
1586
|
+
```ruby
|
1587
|
+
class User
|
1588
|
+
scope :active, -> { where(accepted: true, locked: false) }
|
1589
|
+
end
|
1536
1590
|
|
1537
|
-
|
1591
|
+
User.active
|
1592
|
+
# ... WHERE `accepted` = 1 AND `locked` = 0
|
1538
1593
|
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1594
|
+
User.active.invert_where
|
1595
|
+
# ... WHERE NOT (`accepted` = 1 AND `locked` = 0)
|
1596
|
+
```
|
1542
1597
|
|
1543
|
-
*
|
1598
|
+
*Kevin Deisz*
|
1544
1599
|
|
1545
|
-
*
|
1600
|
+
* Restore possibility of passing `false` to :polymorphic option of `belongs_to`.
|
1546
1601
|
|
1547
|
-
|
1602
|
+
Previously, passing `false` would trigger the option validation logic
|
1603
|
+
to throw an error saying :polymorphic would not be a valid option.
|
1548
1604
|
|
1549
|
-
*
|
1605
|
+
*glaszig*
|
1550
1606
|
|
1551
|
-
|
1607
|
+
* Remove deprecated `database` kwarg from `connected_to`.
|
1552
1608
|
|
1553
|
-
*
|
1609
|
+
*Eileen M. Uchitelle*, *John Crepezzi*
|
1554
1610
|
|
1555
|
-
|
1611
|
+
* Allow adding nonnamed expression indexes to be revertible.
|
1556
1612
|
|
1557
|
-
|
1613
|
+
Previously, the following code would raise an error, when executed while rolling back,
|
1614
|
+
and the index name should be specified explicitly. Now, the index name is inferred
|
1615
|
+
automatically.
|
1558
1616
|
|
1559
|
-
|
1617
|
+
```ruby
|
1618
|
+
add_index(:items, "to_tsvector('english', description)")
|
1619
|
+
```
|
1560
1620
|
|
1561
|
-
|
1621
|
+
Fixes #40732.
|
1562
1622
|
|
1563
|
-
|
1623
|
+
*fatkodima*
|
1564
1624
|
|
1565
|
-
|
1625
|
+
* Only warn about negative enums if a positive form that would cause conflicts exists.
|
1566
1626
|
|
1567
|
-
|
1627
|
+
Fixes #39065.
|
1568
1628
|
|
1569
|
-
|
1629
|
+
*Alex Ghiculescu*
|
1570
1630
|
|
1571
|
-
|
1631
|
+
* Add option to run `default_scope` on all queries.
|
1572
1632
|
|
1573
|
-
|
1633
|
+
Previously, a `default_scope` would only run on select or insert queries. In some cases, like non-Rails tenant sharding solutions, it may be desirable to run `default_scope` on all queries in order to ensure queries are including a foreign key for the shard (i.e. `blog_id`).
|
1574
1634
|
|
1575
|
-
|
1635
|
+
Now applications can add an option to run on all queries including select, insert, delete, and update by adding an `all_queries` option to the default scope definition.
|
1576
1636
|
|
1577
|
-
|
1637
|
+
```ruby
|
1638
|
+
class Article < ApplicationRecord
|
1639
|
+
default_scope -> { where(blog_id: Current.blog.id) }, all_queries: true
|
1640
|
+
end
|
1641
|
+
```
|
1578
1642
|
|
1579
|
-
*
|
1643
|
+
*Eileen M. Uchitelle*
|
1580
1644
|
|
1581
|
-
*
|
1645
|
+
* Add `where.associated` to check for the presence of an association.
|
1582
1646
|
|
1583
|
-
|
1647
|
+
```ruby
|
1648
|
+
# Before:
|
1649
|
+
account.users.joins(:contact).where.not(contact_id: nil)
|
1584
1650
|
|
1585
|
-
|
1651
|
+
# After:
|
1652
|
+
account.users.where.associated(:contact)
|
1653
|
+
```
|
1586
1654
|
|
1587
|
-
|
1655
|
+
Also mirrors `where.missing`.
|
1588
1656
|
|
1589
|
-
*
|
1657
|
+
*Kasper Timm Hansen*
|
1590
1658
|
|
1591
|
-
* Allow
|
1659
|
+
* Allow constructors (`build_association` and `create_association`) on
|
1660
|
+
`has_one :through` associations.
|
1592
1661
|
|
1593
|
-
*
|
1662
|
+
*Santiago Perez Perret*
|
1594
1663
|
|
1595
1664
|
|
1596
|
-
Please check [6-
|
1665
|
+
Please check [6-1-stable](https://github.com/rails/rails/blob/6-1-stable/activerecord/CHANGELOG.md) for previous changes.
|