Commit Graph

14466 Commits

Author SHA1 Message Date
Ben Sheldon [he/him] 066ce1e34f
Allow Engine url_helpers to respect config.relative_url_root 2022-08-09 10:06:21 -07:00
Jean Boussier 82bab92cfe
Merge pull request #45782 from fatkodima/etag_with_flash-no-flash
Fix `EtagWithFlash` when there is no Flash middleware available
2022-08-08 15:46:41 +02:00
Jonathan Hefner 41c81fdaca
Merge pull request #45787 from skipkayhil/rm-redundant-framework-default
Remove incorrect 7.0 framework default
2022-08-06 20:40:53 -05:00
Hartley McGuire 8f9ec10f3e
Add missing and fix incorrect version defaults
7.1

- `config.active_job.use_big_decimal_serializer` was fixed to be `true`
  in b4fffc3 but the documentation was not updated
- `config.active_record.run_commit_callbacks_on_first_saved_instances_in_transaction`
  was added in 936a862
- `config.active_record.sqlite3_adapter_strict_strings_by_default` was
  added in 21a6dbd but the documented default is incorrect
- `config.log_file_size` was initially set to `100.megabytes` but was
  changed during review to be `100 * 1024 * 1024` for apps using
  `config.active_support.bare` (cdce275)

7.0

- `config.active_support.isolation_level` was added in 540d2f4

6.1

- minor formatting fix

The changes were generated by a script:
5beae39bd8/bin/check-framework-defaults

The parentheses around `100 * 1024 * 1024` were removed from
railties/lib/rails/application/configuration.rb because the script
uses the values exactly.
2022-08-06 19:05:50 -04:00
Hartley McGuire 9bca2b7907
Remove incorrect 7.0 framework default
This was added in 60f7d49, which didn't quite make the cutoff for the
7.0 release, so it doesn't belong under `7.0`.

In addition, the value being set is the same as the underlying
configuration's default. This is likely an oversight because the
`load_default` value was changed during review from `true` to be
`Rails.env` dependent, and later to `false`.
2022-08-06 18:52:15 -04:00
fatkodima b95ddd4ef5 Fix `EtagWithFlash` when there is no Flash middleware available
Co-authored-by: mihaic195 <colceriumi@gmail.com>
2022-08-06 18:41:10 +03:00
Jordan Elver 3168d401a7
Fix typo in CHANGELOG.md 2022-08-06 07:52:22 +01:00
Gannon McGibbon 9414a8b79f
Merge pull request #45701 from gmcgibbon/unused_routes_script
Add `routes --unused` option to detect extraneous routes.
2022-08-04 21:57:09 -05:00
eileencodes 8d0da3d568
Refactor changes in #44931 and add a test
The PR #44931 fell off my radar a few months ago so I decided to add a
test myself to ensure this behavior change is tested. While doing that I
decided to refactor the changes from that PR to make the code a little
cleaner and easier to read.
2022-08-04 12:58:39 -04:00
Gannon McGibbon 5613b1240a Add `routes --unused` option to detect extraneous routes.
Routes take a long time to draw. Over time, a Rails app can become slow
to boot simply because of how many routes it has. This script can be
used to detect routes that are drawn, but aren't actually valid.
Removing routes this script detects can help speed up your app and
remove dead code.

Example:

```
> bin/rails routes --unused

Found 2 unused routes:

Prefix Verb URI Pattern    Controller#Action
   one GET  /one(.:format) action#one
   two GET  /two(.:format) action#two
```
2022-08-02 20:07:45 -05:00
Jonathan Hefner 943fca0de8 Remove remnants from Editor command helper module
The `Editor` command helper module was originally extracted from
`CredentialsCommand`, and still includes a help message mentioning
"credentials" and a `rescue` that is specific to encrypted files.

This commit removes those remnants, fully generalizing the `Editor`
module.  Additionally, this commit changes `SecretsCommand` to make use
of the `Editor` module.
2022-08-02 15:47:28 -05:00
Jonathan Hefner 887dc9af6c
Merge pull request #45716 from jonathanhefner/command-executable-helper
Consistently format commands in help messages
2022-08-02 15:42:38 -05:00
Jean Boussier 73d2cc86d8
Merge pull request #45664 from sambostock/teach-active-job-to-set-configs-on-itself
Teach `ActiveJob` to set configs on itself
2022-08-01 10:51:19 +02:00
Jonathan Hefner 7752be54cd Consistently format commands in help messages
This commit enhances the `Rails::Command::Base.executable` method to
integrate a new `bin` class attribute and optional `subcommand` arg.
So, for example, `CredentialsCommand.executable(:edit)` will now return
"bin/rails credentials:edit".

Additionally, this commit replaces occurrences of "bin/rails COMMAND",
"rails COMMAND", and "COMMAND" in help messages with calls to
`executable`, for improved consistency.
2022-07-31 15:06:31 -05:00
Jonathan Hefner 75d4e878ec Support spaces in file path when invoking editor
In #44910, the `credentials:edit` command was fixed to support spaces in
the temporary file path on Windows.  This commit applies the same fix to
the `encrypted:edit` command.
2022-07-30 15:47:54 -05:00
Sam Bostock b4fffc3c68
Teach ActiveJob to set configs on itself
Previously configs of the form `config.active_job.X` were only forwarded to
`ActiveJob::Base`. This teaches Active Job to set them on `ActiveJob` directly
instead, if the setter exists.

For consistency, this more or less mirrors the way that Active Record does it.

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
Co-authored-by: Sam Bostock <sam.bostock@shopify.com>

---

Fix use_big_decimal_serializer Rails 7.1 default

This config should be enabled for new Rails 7.1 apps, or apps that have updated
their config to `load_defaults 7.1`, not disabled.

This also clarifies the config accessor comment.

---

Add contributor documentation comment to load_defaults

The process for introducing a change in behavior in Rails can be confusing to
new contributors, so a comment is added roughly explaining how to do so, and
what belongs in `load_defaults` and `new_framework_defaults`.

This comment is aimed at contributors, not consumers, so it is added within the
method, rather than above it.
2022-07-30 11:11:15 -04:00
Jonathan Hefner d4e87ca190 Prevent "saved" message when edit command aborted
Prior to this commit, the `credentials:edit` command would display a
save confirmation message even if it was aborted via interrupt:

  ```console
  $ EDITOR='ruby -e "sleep 100"' bin/rails credentials:edit
  <Ctrl+C>
  Aborted changing file: nothing saved.
  File encrypted and saved.
  ```

This commit fixes that behavior:

  ```console
  $ EDITOR='ruby -e "sleep 100"' bin/rails credentials:edit
  <Ctrl+C>
  Aborted changing file: nothing saved.
  ```

This commit also applies the same fix to the `encrypted:edit` command.
2022-07-29 16:57:45 -05:00
Jonathan Hefner 225cc78885
Merge pull request #45689 from jonathanhefner/encrypted-config-preserve-invalid-yaml
Save encrypted config even if YAML is invalid
2022-07-29 15:47:44 -05:00
Jonathan Hefner 73909f45be
Merge pull request #45688 from jonathanhefner/session-cookies-default-same_site
Fix default `SameSite` for session cookies
2022-07-29 15:46:01 -05:00
Jonathan Hefner 772c462738 Save encrypted config even if YAML is invalid
In #30893, the `credentials:edit` command was changed to prevent saving
invalid YAML:

  ```yaml
  # some_invalid_yaml.yml
  secret_key_base: ...
  - new_key: new value
  ```

  ```console
  $ EDITOR='cp some_invalid_yaml.yml' bin/rails credentials:edit
  ruby-3.1.2/lib/ruby/3.1.0/psych.rb:455:in `parse': (<unknown>): did not find expected key while parsing a block mapping at line 3 column 1 (Psych::SyntaxError)

  $ bin/rails credentials:show
  secret_key_base: ...
  ```

However, throwing away user input is not ideal.  Such behavior can be
particularly troublesome when copying secrets from ephemeral sources.

This commit changes the `credentials:edit` command to always save user
input, and display a helpful warning when saving invalid YAML:

  ```console
  $ EDITOR='cp some_invalid_yaml.yml' bin/rails credentials:edit
  File encrypted and saved.
  WARNING: Invalid YAML in '/path/to/app/config/credentials.yml.enc'.

    (/path/to/app/config/credentials.yml.enc): did not find expected key while parsing a block mapping at line 3 column 1

  Your application will not be able to load 'config/credentials.yml.enc' until the error has been fixed.

  $ bin/rails credentials:show
  # some_invalid_yaml.yml
  secret_key_base: ...
  - new_key: new value
  ```

This commit also applies the same fix to the `encrypted:edit` command.
2022-07-28 16:37:18 -05:00
Jonathan Hefner c95780d7c6 Fix default SameSite for session cookies
Follow-up to #45501.

The Rack base class that `CookieStore` inherits from [always sets
`:same_site`][1].  Thus, `options.key?(:same_site)` always returns true
for session cookies, preventing a default value from being set.

It would be possible to change Rack to conditionally set `:same_site`,
but, from Rack's perspective, it has no reason to not set `:same_site`,
because it treats a `nil` value the same as no value.

Therefore, this commit specifies a default `:same_site` in `CookieStore`,
which simply defers to `request.cookies_same_site_protection` as
`CookieJar` does.

Fixes #45681.

[1]: https://github.com/rack/rack/blob/2.2.4/lib/rack/session/abstract/id.rb#L398-L402
2022-07-28 16:19:21 -05:00
Jonathan Hefner 1740b1f2cb Generate master.key even when require_master_key
Prior to this commit, if `config.require_master_key` was set to true in
`config/application.rb`, the `credentials:edit` command could not
automatically generate a master key file:

  ```ruby
  # In config/application.rb
  config.require_master_key = true
  ```

  ```console
  # No previously existing credentials
  $ rm config/master.key config/credentials.yml.enc
  ```

  ```console
  $ bin/rails credentials:edit
  activesupport/lib/active_support/encrypted_file.rb:118:in `handle_missing_key': Missing encryption key to decrypt file with. Ask your team for your master key and write it to config/master.key or put it in the ENV['RAILS_MASTER_KEY']. (ActiveSupport::EncryptedFile::MissingKeyError)
  ```

