Commit Graph

84598 Commits

Author SHA1 Message Date
Aaron Patterson f985028bcc
Stop gap solution for long output on test cases
This patch just changes the inspect method on test case instances.
Seeing test instance internals probably isn't helpful when an exception
is raised (for example a `NoMethodError`).

This isn't as good as #45122, but should fix #45121
2022-08-24 09:54:02 -07:00
Eileen M. Uchitelle 61733bde03
Merge pull request #45875 from tekin/rails-runner-spots-missing-files
Better rails runner output for missing files
2022-08-24 09:38:43 -04:00
Eileen M. Uchitelle 1c35f9ea72
Merge pull request #45878 from etiennebarrie/remove-application-assets
Remove `Rails::Application` `assets` accessor
2022-08-24 09:36:18 -04:00
Étienne Barrié 73d96c4947 Remove Rails::Application#assets accessor
It was used to store a Sprockets::Environment in
3e7985c9c1 but then it ended up being
monkey-patched from sprockets-rails in
78519dca00
2022-08-24 11:07:30 +02:00
Jonathan Hefner 2045cef03b
Merge pull request #45859 from kratob/no-csp-response-for-304
Do not return CSP headers for 304 Not Modified responses
2022-08-23 15:13:09 -05:00
Jean Boussier 50cbcc82e5
Merge pull request #45818 from mame/support-error_highlight
Use error_highlight gem to locate the columns where an error was raised
2022-08-23 17:46:55 +02:00
Yusuke Endoh 3beb2aff3b Use error_highlight gem to locate the columns where an error was raised
This change incorporates to Rails a feature called error_highlight that
has been available since Ruby 3.1. This allow Rails' error report screen
to display the fine-grained location where an error occurred (not only a
line number but also beginning and end column numbers of the code
fragment).

For ErrorHighlight, see https://bugs.ruby-lang.org/issues/17930 in
detail.

To use error_highlight, ExceptionWrapper now prefers
`Exception#backtrace_locations` (since Ruby 2.1), which returns an array
of `Thread::Backtrace::Location`s, instead of `Exception#backtrace`.
This is because error_highlight requires `Thread::Backtrace::Location`
to locate the column where an error was raised.

Co-Authored-By: John Hawthorn <john@hawthorn.email>
Co-Authored-By: Jean Boussier <jean.boussier@gmail.com>
2022-08-24 00:28:50 +09:00
Tekin Süleyman 7088003d19
Better rails runner output for missing files
This updates The Rails runner command to give a more helpful error
message when given a path as input but the file does not exist.

Instead of the cryptic "undefined local variable or method" the user is
told that the file could not be found.

