Commit Graph

13510 Commits

Author SHA1 Message Date
Rafael Mendonça França
e152f83c64
Remove deprecated rake routes tasks 2020-10-30 00:26:08 +00:00
Rafael Mendonça França
01f0020a55
Remove deprecated rake dev:cache tasks 2020-10-30 00:26:08 +00:00
Rafael Mendonça França
c0728ad321
Remove deprecated support for using the HOST environment variable to specify the server IP 2020-10-30 00:26:07 +00:00
Rafael Mendonça França
5a7028a1a5
Remove deprecated server argument from the rails server command 2020-10-30 00:26:06 +00:00
Rafael Mendonça França
6204651412
Remove deprecated SOURCE_ANNOTATION_DIRECTORIES ENV 2020-10-30 00:26:06 +00:00
Rafael Mendonça França
8513769ad6
Remove deprecated connection option in the rails dbconsole command 2020-10-30 00:26:05 +00:00
Rafael Mendonça França
227d04f46a
Remove depreated rake notes tasks 2020-10-30 00:26:04 +00:00
Rafael Mendonça França
4bd56f4428
Remove deprecated fallback to I18n.default_local when config.i18n.fallbacks is empty 2020-10-30 00:25:50 +00:00
Rafael Mendonça França
64efe502f3
Change ActionDispatch::Response#content_type to return the full Content-Type header
And deprecate the config to keep the previous behavior.
2020-10-30 00:25:49 +00:00
Rafael Mendonça França
fd24cf1c5d
Return false when enqueuing a job is aborted 2020-10-30 00:25:31 +00:00
Rafael Mendonça França
71ddfd78d4
Remove deprecated finalize_compiled_template_methods 2020-10-30 00:25:16 +00:00
Eugene Kenny
12ce3e7723
Merge pull request #40478 from eugeneius/undeprecate_use_sha1_digests
Improve config.active_support.use_sha1_digests deprecation message
2020-10-30 00:10:29 +00:00
Omri Gabay
43d83e96c9
Add option to mute multiple database yaml warning
Adds an option to silence the warning that database configurations can
throw when it's unparsable.
2020-10-29 16:57:57 -04:00
Eugene Kenny
a03a2c4188 Improve use_sha1_digests deprecation message
We now provide the full setting that maintains the existing behaviour.
2020-10-29 18:46:21 +00:00
eileencodes
31461d8a79
Implement granular role and shard swapping
This change allows for a connection to be swapped on role or shard for a
class. Previously calling `connected_to` would swap all the connections
to a particular role or shard. Granular connection swapping is useful
for swapping one connection to reading while leaving all other
connection classes on writing.

The public methods on connection handler have been updated to behave the
same as they did previously on the different handlers. The difference
however is instead of calling
`ActiveRecord::Base.connection_handlers[:reading].clear_all_connections!`
you now call
`ActiveRecord::Base.connection_handler.clear_all_connections!` which
will clear based on current role set by a `connected_to` block. Outside
the context of a `connected_to` block, `clear_all_connections!` can take
an optional parameter to clear specific connections by role.

The major changes in this PR are:

* We introduced a `legacy_connection_handling` configuration option that
is set to true by default. It will be set to `false` for all new
applications.
* In the new connection handling there will be one only connection
handler. Previously there was a connection handler for each role. Now
the role is stored in the `PoolManager`. In order to maintain backwards
compatibility we introduced a `LegacyPoolManager` to avoid duplicate
conditionals. See diagram in PR body for changes to connection
management.
* `connected_to` will now use a stacked concurrent map to keep track of
the connection for each class. For each opened block the `class`,
`role`, and `shard` will be added to the stack, when the block is exited
the `class`, `role`, `shard` array will be removed from the stack.
* With these changes `ActiveRecord::Base.connected_to` will remain
global. If called all connections in the block will use the `role` and
`shard` that was switched to. If called with a parent class like
`AnimalsRecord.connected_to` only models under `AnimalsRecord` will be
switched and everything else will remain the same.