This commit adds a `EncryptedFile#key?` method that can be used to check
for the existence of a key without raising `MissingKeyError`, and the
`credentials:edit` command now uses this method:

  ```console
  $ bin/rails credentials:edit
  Adding config/master.key to store the encryption key: ...

  Save this in a password manager your team can access.

  If you lose the key, no one, including you, can access anything encrypted with it.

        create  config/master.key
  ```

This commit also applies the same fix to the `encrypted:edit` command.
2022-07-27 11:22:41 -05:00
Jonathan Hefner 1a6cca614e
Merge pull request #45663 from jonathanhefner/encrypted_command-refactor-like-credentials_command
Refactor `EncryptedCommand` a la `CredentialsCommand`
2022-07-27 11:14:13 -05:00
Gannon McGibbon 8b94e87c11
Merge pull request #45659 from gmcgibbon/parent_option_default
Parent option defaults
2022-07-26 18:35:29 -05:00
Gannon McGibbon fe199f3d90 Fix typo in controller generator USAGE
Fixes typo introduced in #45627.
2022-07-26 18:13:03 -05:00
Gannon McGibbon e1635ff4a7 Show default parent classes for generators in help texts
Print default parent class for controller, job, and model generators.

Before:

[--parent=PARENT]               # The parent class for the generated job

After:

[--parent=PARENT]               # The parent class for the generated job
                                # Default: ApplicationJob
2022-07-26 18:13:02 -05:00
Gannon McGibbon a28381e1eb
Merge pull request #45661 from gmcgibbon/undef_generator_hook_method
Clean up hook generator method when hook is removed
2022-07-26 11:05:10 -05:00
Jonathan Hefner ff688a00b0 Refactor EncryptedCommand a la CredentialsCommand
This refactors `EncryptedCommand` and its tests to more closely mirror
`CredentialsCommand`, in preparation for forthcoming shared fixes.

Specifically, `EncryptedCommand` now uses a central
`encrypted_configuration` instance instead of passing arguments around,
and the tests now use a default encrypted file path instead of passing
it around.

This commit also imports tests concerning key file generation from the
`CredentialsCommand` tests.
2022-07-26 10:58:37 -05:00
Jonathan Hefner 5e3d064630
Merge pull request #45657 from jonathanhefner/encrypted_file-post-hoc-key-file
Make `EncryptedFile` more memoization-friendly
2022-07-26 10:33:52 -05:00
Hartley McGuire 337fa757d5
Remove unneeded subdirectories in generator tests
This pattern is useful in some cases, like moving the project folder
from myapp to myapp_moved, or testing generated behavior based on the
app name being hyphenated-app.

However, for the majority of cases this is not needed and adds
additional complexity to some assertion helpers. For example,
`assert_file` always expands paths relative to `destination_root`
instead of whatever the working directory is.
2022-07-25 17:39:46 -04:00
Gannon McGibbon 5a9113dc6b Clean up hook generator method when hook is removed
Removes "#{name}_generator" method for removed hooks introduced in #45315
2022-07-25 14:50:28 -05:00
Jonathan Hefner 2af4b98870 Make EncryptedFile more memoization-friendly
Prior to this commit, `EncryptedFile` would internally memoize when a
key file was non-existent.  This meant that a key file generated after
checking `encrypted_file.key.nil?` would not be recognized, unless a new
`EncryptedFile` instance was used.  (Though setting the key in a
designated environment variable instead would entirely bypass this
memoization.)

This commit changes `EncryptedFile` to only memoize when the key file
has been read.  Consequently, this commit memoizes the `EncryptedFile`
(or, specifically, `EncryptedConfiguration`) instance used by
`CredentialsCommand`.

This commit also adds more test coverage around key file generation for
`CredentialsCommand`.
2022-07-25 11:57:04 -05:00
Hartley McGuire 3b667adcdf
Test skip_system_test being preserved after update
Followup to 37d0516

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-07-24 17:34:40 -04:00
Jonathan Hefner 76b0ef8d00
Merge pull request #45647 from skipkayhil/rm-redundant-updating-option
Remove duplicate 'updating' option from generator
2022-07-24 11:04:37 -05:00
Jonathan Hefner 7548d76bdc
Merge pull request #45648 from skipkayhil/fix-skip-system-test-update
Fix skip_system_test not being preserved in update
2022-07-23 21:13:44 -05:00
Hartley McGuire f99f04a0c1
Fix extra line added to bin/setup in app:update
155b757 added some yarn commands to bin/setup for apps that are using
Node. However, the blank line between the added block and the
skip_active_record block results in an extraneous blank line added to
apps when running app:update.
2022-07-23 16:44:44 -04:00
Hartley McGuire 37d051612d
Fix skip_system_test not being preserved in update
Previously running app:update always would template an application.rb
file without the `config.generators.system_tests = nil` set, even for
applications that previously had it set.
2022-07-23 16:23:07 -04:00
Hartley McGuire 7fe640b8ca
Remove duplicate 'updating' option from generator
:update has been defined since a7adec9, and both options serve the same
purpose
2022-07-23 16:08:11 -04:00
Gannon McGibbon 97f13c2b82
Merge pull request #45627 from gmcgibbon/controller_generator_improvements
Controller generator improvements
2022-07-22 14:23:00 -05:00
Gannon McGibbon aaeffb2a56 Add --parent option to controller generator to specify parent class of controller.
Example:

`bin/rails g controller admin/users --parent=admin_controller` generates:

```ruby
class Admin::UsersController < AdminController
  # ...
end
```
2022-07-20 19:20:00 -05:00
Gannon McGibbon bf4858722c Add more detailed description to controller generator
- Arguments are actions, not views
- URLs are routes
- Adds example for --skip-routes option
2022-07-19 16:13:42 -05:00
Sam Bostock bc1f323338
Add ActiveJob::Serializers::BigDecimalSerializer
Previously, BigDecimal was listed as not needing a serializer.  However,
when used with an adapter storing the job arguments as JSON, it would get
serialized as a simple String, resulting in deserialization also producing
a String (instead of a BigDecimal).

By using a serializer, we ensure the round trip is safe.

During upgrade deployments of applications with multiple replicas making use of
BigDecimal job arguments with a queue adapter serializing to JSON, there exists
a possible race condition, whereby a "new" replica enqueues a job with an
argument serialized using `BigDecimalSerializer`, and an "old" replica fails to
deserialize it (as it does not have `BigDecimalSerializer`).

Therefore, to ensure safe upgrades, serialization will not use
`BigDecimalSerializer` until `config.active_job.use_big_decimal_serializer` is
enabled, which can be done safely after successful deployment of Rails 7.1.

This option will be removed in Rails 7.2, when it will become the default.
2022-07-19 15:31:41 -04:00
Sam Bostock b48b196910
Consolidate active_record 7.1 default loading
There appears to be no reason for these configs to be separate, and it makes
sense to group by framework.
2022-07-19 14:43:29 -04:00
Jonathan Hefner 166db2f2f6
Merge pull request #45574 from jonathanhefner/credentials-omit-secret_key_base-in-dev-and-test
Omit `secret_key_base` for dev and test credentials
2022-07-19 11:48:12 -05:00
Hartley McGuire 1c13e6a47e
Fix testing changing yaml_column_permitted_classes
The default was changed in c2b96e3 but this test was not updated. Since
the assertion would pass even if the initializer failed, it needs to be
changed to something else.
2022-07-13 17:56:55 -04:00
Ben Sheldon 8fceabd1e8 Defer constant loading of `ActiveRecord::DestroyAssociationAsyncJob` via a String instead of a class constant
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-07-13 11:58:20 -05:00
Étienne Barrié c2b96e3e89 Allow Symbols by default in YAML serialized columns
In Ruby 3.1, YAML.load also defaults to allowing Symbols even it's using
safe_load.