I've gone for the simplest approach I could think of to identify when
the input is likely a file path: it ends with `".rb"`. This isn't a
perfect approach – there's nothing stopping someone passing a script
without an extension, and it's also possible to have valid Ruby syntax
that ends in `".rb" – but it does feel "good enough" and will make the
Rails runner slightly more user-friendly for those working with file
paths without negatively impacting those that are passing in Ruby
code.
2022-08-23 17:06:19 +02:00
Petrik de Heus d33b1ae495
Merge pull request #45819 from OlivierJM/patch-1
doc: correct the web socket url when creating consumer
2022-08-23 13:34:22 +02:00
Oli 8a1ff2f414 Correct the web socket url when creating consumer
In this https://github.com/rails/rails/blob/main/guides/source/action_cable_overview.md#connect-consumer  part of the documentation it mentions a url that has https in it, when it should be a web socket url as mentioned here https://guides.rubyonrails.org/action_cable_overview.html#client-server-interaction

This MR is to create the confusion and to have consistency across the docs for the web socket url

Update guides/source/action_cable_overview.md

Co-authored-by: Petrik de Heus <petrik@deheus.net>

Update guides/source/action_cable_overview.md

Co-authored-by: Petrik de Heus <petrik@deheus.net>

remove cable.js in action_cable overview

Update guides/source/action_cable_overview.md

Co-authored-by: Petrik de Heus <petrik@deheus.net>

Update guides/source/action_cable_overview.md

Co-authored-by: Petrik de Heus <petrik@deheus.net>

remove cable.js in action_cable overview

Update guides/source/action_cable_overview.md

Co-authored-by: Petrik de Heus <petrik@deheus.net>

Update guides/source/action_cable_overview.md

Co-authored-by: Petrik de Heus <petrik@deheus.net>

remove cable.js in action_cable overview
2022-08-23 11:15:33 +00:00
Eileen M. Uchitelle 342bd8545a
Merge pull request #45870 from eileencodes/clean-up-schema-load-tasks
Clean up schema load tasks
2022-08-22 15:10:22 -04:00
eileencodes 92240559f2
Clean up schema load tasks
While working on another PR I noticed that we have 6 different tasks for
loading the schema; 3 for all dbs, 3 for namespaced dbs. They are:

* `db:schema:load`
* `db:schema:load:namespace`
* `db:test:load_schema`
* `db:test:load_schema:namespace`
* `db:test:load` (calls `db:test:load_schema` only)
* `db:test:load:namespace` (calls `db:test:load_schema:namespace` only)

I've removed the last two because they didn't call anything except for
another task and they were already private. I believe the original
`db:test:load` and `db:test:load:namescace` were added to handle whether
to load sql or ruby schema fules, but we no longer need that (it's
handled by the `load_schema` method directly now).

In addition I removed `:load_config, :check_protected_environments` from
the `db:schema:load:namespace` task because `purge` already calls these
so we don't need to do it twice.

I tried to remove `db:test:load_schema:namespace` and call
`db:schema:load:namespace` instead but we can't do that because
`db:test:load_schema:namespace` needs to set the environment to "test",
not use the current environment. `db:schema:load` also can't be used
instead of `db:test:load_schema` because it tries to load both dev and
test schemas, but it's callers want just the test schema.
2022-08-22 14:35:33 -04:00
Jean Boussier 5f4273a338
Merge pull request #45869 from the-spectator/remove_eval_from_log_subscriber
Remove unnecessary class_eval from log_subscriber.rb
2022-08-22 19:29:59 +02:00
Akshay Birajdar 8e6d1f55e2 Remove unnecessary class_eval from log_subscriber.rb 2022-08-22 20:00:58 +05:30
Jean Boussier 2418939007
Merge pull request #45796 from Shopify/log-subscriber-silenced
Optimize AS::LogSubscriber
2022-08-22 10:43:49 +02:00
Jean Boussier 1aac6f3bf4
Merge pull request #45856 from Shopify/redis-5.0.0-compatibility
Redis 5.0.0 compatibility
2022-08-22 10:17:16 +02:00
Jean Boussier 341d627a57 RedisCacheStore: don't pass url if it's nil
In 5.0 it will prevent `$REDIS_URL` from being used.
2022-08-22 09:01:27 +02:00
Jean Boussier adb5028ff8 RedisCacheStore: update test suite for redis-rb 5.0
No real code change, only our stubs need to be changed.
2022-08-22 09:01:27 +02:00
Jean Boussier 7587b7e99a Action Cable redis-rb 5+ compatibility
- Pass configuration as symbols
- Stop messing with the raw client
2022-08-22 09:01:27 +02:00
Jean Boussier 71c24aa0bc Refactor Action Cable's Redis adapter to isolate the layer breach 2022-08-22 09:01:27 +02:00
Jean Boussier 7559957ba2 Require redis 4+ in Action Cable
RedisCacheStore already does, no point keeping compatiblity
with 3.x.

Redis 4+ always responds to `_client` so we can simplify the code.
2022-08-22 09:01:27 +02:00
Jean Boussier f2ab66da73 Stop testing hiredis
The driver being used is an implementation detail of `redis-rb`.
If somehow something break on one driver but not the other, it should
be reported to redis-rb and fixed there.

Also `redis-rb` `5.0` has a totally new client and hiredis binding
so all this code no longer works with redis-rb 5.0.
2022-08-22 09:01:27 +02:00
Jean Boussier d1a9a9928d
Merge pull request #45015 from fatkodima/missing-and-associated-parent-child
Fix `where.missing` and `where.associated` for parent/child associations
2022-08-20 08:43:16 +02:00
Jean Boussier 8cd4d65fe2
Merge pull request #45808 from fatkodima/check_constraint_exists-public
Make `check_constraint_exists?` public
2022-08-19 23:20:04 +02:00
Jean Boussier da2a0a5070
Merge pull request #45857 from Shopify/monitor-hook
Replace MutexHook by MonitorHook to allow reentrancy
2022-08-19 23:04:15 +02:00
Jean Boussier 3dcd28acdd
Merge pull request #45860 from HParker/add-sqlite=binary-column-default-support
Add binary column default value support to sqlite
2022-08-19 23:00:57 +02:00
Eileen M. Uchitelle e12866cc54
Merge pull request #45862 from eileencodes/move-internal-metadata-to-migration-context
Move InternalMetadata to migration context
2022-08-19 16:29:09 -04:00
eileencodes 46e4580d39
Move InternalMetadata to migration context
This PR is similar to #36439 which moved schema migrations to the
connection and migration context. This PR does the same thing for the
internal metadata class.

Previously the metadata class was only accessible on Base which makes it
a little weird when switching connections. This didn't really cause any
issues but I noticed it when working on a PR to change how connections
work in the database rake tasks.

In a multiple database environment it makes sense to access the
`ar_internal_metadata` on the existing connection rather than on
`Base.connection`. I previously didn't implement this because I felt we
wanted only one place to ask for the metadata information but recently
realized that we were still recording metadata on all the databases, so
we might as well record it correctly.

Applications should notice no change in behavior because this is only
accessed through the rake tasks. Additionally behavior between schema
migrations and internal metadata are now more similar. Eventually I'd
like neither of these to inherit from or rely on Base, but we have a
lot more work to do before that's possible.
2022-08-19 16:05:15 -04:00
John Hawthorn b945dfbb69
Merge pull request #45849 from jhawthorn/reuse_strict_local_templates
Reuse strict locals templates for any locals
2022-08-19 12:26:59 -07:00
John Hawthorn 9dca8a1a13
Merge pull request #45850 from HParker/fix-ripper-tracker-for-assoc_hash
Fix assoc_hash handling in ripper tracker
2022-08-19 12:26:40 -07:00
HParker e1bf63fca9 Add binary column default value support to sqlite
This adds support for reading binary column default values before the column data is read from the database.
This makes binary columns behave more like other column types with default values
2022-08-19 11:27:21 -07:00
Tobias Kraze 3fadf8ac7f Do not return CSP headers for 304 Not Modified responses 2022-08-19 15:10:47 +02:00
akmhmgc 2f9c84a604
Fix trivial typo (#45855) 2022-08-19 08:29:52 -04:00
Jean Boussier bc4c3ef969 Replace MutexHook by MonitorHook to allow reentrancy
Ref: https://github.com/rails/rails/pull/43550

If `executor_around_test_case` is enabled, all hooks must be
reentrant. However `allow_concurrency = false` register a
`MutexHook` that isn't reentrant.

So this commit replace it by a `Monitor` which does allow
reentrancy.
2022-08-19 13:18:26 +02:00
John Hawthorn 11612244c7 Avoid calling present? on @strict_locals
This will either be nil or a non-blank string. The case that it is the
empty string is handled (and replaced) at compile. I'd also argue that
it's possible that the empty string could be a valid value that's
considered to be a strict local, we just happen not to use that value
(preferring the "nokey" syntax).
2022-08-18 16:32:11 -07:00
John Hawthorn 5e13363354 Reuse strict locals templates for any locals
This commit allows template lookups and render calls which find a
"strict local" template to reuse that template even when provided with
different locals.

This also makes Template#locals return nil for strict local templates,
to avoid leaking the details of the first lookup. Almost nothing calls
this method, so this should not be a significant change.
2022-08-18 16:32:11 -07:00
Guillermo Iguaran 3d84e1298b
Merge pull request #45842 from trevorturk/validate-expires-at
Log a warning if ActiveSupport::Cache is given an expiration in the past
2022-08-18 15:32:13 -07:00
Trevor Turk 7eeb4af7c8 Log a warning if ActiveSupport::Cache is given an expiration in the past 2022-08-18 17:04:21 -05:00
John Hawthorn cc138468ca
Merge pull request #45845 from iancanderson/xml-name-escape-common-case
`xml_name_escape`: Optimize the common case by 2-3x
2022-08-18 14:25:09 -07:00
Ian C. Anderson d5695001f1 `xml_name_escape`: Optimize the common case
This came out of a conversation I had with @jhawthorn about how we could further
optimize the performance of `xml_name_escape`, which is a hot path for view
rendering.

This adds an early return to the method that improves the "valid name" case
performance by 2-3x, without adding any measurable overhead to the "invalid
case" (according to the benchmark below).

This also updates the constant names to reflect the fact that the regular
expressions are matching characters that are _not_ valid.

Benchmark results:

```
============================ Common tag name short =============================