Examples:

Given an application we have a `User` model that inherits from
`ApplicationRecord` and a `Dog` model that inherits from
`AnimalsRecord`. `AnimalsRecord` and `ApplicationRecord` have writing
and reading connections as well as shard `default`, `one`, and `two`.

```ruby
ActiveRecord::Base.connected_to(role: :reading) do
  User.first # reads from default replica
  Dog.first # reads from default replica

  AnimalsRecord.connected_to(role: :writing, shard: :one) do
    User.first # reads from default replica
    Dog.first # reads from shard one primary
  end

  User.first # reads from default replica
  Dog.first # reads from default replica

  ApplicationRecord.connected_to(role: :writing, shard: :two) do
    User.first # reads from shard two primary
    Dog.first # reads from default replica
  end
end
```

Things this PR does not solve:

* Currently there is no API for swapping more than one but not all
connections. Apps with many primaries may want to swap 3 but not all 10
connections. We plan to build an API for that in a followup PR.
* The middleware remains the same and is using the global switching
methods. Therefore at this time to use this new feature applications
must manually switch connections. We will also address this in a
followup PR.
* The `schema_cache` is currently on the `PoolConfig`. We plan on trying
to move this up to the `PoolManager` or elsewhere later on so each
`PoolConfig` doesn't need to hold a reference to the `schema_cache`.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-10-28 15:11:35 -04:00
Eugene Kenny
9d1f3b7d52 Fix a typo in new_framework_defaults_6_1.rb 2020-10-27 23:44:58 +00:00
Ryuta Kamizono
154ee7b4b0 Revert "Don't enable Zeitwerk on MRI in 6.1 load_defaults"
This reverts commit 379db92364c4c212ac22a20dac72cd502156710d.

