Commit Graph

92092 Commits

Author SHA1 Message Date
Xavier Noria
de4d874474 Add a comment for dirname vs __dir__ usage 2024-06-24 10:35:43 +02:00
Xavier Noria
5f5aee9aca
Merge pull request #52184 from sajan45/ac_lib_path
Fix Action Cable loader path prefix
2024-06-24 10:24:20 +02:00
Jean Boussier
1690a3e083
Merge pull request #52205 from Shopify/local-assigns-strict-locals
Also pass `local_assigns` to strict locals templates
2024-06-24 10:09:02 +02:00
Jean Boussier
9ea92d4cda Also pass local_assigns to strict locals templates
If one of the locals conflict with a keyword, typically `class`.

The potentially confusing part however is that if you define a default
value, `local_assigns` won't respect it.
2024-06-24 09:16:03 +02:00
fatkodima
770b353896
Merge pull request #52195 from jaynetics/fix-ar-dirty-doc
Fix ActiveRecord dirty docs
2024-06-23 22:20:08 +03:00
Sam Ruby
0350187882 Fix mailer templates to be rubocop compliant
- spaces around array elements
- only emit blank lines between new blocks of code
2024-06-23 07:28:25 -04:00
heka1024
d98cfde86a Support immutable directive in Cache-Control 2024-06-23 08:49:34 +09:00
Janosch Müller
03a1af9b9b
Fix ActiveRecord dirty docs 2024-06-22 21:30:51 +02:00
Rafael Mendonça França
5bec50bc70
Merge pull request #50405 from skipkayhil/hm-filter-dbconfig-inspect
Add condensed #inspect for Pool, Adapter, Config
2024-06-21 17:45:53 -04:00
Rafael Mendonça França
5cfa13687d
Merge pull request #52185 from Shopify/vs/turn_action_controller_inclusions_explicit
Turn ActionController::Base inclusions explicit
2024-06-21 17:45:32 -04:00
Rafael Mendonça França
3a2ec9bb3a
Merge pull request #51740 from heka1024/parallel-upload-in-mirror-service
Improve performance in `ActiveStorage::Service::MirrorService`
2024-06-21 17:28:47 -04: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
Rafael Mendonça França
fc60a394c0
Merge pull request #52175 from y-yagi/correctly_generate_devcontainer_for_mysql2
Correctly generate Devcontainer setting for applications that using mysql2 gem
2024-06-21 17:09:17 -04:00
Vinicius Stock
0966b1983b
Turn ActionController::Base inclusions explicit
Currently, we are using dynamic inclusions to
guarantee that the list of MODULES is always up to
date with what gets included into Base. However,
that prevents static analysis tools from
understanding the ancestors of controllers, which
prevents completion and other editor features from
working correctly. We can instead use a unit test
to verify that both lists are synchronized, which
retains the original behavior while allowing for
more accurate static analysis.
2024-06-21 21:07:12 +00:00
Rafael Mendonça França
bc67961679
Merge pull request #52179 from Uaitt/Dockerfile-FromAsCasing-offenses
Fix `FromAsCasing` offense while building a Dockerfile
2024-06-21 16:59:59 -04:00
Jeremy Daer
eea3d5adcf Revert lazy routesets (#52012) due to polymorphic routing regression
References https://github.com/rails/rails/pull/52012#issuecomment-2183415161

Revert "Merge pull request #52033 from Shopify/amend_lazy_routes_changelog"

This reverts commit 743128b2307b6e1bd59acb9dc8358592d264c573, reversing
changes made to 6622075802bdcca22ab3e32ef6e3f6d2b9a881f8.

Revert "Merge pull request #52012 from Shopify/defer_route_drawing"

This reverts commit 6622075802bdcca22ab3e32ef6e3f6d2b9a881f8, reversing
changes made to 5dabff4b7bf4cc5e2e552efb78c6a3f3e44bed37.
2024-06-21 13:59:43 -07:00
Sajan
f0aaa3cfe8 Fix Action Cable loader path prefix
Since `__dir__` uses real path, when we load the gems from a symlinked path, Zeitwek loader
tries to load the ignored files and throws warnings or errors.
2024-06-21 15:07:54 +00:00
Lorenzo Zabot
9b43e91108 Fix FromAsCasing offenses while building a Dockerfile 2024-06-21 14:07:02 +02:00
Yuji Yaginuma
fd0d2de9ad Correctly generate Devcontainer setting for applications that using mysql2 gem
Currently, `devcontainer` command sets an adapter name, but
`DevcontainerGenerator` requires a database name.
16d8b82d5e/railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb (L11)
16d8b82d5e/railties/lib/rails/generators/database.rb (L6)

So the `devcontainer` command doesn't generate the setting for
MySQL. This fixes to generate the correct setting.
2024-06-21 17:16:40 +09: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
Yasuo Honda
7606f99649
Merge pull request #52172 from yahonda/selenium-webdriver-4220
Support selenium-webdriver 4.22.0 that enables CDP in Firefox by default
2024-06-21 10:20:48 +09:00
Yasuo Honda
1b905edd2e Support selenium-webdriver 4.22.0 that enables CDP in Firefox by default
This pull request supports selenium-webdriver 4.22.0 that enables CDP in Firefox by default.
because Firefox 129 deprecates Chrome DevTools Protocol (CDP).
selenium-webdriver 4.22.0 enables CDP explicitly by adding "remote.active-protocols"=>3 .

- Steps to reproduce and this commit addresses these failures.
```ruby
$ bundle update selenium-webdriver --conservative
$ git diff main ../Gemfile.lock
diff --git a/Gemfile.lock b/Gemfile.lock
index 4e1c049ac0..e05f4b3b3c 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -512,8 +512,9 @@ GEM
       google-protobuf (~> 3.25)
     sass-embedded (1.69.6-x86_64-linux-gnu)
       google-protobuf (~> 3.25)
-    selenium-webdriver (4.20.1)
+    selenium-webdriver (4.22.0)
       base64 (~> 0.2)
+      logger (~> 1.4)
       rexml (~> 3.2, >= 3.2.5)
       rubyzip (>= 1.2.2, < 3.0)
       websocket (~> 1.0)
$ cd actionpack
$ bin/test test/dispatch/system_testing/driver_test.rb test/dispatch/system_testing/driver_test.rb
Running 18 tests in a single process (parallelization threshold is 50)
Run options: --seed 58668

.....F

Failure:
DriverTest#test_define_extra_capabilities_using_firefox [test/dispatch/system_testing/driver_test.rb:127]:
--- expected
+++ actual
@@ -1 +1 @@
-{"moz:firefoxOptions"=>{"args"=>["--host=127.0.0.1"], "prefs"=>{"browser.startup.homepage"=>"http://www.seleniumhq.com/"}}, "browserName"=>"firefox"}
+{"moz:firefoxOptions"=>{"args"=>["--host=127.0.0.1"], "prefs"=>{"remote.active-protocols"=>3, "browser.startup.homepage"=>"http://www.seleniumhq.com/"}}, "browserName"=>"firefox"}

bin/test test/dispatch/system_testing/driver_test.rb:113

.F

Failure:
DriverTest#test_define_extra_capabilities_using_headless_firefox [test/dispatch/system_testing/driver_test.rb:144]:
--- expected
+++ actual
@@ -1 +1 @@
-{"moz:firefoxOptions"=>{"args"=>["-headless", "--host=127.0.0.1"], "prefs"=>{"browser.startup.homepage"=>"http://www.seleniumhq.com/"}}, "browserName"=>"firefox"}
+{"moz:firefoxOptions"=>{"args"=>["-headless", "--host=127.0.0.1"], "prefs"=>{"remote.active-protocols"=>3, "browser.startup.homepage"=>"http://www.seleniumhq.com/"}}, "browserName"=>"firefox"}

bin/test test/dispatch/system_testing/driver_test.rb:130

..........

Finished in 0.007717s, 2332.3654 runs/s, 4794.3066 assertions/s.
18 runs, 37 assertions, 2 failures, 0 errors, 0 skips
```

- Planned Deprecation of CDP in Firefox
https://groups.google.com/a/mozilla.org/g/dev-platform/c/Z6Qu3ZT1MJ0?pli=1

- Add preference to enable CDP in Firefox by default
https://github.com/SeleniumHQ/selenium/pull/14091

- [rb] Add logger gem as a runtime dependency #14082
https://github.com/SeleniumHQ/selenium/pull/14082
2024-06-21 09:57:50 +09:00
Xavier Noria
242216803e
Merge pull request #52165 from rails/fxn/changelog
Register 8b173d9 in the guides CHANGELOG
2024-06-20 10:47:01 +02:00
Xavier Noria
b448202768 Register 8b173d9 in the guides CHANGELOG 2024-06-20 09:45:25 +02:00
Xavier Noria
13f4d0f624 Fixes typo 2024-06-20 08:30:40 +02:00
Hartley McGuire
03a399bb08
Merge pull request #52164 from sampatbadhe/patch-17
Correct table name in schema for add_reference migration in Active Record Migration Documentation [ci skip]
2024-06-20 00:31:33 -04:00
Sampat Badhe
dcadd27cdd
correct table name in schema for add_reference migration in Active Record Migration Documentation [ci skip] 2024-06-20 09:52:52 +05:30
Jean Boussier
3accb8d354
Merge pull request #52157 from byroot/lazy-ast
Lazily generate assertion failure messages
2024-06-19 20:53:29 +02:00
Jean Boussier
cac62630a3 Lazily generate assertion failure messages
Some of them can be a bit costly to generate, particularly
when inspecting very large objects or when accessing the AST
of procs.

Minitest supports passing the message as a callable, which allow
us to defer all these computations.
2024-06-19 20:06:42 +02:00
Petrik de Heus
ab7d62e127
Merge pull request #52156 from p8/guides/remove-zeitwerk-guide
Remove "Migrating from Classic to Zeitwerk" guide [ci-skip]
2024-06-19 17:44:35 +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
Petrik
8b173d9551 Remove "Migrating from Classic to Zeitwerk" guide [ci-skip]
Rails 7 apps require running in `zeitwerk` mode, so we no longer need
the migration guide. The "Autoloading and Reloading Constants" guide
also handles autoloading with Zeitwerk, including `autoload_paths`, the
`zeitwerk:check` command and more.
2024-06-19 15:27:17 +02:00
Jean Boussier
362fbd2a7f
Merge pull request #52151 from fatkodima/exists-with-updated-records
Fix `ActiveRecord::Relation#exists?` with no conditions for loaded relations and updated records
2024-06-19 15:12:31 +02:00
Petrik
b06c2c7da0 Move performance section under "Digging Deeper" [ci-skip]
This section was moved to "Advanced Active Record" in
35ad43b1a5b8eaed48588c8045 by accident.
2024-06-19 15:08:47 +02:00
Ridhwana
168b2b369c
[RF-DOCS] Active Record Migration Documentation (#51928)
- [x] We talk about transactions and how to disable them early on, as part of the first few paragraphs, that could be a subsection to add separation.
- [x] The guide goes into reversible early, before even talking about generating migrations and other basic stuff, and then later talks more about it. I think it's important to lay the foundation of change vs up/down, and the fact that migrations should always try to be able to be rolled back, but maybe we can do that without going into reversible that early in the guide, which complicates things further.
- [x] We mention bin/rails generate --help to look for more details, but we could mention specific generators also offer help too, e.g. bin/rails generate model --help or bin/rails generate migration --help
- [x] There's a small section about composite PKs, but there's also [a whole guide on it](https://edgeguides.rubyonrails.org/active_record_composite_primary_keys.html), we can add a link to it from the migrations guide at least for more info on the topic.
- [x] The section that talks about execute shows an example of a specific model Product.connection.execute, but we can call execute directly within a migration, that might be simpler to show.
- [x] We talk about "seed data " in setup & preparing sections, but never mention it before, or explain what seed data is. There's a section about seed data later in the guide, perhaps we can link to it to facilitate.
- [x] In "running specific migrations", bin/rails db:migrate VERSION=zomg actually raises a different error: Invalid format of target version: `VERSION=zomg`... might need a different example there for the unknown migration version error. (no need to show this invalid format one)
- [x] The section about referential integrity can likely be better explained, in particular I think it's worth mentioning that even though the "AR method/way" (the pattern, I mean) doesn't think such "intelligence" belongs to the database, foreign keys and unique indexes are generally safer at the database level. (and should likely have their counterparts explicitly added in code with associations and validations). I just don't want to convey that someone shouldn't be adding FKs & unique constraints, because not adding these can definitely bite you.

Other changes: 
- [x] Moved sections around 
- [x] Added a section about "Rails Migration Version Control"
- [x] Added a section about "Using UUIDs instead of IDs for Primary Keys"

Co-authored-by: bhumi1102 <bhumi1102@gmail.com>
Co-authored-by: Bartosz Łęcki <bart.lecki@gmail.com>
Co-authored-by: Cecile Veneziani <cveneziani@users.noreply.github.com>
Co-authored-by: hatsu <hajiwata0308@gmail.com>
Co-authored-by: Petrik de Heus <petrik@deheus.net>
Co-authored-by: Ahmad hamza  <ahmad.hamza@toptal.com>
Co-authored-by:  Bart de Water <496367+bdewater@users.noreply.github.com>
Co-authored-by: Amanda Perino <58528404+AmandaPerino@users.noreply.github.com>
Co-authored-by: Andy Atkinson <andyatkinson@gmail.com>
Co-authored-by: Jamie Gaskins <jgaskins@gmail.com>
2024-06-19 13:06:58 +02:00
fatkodima
5dd2da7ee8 Fix ActiveRecord::Relation#exists? with no conditions for loaded relations and updated records 2024-06-19 12:15:20 +03:00
Jean Boussier
869c7bf094 Improve BacktraceCleaner code example [ci-skip]
With newer rubies, removing a prefix is much simpler.
2024-06-19 09:16:30 +02: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
43e2281da8
Merge pull request #52147 from rails/fxn/ar-transaction-docs
Iterate the docs of ActiveRecord::Transaction
2024-06-18 13:34:54 +02: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
9ccbab1658
Merge pull request #52145 from Shopify/alias-attribute-override-inherited-methods
Fix `alias_attribute` to ignore methods defined in parent classes
2024-06-18 10:04:11 +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
Yasuo Honda
98636b3a78
Merge pull request #52141 from MaxLap/fix_log_causes_ruby_head
Fixes tests with nested exception backtraces on Ruby master
2024-06-18 08:19:37 +09: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
Aaron Patterson
6d126e03db
Merge pull request #51288 from JoeDupuis/stop-caching-on-failure-activestorage-proxy-controller
Expire caching when a download fail while proxying in ActiveStorage
2024-06-17 10:35:42 -07:00
Joé Dupuis
f7ecde8331
Expire caching when a download fail while proxying in ActiveStorage
Fix #51284

Both Proxy controllers in Active Storage set the caching headers early
before streaming.

In some instances (see #51284), it is possible for the file
download (from the service to server) to fail before we send the first
byte to the client (response not yet committed).

In those instances, this change would invalidate the cache and return
a better response status before closing the stream.
2024-06-17 09:49:11 -07:00
Maxime Lapointe
3e816bfe72 Fixes tests with nested exception backtraces on Ruby master
Ruby master did the following changes (and probably more)
https://bugs.ruby-lang.org/issues/16495
https://bugs.ruby-lang.org/issues/20275
2024-06-17 09:43:28 -04:00
Jean Boussier
5ef7f73c30
Merge pull request #52131 from fatkodima/remove-unneeded-with_connection
Fix more `with_connection` offences inside Active Record
2024-06-17 08:07:15 +02:00