Commit Graph

18353 Commits

Author SHA1 Message Date
fatkodima
0e7826e10e Fix upsert warning for MySQL 2024-03-07 15:35:14 +02:00
Carlos Antonio da Silva
2fa3294618
Merge pull request #51263 from buncis/update-delegate-type-doc
[ci skip] update delegated type doc to make it less confusing and in sync with rails guide
2024-03-06 13:22:44 -03:00
buncis
1c54153dc6
update docs to make it less confusing and in sync with rails guide 2024-03-06 20:31:34 +07:00
Donal McBreen
5d528ba0c8
Add dirties option to uncached (#51204)
This adds a `dirties` option to `ActiveRecord::Base.uncached` and
`ActiveRecord::ConnectionAdapters::ConnectionPool#uncached`.

Setting `dirties` to `false`, means database writes to the connection
pool will not mark any query caches as dirty.

The option defaults to `true` which retains the existing behaviour and
clears query caches on all connection pools used by the current thread.

Co-authored-by: Jeremy Daer <jeremy@rubyonrails.org>
2024-03-02 19:01:24 -08:00
Jean Boussier
ec7deca7ee Refactor InsertAll not to permanently lease a connection
Extracted from: https://github.com/rails/rails/pull/50793
2024-03-01 15:33:45 +01:00
Jean Boussier
2bf0d68de6 Fix a typo in ConnectionPoool#connection deprecation (again) 2024-03-01 15:32:58 +01:00
Jean Boussier
f657842090 Fix a typo in ConnectionPoool#connection deprecation 2024-03-01 15:28:00 +01:00
Jean Boussier
7263da542b Deprecate ConnectionPool#connection
Replaced by `#lease_connection` to better reflect what it does.

`ActiveRecord::Base#connection` is deprecated in the same way
but without a removal timeline nor a deprecation warning.

Inside the Active Record test suite, we do remove `Base.connection`
to ensure it's not used internally.

Some callsites have been converted to use `with_connection`,
some other have been more simply migrated to `lease_connection`
and will serve as a list of callsites to convert for
https://github.com/rails/rails/pull/50793
2024-03-01 14:32:55 +01:00
Jean Boussier
38e8609ded Make .connection always return a permanently leased connection
Ref: https://github.com/rails/rails/pull/51083

The introduction of `ActiveRecord::Base.with_connection` somewhat broke
some expectations, namely that calling `.connection` would cause the
connection to be permenently leased, hence that future calls to it
would return the same connection, with all it's possible environmental
changes.

So any call to `.connection`, even inside `.with_connection` should
cause the lease to be sticky, and persist beyond the `with_connection`
block.

Also rename `.connection` into `.lease_connection`, as to make it
more explicit.
2024-03-01 11:37:06 +01:00
Jean Boussier
1d0b396f40 Clear all threads query cache when a connection is pinned
Ref: https://github.com/Shopify/maintenance_tasks/pull/983#issuecomment-1969407080
Ref: https://github.com/rails/rails/pull/51151

Now that query caches are owned by the pool, and assigned on connections
during checkout, when running multithreaded code inside transactional
tests (typically system tests), the two threads uses the same connection
but not the same cache.

So it's important that we do clear the caches for all threads when
a connection is pinned.
2024-02-29 09:21:18 +01:00
Jean Boussier
ef947f9932 Expose a generic fixture accessor for fixture names that may conflict with Minitest
```ruby
assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
```

This was brought to me by someone with a `Metadata` model. The fixtures
accessor being `metadata` which conflicts with the `metadata` method
recently added in `Minitest`.
2024-02-28 16:09:23 +01:00
Joshua Young
f42d9cbc32 [Fix #51164] Model.query_constraints with single non-primary-key column raises incorrect error 2024-02-28 14:19:10 +10:00
Rafael Mendonça França
649e99bf92
Merge pull request #51206 from fatkodima/fix-indexes-for-nonexisting-table
Fix `indexes` method for non-existing MySQL tables
2024-02-27 16:29:49 -05:00
fatkodima
8510efa841 Fix indexes method for non-existing MySQL tables 2024-02-27 13:39:01 +02:00
Jean Boussier
0016280f4f Don't require an active connection for table and column quoting
Extracted from: https://github.com/rails/rails/pull/50793

Right now quoting table or column names requires a leased Adapter
instance, even though none of the implementations actually requires
an active connection.

The idea here is to move these methods to the class so that the quoting
can be done without leasing a connection or even needing the connection
to ever have been established.

I also checked `activerecord-sqlserver-adapter` and `oracle-enhanced`
gems, and neither need an active connection.
2024-02-27 11:19:00 +01:00
Carlos Antonio da Silva
c402ec7872 Add punctuation on some newly added API docs [ci skip] 2024-02-26 13:32:39 -03:00
Jean Boussier
6e74098b8c Fix Migrator.current_version to instantiate SchemaMigration correctly
This was missed in https://github.com/rails/rails/pull/51162
2024-02-26 12:49:31 +01:00
Vipul A M
2abee307fe
Merge pull request #51184 from ConfusedVorlon/document_after_commit_deduplication
[ci skip] Add warning about deduplication of after_xxx_commit shortcuts
2024-02-25 16:30:11 -05:00
Lex Cao
48036fee0d Fix typo 2024-02-25 23:11:40 +08:00
Rob Jonson
79fa0e3b2d Add warning about deduplication of after_xxx_commit shortcuts 2024-02-24 19:40:13 +00:00
Jean Boussier
2cbedfdb2e Refactor FixtureSet to deal with connection pools
Extracted from: https://github.com/rails/rails/pull/50793

Allow to check the cache without checking out a connection.
2024-02-23 11:25:33 +01:00
Jean Boussier
932e029ad7
ConnectionPool: handle schema_reflection being re-assigned
This fixes a regression recently introduced that a schema_reflection
assignment from taking effect.
2024-02-22 21:13:04 +00:00
Jean Boussier
a918394974 Refactor InternalMetadata, MigrationContext to belong to the pool
Extracted from: https://github.com/rails/rails/pull/50793

Similar to the recent refactoring of schema caches, rather than to directly
hold a connection, they now hold a pool and checkout a connection when needed.
2024-02-22 12:46:41 +01:00
Rafael Mendonça França
939742d69e
Merge pull request #50901 from joshuay03/fix-autosave-has-one-setting-fk-when-unchanged
[Fix #50897] Autosaving `has_one` sets foreign key attribute when unchanged
2024-02-21 15:21:23 -05:00
Rafael Mendonça França
3e42d79c99
Merge pull request #51078 from saleh-alhaddad/support_join_types_in_where_associated
Fix override existing join types in the query in the `where.associated` method
2024-02-21 15:10:24 -05:00
saleh-alhaddad
3400aac7f9
Fix an issue in the where.associated method 2024-02-21 18:12:15 +00:00
mylesboone
278d6574cf
ActiveRecord::Relation#order supports hash like ActiveRecord::Relation#where (#50000)
* relation#order supports hash like relation#where

This allows for an ActiveRecord::Relation to take a hash such as
`Topic.includes(:posts).order(posts: { created_at: :desc })`

* use is_a? to support subclasses of each

Co-authored-by: Rafael Mendonça França <rafael@rubyonrails.org>
2024-02-21 12:43:15 -05:00
Jean Boussier
684131a4f0
Merge pull request #51139 from Shopify/relation-bound-sql-literal
Relation#where build BoundSqlLiteral rather than eagerly interpolate
2024-02-21 16:49:45 +01:00
Carlos Antonio da Silva
baad391196 Remove unused intermediate variable
It was necessary to return properly with the previous deprecation code,
which was removed in eccc6061f4f3abdfdeb9a363987a898418d9498f.
2024-02-21 09:31:27 -03:00
Jean Boussier
8e6a5deca6 Relation#where build BoundSqlLiteral rather than eagerly interpolate
Ref: https://github.com/rails/rails/pull/50793

To make not caching connection checkout viable, we need to reduced
the amount of places where we need a connection.

Once big source of this is query/relation building, where in many
cases it eagerly quote and interpolation bound values in SQL fragments.

Doing this requires an active connection because both MySQL and Postgres
may quote values differently based on the connection settings.

Instead of eagerly doing all this, we can instead just insert these
as bound values in the Arel AST. For adapters with prepared statements
this is better anyway as it will avoid leaking statements, and for those
that don't support it, it will simply delay the quoting to just
before the query is executed.

However, the `%` API (`where("title = %s", something)`) can't realistically
be fixed this way, but I don't see much value in it and it probably should
be deprecated and removed.
2024-02-21 13:22:55 +01:00
Jean Boussier
1af361b30b Don't forcefully clear temporarily disabled query caches
Followup: https://github.com/rails/rails/pull/50938

The behavior changed to always clear the query cache as soon
as it's disabled, on the assumption that once queries have been
performed without it, all bets are off.

However that isn't quite true, we can apply the same clearing
logic than when it's enabled. If you perform read only queries
without the cache, there is no reason to clear it.
2024-02-21 11:47:54 +01:00
Jean Boussier
e88dba8f0b clear_query_caches_for_current_thread: avoid pinning all connections
Ref: https://github.com/rails/rails/pull/51083#discussion_r1496720821

Now that the pool is owned by the pool, we can clear it without
hacing to checkout a connection.
2024-02-21 10:26:28 +01:00
Joshua Young
a518b5a9d9 [Fix #50897] Autosaving has_one sets foreign key attribute when unchanged 2024-02-21 18:07:44 +10:00
Rafael Mendonça França
842ca2137f
Merge pull request #51047 from johnpitchko/add-query-docs-to-delegated-type
Add query docs for delegated type [ci skip]
2024-02-20 18:22:49 -05:00
Rafael Mendonça França
eccc6061f4
Remove deprecated behavior that would rollback a transaction block when exited using return, break or throw. 2024-02-20 21:56:51 +00:00
Rafael Mendonça França
c313d91613
Remove deprecated support to pass rewhere to ActiveRecord::Relation#merge 2024-02-20 21:56:13 +00:00
Rafael Mendonça França
c09956a3ad
Remove deprecated support to pass deferrable: true to add_foreign_key 2024-02-20 21:55:04 +00:00
Rafael Mendonça França
95b48d8a3b
Remove deprecated support to quote ActiveSupport::Duration 2024-02-20 21:54:57 +00:00
Rafael Mendonça França
240e9dac0a
Remove deprecated #quote_bound_value 2024-02-20 21:53:43 +00:00
Rafael Mendonça França
465fbd78f4
Remove deprecated ActiveRecord::ConnectionAdapters::ConnectionPool#connection_klass 2024-02-20 21:53:41 +00:00
Rafael Mendonça França
64cbcd7a8c
Remove deprecated support to apply some methods in the Connection Handle to the connections pools for the current role when the role arguments isn't provided 2024-02-20 21:53:38 +00:00
Rafael Mendonça França
9489c14b7b
Remove deprecated #all_connection_pools 2024-02-20 21:53:17 +00:00
Rafael Mendonça França
7ddf423329
Remove deprecated ActiveRecord::ConnectionAdapters::SchemaCache#data_sources 2024-02-20 21:53:13 +00:00
Rafael Mendonça França
5667de1aca
Remove deprecated ActiveRecord::ConnectionAdapters::SchemaCache.load_from 2024-02-20 21:52:11 +00:00
Rafael Mendonça França
9391f0a0e8
Remove deprecated #all_foreign_keys_valid? from database adapters 2024-02-20 21:51:31 +00:00
Rafael Mendonça França
8069cc83e8
Remove deprecated support to passing coder and class as second argument to serialize 2024-02-20 21:51:30 +00:00
Rafael Mendonça França
39997a0f04
Remove deprecated support to ActiveRecord::Base#read_attribute(:id) to return the custom primary key value 2024-02-20 21:51:29 +00:00
Rafael Mendonça França
f40a697e0f
Remove deprecated TestFixtures.fixture_path 2024-02-20 21:51:26 +00:00
Rafael Mendonça França
4bf2c4cea1
Remove deprecated behavior to support referring to a singular association by its plural name 2024-02-20 21:50:36 +00:00
Rafael Mendonça França
9ef79385d9
Remove deprecated support to passing SchemaMigration and InternalMetadata classes as arguments to ActiveRecord::MigrationContext. 2024-02-20 21:50:05 +00:00