Warming up --------------------------------------
     xml_name_escape   159.671k i/100ms
fast_xml_name_escape   343.621k i/100ms
one_regex_xml_name_escape
                       353.869k i/100ms
Calculating -------------------------------------
     xml_name_escape      1.582M (± 3.8%) i/s -      7.984M in   5.055645s
fast_xml_name_escape      3.341M (± 4.7%) i/s -     16.837M in   5.051945s
one_regex_xml_name_escape
                          3.489M (± 4.9%) i/s -     17.693M in   5.085095s

Comparison:
one_regex_xml_name_escape:  3488934.6 i/s
fast_xml_name_escape:  3341149.3 i/s - same-ish: difference falls within error
     xml_name_escape:  1581604.8 i/s - 2.21x  (± 0.00) slower

============================= Common tag name long =============================

Warming up --------------------------------------
     xml_name_escape    76.117k i/100ms
fast_xml_name_escape   266.879k i/100ms
one_regex_xml_name_escape
                       257.459k i/100ms
Calculating -------------------------------------
     xml_name_escape    826.616k (± 4.3%) i/s -      4.186M in   5.074708s
fast_xml_name_escape      2.751M (± 5.0%) i/s -     13.878M in   5.060281s
one_regex_xml_name_escape
                          2.545M (± 5.6%) i/s -     12.873M in   5.077729s

