Commit Graph

90761 Commits

Author SHA1 Message Date
Jean Boussier
1dcb411429 Decouple transactional fixtures and active connections
Ref: https://github.com/rails/rails/pull/50793

Transactional fixtures are currently tightly coupled with the pool
active connection. It assumes calling `pool.connection` will memoize
the checked out connection and leverage that to start a transaction
on it and ensure all subsequent accesses will get the same connection.

To allow to remove checkout caching (or make it optional), we first
must decouple transactional fixtures to not rely on it.

The idea is to behave similarly, but store the connection in
the pool as a special "pinned" connection, and not as the regular
active connection.

This allows to always return the same pinned connection,
but without necessarily assigning it as the active connection.

Additionally, this pinning impact all threads and fibers, so
that all threads have a consistent view of the database state.
2024-02-09 12:59:40 +01:00
Jean Boussier
f5ea4a5260
Merge pull request #51021 from Shopify/test-tweaks
Make various minor fixes to the Active Record test suite
2024-02-09 10:50:20 +01:00
Jean Boussier
29fe34486d Make various minor fixes to the Active Record test suite
Extracted from: https://github.com/rails/rails/pull/50999

- Make fixtures setup and teardown methods private.
- Don't run adapter thread safety tests with sqlite3_mem
- Make foreign_key_tests more resilient to leaked state
- Use `exit!` in fork to avoid `at_exit` side effects.
- Disable transactional fixtures in tests that do a lot of low level
  assertions on connections or connection pools.
2024-02-09 10:01:45 +01:00
Jean Boussier
bfcfede66a
Merge pull request #50983 from fatkodima/fix-counter-caches-for-cpk
Fix counter caches to work for models with composite primary keys
2024-02-09 08:16:51 +01:00
Hartley McGuire
d127335670
Merge pull request #50199 from apoorv1316/improve-action-view-helper-guide
Improve action view helpers guide [ci skip]

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
2024-02-09 00:10:37 -05:00
Hartley McGuire
282d112cd8
Add some missing Oxford Commas [ci-skip]
Co-authored-by: Apoorv Tiwari <tiwari.apoorv1316@gmail.com>
2024-02-09 00:06:20 -05:00
John Hawthorn
48856482c8 Remove memoization of key provider
This is necessary to allow key_provider to be changed dynamically using
with_encryption_context.

Co-authored-by: Kyle Stevens <kstevens715@gmail.com>
2024-02-08 14:47:25 -08:00
Petrik
15b45953a0 Improve documentation of ActiveSupport::TimeZone.create [ci-skip]
The `create` method is currently marked as an alias of `new`. However,
because `new` is later overridden, it's no longer an alias.

This requires wrapping the `alias_method` with stopdoc/startdoc, as the
method is still marked as an alias otherwise (adding `:nodoc:` doesn't
help).

The `initialize` method has to be wrapped with stopdoc/startdoc as well,
as the `new` method will still be documented for the initialized.
Adding a `:nodoc:` instead will remove documentation for all following
methods.
2024-02-08 21:21:57 +01:00
Carlos Antonio da Silva
1007cc8c03
Merge pull request #50988 from p8/guides/improve-dom-id-uniqueness
Improve `dom_id` uniqueness in guides
2024-02-08 15:38:10 -03:00
Petrik
a720480a0a Improve dom_id uniqueness in guides
All headers in a guide get a unique `dom_id` to make anchor links work.
If a header is already present we would prefix it with the `dom_id` of
the parent node. This would not work for headers without parent nodes.

This commit simplifies the `dom_id` uniqueness by only prepending the
parent node if it exists. This can still result in duplicates at the
same level, but for these we already show a warning:

    *** DUPLICATE ID: 'some_id', please make sure that there are no
    headings with the same name at the same level.

Co-authored-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
2024-02-08 19:18:58 +01:00
Eileen M. Uchitelle
6bbfbdb8ad
Merge pull request #50400 from adrianna-chang-shopify/ac-validate-migration-timestamps-2
Add `active_record.config.validate_migration_timestamps` config option.
2024-02-08 12:52:28 -05:00
Adrianna Chang
06b7e345af
Add active_record.config.validate_migration_timestamps config option.
When set, an `ActiveRecord::InvalidMigrationTimestampError` will be raised if the timestamp
prefix for a migration is more than a day ahead of the timestamp associated with the current time.
This is done to prevent forward-dating of migration files, which can impact migration generation
and other migration commands.