https://docs.ruby-lang.org/en/3.1/Psych.html#method-c-load
2022-07-13 13:27:19 +02:00
Zack Deveau 611990f1a6
Change ActiveRecord::Coders::YAMLColumn default to safe_load
In Psych >= 4.0.0, load defaults to safe_load. This commit
makes the ActiveRecord::Coders::YAMLColum class use Psych safe_load
as the Rails default.

This default is configurable via ActiveRecord.use_yaml_unsafe_load

We conditionally fallback to the correct unsafe load if use_yaml_unsafe_load
is set to true. unsafe_load was introduced in Psych 4.0.0

The list of safe_load permitted classes is configurable via
ActiveRecord.yaml_column_permitted_classes

[CVE-2022-32224]
2022-07-12 09:26:46 -07:00
Jonathan Hefner 52c680c4dc Omit secret_key_base for dev and test credentials
Follow-up to #45543.

This prevents confusion with the `secret_key_base` stored in
`tmp/development_secret.txt`.
2022-07-12 09:52:45 -05:00
Jonathan Hefner d7a4c913ec Support custom credentials templates
This commit adds support for in-app custom credentials templates.  When
a credentials file does not exist, `rails credentials:edit` will now try
to use `lib/templates/rails/credentials/credentials.yml.tt` to generate
the credentials file, before falling back to the default template.

This allows e.g. an open-source Rails app (which would not include
encrypted credentials files in its repo) to include a credentials
template, so that users who install the app will get a custom pre-filled
credentials file when they run `rails credentials:edit`.
2022-07-11 16:15:02 -05:00
Jonathan Hefner 915776ad04 Generate secret_key_base for all new credentials
Currently, when `config/credentials.yml.enc` is generated by
`CredentialsGenerator`, it includes a `secret_key_base` for convenience.
However, because `config/credentials/#{environment}.yml.enc` files are
generated by a different generator (`EncryptedFileGenerator`), they do
not include a `secret_key_base`.

This commit revises `CredentialsGenerator` to be more generator-like,
and changes `rails credentials:edit` to use it for generating both
`config/credentials.yml.enc` and `config/credentials/#{environment}.yml.enc`
files, thereby always including a `secret_key_base`.
2022-07-11 15:36:54 -05:00
Alex Ghiculescu 75c406d774 Make `timestamptz` a time zone aware type for Postgres
https://github.com/rails/rails/pull/41395 added support for the `timestamptz` type on the Postgres adapter.

