Commit Graph

12484 Commits

Author SHA1 Message Date
Xavier Noria
32c5a358bb Delete the deprecated constant ActiveRecord::ImmutableRelation 2024-07-13 12:51:40 +02:00
Xavier Noria
1e2c260284 Deprecate ActiveRecord::ImmutableRelation 2024-07-13 12:27:21 +02:00
Yasuo Honda
86d3ea1089
Merge pull request #52306 from fatkodima/fix-flaky-instrumentation-test
Fix a flaky Active Record instrumentation test
2024-07-11 21:33:03 +09:00
fatkodima
182988b41f Fix a flaky Active Record instrumentation test 2024-07-11 11:35:27 +03:00
Joshua Young
38c1d5a70b [Fix #52304] Avoid computing klass if reflection is a belongs_to in ActiveRecord::AutosaveAssociation#inverse_belongs_to_association_for 2024-07-11 13:09:07 +05:30
Jean Boussier
16f1222753
Merge pull request #49847 from joshuay03/duplicate-callbacks-when-child-autosaves-parent
[Fix #48688] Duplicate callback execution when child autosaves parent with `has_one` and `belongs_to`
2024-07-08 19:00:47 +02:00
Jean Boussier
78ce2994b2
Merge pull request #51948 from justinko/issue-51938
Restore inferred association class with the same modularized name
2024-07-08 17:53:03 +02:00
Joshua Young
154f4a4ba9 [Fix 48688] Duplicate callback execution when child autosaves parent with has_one and belongs_to 2024-07-07 11:03:45 +05:30
fatkodima
2410312733 Skip triggering "instantiation.active_record" notification when there are no records 2024-07-04 21:43:46 +03:00
heka1024
75421601ce Introduce compressor option to ActiveRecord::Encryption::Encryptor 2024-07-03 18:48:07 +09:00
Hartley McGuire
5c8172554f
Add condensed #inspect for Pool, Adapter, Config
Previously, it was very easy to accidentally leak a database password in
production logs if an error ends up calling inspect on a ConnectionPool
or an individual connection (Adapter). This is due to the default
`#inspect` output for Pools and Adapters being unnecessarily large, and
both currently including passwords (through the DatabaseConfig of a
Pool, and the internal configuration of an Adapter).

This commit addresses these issues by defining a custom `#inspect` for
ConnectionPool, AbstractAdapter, and DatabaseConfig. The condensed
`#inspect` only includes a few valuable fields instead of all of the
internals, which prevents both the large output and passwords from being
included.
2024-06-21 21:23:04 +00:00
fatkodima
d11a5207be
Merge pull request #51993 from wonda-tea-coffee/fix-test-title-mismatch
Fix test title mismatch
2024-06-19 18:10:46 +03:00
fatkodima
5dd2da7ee8 Fix ActiveRecord::Relation#exists? with no conditions for loaded relations and updated records 2024-06-19 12:15:20 +03:00
Eileen M. Uchitelle
ff0ef93e28
Merge pull request #51009 from HeyNonster/nony--add-on-all-shards
Add `.shard_keys`, `.sharded?`, & `.connected_to_all_shards` methods to AR Models
2024-06-18 05:45:23 -07:00
Jean Boussier
403743ed88 Fix alias_attribute to ignore methods defined in parent classes
Fix: https://github.com/rails/rails/issues/52144

When defining regular attributes, inherited methods aren't overriden,
however when defining aliased attributes, inherited methods aren't
considered.

This behavior could be debatted, but that was the behavior prior
to https://github.com/rails/rails/pull/52118, so I'm restoring it.
2024-06-18 09:44:31 +02:00
Nony Dutton
77cf5e6d92 Add .shard_keys & .connected_to_all_shards
Currently, there is no (simple) way to ask a model if it connects to a
single database or to multiple shards. Furthermore, without looping
through a model's connections, I don't believe there's an easy way to
return a list of shards a model can connect to.

This commit adds a `@shard_keys` ivar that's set whenever `.connects_to`
is called. It sets the ivar to the result of `shards.keys`. `shards` in
`.connects_to` defaults to an empty hash and therefore when calling
`connects_to database: {...}` `@shard_keys` will be set to an empty array.

`@shard_keys` is set _before_ the following lines:

```
if shards.empty?
  shards[:default] = database
end
```

This conditional sets the one and only shard (`:default`) to the value of `database`
that we pass to `.connects_to`. This allows for calling
`connected_to(shard: :default)` on models configured to only connect to
a database e.g.:

```ruby
class UnshardedBase < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary }
end

class UnshardedModel < UnshardedBase
end

UnshardedBase.connected_to(shard: :default) {
UnshardedBase.connection_pool.db_config.name } => primary
```

This is ultimately still an _unsharded_ model which is why `@shard_keys`
gets set before the conditional.

With the new `@shard_keys` ivar we need a way for descendants of the
abstract AR model to return that same value. For that we leverage the
existing `.connection_class_for_self` method. That method returns the
ancestor of the model where `.connects_to` was called, or returns self if
it's the connection class:

```ruby
class UnshardedBase < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary }
end

class UnshardedModel < UnshardedBase
end

ActiveRecord::Base.connection_class_for_self => ActiveRecord::Base

UnshardedBase.connection_class_for_self => UnshardedBase(abstract)

UnshardedModel.connection_class_for_self => UnshardedBase(abstract)
```

The new `.shard_keys` method is a getter which returns the value of
`@shard_keys` from the connection class or it returns an empty array.
The empty array is necessary in cases where `connects_to` was never
called.

Finally, I've added an `.connected_to_all_shards` method which takes all of the
arguments for `.connected_to` except for `shard`. Instead, it loops through
every shard key and then delegates everything else to `.connected_to`. I've
used `.map` instead of `.each` so that we can collect the results of each block.
2024-06-17 19:54:25 +02:00
fatkodima
9e85d04e2e Optimize ActiveRecord::Relation#exists? with no conditions for loaded relations 2024-06-14 13:49:11 +02:00
Rafael Mendonça França
f5355a21ae
Merge pull request #51219 from mylesboone/order_references_arel_attribute
[Fix #49999] properly reference from Arel::Attribute args
2024-06-13 15:40:46 -04:00
Jean Boussier
514d474836 Fix a performance regression in attribute methods
Fix: #52111
Fix: 5dbc7b4

The above commit caused the size of the `CodeGenerator` method cache
to explode, because the dynamic namespace is way too granular.

But there is actually a much better fix for that, since `alias_attribute`
is now generating exactly the same code as the attribute it's aliasing,
we can generated it as the canonical method in the cache, and then just
define it in the model as the aliased name.

This prevent the cache from growing a lot, and even reduce memory
usage further as the original attribute and its alias now share
the same method cache.
2024-06-13 17:50:11 +02:00
Jean Boussier
884af53be2 ActiveRecord::Transaction#open? returns false if the transaction is finalized
Followup: https://github.com/rails/rails/pull/52104
2024-06-13 10:55:55 +02:00
Matthew Draper
fadb6830f8 Harden the .current_transaction API
Based on https://github.com/rails/rails/pull/52017

One concern raised by Xavier is users holding on the return value
of `.current_transaction` beyond the point where it is committed /
rolled back / invalidated.

I believe this is an invalid use of the API, just like holding
`ActiveRecord::Base.connection` beyond the scope of a request is.

However we can be more explicit about it, so I changed the callback
registration methods to raise an error when called on a finalized
transaction.

Another concern was the usability of the null-object in the Active
Record notification payloads, and I agree that while the null-object
make sense when calling `Model.current_transaction`, it doesn't make
sense to include it in the payload of events. The goal of the
`.current_transaction` API is to allow implementing transaction aware
code in a streamlined way. The goal of the `:transaction` in events
however it to allow logging whether a query was inside a transaction
or not, so it's much more ergonomic for it to be nilable.
So I kept Matthew's change that passes `transaction: nil` in `sql.active_record` events
when not inside a transaction. I also added test coverage to make
sure it behaves consistently whether we're inside a transactional
test or not.

I also kept the separation between internal and "user" transaction
objects, as I think it's a nice way to limit the effectively exposed
API, and prevent users from abusing that API too much.

Co-Authored-By: Jean Boussier <jean.boussier@gmail.com>
2024-06-13 09:34:58 +02:00
Myles Boone
e0c2363d9a properly reference tables from Arel in #order 2024-06-13 00:16:32 -04:00
Igor Depolli
00f38563a4
[ActiveRecord] Add option filter on in_order_of (#51761)
* Add option  on

* Add CHANGELOG and fix method doc

* Rename option to 'filter' and fix grammar

* Adjust filter attribute

* Fix typo and solve conflict

* Update activerecord/test/cases/relation/field_ordered_values_test.rb

Co-authored-by: Timo Schilling <timo@schilling.io>

---------

Co-authored-by: Timo Schilling <timo@schilling.io>
Co-authored-by: Rafael Mendonça França <rafael@rubyonrails.org>
2024-06-12 15:14:06 -07:00
Rafael Mendonça França
445529cc3a
Merge pull request #52054 from justinko/issue-52000
Document TestFixtures#fixture
2024-06-12 16:59:19 -04:00
Xavier Noria
f64a4134df Define the new start_transaction.active_record event 2024-06-10 17:30:37 +02:00
Justin Ko
285d4c812c Document TestFixtures#fixture
Fixes #52000

Co-Authored-By: Hartley McGuire <skipkayhil@gmail.com>
2024-06-09 10:24:27 -06:00
Vladimir Dementyev
e44cdcb7a7 feat: support nested connection pinning 2024-06-08 09:16:03 +02:00
Eileen M. Uchitelle
5dabff4b7b
Merge pull request #51167 from Jay0921/fix-preload-ids-reader-composite-pk
[Fix #51129] Fix issue with IDs reader on preloaded associations for composite primary keys
2024-06-05 07:55:08 -07:00
Carlos Antonio da Silva
d36c752287 Add ensure to reset table name in tests 2024-06-05 11:03:18 -03:00
Yasuo Honda
a6a840f150
Merge pull request #51969 from npezza93/insert-all-fix
Parameterize table_name when constructing insert alias to avoid syntax error when table_name contains the database name for mysql
2024-06-05 08:32:23 +09:00
Jay Ang
3e371c6dd6 Fix issue with IDs reader on preloaded associations for composite primary keys 2024-06-04 22:13:05 +08:00
Nick Pezza
7f4b396f4c
Parameterize table_name when constructing insert alias to avoid syntax error when table_name contains the database name 2024-06-04 09:26:49 -04:00
maniSHarma7575
a35074e6f1 Fix test_pluck_functions_without_alias 2024-06-04 12:07:08 +05:30
maniSHarma7575
d7c02de1a3 Minimum in test case should be picked after conditions 2024-06-04 11:47:47 +05:30
maniSHarma7575
0077a167f6 [FIX] Pluck columns should correctly casts types when using postgresql 2024-06-04 11:37:04 +05:30
Xavier Noria
4a4ed0fb0e Pass transaction: nil in sql.active_record events if no transaction is open 2024-06-03 17:36:20 +02:00
Jean Boussier
0f3dbe2f97
Merge pull request #52005 from Shopify/transaction-blank
Fix `ActiveRecord::Transaction#blank?`
2024-06-03 15:12:59 +02:00
Jean Boussier
570530996a Fix ActiveRecord::Transaction#blank?
Followup: https://github.com/rails/rails/pull/51968

The wrong method was aliased.
2024-06-03 15:04:41 +02:00
Garen J. Torikian
5ff9915db9
Allow one to set strict_loading_mode globally 2024-06-02 14:38:38 -05:00
wonda-tea-coffee
8936dcf03d Fix test title mismatch
Following up 317465a4a8f8c0ebaf9534921350c4540584c03c.
2024-06-02 10:57:55 +09:00
Jean Boussier
3b247873e2
Revert "Optimize ActiveRecord::Relation#exists? with no conditions for loaded relations" 2024-06-01 10:35:22 +02:00
Jean Boussier
0378b056de
Merge pull request #51966 from fatkodima/exists-and-loaded
Optimize `ActiveRecord::Relation#exists?` with no conditions for loaded relations
2024-06-01 10:26:22 +02:00
Xavier Noria
c16d552c32 Include the current transaction in sql.active_record event payloads 2024-05-31 13:05:20 +02:00
fatkodima
6cab6744e8 Optimize ActiveRecord::Relation#exists? with no conditions for loaded relations 2024-05-31 14:04:34 +03:00
Jean Boussier
f6666de591
Merge pull request #51941 from Liamjen/allow_unscope_cte
Allow unscoping ActiveRecord Query CTEs
2024-05-31 11:10:36 +02:00
Xavier Noria
ea0f0a2c96 Pass the transaction to the transaction.active_record event 2024-05-30 20:47:43 +02:00
Liamjen
07b1f2293f Allow unscoping CTEs 2024-05-30 08:15:25 -07:00
Xavier Noria
23df2aabbe Lazily generate a UUID for AR transactions 2024-05-30 13:51:17 +02:00
Justin Ko
939a8f665f Restore inferred association class with same modularized name
Fixes #51938
2024-05-30 03:46:51 -06:00
Rafael Mendonça França
e46e56b1f4
Merge pull request #51892 from florin555/fix_pretty_print
Make `pretty_print` behave more similar to `inspect`.
2024-05-28 18:00:40 -04:00