Comparison:
fast_xml_name_escape:  2751129.4 i/s
one_regex_xml_name_escape:  2544673.8 i/s - same-ish: difference falls within error
     xml_name_escape:   826616.2 i/s - 3.33x  (± 0.00) slower

=============================== Custom tag name ================================

Warming up --------------------------------------
     xml_name_escape    78.065k i/100ms
fast_xml_name_escape   242.412k i/100ms
one_regex_xml_name_escape
                       231.727k i/100ms
Calculating -------------------------------------
     xml_name_escape    767.433k (± 6.5%) i/s -      3.825M in   5.012273s
fast_xml_name_escape      2.463M (± 4.3%) i/s -     12.363M in   5.030648s
one_regex_xml_name_escape
                          2.239M (± 6.5%) i/s -     11.355M in   5.095769s

Comparison:
fast_xml_name_escape:  2462694.6 i/s
one_regex_xml_name_escape:  2238947.7 i/s - same-ish: difference falls within error
     xml_name_escape:   767432.9 i/s - 3.21x  (± 0.00) slower

========================== Custom tag name non-ascii ===========================

Warming up --------------------------------------
     xml_name_escape    66.959k i/100ms
fast_xml_name_escape    63.131k i/100ms
one_regex_xml_name_escape
                       206.018k i/100ms