As we found [here](https://github.com/rails/rails/pull/41084#issuecomment-1056430921) this causes issues because in some scenarios the new type is not considered a time zone aware attribute, meaning values of this type in the DB are presented as a `Time`, not an `ActiveSupport::TimeWithZone`.

This PR fixes that by ensuring that `timestamptz` is always a time zone aware type, for Postgres users.
2022-07-10 15:32:18 -05:00
Hartley McGuire 7d116c93cf
Convert rails-ujs to ES2015 modules
Building and linting are setup similar to other packages

Most of the changes are related to converting from sprockets requires to
ESM imports/export. However, there are a few notable changes as well:

- A few methods have been refactored to store the Rails object in a
  closure so that properties on it can be overriden by applications (as
  documented and tested). This also resulted in the "start" module
  getting inlined so that it can use the resulting functions.

- The logic for running Rails.start() automatically had to change
  because Rollup uses a slightly different module format than the
  previous coffeescript bundle. The Rollup bundle does not set
  window.Rails until the end up the bundle, so the condition had to be
  updated and window.Rails had to be set manually to ensure backwards
  compatability with scripts listening to the rails:attachBindings event
2022-07-07 21:52:08 -04:00
Gannon McGibbon e240daa6cc
Merge pull request #45528 from gmcgibbon/job_parent_option
Add `--parent` option to job generator to specify parent class of job.
2022-07-07 16:57:35 -04:00
Gannon McGibbon 02444d9267 Add `--parent` option to job generator to specify parent class of job.
Example:

`bin/rails g job process_payment --parent=payment_job` generates:

```ruby
class ProcessPaymentJob < PaymentJob
  # ...
end
```
2022-07-05 20:55:38 -04:00
Jonathan Hefner 4c67975c9c Improve generator implied option handling
This commit improves handling of implied options for `AppGenerator` and
`PluginGenerator` in a few different ways.

Implied options are now reported:

```console
$ rails new my_cool_app --skip-active-job
Based on the specified options, the following options will also be activated:

  --skip-action-mailer [due to --skip-active-job]
  --skip-active-storage [due to --skip-active-job]
  --skip-action-mailbox [due to --skip-active-storage]
  --skip-action-text [due to --skip-active-storage]

...
```

Option conflicts raise an error:

```console
$ rails new my_cool_app --skip-active-job --no-skip-active-storage
Based on the specified options, the following options will also be activated:

  --skip-action-mailer [due to --skip-active-job]
  --skip-active-storage [due to --skip-active-job]
    ERROR: Conflicts with --no-skip-active-storage
  --skip-action-mailbox [due to --skip-active-storage]
  --skip-action-text [due to --skip-active-storage]

railties/lib/rails/generators/app_base.rb:206:in `report_implied_options': Cannot proceed due to conflicting options (RuntimeError)
```

Meta option (e.g. `--minimal`) implications are also reported:

```console
$ rails new my_cool_app --minimal
Based on the specified options, the following options will also be activated:

  --skip-active-job [due to --minimal]
  --skip-action-mailer [due to --skip-active-job, --minimal]
  --skip-active-storage [due to --skip-active-job, --minimal]
  --skip-action-mailbox [due to --skip-active-storage, --minimal]
  --skip-action-text [due to --skip-active-storage, --minimal]
  --skip-javascript [due to --minimal]
  --skip-hotwire [due to --skip-javascript, --minimal]
  --skip-action-cable [due to --minimal]
  --skip-bootsnap [due to --minimal]
  --skip-dev-gems [due to --minimal]
  --skip-system-test [due to --minimal]

...
```

`--no-*` options now work with meta options, and are both comprehensive
and precise:

```console
$ rails new my_cool_app --minimal --no-skip-active-storage
Based on the specified options, the following options will also be activated:

  --skip-action-mailer [due to --minimal]
  --skip-action-mailbox [due to --minimal]
  --skip-action-text [due to --minimal]
  --skip-javascript [due to --minimal]
  --skip-hotwire [due to --skip-javascript, --minimal]
  --skip-action-cable [due to --minimal]
  --skip-bootsnap [due to --minimal]
  --skip-dev-gems [due to --minimal]
  --skip-system-test [due to --minimal]

...
```

Closes #45223.
Closes #45205.

Co-authored-by: Brad Trick <btrick@appacademy.io>
2022-07-05 15:16:28 -05:00
Eileen M. Uchitelle 5cf939ff18
Merge pull request #45439 from etiennebarrie/pass-test-options
Pass options to the test runner for test:{all,system,generators}
2022-06-30 11:01:35 -04:00
fatkodima 64ad045e47 Support multiple preview paths for mailers 2022-06-30 15:09:59 +03:00
eileencodes adb64db43d
Only remove connection for an existing pool if the config is different
Previously Rails would always remove the connection if it found a
matching class in the pool manager. Therefore if
`ActiveRecord::Base.establish_connection` was called with the same
config, each time it was called it would be clobbered, even though the
config hasn't changed and the existing connection is prefectly fine. As
far as I can tell from conversations and reading the history this
functionality was added for ActiveRecord tests to be able to clobber the
connection and use a new config, then re-establish the old connection.
Essentially outside Rake tasks and AR tests, this functionality doesn't
have a ton of value.

On top of not adding a ton of value, this has resulted in a few bugs. In
Rails 6.0 I made it so that if you established a connection on
`ApplicationRecord` Rails would treat that connection the same as
`ActiveRecord::Base.` The reason for this is that the Railtie
establishes a connection on boot to the first database, but then if
you're using multiple databases you're calling `connects_to` in your
`ApplicationRecord` or primary abstract class which essentially doubles
your connections to the same database. To avoid opening 2 connections to
the same database, Rails treats them the same.

However, because we have this code that removes existing connections,
when an application boots, `ApplicationRecord` will clobber the
connection that the Railtie established even though the connection
configs are the same.

This removal of the connection caused bugs in migrations that load up a
model connected to `ApplicationRecord` (ex `Post.first`) and then calls
`execute("SELECT 1")` (obviously a simplified example). When `execute`
runs the connection is different from the one opened to run the
migration and essentially it is lost when the `remove_connection` code
is called.

To fix this I've updated the code to only remove the connection if the
database config is different. Ultimately I'd like to remove this code
altogether but to do that we first need to stop using
`Base.establish_connection` in the rake tasks and tests. This will fix
the major bugs until I come up with a solution for the areas that
currently need to call `establish_connection` on Base.

The added benefit of this change is that if your app is calling
`establish_connection` multiple times with the same config, it is now
3x faster than the previous implementation because we can return the
found pool instead of setting it up again. To benchmark this I
duplicated the `establish_connection` method to use the new behavior
with a new name.

Benchmark script:

```ruby
require "active_record"
require "logger"
require "benchmark/ips"

config_hash = { "development" => { "primary" => { "adapter" => "mysql2", "username" => "rails", "database" => "activerecord_unittest"}}}
ActiveRecord::Base.configurations = config_hash

db_config = ActiveRecord::Base.configurations.configs_for(env_name: "development", name: "primary")

p "Same model same config"
ActiveRecord::Base.connected_to(role: :writing, prevent_writes: true) do
  Benchmark.ips do |x|
    x.report "establish_connection with remove" do
      ActiveRecord::Base.establish_connection(db_config)
    end

    x.report "establish_connection without remove" do
      ActiveRecord::Base.establish_connection_no_remove(db_config)
    end

    x.compare!
  end
end
```

Benchmark results:

```
Warming up --------------------------------------
establish_connection with remove
                         4.677k i/100ms
establish_connection without remove
                        19.501k i/100ms
Calculating -------------------------------------
establish_connection with remove
                         41.252k (±11.3%) i/s -    205.788k in   5.075525s
establish_connection without remove
                        179.205k (± 6.9%) i/s -    897.046k in   5.029742s

Comparison:
establish_connection without remove:   179205.1 i/s
establish_connection with remove:    41252.3 i/s - 4.34x  (± 0.00) slower
```

Other changes:

1) sqlite3 now disconnects and reconnects the connection when `purge` is
called. This is necessary now that a new connection isn't created
everyt time `establish_connection` is called. Without this change to
purge the new database is left in an inaccessible state causing a
readonly error from the sqlite3 client. This wasn't happening in mysql
or postgres because they were already reconnecting the db connection.
2) I added `remove_connection` to tests that use `ApplicationRecord`.
This is required because `ApplicationRecord` or any class that is a
`primary_abstract_class` will be treated the same as
`ActiveRecord::Base`. This is fine in applications because they are
shared connections, but in the AR test environment, we don't want those
connnections to stick around (we want AR::Base back).
3) In the async tests I removed 2 calls to `establish_connection`. These
were causing sqlite3 tests to leak the state of async_executor because
it's stored on the connection. I'm not sure why these were calling
`establish_connection` but it's not necessary and was leaking state when
now that we are no longer removing the connection.

Fixes: #41855
Fixes: #41876
Fixes: #42873
Fixes: #43004
2022-06-29 11:25:17 -04:00
Ben Sheldon [he/him] 660fee087d
Allow db:prepare to load schema if database already exists but is empty; also dumps schema after migrations 2022-06-28 06:36:24 -07:00
Niklas Haeusele 57b06b7e2e Changes the API documentation link to be relative instead of being absolute. This has been mentioned in #45001
I'm not sure if there's a way to update the guides.rubyonrails.org links as well with the correct version number.
2022-06-27 22:39:21 +02:00
Étienne Barrié 0b33193bf5 Pass options to the test runner for test:{all,system,generators} 2022-06-24 11:52:29 +02:00
Jonathan Hefner 369afa63d0 Fix allow_deprecated_singular_associations_name framework default
Follow-up to #45344.

When using `config.load_default "7.1"`, the value is `false`, so the
value in `new_framework_defaults_7_1.rb` should be `false` as well.

This commit also adds configuration tests.
2022-06-23 14:53:27 -05:00
eileencodes 9766eb4a83
Fix tests for minitest 5.16
In minitest/minitest@6e06ac9 minitest changed such that it now accepts
`kwargs` instead of requiring kwargs to be shoved into the args array.
This is a good change but required some updates to our test code to get
the new version of minitest passing.

Changes are as follows:

1) Lock minitest to 5.15 for Ruby 2.7. We don't love this change but
it's pretty difficult to get 2.7 and 3.0 to play nicely together with
the new kwargs changes. Dropping 2.7 support isn't an option right
now for Rails. This is safe because all of the code changes here are
internal methods to Rails like assert_called_with. Applications
shouldn't be consuming them as they are no-doc'd.
2) Update the `assert_called_with` method to take any kwargs but also
the returns kwarg.
3) Update callers of `assert_called_with` to move the kwargs outside the
args array.
4) Update the message from marshaled exceptions. In 5.16 the exception
message is "result not reported" instead of "Wrapped undumpable
exception".

Co-authored-by: Matthew Draper <matthew@trebex.net>
2022-06-23 08:32:11 -04:00
Eugene Kenny 7669a4a975
Merge pull request #45404 from skipkayhil/rm-load-path-modification
Remove load path modification from Rails 3 days
2022-06-20 21:30:40 +01:00
Jonathan Hefner 5a5440ba72
Merge pull request #45402 from brtrick/bt/add-skip-dev-gems-as-class-option
Set `skip_dev_gems` to be a `class_option`
2022-06-20 14:20:28 -05:00
Hartley McGuire 27285e7881
Remove special handling for ActiveRecordStore
activerecord-session_store was removed in 0ffe190, and has been
displaying a special error message when missing since Rails 4.0.

Replace the specific error message so that third party stores get nicer
error handling as well
2022-06-20 00:35:36 -04:00
Hartley McGuire 253a6338ca
Remove load path modification from Rails 3 days
This seems to be a pattern that was common around Rails 3 (for example,
it was last changed in 7ee5843). However, most of the usages were
removed in 36dd185 and this one looks like it was missed.

Some of the other files changed in 7ee5843 were later consolidated into
a root `load_paths.rb` file in 9f01dff which was later removed in
2abcdfd.
2022-06-20 00:29:39 -04:00
Brad Trick 52222b6d29 Set `skip_dev_gems` to be a `class_option` 2022-06-20 00:06:39 -04:00
Eileen M. Uchitelle c00c07b66e
Merge pull request #45380 from seejohnrun/remove-multi-use-assert_called_with
Remove the multi-call form of assert_called_with
2022-06-16 17:54:01 -04:00
Gannon McGibbon c47e663750
Merge pull request #45377 from gmcgibbon/update_migration_docs
Add more detailed description to migration generator
2022-06-16 12:49:17 -05:00
Gannon McGibbon 179024de99 Add more detailed description to migration generator 2022-06-16 12:11:05 -05:00
HParker 3a04c7b339 Add configurable deprecation warning for singular associations
This removes the singularize from `where` which runs on all `expand_from_hash` keys which might be reflections or column names. This saves a lot of time by avoiding singularizing column names.

Previously in https://github.com/rails/rails/pull/45163 the singularize was removed entirely. after some reflection, I think it is better to at least give a warning for one release since `where` is a very popular API and the problems you can run into with incorrect relation could be hard to debug.

Configurable with `ActiveRecord::Base.allow_deprecated_singular_assocaitions_name = false` / `config.active_record.allow_deprecated_singular_assocaitions_name = false`
2022-06-16 09:14:12 -07:00
John Crepezzi df0de681dc Remove the multi-call form of assert_called_with
The `assert_called_with` helper allows passing a multi-dimensional array to
mock multiple calls to the same method for a given block. This works
fine now, but when adding support for real kwargs arguments to line up with
recent upgrades in Minitest, this approach is no longer workable because
we can't pass multiple sets of differing kwargs.

Rather than complicated this method further, this commit removes the
multi-call form of `assert_called_with` and modifies the tests that
currently make use of that functionality to just use the underlying
`Minitest::Mock` calls.

Co-authored-by: Eileen M. Uchitelle <eileencodes@gmail.com>
2022-06-16 11:13:57 -04:00
Alex Ghiculescu 52e1ea4fba Make `sqlite3_adapter_strict_strings_by_default` work from an initializer
https://github.com/rails/rails/pull/45346 added an entry to new framework defaults, this PR ensures that configuration works and adds a test for it.
2022-06-15 15:20:45 -05:00
Eileen M. Uchitelle fd0f114f95
Merge pull request #45368 from eileencodes/move-db-and-shard-selector-middleware-to-railtie
Move db and shard selector initalizer to Railties
2022-06-15 13:27:06 -04:00
Asherah Connor ec34400abb
Behaviour constant deprecation fix (#45367)
* Add failing test for Behaviour deprecated alias

* Correct DeprecatedConstantProxy use

* DeprecatedConstantProxy docs indicate "full" constant names
2022-06-15 10:21:17 -07:00
eileencodes d179383284
Move db and shard selector initalizer to Railties
In #45162 it was discovered that the multi_db generator that was created
for 7.0 doesn't work correctly because that file is loaded _after_ the
initializer in active record is run. I tried moving everything to an
after_initialize but the middleware stack is frozen at that point. I
also attempted to fix this in #45353 but it just didn't feel right to
have to deprecate this behavior that _should_ work.

I then realized that most of the middleware in Rails is installed in the
middleware stack file in railties. If I move the middleware installation
to this file, everything works as necessary.

The only caveat is we need to check if `config` responds to
`active_record` but I think that should be fine - we already do that in
the framework defaults configuration.

Fixes #45162
2022-06-15 12:26:27 -04:00
fatkodima 21a6dbd313 Enable strict strings mode for `SQLite3Adapter`
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2022-06-14 23:59:17 +03:00
Rafael Mendonça França 4962e03683
Merge pull request #45280 from cbothner/run-transactional-callbacks-on-instances-most-likely-to-match-database-state
Run transactional callbacks on instances most likely to match DB state
2022-06-14 15:48:18 -04:00
Cameron Bothner 936a862f3c
Run transactional callbacks on instances most likely to match DB state 2022-06-14 13:34:25 -04:00
Étienne Barrié 9578865329 Skip Active Storage and Action Mailer if Active Job is skipped
If you use either of these frameworks, Active Job is used. That means
that only skipping Active Job will not really skip it, and whenever
app:update runs, Active Job will be added back.
2022-06-14 18:04:28 +02:00
Étienne Barrié 85a31f4cdc Correctly check if frameworks are disabled when running app:update
Co-authored-by: Paulo Barros <paulo@paulobarros.me>
2022-06-14 14:31:42 +02:00
Xavier Noria 3bcd48f6d8 Require Zeitwerk 2.6 2022-06-13 23:43:42 +02:00
Gannon McGibbon fff53e4455 Delegate model generator description to orm hooked generator
Use orm hooked generator description for model generator help.
2022-06-10 13:16:21 -05:00
Eileen M. Uchitelle 7c1165c8c8
Merge pull request #45322 from eileencodes/fix-ruby-trunk-did-you-mean
Fix did you mean tests for ruby-trunk (3.2)
2022-06-10 13:30:38 -04:00
eileencodes 935120fdfd
Fix did you mean tests for ruby-trunk (3.2)
In
f075be3dcb
did_you_mean and error_highlight now use `detailed_message` over
`message` to display errors.

For cases where we are testing `message`, in 3.2 and above we need to
test against `detailed_message` instead.

As far as I can tell in a Rails console when these errors are raised the
`detailed_message` is used so we shouldn't need to make other changes to
Rails. The only case where this isn't true is in the Railties changes -
we are explicitly formatting the did you mean message so we need to be
sure to call `detailed_message` here.

This fixes most of the failing tests for ruby-trunk.
2022-06-10 13:06:42 -04:00
Jean Boussier 115be62709 Wrap rails runner in executor
The main reason is to automatically report uncaught exceptions
since `rails runner` is often used for cron tasks and such.
2022-06-10 14:16:49 +02:00
Lachlan Sylvester 7f509ed7be remove call to conditionally configure legacy_connection_handling 2022-06-10 15:19:16 +10:00
eileencodes 866598d51b
Fix shared config feature in 3-tier config
Rails allows you to use a `shared` entry in the database yaml which gets
merged into other entries. This was broken when using a 3-tier config.

The change here ensures we're looping through all the configs. We know
we have a 3-tier config if the config is a hash and all the values
inside that config are a hash. If so we need to keep looping, otherwise
we have a standard 2-tier config.

Fixes #
2022-06-09 08:29:29 -04:00
Jean Boussier 82095de39f
Merge pull request #45293 from ghiculescu/cache_format_version_setting
Correctly read the `cache_format_version` setting on boot
2022-06-08 20:55:15 +02:00
Alex Ghiculescu 08352a2478 Correctly read the `cache_format_version` setting on boot
Fixes https://github.com/rails/rails/issues/45289
2022-06-08 13:21:29 -05:00
John Hawthorn 3ab1616325 Use AS::N build_handle API in Rails::Rack::Logger
Rails::Rack::Logger is one of the few (the only?) places where we aren't
able to use the preferred AS::N.instrument API with a block. This is
because we want to finish the event when the Rack body is closed, rather
than when the status and headers are first returned.

This switches to using the low-level `build_handle` API, which allows
safe interleaving of events and doesn't make use of thread or fiber
locals.
2022-06-06 15:51:02 -07:00
John Hawthorn 2c1c65b83b
Merge pull request #45251 from jhawthorn/deprecated_constant_for_module
Fix includes of deprecation proxy modules
2022-06-06 10:07:58 -07:00
Jonathan Hefner 5f08722c40
Merge pull request #45206 from jonathanhefner/puma-config-omit-workers-and-preload_app
Omit `workers` and `preload_app!` from Puma config
2022-06-03 17:12:36 -05:00
Jonathan Hefner 471ab2347f Omit workers and preload_app! from Puma config
Since Puma 5.0 (puma/puma@05936689c8),
Puma will automatically set `workers` to `ENV["WEB_CONCURRENCY"] || 0`.
Additionally, if `ENV["WEB_CONCURRENCY"]` > 1, Puma will automatically
set `preload_app`.

This can lead to confusing scenarios for users who are unaware of this
behavior and have customized `config/puma.rb`.  For example, if a user
uncomments the `workers` and `preload_app!` directives, it is clear that
Puma will preload the app, and the number of workers can be configured
by setting `ENV["WEB_CONCURRENCY"]`.  If the user sets
`ENV["WEB_CONCURRENCY"]` > 1, but then changes their mind and removes
the `workers` or `preload_app!` directives *without* clearing
`ENV["WEB_CONCURRENCY"]`, Puma will still preload the app and launch
`ENV["WEB_CONCURRENCY"]` number of workers.  Similarly, if a user
uncomments *only* the `workers` directive and sets
`ENV["WEB_CONCURRENCY"]` > 1, Puma will preload the app even though the
`preload_app!` directive is still commented out.

To avoid such scenarios, this commit removes the commented-out `workers`
and `preload_app!` directives from the default `config/puma.rb`.

Also, to improve discoverability of available configuration options,
this commit adds a link to the Puma DSL documentation at the top of the
file.
2022-06-03 12:38:21 -05:00
John Hawthorn c81a9bca11 Fix name of deprecated constant
The first argument to DeprecatedConstantProxy should be the old constant
name. I suspect this was automatically corrected by accident.
2022-06-02 18:13:31 -07:00
Jean Boussier 920fcce54c
Merge pull request #45230 from natematykiewicz/actionmailbox_unroutable_routes
Remove 4 unroutable routes from ActionMailbox
2022-06-01 13:33:05 +02:00
Nate Matykiewicz c9e48feef2
Remove 4 unroutable routes from ActionMailbox
I was auditing which routes in my app do not actually a
have corresponding controller action. ActionMailbox's
inbound_mails has these 4 routes defined, which do not
actually work.

GET /rails/conductor/action_mailbox/inbound_emails/:id/edit
PATCH /rails/conductor/action_mailbox/inbound_emails/:id
PUT /rails/conductor/action_mailbox/inbound_emails/:id
DELETE /rails/conductor/action_mailbox/inbound_emails/:id
2022-05-31 15:38:45 -05:00
Étienne Barrié a627ef47ea Hide rails:all from rails help
It's already listed as a Rake task.
2022-05-31 11:13:15 +02:00
Étienne Barrié 3d52212a0c Define Thor tasks for test subcommands
This avoids loading the Rakefile, which results in the app booting
twice, once in the development environment to load the Rake tasks, and
then another time in a fork to run the test runner in the test
environment.

Using Thor tasks with no parameters means passing arguments to the task
raises, whereas Rake accepts additional arguments that are not used:

    $ bin/rake test:system SOMETHING=anything

Similarly, with Rake you can use --trace which won't work here.
2022-05-31 11:13:15 +02:00
Brad Trick 958146b3e4 Fix incorrect paths in generator tests
This commit fixes three `assert_no_file` tests with incorrect paths.
2022-05-30 01:06:06 -04:00
fatkodima 580da90fce Pass additional connection options to `psql` when running `rails dbconsole` 2022-05-30 00:02:51 +03:00
Gannon McGibbon 01f58d62c2
Merge pull request #45180 from gmcgibbon/deprecate_behaviour_constant
Deprecate behaviour constant
2022-05-27 09:06:54 -04:00
Gannon McGibbon 3dad2919d5 Rename behaviour to behavior in documentation 2022-05-26 17:14:18 -04:00
Gannon McGibbon 52989bf7cc Rename Deprecate Rails::Generators::Testing::Behaviour to Behavior 2022-05-26 17:14:16 -04:00
eileencodes 58f6710e2f
Allow setting db with use_postgresql tests
If more than one test is using `use_postgresql` for it's tests and both
need to run `db:create` those tests will fail often due to them
running in parallel. I tried turning off parallelization for these tests
and they still ran in separate processes. Ultimately though, turning off
parallelization means we don't aren't testing parallelization. Instead I've
added `Process.pid` to the database name so it creates a database
specifically for that test. For cases where we need to set an explict
name a name can be passed in.

I also added an ensure to one of the tests so that the databases get
cleaned up and not left behind.

Fixes #45114
Fixes #45158
2022-05-26 11:01:39 -04:00
Jean Boussier 7a15d15169
Merge pull request #45142 from jonathanhefner/action_mailer-base-no-load-action_contoller-base
Avoid loading `ActionController::Base` when loading `ActionMailer::Base`
2022-05-21 08:17:40 -07:00
Jonathan Hefner 7415b93580 Avoid ActionController on ActionMailer::Base load
This refactor avoids unnecessarily loading `ActionController::Base` when
loading `ActionMailer::Base`.

Before:

```
$ bin/rails r 'Benchmark.memory { |x| x.report("load"){ ActionMailer::Base } }'
Calculating -------------------------------------
                load    10.013M memsize (     1.372M retained)
                        78.341k objects (    14.363k retained)
                        50.000  strings (    50.000  retained)
```

After:

```
$ bin/rails r 'Benchmark.memory { |x| x.report("load"){ ActionMailer::Base } }'
Calculating -------------------------------------
                load     5.043M memsize (   729.741k retained)
                        38.854k objects (     7.809k retained)
                        50.000  strings (    50.000  retained)
```
2022-05-20 11:57:34 -05:00
Bernie Chiu cdce275f70
Rotate default logs on each 100MB 2022-05-20 16:42:17 +08:00
Jonathan Hefner 702551d86f Enroll new apps in decrypted diffs of credentials
Users, especially those who are creating an app for the first time,
might not be aware of the `rails credentials:diff --enroll` command.
This commit enrolls new apps in decrypted diffs by default so that users
benefit automatically.  This behavior can be opted out of with the app
generator's `--skip-decrypted-diffs` flag.
2022-05-19 15:20:03 -05:00
Jonathan Hefner da651a21e1 Support declarative-style test name filters
This makes it possible to run a declarative-style test such as:

```ruby
class MyTest < ActiveSupport::TestCase
  test "does something" do
    # ...
  end
end
```

Using its declared name:

```bash
$ bin/rails test test/my_test.rb -n "does something"
```

Instead of having to specify its expanded method name:

```bash
$ bin/rails test test/my_test.rb -n test_does_something
```

Co-authored-by: Eugene Kenny <elkenny@gmail.com>
2022-05-19 08:50:26 -05:00
Jean Boussier 2a3a6e5efc Improve `rails s` error message when no server could be found.
Fix: https://github.com/rails/rails/issues/45092

Up until 3.0, `rails s` would always at least find webrick. But since it
was removed in Ruby 3.0, it's now possible to end up with no available server
at all.

In such case we should recommend adding puma to the Gemfile.
2022-05-14 09:43:34 -04:00
Dorian Marié 7b244c99f6 Add `--js` and --skip-javascript` options to `rails new`
`--js` alias to `rails new --javascript ...`

Same as `-j`, e.g. `rails new --js esbuild ...`

`--skip-js` alias to `rails new --skip-javascript ...`

Same as `-J`, e.g. `rails new --skip-js ...`
2022-05-13 22:36:36 +02:00
Shouichi Kamiya 44b56fba20 Fix rails test command to handle leading dot slash
Co-authored-by: oljfte <oljfte@gmail.com>
2022-05-13 19:59:10 +09:00
Petrik 55073ac10f Use the CSS badge for the API docs as well
Sdoc 2.4.0 supports a CSS badge that can be set by passing the version
as the HORO_BADGE_VERSION env variable.

The old badge image can be removed, as it no longer needs to be copied
by the docs server: https://github.com/rails/rails-docs-server/pull/28
2022-05-10 21:05:59 +02:00
Masataka Pocke Kuwabara 0768e83492 Add anchor to a link in `load_defaults`'s api guide [ci-skip] 2022-05-09 17:29:28 +09:00
Jean Boussier 38d5846e06
Merge pull request #44910 from jonathanhefner/credentials-avoid-escaping-paths
Avoid escaping paths when editing credentials
2022-05-09 10:11:39 +02:00
Jean Boussier 70fc351619
Merge pull request #44980 from ghiculescu/isolation-level-config
Allow setting some Active Support settings via initializers
2022-05-03 09:53:52 +02:00
Alex Ghiculescu 2187bc78e1 Allow setting some active support settings via initializers 2022-05-01 09:02:21 +10:00
Jonathan Hefner 4d92e965ef Remove dead URL
The `rubysl/rubysl-yaml` repo and its issue tracker are gone, and there
doesn't seem to be an alternative URL.
2022-04-29 13:47:51 -05:00
Aaron Patterson 89471b2d7d
Merge pull request #44941 from nicolasiensen/add-name-option-to-app-generator
Add --name option to the app generator
2022-04-28 12:52:53 -07:00
Baoshuo Ren 95253b2582
chore: remove git.io
see: https://github.blog/changelog/2022-04-25-git-io-deprecation/
2022-04-26 21:01:32 +08:00
fatkodima 2344bc802e Fix `db:prepare` to run seeds once 2022-04-26 01:44:39 +03:00
Eileen M. Uchitelle 8ff748b32f
Merge pull request #44935 from dorianmariefr/patch-1
Updates --skip-git option description to be more accurate
2022-04-25 16:37:14 -04:00
Nícolas Iensen 3ab4a9d75b Add --name option to the app generator
In this commit, we are adding the option to set the name of the app
when generating a new one with `rails new`.

The option `--name` will override the application name to be different
from the folder name.

```
rails new my-app-folder --name=my-actual-app-name"
```

The command above will generate a
new Rails application in the folder `my-app-folder`, but the file
`config/application.rb` would have the following structure:

module MyActualAppName
  class Application < Rails::Application
  end
end

This option would be most useful when generating a Rails application in
the current folder:

```
rails new . --name=my-app
```
2022-04-22 23:22:45 +00:00
Dorian Marié 1be896a12c
Updates --skip-git option description to be more accurate
It skips git init, .gitignore and .gitattributes not only .gitignore
2022-04-22 16:39:35 +02:00
Jonathan Hefner 2f66df0e10 Avoid escaping paths when editing credentials
`Shellwords.escape` escapes unquoted spaces with a backslash, but
Windows does not treat backslash as an escape character.  Escaping is
also a problem when paths are expressed in shortened 8.3 format (e.g.
`C:\Users\RubyOn~1\AppData\Local\Temp\...`) because a backslash will be
erroneously added before the `~`.

We can avoid the need to escape by using `system(command_name, *args)`
instead of `system(command_line)`, but we must still support
`ENV["EDITOR"]` values that embed command line arguments, such as
`subl -w`.

This commit changes to `system(command_name, *args)`, but uses
`Shellwords.split` to extract any embedded arguments from
`ENV["EDITOR"]`.  This requires that Windows users put quotes around the
entire path of their editor if it contains spaces, such as:

```
SET EDITOR="C:\Program Files\Microsoft VS Code\Code.exe" -w
```

In other words, the following are **not** supported on Windows:

```
SET "EDITOR=C:\Program Files\Microsoft VS Code\Code.exe"
SET EDITOR=C:\Program Files\Microsoft VS Code\Code.exe
SET EDITOR=C:\"Program Files"\"Microsoft VS Code"\Code.exe -w
SET EDITOR=C:\Program^ Files\Microsoft^ VS^ Code\Code.exe -w
SET EDITOR=C:\Program` Files\Microsoft` VS` Code\Code.exe -w
```

Fixes #41617 (again).
Closes #44890.
2022-04-17 11:17:04 -05:00
Xavier Noria e3f54144d8 Register the interlock hook only if reloading
If reloading is enabled, we need the interlock to synchronize reloads.

If reloading is disabled and so is eager loading, in the past you still needed
to synchronize autoloads because `classic` was not thread-safe. With Zeitwerk,
this is no longer needed.
2022-04-15 12:57:35 +02:00
Xavier Noria cbfe735c69 Deprecate config.enable_dependency_loading 2022-04-14 22:31:26 +02:00
Xavier Noria 2953ae5c8a Define config.enable_reloading to be !config.cache_classes
Every time I write `config.cache_classes` I have to pause for a moment to make
sure I get it right. It makes you think.

On the other hand, if you read `config.enable_reloading = true`, does the
application reload? You do not need to spend 1 cycle of brain CPU to nod.
2022-04-14 18:11:36 +02:00
Xavier Noria 9fb131cb39 Move misplaced conditional 2022-04-13 15:58:36 +02:00
fatkodima 9a4904d7d3 Fix handling disabled schema dumping for test environment 2022-04-13 00:11:25 +03:00
Xavier Noria fa6c75be32 Fixes style 2022-04-12 19:58:22 +02:00
Xavier Noria 73f306a4bb Eager load after reload, if enabled
Fixes #24819
2022-04-12 19:54:53 +02:00
Aaron Patterson cfa7284789
Merge pull request #44826 from stefkin/acp-eql-flag
Add a flag to disable deprecated AC::Parameters comparison
2022-04-04 09:09:44 -07:00
eileencodes ad52c0a197
Remove legacy_connection_handling
This functionality has been deprecated since Rails 6.1 and can now be
removed. I've deleted all code, docs, references, and tests related to
this feature.
2022-04-04 09:36:13 -04:00
Eileen M. Uchitelle ca6d15ba17
Merge pull request #44834 from ghiculescu/schema-dump-env
`rails db:schema:{dump,load}` now checks `ENV["SCHEMA_FORMAT"]` before config
2022-04-04 09:25:17 -04:00
Seva Stefkin 67762c2407
Add a flag to disable deprecated AC::Parameters comparison 2022-04-04 10:08:05 +02:00
Alex Ghiculescu d5fc7da4c3 `rails db:schema:{dump,load}` now checks `ENV["SCHEMA_FORMAT"]` before config
Since `rails db:structure:{dump,load}` was deprecated there wasn't a simple way to dump a schema to both SQL and Ruby formats. This PR implements @jeremy suggestion @ https://github.com/rails/rails/pull/39470#pullrequestreview-483599714, so you can now do this with an environment variable. For example:

```
SCHEMA_FORMAT=sql rake db:schema:dump
```
2022-04-03 19:30:52 -04:00
fatkodima dbefed5019 Fix `db:prepare` task to create/migrate test db when dev db already exists
Fixes #44815.
2022-04-01 15:59:12 +03:00
John Bampton 3b7f55c179 Change `yaml` to `YAML` 2022-03-29 15:19:22 +10:00
Gannon McGibbon dfd0c9e5bd
Merge pull request #44717 from duduribeiro/add-js-dependencies-setup-when-not-using-importmaps
Add yarn install into bin/setup when not using importmap
2022-03-28 15:34:17 -04:00
Cadu Ribeiro 155b7570fa Add yarn install into bin/setup when not using importmap
After af7428c4ac the yarn install
instructions was dropped from bin/setup. This commmit adds it into
the setup script again if the user is not using importmap.
2022-03-28 15:50:28 -03:00
Gannon McGibbon d38ed8bf0d
Merge pull request #44781 from ghousemohamed/added-missing-session-store-to-docs
Added missing `config.session_store` option `:cache_store` to docs [ci-skip]
2022-03-28 14:22:24 -04:00
Ghouse Mohamed f5a79cc197 Added missing config.session_store option to docs 2022-03-28 19:17:16 +05:30
Rafael Mendonça França af0733a8e7
Make sure respond_to? and method_missing works in abstract Railties
Those methods were raising an confusing exception when trying to call
`Rails::Railtie.respond_to?(:something)`. Now they just work.

Fixes #44761
2022-03-25 23:56:34 +00:00
Ghouse Mohamed d5257887da Removed code which sets host! in console mode as it is not needed 2022-03-23 02:59:52 +05:30
Xavier Noria b764fc24c8 Assume less in the on_unload callback for AR scopes
Please check the comment in the patch for details.

Thanks to @casperisfine for suggesting this refactor.
2022-03-21 12:04:45 +01:00
Xavier Noria 7d87086f10 Reset current scopes in AR using an on_unload callback
Rails 6.0 and Rails 6.1 didn't support the undocumented `before_remove_const` in
`zeitwerk` mode. I noticed this cleanup in AR was not being executed, and
restored the original code for Rails 7.

However, invoking `respond_to?` in an `on_unload` callback may have unexpected
side-effects, as seen in #44125. So, this patch reimplements the cleanup in a
more modern way.

Fixes #44125.
2022-03-20 21:41:08 +01:00
André Luis Leal Cardoso Junior 921263bb92 Only configure smtp_settings if provided on configuration 2022-03-15 22:31:04 -03:00
Rafael Mendonça França 5c1bd20f0d
Merge pull request #44693 from ghousemohamed/fix-docs-related-gem-versions
Fix `#version` method docs and some typos [ci-skip]
2022-03-15 16:28:07 -04:00
Ghouse Mohamed 6ee6cb554b Fix #version docs and some typos 2022-03-16 01:48:37 +05:30
Hartley McGuire 1faf619813 remove extra core_ext/object/blank requires
activerecord/lib/active_record/connection_adapters/postgresql/column.rb
- usage added in 64fd666
- unneeded because of active_support/rails: 8f58d6e

railties/lib/rails/rack/logger.rb
- usage added in c83d9a1
- usage removed in c131211

activesupport/lib/active_support/number_helper/number_converter.rb
- the NumberHelper was split into multiple classes in 2da9d67, however
  the require was left in NumberConverter even though
  NumberToPhoneConverter is the only class where it's used

activesupport/lib/active_support/duration/iso8601_serializer.rb
- usage added in 04c512d
- usage removed in 51e991f
2022-03-14 21:58:10 -04:00
Andrew White 7f5b3e04c8
Use `controller_class_path` in `NamedBase#route_url`
The `route_url` method now returns the correct path when generating
a namespaced controller with a top-level model using `--model-name`.

Fixes #44662.
2022-03-12 09:45:10 +00:00
Ghouse Mohamed 42b2869d06 Removed unwanted requires of and fixed rubocop errors 2022-03-10 01:19:55 +05:30
Nick Holden c773ae65af Add `active_record.destroy_association_async_batch_size` configuration
This allows applications to specify the maximum number of records that
will be destroyed in a single background job by the `dependent:
:destroy_async` association option. By default, the current behavior
will remain the same: when a parent record is destroyed, all dependent
records will be destroyed in a single background job. If the number of
dependent records is greater than this configuration, the records will
be destroyed in multiple background jobs.

At GitHub, we have a custom method for destroying associated records
in the background that we'd like to replace with
`dependent: :destroy_async`. Some associations have a large number of
dependent records, and our infrastructure requires that background jobs
complete quickly, so we limit the maximum number of dependent records
destroyed in a single background job and enqueue additional jobs when
the number of records exceeds that limit.
2022-03-09 11:13:16 -07:00
Hartley McGuire c3844a9dc0 fix rubocop errors
Using `bundle exec rubocop -a` and visually verified
2022-03-08 15:36:24 -05:00
Zack b2ab8dd3a4
Added image trasnformation validation via configurable allow-list
ImageProcessingTransformer now offers a configurable allow-list for
transformation methods in addition to a configurable deny-list for arguments.

[CVE-2022-21831]
2022-03-08 09:28:16 -08:00
Hartley McGuire 85d371ed06 fix namespaced schema load ignoring protected env
This makes the task requirements for db:schema:load:namespace the same
as db:schema:load (adding check_protected_environments)
2022-03-07 19:46:07 -05:00
Rafael Mendonça França 2a98e28a31
Merge pull request #44615 from FestaLab/propshaft-assets
Do not remove assets initializer when using propshaft
2022-03-04 16:27:50 -05:00
Breno Gazzola c744439631 Do not remove assets initializer when using propshaft 2022-03-04 14:43:29 -03:00
Ryuta Kamizono bd4d97ce11
Merge pull request #44569 from ghousemohamed/removed-unused-method-argument
Removed options method argument from application tests in railties
2022-03-04 16:56:08 +09:00
Jean Boussier c6b6833ec4
Merge pull request #44586 from Shopify/action-view-eager-loading
Eager load controllers `view_context_class`
2022-03-02 09:22:50 +01:00
Jean Boussier 2fd34270eb Eager load controllers `view_context_class`
These classes are relatively small, however they include lots of
modules as helpers. And if any of the included module hold constants
including it cause the global constant cache to be invalidated
which is really bad for performance.

So when eager loading is enabled we create all the possible classes
as part of the application boot.
2022-03-02 08:47:54 +01:00
Rafael Mendonça França e3353e53c9
Merge pull request #44495 from Edouard-chin/ec-integration-test-inspect
Don't output the whole Rails::Railtie object when a NoMethodError is raised
2022-03-01 23:31:17 -05:00
Aaron Patterson ea9f0103fd
Revert "Revert "Merge pull request #42843 from buckley-w-david/message-verifier-default-serializer""
This reverts commit fd4e63cc28.
2022-03-01 15:14:43 -08:00
Aaron Patterson fd4e63cc28
Revert "Merge pull request #42843 from buckley-w-david/message-verifier-default-serializer"
This reverts commit a40d7815ac, reversing
changes made to ad2529be4b.
2022-03-01 13:58:40 -08:00
Saba Kiaei 5256c90327 Switch ActiveSupport::MessageVerifier's default serialization to JSON 2022-03-01 13:02:17 -05:00
Yasuo Honda a51e77053c Revert "Skip apparently-troublesome test on CI for now"
This reverts commit bdb0610c10.
2022-03-01 15:18:59 +09:00
Edouard CHIN e60dd826c0 Don't output the whole Rails::Railtie object
- ### Summary

  The terminal output on a Rails application running ruby 3 will be
  cluttered with thousands of lines if one inadventarly call a
  unexisting method.
  This happen in various places (IntegrationTest, when running a db
  migration ...).
  This is related to a change in ruby 3 when a NoMethodError is
  raised.

  ### Simple reproduction

  ```
  class A
    def initialize(session)
      @a = session
    end
  end

  test = A.new("*" * 36)
  test.dsad # undefined method `dsad' for #<A:0x00007f847d8494b0 @a="************************************"> (NoMethodError)
  # Note that the "#<A:0x00007f847d8494b0 @a="************************************">" part
  # is 65 chars long.

  test = test = A.new("*" * 37)
  test.dsad # undefined method `dsad' for #<A:0x00007fa8c38299c0> (NoMethodError)
  ```

  On Ruby < 3, the NoMethodError message (everything starting from the
  "#" char) could only be 65 characters long. If it was above that
  ruby would only output the name of the class and its address.

  On Ruby >= 3, that limitation has been removed and the message can
  be any length long.

  ### On Rails

  Anytime a method is called on a object that holds the entire
  Rails::Application, the terminal would output the entire application
  which is annoying be can be dangerous because it will leak
  everything containing the credentials (stored inside the Application
  object).
2022-02-28 20:27:07 +01:00
Ghouse Mohamed c4266c6be0 Removed options method argument from application tests in railties 2022-02-28 16:59:07 +05:30
Brad Trick 880a1bedb9 Allow skip_forgery_protection if no protection set
Calling `skip_forgery_protection` without first calling
`protect_from_forgery`--either manually or through default
settings--raises an `ArgumentError` because `verify_authenticity_token`
has not been defined as a callback.

Since Rails 7.0 adds `skip_forgery_protection` to the
`Rails::WelcomeController` (PR #42864), this behavior means that setting
`default_protect_from_forgery` to false and visiting the Rails Welcome
page (`/`) raises an error.

This behavior also created an issue for `ActionMailbox` that was
previously fixed in the Mailbox controller by running
`skip_forgery_protection` only if `default_protect_from_forgery` was
true (PR #35935).

This PR addresses the underlying issue by setting the `raise` option for
`skip_before_action` to default to false inside
`skip_forgery_protection`.

The fix is implemented in `request_forgery_protection.rb`. The change to
`ActionMailbox`'s `base_controller.rb` removes the now-unnecessary
check of `default_protect_from_forgery`.

The tests added in `request_forgery_protection_test.rb` and
`routing_test.rb` both raise an error when run against the current
codebase and pass with the changes noted above.
2022-02-27 21:58:42 -05:00
Rafael Mendonça França fec53a141a
Merge pull request #44488 from p8/railties/csp-initializer-copy
Correct CSP initializer copy
2022-02-25 15:43:06 -05:00
John Hawthorn 0a44c3f90b
Merge pull request #44454 from jhawthorn/rack_logger_finish_with_state
Use finish_with_state inside Rack::Logger
2022-02-24 15:24:55 -08:00
Eileen M. Uchitelle be4b9057bb
Merge pull request #44504 from sanjioh/fix-typos
Fix some typos
2022-02-23 16:58:06 -05:00
Jonathan Hefner 497ab719d0
Merge pull request #44509 from jonathanhefner/apidocs-cross-link-docs
Cross-link API docs [ci-skip]
2022-02-23 12:08:41 -06:00
Jonathan Hefner 840ecf1e96
Merge pull request #44508 from jonathanhefner/apidocs-format-application-booting-process
Fix formatting of "Booting process" section [ci-skip]
2022-02-23 12:07:56 -06:00
Matthew Draper 2b2e789cb7
Merge pull request #44515 from SkipKayhil/git-init-b
use `git init -b` in generator when supported
2022-02-24 02:50:11 +10:30
Hartley McGuire 67b9347c16 use `git init -b` in generator when supported
This has the benefit of hiding the warning message from git when
initialBranch configuration is unset, and was a recommendation on the
original commit adding main as the default branch for generators.

Ref: eb261937ac
2022-02-22 11:05:46 -05:00
Matthew Draper bdb0610c10 Skip apparently-troublesome test on CI for now
I haven't yet identified why this particular test is causing issues when
other similarly-shaped ones seem fine, but if skipping it gets CI
working again, that's an improvement for now.
2022-02-22 20:43:05 +10:30
Petrik ba3ff2def8 Improve CSP initializer copy
The `content_security_policy_report_only` config does not enable
violation reporting.
It makes sure the policy isn't enforced, only reported.

Also link to the guide instead of external documentation.
2022-02-21 21:14:34 +01:00
Jonathan Hefner a199aaedb8 Cross-link API docs [ci-skip]
RDoc will automatically format and link API references as long as they
are not already marked up as inline code.

This commit removes markup from various API references so that those
references will link to the relevant API docs.
2022-02-21 11:45:25 -06:00
Jonathan Hefner fe3fc94eb0 Fix formatting of "Booting process" section [ci-skip]
Rendering the list of boot steps as a code block confused the syntax
highlighter.

This commit changes the list to use RDoc's ordered list syntax, and adds
inline code markup as appropriate.
2022-02-21 11:45:19 -06:00
Jonathan Hefner a801aa7cde Mark up inline code [ci-skip] 2022-02-21 11:11:11 -06:00
Jonathan Hefner e37adfed4e Add Oxford commas [ci-skip] 2022-02-21 11:11:11 -06:00
Jonathan Hefner 5fdbd217d1 Fix typos [ci-skip] 2022-02-21 11:11:11 -06:00
Fabio Sangiovanni 6e30c183b6 Fix some typos. 2022-02-21 17:58:23 +01:00
Matthew Draper ad0f0e6980 Be explicit every time we invoke 'rails'
This almost never matters, but if the path-global 'rake' or 'rails'
points to a specific (and wrong) ruby version, (or, possible in CI,
there is no installed 'rails' executable), things get confused.

Instead, any time we mean "run a global 'rails', as for 'new'", use a
fully-qualified path to our in-tree copy. And any time we're working
inside an application, use the bin/rails script directly. It would be
equivalently valid to always use the one in exe/, because that handles
searching for bin/rails internally... but it's uglier to fully-qualify,
plus 'rake' would then be more complicated.
2022-02-22 00:54:10 +10:30
Eddie Lebow 935cc1b10c
Document horo variables in RDoc configuration
https://github.com/zzak/sdoc/blob/master/lib/rdoc/generator/template/rails/class.rhtml
2022-02-18 16:45:21 -05:00
Petrik 7118ebabbb Fix rdoc for session_store documentation [ci-skip]
Use + instead of ` for values to pass.
2022-02-17 19:12:16 +01:00
John Hawthorn c8350cc723 Use finish_with_state inside Rack::Logger
This ensures that the finish instrumenter is sent to the same
subscribers that the initial request was sent to.

This solves an issue where in a threaded environment the dynamic
subscriptions from ActionDispatch::ServerTiming would cause a mismatch
in the number of subscriptions to a topic, which would pop too many
values off of the thread-local stacks, leading to invalid events being
sent to subscribers.

Fixes #44167
2022-02-16 16:31:28 -08:00
John Hawthorn 0dbe699d5c
Merge pull request #44309 from nholden/fix-destroy-async-config
Fix `config.active_record.destroy_association_async_job` configuration
2022-02-16 14:55:24 -08:00
Petrik 7c7d0b5ef1 Document some methods in Application::Configuration [ci-skip]
The documentation was mostly copied from the configurations
guide.
2022-02-16 21:07:40 +01:00
Nick Holden edf4ef9b26 Fix `config.active_record.destroy_association_async_job` configuration
`config.active_record.destroy_association_async_job` should allow applications
to specify the job that will be used to destroy associated records in the
background for `has_many` associations with the `dependent: :destroy_async`
option. That's being ignored, which means the default
`ActiveRecord::DestroyAssociationAsyncJob` always destroys records in the
background. This change stops ignoring the configuration.
2022-02-16 10:42:25 -07:00
Gannon McGibbon 965367e390
Merge pull request #44393 from Edouard-chin/ec-class-module-children
Add module/class nesting consistency on newly generated application:
2022-02-10 15:51:10 -05:00
Edouard CHIN 5875c68209 Add module/class nesting consistency on newly generated application:
- These two generated files are the only two that uses a compact style
  when defining a class, (it's not the case for their non-test
  counterpart i.e. 0f3642596f/railties/lib/rails/generators/rails/app/templates/app/channels/application_cable/connection.rb.tt (L1-L2)).

  This fixes that to add some consistency and will avoid users using
  rubocop with the `Style/ClassAndModuleChildren: nested` rule to have
  to manually edit those files when generating a new app.
2022-02-10 20:02:36 +01:00
Nikita Vasilevsky 6758397019 Increment assertions count on assert_nothing_raised 2022-02-09 01:01:10 +00:00
Rafael Mendonça França de7c495209
Merge pull request #44354 from rails/to-fs-as-the-primary
Make #to_fs the default replacement for #to_s(:format)
2022-02-08 11:50:24 -05:00
Zack 5d0c2b0dd2 Change MessageEncryptor default serializer to JSON for Rails 7.1
These changes include adding a hybrid serializer class
named JsonWithMarshalFallback in order for existing apps
to have an upgrade path from Marshal to JSON.
2022-02-07 12:19:36 -05:00