It is turned off by default, but will be turned on for applications starting in Rails 7.2.
2024-02-08 12:14:26 -05:00
fatkodima
ea3804835c Fix counter caches to work for models with composite primary keys 2024-02-08 18:59:39 +02:00
Glauco Custódio
686763e1e5 add a note on the postgres limit 2024-02-08 16:47:21 +00:00
Jean Boussier
257c6306b5
Merge pull request #51012 from Shopify/fix-thread-safety-in-mysql-adapters
Properly synchronize `Mysql2Adapter#active?` and `TrilogyAdapter#active?`
2024-02-08 15:10:16 +01:00
Jean Boussier
6f3b46b02d Properly synchronize Mysql2Adapter#active? and TrilogyAdapter#active?
As well as `disconnect!` and `verify!`.

This generally isn't a big problem as connections must not be shared between
threads, but is required when running transactional tests or system tests
and could lead to a SEGV.
2024-02-08 15:01:26 +01:00
Jean Boussier
6daddd99eb
Merge pull request #51010 from Shopify/handle-transactional-fixtures-leak
Gracefully handle transactional fixtures leaks
2024-02-08 11:43:25 +01:00
Jean Boussier
26de25d7ee Gracefully handle transactional fixtures leaks
Extracted from: https://github.com/rails/rails/pull/50999

Some tests may use the connections in ways that cause the fixtures
transaction to be committed or rolled back. The typical case being
doing schema change query in MySQL, which automatically commits the
transaction. But ther eare more subtle cases.

The general idea here is to ensure our transaction is correctly
rolling back during teardown. If it fails, then we assume something
might have mutated some of the inserted fixtures, so we invalidate
the cache to ensure the next test will reset them.

This issue is particularly common in Active Record's own test suite
since transaction fixtures are enabled by default but we have
many tests create tables and such.

We could treat this case as an error, but since we can gracefully
recover from it, I don't think it's worth it.
2024-02-08 11:28:42 +01:00
Kevin Newton
11a97e6bca
Parse tests using prism 2024-02-07 23:14:04 -05:00
Jean Boussier
bab4aa7cb2
Merge pull request #51001 from Shopify/fixtures-cache-key
Share already loaded fixtures across test classes
2024-02-07 19:10:38 +01:00
Jean Boussier
6ad3d0ed77 Share already loaded fixtures across test classes
`self.class` is a fairly narrow cache key, so it doesn't hit that much,
but more importantly, since nothing clears that cache, on large test
suites it keeps growing extremely large.

Using the list of fixtures as a cache key doesn't strictly solve
the growth issue, but most classes actually load all fixtures so
this should shrink the cache size considerably.
2024-02-07 17:45:32 +01:00
Jean Boussier
babad404a9
Merge pull request #51000 from Shopify/fix-race-condition
Fix a race condition in FutureResult#instrument
2024-02-07 17:38:35 +01:00
Jean Boussier
f7eb822856 Fix a reace condition in FutureResult#instrument
We must wait until the event is fully recorded before adding
it to the list, otherwise it might be flushed while incomplete.
2024-02-07 16:06:45 +01:00
Petrik de Heus
c13b82a088
Merge pull request #50994 from p8/activerecord/document-alias-attribute
Document ActiveRecord `alias_attribute` [ci-skip]
2024-02-07 14:50:12 +01:00
Petrik
35bd5e0f55 Document ActiveRecord alias_attribute [ci-skip]
This documents how `alias_attribute` works, including for queries.
2024-02-07 10:47:07 +01:00
KJ Tsanaktsidis
5faeb7007d
Don't pop logger tags in Rails::Rack::Logger until request is finished
At the moment, Rails::Rack::Logger tags the logger (if it's
ActiveSupport::TaggedLogging) for the duration of the @app.call, but
only fires the request.action_dispatch event later, on body close. That
means anything logged in request.action_dispatch handlers won't have the
same tags as the rest of the request.