Calculating -------------------------------------
     xml_name_escape    686.496k (± 4.9%) i/s -      3.482M in   5.085224s
fast_xml_name_escape    643.266k (± 4.5%) i/s -      3.220M in   5.016957s
one_regex_xml_name_escape
                          2.010M (± 4.8%) i/s -     10.095M in   5.034582s

Comparison:
one_regex_xml_name_escape:  2010415.6 i/s
     xml_name_escape:   686496.1 i/s - 2.93x  (± 0.00) slower
fast_xml_name_escape:   643266.0 i/s - 3.13x  (± 0.00) slower

================================ Bad tag name 1 ================================

Warming up --------------------------------------
     xml_name_escape    40.491k i/100ms
fast_xml_name_escape    40.465k i/100ms
one_regex_xml_name_escape
                        38.244k i/100ms
Calculating -------------------------------------
     xml_name_escape    393.998k (± 6.7%) i/s -      1.984M in   5.062913s
fast_xml_name_escape    386.906k (± 5.2%) i/s -      1.942M in   5.035445s
one_regex_xml_name_escape
                        377.664k (± 6.6%) i/s -      1.912M in   5.088602s

Comparison:
     xml_name_escape:   393998.2 i/s
fast_xml_name_escape:   386906.4 i/s - same-ish: difference falls within error
one_regex_xml_name_escape:   377663.7 i/s - same-ish: difference falls within error

================================ Bad tag name 2 ================================

Warming up --------------------------------------
     xml_name_escape    15.627k i/100ms
fast_xml_name_escape    15.165k i/100ms
one_regex_xml_name_escape
                        16.028k i/100ms
Calculating -------------------------------------
     xml_name_escape    161.988k (± 5.5%) i/s -    812.604k in   5.032083s
fast_xml_name_escape    161.438k (± 5.7%) i/s -    818.910k in   5.090467s
one_regex_xml_name_escape
                        160.224k (± 5.1%) i/s -    801.400k in   5.016219s

Comparison:
     xml_name_escape:   161987.9 i/s
fast_xml_name_escape:   161437.6 i/s - same-ish: difference falls within error
one_regex_xml_name_escape:   160224.2 i/s - same-ish: difference falls within error
```

Benchmark code:

```ruby

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
  gem "benchmark-ips"
end

require "active_support"
require "active_support/core_ext/string/output_safety"

