Commit Graph

23780 Commits

Author SHA1 Message Date
Hana Harencarova
0bdf44d921 Raise specific exception when a connection is not defined
Co-authored-by: Matthew Draper <matthewd@github.com>
2024-07-16 19:53:46 +00:00
Joshua Young
9920ad3ef4 [Fix #52332] Handle record changed case in #save_has_one_association's inverse belongs_to association updated check 2024-07-15 09:17:44 +05:30
Ryuta Kamizono
f74dc0d8aa
Merge pull request #52322 from fatkodima/fix-loaded-relation-batching
Fix loaded relation batching with limits and reverse order
2024-07-14 14:36:34 +09:00
nisusam
bb33c5c902 Fix typo from tests [ci skip] 2024-07-13 23:35:09 +05:30
Xavier Noria
32c5a358bb Delete the deprecated constant ActiveRecord::ImmutableRelation 2024-07-13 12:51:40 +02:00
fatkodima
d21f95940c Fix loaded relation batching with limits and reverse order 2024-07-13 13:42:52 +03: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
eileencodes
4fa56814f1
fix newlines
New lines for these were off, only the new options were put on a new
line so the code looked awkward. I'd rather just oneline it.
2024-07-03 08:41:48 -04:00
Eileen M. Uchitelle
71e14aa7e9
Merge pull request #51735 from heka1024/encryption-compressor
Introduce `compressor` option to `ActiveRecord::Encryption::Encryptor`
2024-07-03 08:39:13 -04:00
heka1024
75421601ce Introduce compressor option to ActiveRecord::Encryption::Encryptor 2024-07-03 18:48:07 +09:00
fatkodima
803c78d9f4 Fix a deadlock in ConnectionPool#checkout 2024-06-29 02:35:16 +03:00
Xavier Noria
b9d6759401 Style pass over active_record/associations/builder/association.rb
I was reading this source code and made a few edits on passing to ensure
style details agree within the file itself and Rails.

The addition of "# noop" makes the method body look similar to other
analogous ones in the same file (not shown in the diff).
2024-06-28 18:35:13 +02:00
Rafael Mendonça França
f46d06b3b5
Merge pull request #52031 from matthewd/quieter-to_time
Don't emit to_time deprecations in known-safe contexts
2024-06-26 16:09:59 -04:00
heka1024
45c4802459 Remove unused constant in MySQLDatabaseTasks 2024-06-26 16:43:00 +09:00
Janosch Müller
03a1af9b9b
Fix ActiveRecord dirty docs 2024-06-22 21:30:51 +02: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
Jean Boussier
16d8b82d5e
Merge pull request #52161 from rubys/rubocop-empty-scaffolding
make "g scaffold" with no field produce rubocop compliant code
2024-06-21 08:20:17 +02:00
Sam Ruby
8eafbc1b19 make "g scaffold" with no field produce rubocop compliant code
When there are no fields:
  * Omit blank line in migration prior to "t.timestamps"
  * Omit leading and trailing spaced in empty hashes in
    create and update controller and api functional tests

Co-authored-by: zzak <zzakscott@gmail.com>
2024-06-20 22:11:09 -04:00
Xavier Noria
13f4d0f624 Fixes typo 2024-06-20 08:30:40 +02: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
Xavier Noria
0ec6f23197
Update activerecord/lib/active_record/transaction.rb
Co-authored-by: Jean byroot Boussier <jean.boussier+github@shopify.com>
2024-06-18 13:02:32 +02:00
Xavier Noria
f8ee9ebb66 Iterate the docs of ActiveRecord::Transaction 2024-06-18 13:01:55 +02: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
ae62d08e92 Fix more with_connection offences inside Active Record 2024-06-16 11:36:09 +03: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
Rafael Mendonça França
827f4ef15c
Merge pull request #52059 from fatkodima/move-associations-errors-to-errors
Explicitly load Active Record associations related errors
2024-06-12 18:21:17 -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
68b6a1cc57
Merge pull request #52064 from feliperaul/improve_message_verifier_and_signed_id_docs
[ci skip] Improve ActiveSupport::MessageVerifier and ActiveRecord::SignedId docs
2024-06-12 18:00:28 -04: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
Felipe
ad20d9e7ec
Improve ActiveSupport::MessageVerifier and ActiveRecord::SignedId docs
The documentation on ActiveSupport::MessageVerifier used the “sensitive data” string as an example; that wording might induce the developer to think we’re dealing with encryption, while the payload is actually only Base64 encoded and is not protected at all.

We also improve the documentation on ActiveRecord::SignedId, which uses MessageVerifier and thereby will also expose the ID as encoded cleartext, making explicit that it’s not encryption, only signing.

Lastly, we refer the developer to MessageEncryptor if the payload needs to be encrypted.
2024-06-12 20:46:27 +00:00
Anatoly Busygin
564b434e82
Fix typo in global_executor_concurrency error message (#52103)
* Fix typo in `global_executor_concurrency` error message

* removed `using` and replace multi_thread_pool with `:multi_thread_pool`

Co-authored-by: Petrik de Heus <petrik@deheus.net>

---------

Co-authored-by: Petrik de Heus <petrik@deheus.net>
Co-authored-by: Rafael Mendonça França <rafael@rubyonrails.org>
2024-06-12 12:53:40 -07:00
Joel Lubrano
247f71bf5b Avoid shadowing block local variable 'configs'
Fix #52098
2024-06-11 23:31:36 -04:00
Matthew Draper
d47c37183a Don't emit to_time deprecations in known-safe contexts
If the value is already a local time, there's no difference, so no need
to warn.

Correspondingly, avoid calling to_time in the handful of places we were
using it internally: it's easy to do, and we know we don't care about
the zone.
2024-06-11 15:51:46 -07: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