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