class ERB
  module Util
    TAG_NAME_COMMON_CASE = /\A[a-z][a-z\-]*\z/

    def fast_xml_name_escape(name)
      name = name.to_s
      return "" if name.blank?
      return name if name.match?(TAG_NAME_COMMON_CASE)

      starting_char = name[0]
      starting_char.gsub!(TAG_NAME_START_REGEXP, TAG_NAME_REPLACEMENT_CHAR)

      return starting_char if name.size == 1

      following_chars = name[1..-1]
      following_chars.gsub!(TAG_NAME_FOLLOWING_REGEXP, TAG_NAME_REPLACEMENT_CHAR)

      starting_char << following_chars
    end
    module_function :fast_xml_name_escape

    TAG_NAME_FOLLOWING_REGEXP_SET = "#{TAG_NAME_START_REGEXP_SET}\-.0-9\u{B7}\u{0300}-\u{036F}\u{203F}-\u{2040}"
    SAFE_XML_TAG_NAME_REGEXP = /\A[#{TAG_NAME_START_REGEXP_SET}][#{TAG_NAME_FOLLOWING_REGEXP_SET}]*\z/

    def one_regex_xml_name_escape(name)
      name = name.to_s
      return "" if name.blank?
      return name if name.match?(SAFE_XML_TAG_NAME_REGEXP)

      starting_char = name[0]
      starting_char.gsub!(TAG_NAME_START_REGEXP, TAG_NAME_REPLACEMENT_CHAR)

      return starting_char if name.size == 1

      following_chars = name[1..-1]
      following_chars.gsub!(TAG_NAME_FOLLOWING_REGEXP, TAG_NAME_REPLACEMENT_CHAR)

      starting_char << following_chars
    end
    module_function :one_regex_xml_name_escape
  end
end

SCENARIOS = {
  "Common tag name short" => "p",
  "Common tag name long"  => "textarea",
  "Custom tag name"       => "super-custom",
  "Custom tag name non-ascii" => "süper-custom",
  "Bad tag name 1"        => "1abc",
  "Bad tag name 2"        => "1 < 2 & 3",
}

SCENARIOS.each_pair do |name, value|
  puts
  puts " #{name} ".center(80, "=")
  puts

  Benchmark.ips do |x|
    x.report("xml_name_escape")      { ERB::Util.xml_name_escape(value) }
    x.report("fast_xml_name_escape") { ERB::Util.fast_xml_name_escape(value) }
    x.report("one_regex_xml_name_escape") { ERB::Util.one_regex_xml_name_escape(value) }
    x.compare!
  end
end
```
2022-08-18 20:21:56 +00:00
John Hawthorn 4de65d348e
Merge pull request #45851 from jhawthorn/codespell_version
Install specific version of codespell
2022-08-18 13:04:55 -07:00
John Hawthorn 60c0a3de29 Install specific version of codespell
As the codespell dictionary changes between versions, we should declare
explicitly the version we are ready for so that this CI doesn't break
every time there is a new version.

Ideally this would be upgraded periodically to catch new errors, but I
don't think the need is urgent enough for us to fail CI on PRs.
2022-08-18 12:58:50 -07:00
HParker ba5f653878 Fix assoc_hash handling in ripper tracker
Previously ripper tracker would misidentify the method name when called with a constant and assoc_hash.

This correctly finds the method name in those cases
2022-08-18 12:27:37 -07:00
eileencodes 43a6b4fc0f
Use Time instead of Date for comparison
In Ruby trunk date changed to use the gregorian calendar in
https://github.com/ruby/date/pull/73.

This had the result that dates not in the gregorian calendar can't be
compared to times not in the gregorian calendar. Since this test isn't
testing datetime comparison but rather that bc timestamps can be stored
in the database, we can compare with `Time.new(0) - 1.week` rather than
`Date.new(0) - 1.week`. I think this might be a bug in Ruby but I'm not
sure there's a great fix. For now however, let's get this test passing.
2022-08-18 14:48:53 -04:00
John Hawthorn 51932e58f4
Merge pull request #45848 from jhawthorn/fix_ci_minitest_5_16_3
Fix "unmarshalable" test for minitest 5.16.3
2022-08-18 10:28:38 -07:00
John Hawthorn 85db5be358 Fix "unmarshalable" test for minitest 5.16.3 2022-08-18 09:47:50 -07:00
Ali Deishidi c13856e477
Reference consistency check correction (#45840)
* Reference consistency check correction 

By default all belongs_to relations are set to required, and required means Rails checks the reference consistency. That sentence is correct only if the optional is set to true, then in this case Rails not only allows this field to be null but also it does not check the reference consistency. But this is not the default behavior.

Co-authored-by: Eileen M. Uchitelle <eileencodes@users.noreply.github.com>
2022-08-18 08:51:11 -04:00
Petrik de Heus 487282cb40
Merge pull request #45843 from p8/guides/fix-deprecation-formatting
Fix some small formatting issues [ci-skip]
2022-08-18 08:31:48 +02:00
Petrik 680ce01896 Fix some small formatting issues [ci-skip] 2022-08-18 08:24:46 +02:00
Guillermo Iguaran 8b55410c38
Merge pull request #45831 from p8/guides/document-contributing-deprecations
Add description of breaking changes and the deprecation cycle to guides.
2022-08-17 21:25:35 -07:00