Commit Graph

23566 Commits

Author SHA1 Message Date
Fabian Winkler
46c9012196 Rename MoneyType to PriceType 2024-04-11 21:16:10 +02:00
Claire
85c7530a57 Arel: wrap SELECT statements in parentheses when generating SQL for UNION
This commit fixes generated SQL for `UNION` and `UNION ALL` involving `LIMIT`
or `ORDER BY` by wrapping `SELECT` statements appearing in an `UNION` or
`UNION ALL` in parentheses.
2024-04-11 17:55:41 +02:00
Jean Boussier
93df871020
Merge pull request #51540 from vahe/lazily_load_schema_cache-docs-cmment-cleanup
Remove misleading comment related to lazily_load_schema_cache and use_schema_cache_dump
2024-04-11 08:46:44 +02:00
Yasuo Honda
81cbca7569
Merge pull request #51531 from fatkodima/fix-sqlite-copy-virtual-columns
Fix copying virtual columns when altering a table in sqlite3
2024-04-11 08:18:52 +09:00
Vahe Khachikyan
9be9a8183d
Remove misleading comment related to lazily_load_schema_cache and use_schema_cache_dump 2024-04-10 13:19:37 -04:00
fatkodima
041de49399 Remove usage of OpenStruct 2024-04-09 21:35:08 +03:00
fatkodima
a457b121a0 Fix copying virtual columns when altering a table in sqlite3 2024-04-09 20:47:37 +03:00
Carlos Antonio da Silva
8feeab2338 Add missing ostruct require on AR test suite
Similar to 50515fb45f36dfad067adbdda9fee41fcb326ca9, make sure we
require `ostruct` where we use `OpenStruct`, to get the build back to
green, while we work to remove its usage on tests. (see #51510.)

Sample error:

```
Error:
ActiveRecord::Encryption::ConfigurableTest#test_installing_autofiltered_parameters_will_add_the_encrypted_attribute_as_a_filter_parameter_using_the_dot_notation:
NameError: uninitialized constant ActiveRecord::Encryption::ConfigurableTest::OpenStruct
    test/cases/encryption/configurable_test.rb:45:in `block in <class:ConfigurableTest>'
```
2024-04-09 13:56:02 -03:00
Nikita Vasilevsky
ac6217d6fa
Ensure association's foreign_key: and query_constraints: options behave the same 2024-04-09 14:52:17 +00:00
Eileen M. Uchitelle
6f32c7b2e9
Merge pull request #51513 from fatkodima/create_table-force-and-if_not_exists
Raise when both `:force` and `:if_not_exists` provided to `create_table`
2024-04-09 10:14:30 -04:00
fatkodima
e8bf57894e Raise when both :force and :if_not_exists provided to create_table
Co-authored-by: Allison Phillips <aly@thanx.com>
2024-04-08 23:46:48 +03:00
Carlos Antonio da Silva
8ad19d9865 Minor tweaks / improvements to recent changelog/api docs [ci skip] 2024-04-08 14:48:44 -03:00
Petrik
79f0c6504d Fix small typo's in ActiveRecord Changelog [ci-skip] 2024-04-08 15:58:14 +02:00
Jean Boussier
3700aa90e3 Fix a typo in activerecord/CHANGELOG.md 2024-04-08 12:19:29 +02:00
fatkodima
bb779f929f Counter cache columns are not marked as readonly [skip ci] 2024-04-06 12:39:08 +03:00
Ben Sheldon [he/him]
2182419d81
Allow IN with subselect to be preparable 2024-04-04 21:36:39 -07:00
fatkodima
6369e92fbb Add queries count to template rendering instrumentation 2024-04-04 18:25:06 +03:00
Jean Boussier
58158c0f8c
Merge pull request #51478 from kmcphillips/mysql-parse-version-error
Raise named exception in `AbstractMysqlAdapter` when DB reports an invalid version
2024-04-04 17:15:45 +02:00
Jean Boussier
bfcc13ab7c Arel: make Or nodes "Nary" like And
Fix: https://github.com/rails/rails/issues/51386

This significantly reduce the depth of the tree for large `OR`
conditions. I was initially a bit on the fence about that fix,
but given that `And` is already implemented this way, I see no
reasons not to do the same.

Amusingly, the reported repro script now makes SQLite fail:

```ruby
SQLite3::SQLException: Expression tree is too large (maximum depth 1000)
```
2024-04-04 14:59:56 +02:00
Kevin McPhillips
d9995cc00c
Add test for MariaDB 5.5.5- prefix in db version parsing regex. 2024-04-03 16:38:33 -04:00
Kevin McPhillips
869d802c48
Raise a descriptive error if the MySQL adapter fails to parse the version string. 2024-04-03 16:38:33 -04:00
Jean Boussier
c2df237414 Allow to register transaction callbacks outside of a record
Ref: https://github.com/rails/rails/pull/26103
Ref: https://github.com/rails/rails/pull/51426

A fairly common mistake with Rails is to enqueue a job from inside a
transaction, and a record as argumemnt, which then lead to a RecordNotFound
error when picked up by the queue.

This is even one of the arguments advanced for job runners backed by the
database such as `solid_queue`, `delayed_job` or `good_job`. But relying
on this is undesirable iin my opinion as it makes the Active Job abstraction
leaky, and if in the future you need to migrate to another backend or even
just move the queue to a separate database, you may experience a lot of race
conditions of the sort.

But more generally, being able to defer work to after the current transaction
has been a missing feature of Active Record. Right now the only way to do it
is from a model callback, and this forces moving things in Active Record
models that sometimes are better done elsewhere. Even as a self-proclaimed
"service object skeptic", I often wanted this capability over the last decade,
and I'm sure it got asked or desired by many more people.

Also there's some 3rd party gems adding this capability using monkey patches.
It's not a reason to upstream the capability, but it's a proof that there is
demand for it.

Implementation wise, this proof of concept shows that it's not really hard to
implement, even with nested multi-db transactions support.

Co-Authored-By: Cristian Bica <cristian.bica@gmail.com>
2024-04-03 14:26:10 +02:00
Jean Boussier
eac95d531d Fix the ActionRecord typo 2024-04-03 09:32:41 +02:00
fatkodima
e79455f3d4 Add the ability to ignore counter cache columns while they are backfilling 2024-04-02 13:59:46 +03:00
Jean Boussier
d6ec8dbc48 Suggest inverse_of: nil instead of false
Followup: https://github.com/rails/rails/pull/50883
Ref: https://github.com/rails/rails/pull/50284#issuecomment-2027722175
2024-03-30 09:53:38 +01:00
Feng Ce
c1c07b096b Remove delegate_missing_to in FutureResult class and add test cases. 2024-03-27 17:58:12 +08:00
Andrew Novoselac
cd5fe84fbe Add tests guarding against regressions identified in rails/rails@640e3981
We had to revert rails/rails@6dd1929 due to some regressions it caused. Here are some tests that would prevent those regressions in the future. See previous commits for more detail.
2024-03-26 12:32:13 -04:00
Jean Boussier
12dbb80635
Merge pull request #51425 from andrewn617/revert-eliminate-lease-connection-in-type-caster-connection
Revert eliminate lease connection in type caster connection
2024-03-26 16:55:00 +01:00
Andrew Novoselac
42fede57bd Use with_connection instead of lease_connection in TypeCaster::Connection 2024-03-26 10:13:49 -04:00
Andrew Novoselac
640e3981ca Revert "Eliminate lease_connection call in TypeCaster::Connection"
This reverts commit 6dd1929b04de42cdff1264bb5618c7f6abe77a9c.

This introduced a change in behaviour since type_for_attribute is aware of custom types but lookup_cast_type does not.

Additionally, since we no longer to use the table and column info to get attributes, this introduced an issue where attribute types were not be correctly found for some queries, where we were joining a table that has a different name than the name of the reflection for that association.
2024-03-26 10:08:40 -04:00
Adrianna Chang
eabcff22a8
Retry known idempotent SELECT queries on connection-related exceptions
This commit makes two types of queries retry-able by opting into our `allow_retry` flag:
1) SELECT queries we construct by walking the Arel tree via `#to_sql_and_binds`. We use a
new `retryable` attribute on collector classes, which defaults to true for most node types,
but will be set to false for non-idempotent node types (functions, SQL literals, etc). The
`retryable` value is returned from  `#to_sql_and_binds` and used by `#select_all` and
passed down the call stack, eventually reaching the adapter's `#internal_exec_query` method.

Internally-generated SQL literals are marked as retryable via a new `retryable` attribute on
`Arel::Nodes::SqlLiteral`.

2) `#find` and `#find_by` queries with known attributes. We set `allow_retry: true` in `#cached_find_by`,
and pass this down to `#find_by_sql` and `#_query_by_sql`.