Fix this by deferring the popping of tags into
finish_request_instrumentation, in the same way that finishing the
instrumentation handle is deferred.
2024-02-07 14:12:07 +11:00
Jean Boussier
f0d433bb46
Merge pull request #50986 from Shopify/delete-encryption-performance-tests
Delete `EncryptionPerformanceTest`
2024-02-06 18:33:05 +01:00
Jean Boussier
3e7ba58439 Delete EncryptionPerformanceTest
On my machine, running the whole Active Record test suite takes
`88` seconds, and `40` of these are spent in encryption tests.

Some of them also happen to flake because of random blips.

I appreciate the care that has been put into ensuring the overhead
of encrption was reasonable, but I don't think these tests justify
their cost.
2024-02-06 18:05:31 +01:00
Jean Boussier
ed2e77d691
Merge pull request #50985 from Shopify/speedup-connection-pool-tests
Speedup ConnectionPoolTests
2024-02-06 17:46:54 +01:00
Jean Boussier
d0d7425854 Speedup ConnectionPoolTests
Most of the time was spent waiting on the default 5 seconds
checkout timeout.

Reducing in for just these tests saves about 1 minute of run time
but more importantly saves me from trying to figure out if my
refactoring introduced a deadlock of some sort.

Before: `real	1m4.448s`

After: `real	0m6.395s`
2024-02-06 17:32:44 +01:00
Jean Boussier
1ca8c8ea34 Improve the docker-entrypoint comments
Co-Authored-By: Joshua Young <djry1999@gmail.com>
2024-02-06 16:59:50 +01:00
Jean Boussier
b324c221e2 SQLite3AdapterTest: Stop leaking files in fixtures directory 2024-02-06 16:59:04 +01:00
Jean Boussier
38c61bbfc2
Merge pull request #50984 from Shopify/test-unit-reporter-prerecord
Implement `Rails::TestUnitReporter#prerecord`
2024-02-06 16:58:11 +01:00
Jean Boussier
86e260bbad
Merge pull request #50982 from jdelStrother/filename-to-json
Fix JSON-encoding ActiveStorage::Filename
2024-02-06 16:43:09 +01:00
Jean Boussier
932af452e8 Implement Rails::TestUnitReporter#prerecord
This is invoked by Minitest before invoking the test, allowing
to print the test name in advance.

This is useful to debug slow and stuck tests by turning on verbose
mode. This way the stuck test name is printed before the process
deadlock.

Otherwise you have to resort to dirty tricks to figure out which
test is not returning.

This is also how the default Minitest reporter works in verbose
mode.
2024-02-06 16:41:48 +01:00
Jonathan del Strother
54d3934d43
Fix JSON-encoding ActiveStorage::Filename
ActiveStorage::Filename was missing quotes when encoded,
generating invalid json like this -

```
JSON.generate(foo: ActiveStorage::Filename.new("bar.pdf") # => '{"foo":bar.pdf}'
```

Delete to_json and rely on the implementation from ActiveSupport::ToJsonWithActiveSupportEncoder
2024-02-06 12:37:58 +00:00
Jean Boussier
6b2a9f1213
Merge pull request #50967 from zzak/asto/fix-controller-tests
Fix Active Storage test configurations for CI
2024-02-06 09:18:56 +01:00
Jean Boussier
0602e1ed77
Merge pull request #50969 from fatkodima/query_logs-line
Support `:source_location` tag option for query log tags
2024-02-06 09:18:11 +01:00
zzak
4873347538
Fix Active Storage test configurations for CI
This change is an up-port of #50787 which applied a similar fix to 7-1-stable.

