Commit Graph

23723 Commits

Author SHA1 Message Date
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
Carlos Antonio da Silva
060d6ca4d1 Indent code examples to get proper doc code output / highlight
[ci skip]
2024-06-03 13:45:34 -03:00
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
Eileen M. Uchitelle
9cafa55ddd
Merge pull request #51339 from gjtorikian/enable-n_plus_one_only-toggling
Allow one to set `strict_loading_mode` globally
2024-06-03 06:12:04 -07: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
Jean Boussier
e104356c73 Clarify current_transaction behavior.
The documentation wasn't making it clear that a `NullTransaction`
is returned when no transaction is active.

While we're not going to document `NullTransaction` itself, we can
more explictly explain that `current_transaction` always returns an
object that responds to the `ActiveRecord::Transaction` interface.
2024-06-03 10:30:55 +02:00
Garen J. Torikian
5ff9915db9
Allow one to set strict_loading_mode globally 2024-06-02 14:38:38 -05: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
zzak
a2b9e92401
Document callback deduplication with regards to ActiveRecord::Transaction
/cc #51184, #51171, #40648, #48910

Co-authored-by: Rob Jonson <rob@hobbyistsoftware.com>
2024-05-31 21:12:52 +09: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
Jean Boussier
5c96860a3e
Merge pull request #51947 from zzak/rdoc-lease_connection-link 2024-05-30 13:58:20 +02:00
Xavier Noria
23df2aabbe Lazily generate a UUID for AR transactions 2024-05-30 13:51:17 +02:00
zzak
029d7a25b0
Fix link to ConnectionHandling#lease_connection from ConnectionPool docs 2024-05-30 18:41:29 +09:00
Carlos Antonio da Silva
e12ba4d539 Fix method reference in Active Record changelog [ci skip] 2024-05-29 13:52:16 -03:00
Jean Boussier
10dfdc5eb1 Index Result rows rather than to convert them into hashes
Using the same benchmark as https://github.com/rails/rails/pull/51726

A significant part of the memory footprint comes from `Result#each`:

```
Total allocated: 4.61 MB (43077 objects)
Total retained: 3.76 MB (27621 objects)

allocated memory by file
-----------------------------------
 391.52 kB  activerecord/lib/active_record/result.rb

retained memory by file
-----------------------------------
 374.40 kB  activerecord/lib/active_record/result.rb
```

Rows are initially stored as arrays, but `Result#each` convert them
to hashes. Depending on how many elements they contain, hashes use
between 2 and 5 times as much memory than arrays.

|    Length |      Hash |     Array |      Diff |
| --------- | --------- | --------- | --------- |
|         1 |       160B |        40B |      -120B |
|         2 |       160B |        40B |      -120B |
|         3 |       160B |        40B |      -120B |
|         4 |       160B |        80B |       -80B |
|         5 |       160B |        80B |       -80B |
|         6 |       160B |        80B |       -80B |
|         7 |       160B |        80B |       -80B |
|         8 |       160B |        80B |       -80B |
|         9 |       464B |       160B |      -304B |
|        10 |       464B |       160B |      -304B |
|        11 |       464B |       160B |      -304B |
|        12 |       464B |       160B |      -304B |
|        13 |       464B |       160B |      -304B |
|        14 |       464B |       160B |      -304B |
|        15 |       464B |       160B |      -304B |
|        16 |       464B |       160B |      -304B |
|        17 |       912B |       160B |      -752B |
|        18 |       912B |       160B |      -752B |
|        19 |       912B |       320B |      -592B |
|        20 |       912B |       320B |      -592B |
|        21 |       912B |       320B |      -592B |
|        22 |       912B |       320B |      -592B |
|        23 |       912B |       320B |      -592B |
|        24 |       912B |       320B |      -592B |
|        25 |       912B |       320B |      -592B |
|        26 |       912B |       320B |      -592B |
|        27 |       912B |       320B |      -592B |
|        28 |       912B |       320B |      -592B |
|        29 |       912B |       320B |      -592B |
|        30 |       912B |       320B |      -592B |
|        31 |       912B |       320B |      -592B |
|        32 |       912B |       320B |      -592B |
|        33 |      1744B |       320B |     -1424B |
|        34 |      1744B |       320B |     -1424B |
|        35 |      1744B |       320B |     -1424B |
|        36 |      1744B |       320B |     -1424B |
|        37 |      1744B |       320B |     -1424B |
|        38 |      1744B |       320B |     -1424B |
|        39 |      1744B |       640B |     -1104B |
|        40 |      1744B |       640B |     -1104B |
|        41 |      1744B |       640B |     -1104B |
|        42 |      1744B |       640B |     -1104B |
|        43 |      1744B |       640B |     -1104B |
|        44 |      1744B |       640B |     -1104B |
|        45 |      1744B |       640B |     -1104B |
|        46 |      1744B |       640B |     -1104B |
|        47 |      1744B |       640B |     -1104B |
|        48 |      1744B |       640B |     -1104B |
|        49 |      1744B |       640B |     -1104B |
|        50 |      1744B |       640B |     -1104B |