Reason: 94ba417ece (r41923157)
2020-10-25 15:51:36 +09:00
Eugene Kenny
379db92364 Don't enable Zeitwerk on MRI in 6.1 load_defaults
It's already handled by loading the 6.0 defaults two lines up.
2020-10-24 02:14:58 +01:00
Loren Norman
1f9a5dd36b fix broken handling of unknown http methods 2020-10-23 23:34:58 +01:00
Nick Wolf
7cc2b57b8d
Refactor railsrc file location to be xdg compliant (#39411)
* Refactor railsrc file location to be xdg compliant

The XDG Base Directory Specification (which is currently used by
FOSS projects such as Git, Tmux, Pry, Rspec) provides a default
location for various file formats, including config/rc files.

This comment refactors app_generator.rb to load railsrc from
XDG_CONFIG_HOME if both XDG_CONFIG_HOME is set and rails/railrc
exists within the XDG_CONFIG_HOME location.

To maintain backwards compatibility it defaults back to ~/.railsrc
if either XDG_CONFIG_HOME is not set or there is no rails/railsrc.

* Refactor default_rc_file based on jonathanhefner PR feedback

* Update generators/rails/app/USAGE to explain railsrc XDG compliance

* Refactor default_rc_file based on sinsoku PR feedback

* Add test for ARGVScrubber.default_rc_file method

fileutils is required because tmpdir does not allow you to create a new
temp directory with a set title 'inside' another temporary directory, a
file is created for railsrc because tempfile creates a custom and unique
name preventing the specification of a file called 'railsrc'

Currently this test is failing because the ARGVScrubber class seems to
be loading directly from the gem (which still has the previous code)
rather than from the file itself. (loading the code in a pry session
and using show-method ARGVScrubber.default_rc_file reveals it is coming
from
gems/railties-6.0.3.3/lib/rails/generators/rails/app/app_generator.rb:536
instead of
railties/lib/rails/generators/rails/app/app_generator.rb

* Switch to double quote strings

* Add file deletion for argv_scrubber test

* Refactor test_xdg_config_no_custom_rc with jonathanhefner feedback

* Add Changelog for railsrc xdg-specification compliance

* Apply backtick and whitespace refactoring suggestsions from jonathanhefner

* Apply jonathanhefner suggested Changelog refactoring

[Rafael Mendonça França + Nick Wolf + Jonathan Hefner]
2020-10-21 01:21:07 -04:00
Rafael França
9492339979
Merge pull request #38443 from jonathanhefner/plugin-fix-stack-traces
Fix stack traces when testing plugins
2020-10-08 19:06:40 -04:00
Jonathan Hefner
33fdae0584 Fix backtraces for generated plugin tests
`Minitest.plugin_rails_init` sets `Minitest.backtrace_filter` to
`Rails.backtrace_cleaner` right before tests are run, overwriting the
value set in test_helper.rb.

`Rails.backtrace_cleaner` silences backtrace lines that do not start
with `Rails.root` followed by e.g. "lib/" or "test/".  Thus when
`Rails.root` is a subdirectory of the project directory -- for example,
when testing a plugin that has a dummy app -- all lines of the backtrace
are silenced.

This commit adds a fallback such that when all backtrace lines are
silenced, the original `Minitest.backtrace_filter` is used instead.

Additionally, this commit refactors and expands existing test coverage.
2020-10-07 15:40:56 -05:00
Eugene Kenny
81317b077a Fix pending migration actionable error test
Since ddcca86f0ed5064140924c88cc5ad556d5ce32d4, omitting the location
parameter causes the actionable error request to fail.
2020-10-07 20:17:02 +01:00
Akira Matsuda
c01aace309 If it respond_to a method, the method should be public_sendable 2020-10-07 11:25:26 +09:00
Akira Matsuda
6c5e07a462 Both update_config_files and update_bin_files are public methods 2020-10-07 11:25:26 +09:00
Akira Matsuda
0bfa02aba3 This kind of wrapper should better not be able to call private methods 2020-10-07 11:12:02 +09:00
Akira Matsuda
a412e4da64 Get rid of unnecessary meta programming
Each stacked operation is a Proc object instead of operation name since fedde239dcee256b417dc9bcfe5fef603bf0d952,
and so we can now simply inline the target method for each operation
2020-10-07 11:12:02 +09:00
Akira Matsuda
87dfa8617c attr_reader methods are public methods 2020-10-07 11:12:01 +09:00
Rafael Mendonça França
0c8df42b50
Decorate Rails' backtrace cleaner before passing to minitest
The minitest database cleaner never returns empty traces so we need to
make sure ours also have the same behavior before passing to minitest.
2020-10-07 00:28:02 +00:00
Eugene Kenny
ae5ecfe26c Avoid chdir error in Railties tests on Ruby master
5d7953f86b
https://buildkite.com/rails/rails/builds/71925#9839c015-e562-4145-932c-75b9c118bfca/1043-1055
2020-10-04 00:26:03 +01:00
George Claghorn
a50d2f1993 Extract ActiveStorage::Record
Permit applications to hack in custom DB config for ASt models until ASt has first-class multi-DB support:

    ActiveSupport.on_load(:active_storage_record) do
      connects_to reading: :active_storage_replica, writing: :active_storage_primary
    end

rebase
2020-09-29 11:59:14 -04:00
Rafael França
397bfb0e83
Merge pull request #40157 from adrianna-chang-shopify/dependent-destroy-async
Offer dependent: :destroy_async for associations
2020-09-25 14:50:51 -04:00
George Claghorn
4cf7559280 Destroy associations in a background job.
Sometimes cascading association deletions can cause timeouts due to
an IO issue. Perhaps a model has associations that are destroyed on
deletion which in turn trigger other deletions and this can continue
down a complex tree. Along this tree you may also hit other IO
operations. Such deep deletions can lead to server timeouts while
awaiting completion and really the user may not notice all the
changes on their side immediately making them wait unnecesarially or
worse causing a timeout during the operation.

We now allow associations supporting the `dependent:` key to take `:destroy_async`,
which schedules a background job to destroy associations.

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
Co-authored-by: Cory Gwin @gwincr11 <gwincr11@github.com>
2020-09-24 14:24:15 -04:00
Rafael França
e3bc380f0c
Merge pull request #39661 from hahmed/railties/faster-app-generator-test-for-load-defaults
Make the test AppGeneratorTest#test_new_application_load_defaults faster
2020-09-24 12:10:28 -04:00
Eileen M. Uchitelle
5abe09bba7
Merge pull request #40213 from dbussink/allow-setting-digest-class
Add an ActiveSupport option to allow setting a digest class
2020-09-23 12:09:27 -04:00
Dirkjan Bussink
0f6c9bc786
Add an ActiveSupport option to allow setting a digest class
Right now it's already possible to set a digest class manually, but this
is not through a publicly supported and approved API. With these
configuration options, it is possible to configure a digest class to use
for digests inside Rails.

The reason for this is that it allows configuring other classes in
certain conditions like running in FIPS mode. FIPS disallows MD5 and
discourages SHA1. Even though this isn't used in Rails for security
related things, using newer hash algorithms means not having to explain
it to auditors which always makes life easier.

That's not the only reason though. If today Ruby is built and running
for example in an Ubuntu 18.04 FIPS approved environment, the default
Digest classes break, see https://bugs.ruby-lang.org/issues/13681 as
well.

By allowing a class to be configured, the following can be used so that
things work properly (using the new API proposed here):

```
Rails.application.config.active_support.hash_digest_class = OpenSSL::Digest::SHA256
```

This would then use the OpenSSL certified FIPS implementation as well.
2020-09-23 17:47:35 +02:00
Kevin Deisz
da39688e26
Create a gitattributes file for templated apps
.gitattributes files are a useful way to mark certain metadata about paths in a git repository (https://git-scm.com/docs/gitattributes). This can include all kinds of information, but one of the more useful ones is that linguist (https://github.com/github/linguist, the tool that powers the language statistics/diff view of GitHub) respects certain attributes. For example:

* `linguist-vendored` paths will not be counted toward the language statistics
* `linguist-generated` paths will not have their diff shown by default

These little niceties can be really helpful over time, so we should add it to the default app generator.
2020-09-23 10:24:27 -04:00
Paul Keen
133805f1f9 Uncomment bin/yarn in bin/setup for webpacker by default
By adding installing of webpacker, `bin/setup` requires manual update in order to do cold setup. This adds extra steps to generate app and setup it.
2020-09-22 22:01:40 +03:00
Paul Keen
e1b24b04bf Restores environments before testing their settings 2020-09-22 21:43:27 +03:00
Rafael Mendonça França
7307994011
Make sure log level in development is debug
Laso make sure the tests are actually checking the log level of the app,
not the one we set in the test helper.
2020-09-22 16:12:38 +00:00
Eric Payne
229fd2a02f
Change the default log level from :debug to :info
I recently learned that Rails logs at the :debug level (not the :info
level) by default in production environments. This is a surprising
behaviour, as other popular frameworks do not log at this level
by default. It would not be surprising if a developer accidentally
logged personally identifiable information (PII) in a production
environment due to this behaviour.

I noticed that [in 2014](https://github.com/rails/rails/pull/16622), the
Rails project made an intentional decision to set the default log level
to :debug. However, the landscape around logging PII has changed since
then with the introduction of legislation like GDPR, so I thought it
prudent to reopen this discussion.
2020-09-22 15:04:44 +00:00
Rafael França
c342df62ec
Merge pull request #40119 from Shopify/rescue-db-errors-during-eager-load
Rescue Database errors during active_record.define_attribute_methods
2020-09-22 10:21:59 -04:00
Ryuta Kamizono
c848baffd2 ✂️ [ci skip]
895134bcff (diff-7789e18ec0a61c6845755a257e6eb34fR3)
ee9e308f68 (diff-5e9551294914b338d923032fa904c6beR103)
5f63c771f7 (diff-600d5368b55e46ed961abb4295977ac3R506)
48b6bacbc5 (diff-279ac5c088a3ee7e9f954bbc10d1b773R246)
49adb7f4c6 (diff-0cbe7171fdd7821fd5836896849141c0R111)
9bfe89e68e (diff-9c1c95b001e3680ba8a473cf23508b68R8)
a0f18e6090 (diff-bef0ae2c16dacf77bfd2ba099af8a330R47)
8b4d344815 (diff-09660f59aee7ec893ad609315e8843c5R18)
49d1b5a98d (diff-531e71a9d5fdc8181f38ce4416553694R107)
b674f04756 (diff-7521c0bb452244663b689e77658e63e3R98)
a9012af688 (diff-60bdcf1e1954ec56d41fc4c7bd8d3e39R512)
6380aee182 (diff-5158c234d5e19bb1cffadcbc088400f1R262)
c7820d8124 (diff-f4614e7ba8081c0c9e20fe64b6374228R60)
8df7ed3b88 (diff-0495ed68f82d1bbde593ab5491eab24aR169)
6f94260ac6 (diff-7ecb6672f3fd37dfb79c4539395d3857R28)
03e44f9300 (diff-e9234c4c6668852448059c0a35840bcaR185)
2020-09-16 09:52:10 +09:00
Akira Matsuda
47f5459138 Module#const_set is a public method 2020-09-15 17:19:37 +09:00
Jonathan Hefner
be5eeb1a06 Load Rails tasks for each new Rake::Application
Follow-up to #40143.

Rails Rake tasks are loaded for each new `Rake::Application` instance
via `Rails.application.load_tasks`.  However, under the hood,
`Rails.application.load_tasks` used `require` instead of `load`, which
caused Rails tasks to be loaded for only the first `Rake::Application`
instance.

This commit changes the relevant `require` to `load`.

Fixes #40184.
2020-09-06 14:40:14 -05:00
Ryuta Kamizono
7da82b0fc3
Merge pull request #40159 from hahmed/fix-reference-to-rails-command-without-bin
Prefer bin/rails for the credentials command
2020-09-04 11:16:52 +09:00
eileencodes
ee9e308f68
Default db_config should be primary or first
The handling for single database applications has always set a schema.rb
or structure.sql files for loading the database schema. When we first
implemented multiple database support we intended to keep this for the
original, default database. Afterall Rails _has_ to connect to something
on boot. In development only one connection is connected on boot since
we don't eager load the app.

Originally we had thought that all applications should be required to
add a `primary` entry in the database configurations file. However,
this hasn't worked in practice and we have some code now that does not
assume there's a primary. The schema dumping/loading code however,
still assumed there was a "primary" in the configurations file.

We want the "default" database in any application to use the original
files even when converted to a multiple database application as this
reduces the need to make changes when implementing this functionality on
an existing application.

The changes here update Rails to ensure that we treat either "primary"
or the first database configuration for an environment as "default".
If there is a "primary" that will be used as the default configuration.
If there is no primary the configuration that is first for an
environment will be used as the default. For schema dump/load this means
that the default configuration (primary or first) will use `schema.rb`
as the filename and other configurations will use
`[CONFIGURATION_NAME]_schema.rb`.

This should also help us finish the pull request to infer migrations
paths since now we can say the first configuration is the default. This
is a natural assumption for application developers.

Followup to #39536
2020-09-03 15:55:46 -04:00
Haroon Ahmed
3119672411 Prefer bin/rails for the credentials command 2020-09-02 21:03:56 +01:00
eileencodes
e4af759bf3
Use default argument in test
We should use a default argument so that when there is only one option
we can delete the arguments instead of updating all the callers.
2020-09-01 16:26:28 -04:00
Xavier Noria
94ba417ece Move TruffleRuby check to select the autoloading mode to 6.1 defaults 2020-08-31 20:12:26 +02:00