Since these tests weren't running before, we didn't notice when the DirectUploads controller tests were broken. My theory is that it has something to do with changing `response.parsed_body` to return a HWIA (in #49003).

This change is different from the 7-1-stable PR in that it removes the need to stringify or symbolize any keys, since we are comparing the metadata with string keys. This is a follow up to #43705.

Co-authored-by: Sean Doyle <seanpdoyle@users.noreply.github.com>
Co-authored-by: Jean byroot Boussier <jean.boussier+github@shopify.com>
2024-02-06 11:11:23 +09:00
Yasuo Honda
554e5c2d8e
Merge pull request #50978 from yahonda/pin_minitest_521
Pin minitest version to 5.21
2024-02-06 11:05:00 +09:00
Yasuo Honda
0c9329161c Pin minitest version to 5.21
Managed to reproduce CI failure at https://buildkite.com/rails/rails-nightly/builds/133#018d7bb8-8a32-4978-8e36-d7cb9b067813/1196-1204 . It would also reproduce against the released versions of Ruby because this is triggered by minitest v5.22.0 change. ebb468c81c

To avoid all of railties CI failures, pin minitest version to 5.21 tentatively.

* Steps to reproduce
```ruby
git clone https://github.com/rails/rails
cd rails
rm Gemfile.lock
bundle install
cd railties
bin/test test/application/test_runner_test.rb -n test_system_tests_are_not_run_with_the_default_test_command
```

* Expected behavior
It should pass.

* Actual behavior
```ruby
$ bin/test test/application/test_runner_test.rb -n test_system_tests_are_not_run_with_the_default_test_command
Run options: -n test_system_tests_are_not_run_with_the_default_test_command --seed 14574

F

Failure:
ApplicationTests::TestRunnerTest#test_system_tests_are_not_run_with_the_default_test_command [test/application/test_runner_test.rb:1191]:
Expected /0\ runs,\ 0\ assertions,\ 0\ failures,\ 0\ errors,\ 0\ skips/ to match "Nothing ran for filter: \nRunning 0 tests in a single process (parallelization threshold is 50)\nRun options: --seed 45713\n\n# Running:\n\n".

bin/test test/application/test_runner_test.rb:1179

Finished in 9.982314s, 0.1002 runs/s, 0.2004 assertions/s.
1 runs, 2 assertions, 1 failures, 0 errors, 0 skips
$
```
2024-02-06 10:52:58 +09:00
fatkodima
a87668a238 Support :source_location tag option for query log tags 2024-02-06 00:52:57 +02:00
Jean Boussier
f14401aa17
Merge pull request #50972 from akhilgkrishnan/remove-codespell-from-guide
Remove codespell from contribution guide [ci skip]
2024-02-05 19:22:43 +01:00
Akhil G Krishnan
e8786601ec Remove codespell from contribution guide 2024-02-05 23:49:11 +05:30
Jean Boussier
2c5fe6fa7a
Merge pull request #50964 from Shopify/ruby-head-route-location
Improve routes source location detection
2024-02-05 16:52:11 +01:00
Jean Boussier
bbc7ec49fb Improve routes source location detection
Followup: https://github.com/rails/rails/pull/50923

Instead of stopping on the first frame that isn't in
Action Dispatch, we should return the first frame that
isn't filtered by the backtrace cleaner.
2024-02-05 16:37:37 +01:00
Jean Boussier
0add5dba83
Merge pull request #50943 from northeastprince/jemalloc
Setup jemalloc in default Dockerfile to optimize memory allocation
2024-02-04 10:06:53 +01:00
northeastprince
8e98b614e4 Setup jemalloc in default Dockerfile 2024-02-04 10:02:51 +01:00
Edouard CHIN
3d132b8555
Merge pull request #50952 from nicholasdower/nickd/job-docs
Tiny update to callbacks docs [ci skip]
2024-02-02 13:33:18 +01:00
Nick Dower
22bc976576 Tiny update to callbacks docs [ci skip]
The following was added to the `ActiveJob::Callbacks`,
`ActiveModel::Callbacks` and `AbstractController:Callbacks` docs
in #29072:

> NOTE: Calling the same callback multiple times will overwrite
> previous callback definitions.

The comment refers to "calling" callbacks but seems to be about defining
callbacks, as mentioned in the PR description.

In the ActiveJob and AbstractController docs, I believe this will be
misinterpreted as referring to setting callbacks, which, as far as I can
tell, does not have such a restriction.

In the ActiveModel docs, I believe it would be slightly clearer to
replace "calling" with "defining".
2024-02-02 12:11:30 +01:00