Rather than to convert rows into hashes, we can loopkup the column index
into a single Hash common to all rows. To not complexify the code too much,
rather than to pass the row array and the column index, we wrap both into
an `IndexedRow` object, which uses an extra `40B` object, but that's still
less memory even in the worst case.

After:

```
Total allocated: 4.32 MB (43079 objects)
Total retained: 3.65 MB (29725 objects)

allocated memory by file
-----------------------------------
 101.66 kB  activerecord/lib/active_record/result.rb

retained memory by file
-----------------------------------
  84.70 kB  activerecord/lib/active_record/result.rb
```

As for access speed, it's of course a bit slower, but not by much,
it's between `1.5` and `2` times slower, but remains in the 10's of M
iterations per second, so I think this overhead is negligible compared
to all the work needed to access a model attribute.

Also the `LazyAttributeSet` class only access this once, after the
attribute is casted, the resulting value is still stored in a regular
`Hash`.
2024-05-29 07:32:18 +02: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
Rafael Mendonça França
809409ab29
Merge pull request #51925 from zzak/re-51395-proxy_association
Make CollectionProxy#proxy_association public API
2024-05-28 13:39:28 -04:00
Jean Boussier
fae23a37df
Merge pull request #51932 from Shopify/relation-model
Unify `Relation#klass` and `Relation#model`
2024-05-28 15:00:38 +02:00
Xavier Noria
630fc98709 Rewrite unless/else as if/else
I believe it is generally accepted that unless/else is hard to read,
too many negations.
2024-05-28 13:44:13 +02:00
Xavier Noria
2aeb322806 Let's use the delegation to the transaction manager here
`within_new_transaction` is delegated to the transaction manager. Let's remove
this explicit receiver to honor the delegation and for consistency with usage
of the also delegated `current_transaction`.
2024-05-28 13:21:10 +02:00
Jean Boussier
9f901b7350 Unify Relation#klass and Relation#model
One is the alias of the other, and have been so for a very long time.

My issue with this is that `klass` is really a bad name, and it's
very confusing when reading code to see a mix of both names being
used.

There is no point officially deprecating either, but inside
Active Record we should consistently only use one, I chose to
use `model` as it's way more descriptive in my opinion.
2024-05-28 11:24:35 +02:00
Jean Boussier
9ce4d4468e Explictly take a block in Arel add_bind
Fix: https://github.com/rails/rails/issues/51926

This prevent a false positive warning for unused blocks
on Ruby 3.4-dev.
2024-05-28 08:53:30 +02:00
Jean Boussier
fd5bd98b34 Make the Relation -> Model delegation stricter
In https://github.com/rails/rails/pull/50395 I noticed lots of
methods are delegated from `Relation` to the model. The intent of
this code is to allow using use defined class methods like scopes.

But because of this autmated delegation it allows calling any
`ActiveRecord::Base` class method on a `Relation`, which in itself
may be desireable, however we very wastefully define the delegator
on the first call, and worse we wrap it with a global scope setter.

This also has led to bugs in the past, like https://github.com/rails/rails/pull/51776

So I think we should be more strict about it.