These changes ensure that queries we know are safe to retry can be retried automatically.
2024-03-26 09:25:55 -04:00
fatkodima
e9b6185234
Merge pull request #51417 from akhilgkrishnan/test-typo-fix
Fix typo: belong_to -> belongs_to
2024-03-26 11:37:38 +02:00
Joshua Young
31ba84d5cd
Ensure binary encoding check is performed with the correct context in #encrypt_as_text (#51423) 2024-03-25 22:26:35 -07:00
Joshua Young
fc9ccbd89c
Fix ActiveRecord::Encryption::Errors::Encoding incorrectly raises for not-encrypted binary encoded values (#51412) 2024-03-25 18:05:54 -07:00
Yaroslav Kurbatov
3bd2d686c7
Ensure all association options are added to the list unconditionally
Some association options, e.g. `through`, were only added to
`.valid_options` list only if they were provided. It means that
sometimes ArgumentError's message would not mention all the
possible options.
2024-03-26 00:03:45 +03:00
Akhil G Krishnan
155dada1eb Fix Typo: belong_to updated to belongs_to 2024-03-25 21:25:09 +05:30
Carlos Antonio da Silva
61a3e61e77 Minor text / error message tweaks, fixes, and punctuation
Improve a few sentences and add punctuation to some recent changelog &
guide entries.

[ci skip]
2024-03-25 10:21:08 -03:00
Nikita Vasilevsky
3df4d6927d Add CHANGELOG entries for association composite primary and foreign keys 2024-03-25 08:38:56 -04:00
Jean Boussier
246b3b609b
Merge pull request #50883 from Shopify/infer-inverse-of-deprecation
Put plural inverse association inference behind a configuration flag
2024-03-25 12:25:21 +01:00
Rony Vieira
2f64b9d1ef Fix typo on ActiveRecord::Associations::ClassMethods doc 2024-03-25 11:36:50 +01:00
Jean Boussier
7a8e58bcb3 Put plural inverse association inference behind a configuration flag
Ref: https://github.com/rails/rails/pull/50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in https://github.com/rails/rails/pull/50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
2024-03-25 10:24:21 +01:00
Jean Boussier
4db9f51259
Merge pull request #51349 from Shopify/connection-optional-deprecation
Add `config.active_record.permanent_connection_checkout` setting
2024-03-25 08:34:56 +01:00
fatkodima
fa2aea8108 Wrap by parentheses custom complex sql literals in update_all 2024-03-24 21:08:22 +02:00
Jean Boussier
fdde675b88 Implement with_connection(prevent_permanent_checkout: true) option
When `.connection` is called inside a `.with_connection` block
it cause the lease to become permanent (or sticky). This is to
ensure that legacy code that may hold onto this connection
won't cause thread safety issues.

However if you autidted the code that calls `.connection` and
know for sure it won't hold into the returned connection,
you can wrap it with `with_connection(prevent_permanent_checkout: true)`
to prevent the lease from becoming permanent and ensure the connection
is released at the end of the block.
2024-03-21 12:19:15 +01:00
Jean Boussier
dd8fd52c07 Add config.active_record.permanent_connection_checkout setting
Controls whether `ActiveRecord::Base.connection` raises an error, emits a deprecation warning, or neither.

`ActiveRecord::Base.connection` checkouts a database connection from the pool and keep it leased until the end of
the request or job. This behavior can be undesirable in environments that use many more threads or fibers than there
is available connections.

This configuration can be used to track down and eliminate code that calls `ActiveRecord::Base.connection` and
migrate it to use `ActiveRecord::Base.with_connection` instead.

The default behavior remains unchanged, and there is currently no plans to change the default.
2024-03-21 12:19:15 +01:00
Jean Boussier
9f01421d7d Eliminate remaining use of lease_connection in Arel::TreeManager 2024-03-20 20:03:39 +01:00
Jean Boussier
7c68c5210c
Merge pull request #51353 from Shopify/get-rid-lease-connection
Eliminate remaining uses of `lease_connection` inside Active Record
2024-03-19 18:03:37 +01:00
Jean Boussier
f8d8183eca Eliminate lease_connection call in Integration 2024-03-19 15:44:52 +01:00
Jean Boussier
5bc1724535 Eliminate lease_connection call in Transactions 2024-03-19 15:44:52 +01:00
Jean Boussier
ae77151fe0 Eliminate lease_connection call in Arel 2024-03-19 15:33:27 +01:00