We can't deprecate this behavior because gems might depend on it, however we
can ban it from Active Record's own test suite to avoid regressions.
2024-05-28 08:13:23 +02:00
zzak
7aa7e1e3e1
Make CollectionProxy#proxy_association public API
Co-authored-by: Petrik <petrik@deheus.net>
2024-05-28 07:47:43 +09:00
Theodor Tonum
d28e7c29a2 Add ActiveRecord::Relation#readonly?
Indicates whether a relation was marked readonly.
2024-05-27 21:51:04 +02:00
Theodor Tonum
2364b4243d Make source_reflection return nil when no name 2024-05-27 21:08:45 +02:00
Yasuo Honda
ae3c93df0c
Merge pull request #51859 from fatkodima/fix-partial_inserts-with-identity-cpk
Fix non-partial inserts for models with composite identity primary keys
2024-05-27 21:45:29 +09:00
nisusam
9622342950 Fix + symbol [ci skip] 2024-05-25 14:22:49 +05:30
Florin Oltean
b5207ec558 Fix styling. 2024-05-24 09:22:25 +02:00
Jean Boussier
272aa3bd15
Merge pull request #51898 from flavorjones/51699-improve-store-accessor-for 2024-05-24 08:34:42 +02:00
Mike Dalessio
2bf7e25243
Raise a descriptive error when a store column is misconfigured
If a developer has neglected to use a structured column type (hstore
or json) or to declare a serializer with `ActiveRecord.store`:

```ruby
  class User < ActiveRecord::Base
    store_accessor :settings, :notifications
  end
```

then a `ConfigurationError` will now be raised with a descriptive
error message when the accessor is read or written:

```ruby
  puts user.notifications
  # ActiveRecord::ConfigurationError: the column 'settings' has not
  # been configured as a store.  Please make sure the column is
  # declared serializable via 'ActiveRecord.store' or, if your
  # database supports it, use a structured column type like hstore or
  # json.
```

Previously, in this situation, a `NoMethodError` was raised when the
accessor was read or written:

```ruby
  puts user.notifications
  # NoMethodError: undefined method `accessor' for an instance of ActiveRecord::Type::Text
```

Raising a descriptive exception should help developers understand more
quickly what's wrong and how to fix it.

Closes #51699
2024-05-23 15:59:56 -04:00
Rafael Mendonça França
a8a848d76e
Don't enable automatically_invert_plural_associations by default
This can cause a lot of issues that are hard to detect.

It is better to make this opt-in for people that want to use it.

In Rails 8 we can revisit if we want to enable this config by default.
2024-05-23 19:15:01 +00:00
Rafael Mendonça França
43e4916483
Merge pull request #51891 from flavorjones/flavorjones-update-sqlite3-test-messages
update sqlite3 adapter tests to accept error messages from v3.46.0
2024-05-23 12:26:05 -04:00
Rafael Mendonça França
7ee34d9efb
Enable Rails minitest plugin in our rake tasks 2024-05-23 16:16:37 +00:00
Florin Oltean
372d327b91 Make pretty_print behave more similar to inspect. 2024-05-23 18:11:13 +02:00
Mike Dalessio
b2660d6f45
update sqlite3 adapter tests to accept error messages from v3.46.0
Upstream sqlite updated the error messages to be more descriptive
starting in v3.46.0. Where the error message might look like this in
earlier versions:

    no such column: non_existent

in 3.46.0 it looks like:

    no such column: "non_existent" - should this be a string literal in single-quotes?

The tests have been updated to accept either style of message.

The sqlite3-ruby gem will release a version with this vendored version
shortly, see https://github.com/sparklemotion/sqlite3-ruby/pull/536
2024-05-23 11:50:52 -04:00
zzak
d4150ab5cb
Revert "Merge pull request #51184 from ConfusedVorlon/document_after_commit_deduplication"
This reverts commit 2abee307fe622939f005e9dd9a6925df0dd7ec4c, reversing
changes made to e34a0eec38767920e04f56a7aa7978b7c5fb685a.

A warning on each method is excessive and the warning should already be covered under the guide:
https://edgeguides.rubyonrails.org/active_record_callbacks.html#transaction-callbacks
2024-05-23 20:09:09 +09:00