Commit Graph

83375 Commits

Author SHA1 Message Date
Matthew Draper 89e3df569c
Merge pull request #44686 from nvasilevski/flunk-if-test-isnt-using-savepoint-transaction
[Tests only] Flunk if test is not using SavepointTransaction
2022-03-30 15:54:08 +10:30
Vipul A M fe9b5b364a
Merge pull request #44795 from jbampton/fix-case-of-github
Change `github` to `GitHub`
2022-03-29 16:09:15 -04:00
John Bampton 99fa730aba Change `github` to `GitHub` 2022-03-30 05:55:48 +10:00
Petrik de Heus ca9fa85292
Merge pull request #44793 from jbampton/fix-word-case-of-xhtml
Fix word casing of `XHTML` and `XML Builder`
2022-03-29 20:49:07 +02:00
John Bampton c38d585ef0 Fix word casing of `XHTML` and `XML Builder`
Co-authored-by: Petrik de Heus <petrik@deheus.net>
2022-03-30 04:21:59 +10:00
Petrik de Heus 707f252b99
Merge pull request #44782 from p8/activemodel/remove-confusing-line-from-validations-docs
Remove confusing validations comments [ci-skip]
2022-03-29 19:14:04 +02:00
Petrik 8e91abbea1 Remove confusing validations comments [ci-skip]
All validations are called on `#save` by default.
The Absence, Acceptance, Presence validators don't do anything special,
so there is no reason to mention they are called on `#save` by default.

Instead we can mention all validations are called in `#save` by default
in ActiveRecord::Validations.

Co-authored-by: Gannon McGibbon <gannon@hey.com>
2022-03-29 16:04:20 +02:00
Petrik de Heus 888ea75df1
Merge pull request #44785 from p8/activerecord/improve-validation-documentation
Make AR Presence and Absence validation docs more consistent [ci-skip]
2022-03-29 16:01:08 +02:00
Eileen M. Uchitelle 797de7ebb9
Merge pull request #44788 from jbampton/fix-yaml-word-case
Change `yaml` to `YAML`
2022-03-29 09:57:20 -04:00
Eileen M. Uchitelle f49331a4b0
Merge pull request #44789 from jbampton/update-actions-python-version
Update codespell Action to Python `3.10`
2022-03-29 09:56:53 -04:00
John Bampton f5117c792f Update codespell Action to Python `3.10` 2022-03-29 16:50:43 +10:00
John Bampton 2d55c05c37 Fix case of `YAML` 2022-03-29 16:04:19 +10:00
John Bampton 3b7f55c179 Change `yaml` to `YAML` 2022-03-29 15:19:22 +10:00
Petrik b7083eac10 Make AR Presence and Absence validation docs more consistent [ci-skip]
Make it clearer that in the ActiveRecord AbsenceValidator, the
associated object is not _only_ absent if it is marked for destruction.

For the ActiveRecord PresenceValidator we can point to the ActiveModel
validations, like we do for other validations as well. This allows us to
remove some duplicated documentation.
2022-03-28 21:53:15 +02:00
Rafael Mendonça França d907b4420e
Merge pull request #44770 from kevindew/attribute-names-optimisation
Remove override of ActiveModel#attribute_names
2022-03-28 15:39:06 -04: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
Eileen M. Uchitelle 6c16d6df94
Merge pull request #44779 from nvolker/patch-1
Remove duplicates from rdoc comments
2022-03-28 09:33:11 -04:00
Nicholas Volker 660d4a7404
Remove duplicates
Two duplicated option definitions, `:comment` and `:collation`, are present in the docs.
2022-03-28 14:26:42 +02:00
Vipul A M 8372cf7374
Merge pull request #44773 from jbampton/fix-spelling-in-guides 2022-03-27 12:16:48 -04:00
John Bampton d6f47017ad Fix spelling
Change `there're` to `there are`

`there're` is not in the dictionary and we use `there are` in many places

https://www.dictionary.com/misspelling?term=there%27re
2022-03-27 05:19:40 +10:00
Kevin Dew 322e62842c
Remove override of ActiveModel#attribute_names
In https://github.com/rails/rails/pull/43036 an optimisation was applied
to ActiveModel#Serialization to speed up the generation of a
serialized_hash by avoiding loading the subjects attributes by using an
attribute_names method. A fallback method,
ActiveModel::Serialization#attribute_names` was added as #attribute_names
isn't part of the public API of ActiveModel.

Unfortunately, this fallback method happens to override the ActiveRecord
method (as ActiveModel::Serialization is a later mixin than
ActiveRecord::AttributeMethods), so this change didn't actually provide
an optimisation - the full attribute data was loaded as per [1]

This change also, in our case, produced some severe performance issues
as it introduced an N+1 query in a situation where we had one gem,
Globalize [2], which adds in dynamic attributes that are loaded by a query;
and another gem, Transitions [3], that checks attribute names at
initialization. The combination of these meant that for every model that
was initialized an extra query would run - no matter what includes or
eager_load steps were in place. This rapidly hindered our applications'
performance and meant we had to rollback the Rails 7 upgrade.

Following rafaelfranca's suggestion [4] this adds a
`attribute_names_for_serialization` method to Serialization modules in
ActiveRecord and ActiveModel. This allows the ActiveRecord one to
override the ActiveModel fallback and thus be optimised.

Some basic benchmarks of this follow - they use code from
https://github.com/ollietreend/rails-demo and have some pretty large
arrays set as serialized attributes [5] to demonstrate impacts.

Loading attribute names:

Rails 7.0.2.3

```
> Benchmark.ms { Widget.all.map(&:attribute_names) }
  Widget Load (131.1ms)  SELECT "widgets".* FROM "widgets"
=> 20108.852999983355
```

This patch

```
> Benchmark.ms { Widget.all.map(&:attribute_names) }
  Widget Load (144.0ms)  SELECT "widgets".* FROM "widgets"
=> 237.96699999365956
```

Using serializable_hash:

Rails 7.0.2.3

```
> widgets = Widget.all.to_a; Benchmark.ms { widgets.map { |w| w.serializable_hash(only: []) } }
  Widget Load (133.3ms)  SELECT "widgets".* FROM "widgets"
=> 22071.45000001765
```

This patch

```
> widgets = Widget.all.to_a; Benchmark.ms { widgets.map { |w| w.serializable_hash(only: []) } }
  Widget Load (83.5ms)  SELECT "widgets".* FROM "widgets"
=> 67.9039999959059
```

[1]: eeb2cfb686/activemodel/lib/active_model/serialization.rb (L151-L154)
[2]: https://github.com/globalize/globalize
[3]: https://github.com/troessner/transitions
[4]: https://github.com/rails/rails/pull/44770#pullrequestreview-922209612
[5]: 525f88887b/db/seeds.rb
2022-03-26 10:31:11 +00:00
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
Nikita Vasilevsky 6f147cce4c [postgresql tests only] Flunk if test is not using SavepointTransaction 2022-03-25 20:52:32 +00:00
Rafael Mendonça França eeb2cfb686
Merge pull request #44769 from jonathanhefner/active_record-sanitize_sql_like-performance
Improve `sanitize_sql_like` performance
2022-03-25 15:38:44 -04:00
Jonathan Hefner c02d6bfb27 Improve sanitize_sql_like performance
This improves performance and reduces memory allocations for
`sanitize_sql_like` across multiple use cases.

Benchmark script:

```ruby
require "benchmark/memory"
require "benchmark/ips"

def old_sanitize_sql_like(string, escape_character = "\\")
  pattern = Regexp.union(escape_character, "%", "_")
  string.gsub(pattern) { |x| [escape_character, x].join }
end

def new_sanitize_sql_like(string, escape_character = "\\")
  if string.include?(escape_character) && escape_character != "%" && escape_character != "_"
    string = string.gsub(escape_character, '\0\0')
  end

  string.gsub(/(?=[%_])/, escape_character)
end

[
  "no-special-characters",
  "one_wildcard",
  "one\\escape",
  "two_wildcards%and\\two\\escapes",
].each do |string|
  puts "\n= #{string.inspect} ".ljust(72, "=")

  Benchmark.memory do |x|
    # Warmup:
    old_sanitize_sql_like(string)
    new_sanitize_sql_like(string)

    x.report("old") { old_sanitize_sql_like(string) }
    x.report("new") { new_sanitize_sql_like(string) }
  end

  puts

  Benchmark.ips do |x|
    x.report("old") { old_sanitize_sql_like(string) }
    x.report("new") { new_sanitize_sql_like(string) }
    x.compare!
  end
end
```

Benchmark results:

```
= "no-special-characters" =============================================
Calculating -------------------------------------
                 old   875.000  memsize (     0.000  retained)
                        10.000  objects (     0.000  retained)
                         5.000  strings (     0.000  retained)
                 new    40.000  memsize (     0.000  retained)
                         1.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)

Warming up --------------------------------------
                 old     9.483k i/100ms
                 new   119.791k i/100ms
Calculating -------------------------------------
                 old     95.694k (± 0.3%) i/s -    483.633k in   5.053985s
                 new      1.228M (± 0.5%) i/s -      6.229M in   5.072317s

Comparison:
                 new:  1228094.1 i/s
                 old:    95694.3 i/s - 12.83x  (± 0.00) slower

= "one_wildcard" ======================================================
Calculating -------------------------------------
                 old     1.395k memsize (     0.000  retained)
                        15.000  objects (     0.000  retained)
                         6.000  strings (     0.000  retained)
                 new   440.000  memsize (     0.000  retained)
                         3.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)

Warming up --------------------------------------
                 old     6.527k i/100ms
                 new    30.205k i/100ms
Calculating -------------------------------------
                 old     65.255k (± 0.6%) i/s -    326.350k in   5.001318s
                 new    300.058k (± 0.5%) i/s -      1.510M in   5.033333s

Comparison:
                 new:   300057.6 i/s
                 old:    65255.2 i/s - 4.60x  (± 0.00) slower

= "one\\escape" =======================================================
Calculating -------------------------------------
                 old     1.395k memsize (     0.000  retained)
                        15.000  objects (     0.000  retained)
                         6.000  strings (     0.000  retained)
                 new   560.000  memsize (     0.000  retained)
                         6.000  objects (     0.000  retained)
                         2.000  strings (     0.000  retained)

Warming up --------------------------------------
                 old     6.506k i/100ms
                 new    24.312k i/100ms
Calculating -------------------------------------
                 old     65.467k (± 0.4%) i/s -    331.806k in   5.068392s
                 new    245.704k (± 0.5%) i/s -      1.240M in   5.046485s

Comparison:
                 new:   245703.8 i/s
                 old:    65466.9 i/s - 3.75x  (± 0.00) slower

= "two_wildcards%and\\two\\escapes" ===================================
Calculating -------------------------------------
                 old     1.755k memsize (     0.000  retained)
                        24.000  objects (     0.000  retained)
                         8.000  strings (     0.000  retained)
                 new   832.000  memsize (     0.000  retained)
                         8.000  objects (     0.000  retained)
                         3.000  strings (     0.000  retained)

Warming up --------------------------------------
                 old     5.265k i/100ms
                 new    12.360k i/100ms
Calculating -------------------------------------
                 old     52.698k (± 0.3%) i/s -    268.515k in   5.095425s
                 new    124.006k (± 0.3%) i/s -    630.360k in   5.083351s

Comparison:
                 new:   124006.2 i/s
                 old:    52697.9 i/s - 2.35x  (± 0.00) slower
```
2022-03-25 12:55:10 -05:00
Rafael Mendonça França aa5a4a7566
Remove methods from public API that users can't call
Those methods have private visibility , so they can't be called from
outside the class, so they should not be in the public API.
2022-03-25 14:24:20 +00:00
Eileen M. Uchitelle 996980ea17
Merge pull request #44766 from rails/update-local-dev-guide
Update local development guide
2022-03-25 08:59:53 -04:00
eileencodes 10314c1c84
Update local development guide
* Remove outdated information
* Clean up language and sentence structure to be clearer
* Remove commands that are no longer necessary
* Add note about how to move past the native extension issues with
  mysql2 since it's become a big problem.
2022-03-24 17:29:07 -04:00
Eileen M. Uchitelle 52f1f31c41
Merge pull request #44763 from p8/activejob/add-test-adapter-to-adapter-docs
Add the Active Job Test Adapter to QueueAdapter documentation [ci-skip]
2022-03-24 17:24:36 -04:00
Eileen M. Uchitelle 9ec791d9e9
Merge pull request #44720 from ghiculescu/system-test-html-screenshot
Introduce `html:` and `screenshot:` kwargs for system test screenshot helper
2022-03-24 14:57:47 -04:00
Eileen M. Uchitelle b333972a75
Merge branch 'main' into system-test-html-screenshot 2022-03-24 14:55:48 -04:00
Eileen M. Uchitelle 145d86fb37
Merge pull request #44764 from ghousemohamed/highlight-shard-selector-in-docs
Highlight `ShardSelector` class mentioned in the docs [ci-skip]
2022-03-24 12:21:15 -04:00
Ghouse Mohamed 9b2513f219 Highlight ShardSelector class mentioned in the docs 2022-03-24 21:37:24 +05:30
Petrik ed96312797 Add the Active Job TestAdapter to QueueAdapter documentation [ci-skip]
Also removed the duplicate `Job` in the Async description.
2022-03-24 14:41:58 +01:00
Jonathan Hefner ad25dfad87
Merge pull request #44727 from tmyksj/description-about-sql-like-sanitization
docs: Add a description about SQL LIKE sanitization [ci-skip]
2022-03-23 17:49:15 -05:00
Rafael Mendonça França f557a16647
Merge pull request #44756 from bobf/implement-ActionController-Parameters-to_h
Receive a block to ActionController::Parameters.to_h
2022-03-23 18:40:03 -04:00
tmyksj 4dc71e2880 Add a description about LIKE conditions
Condition arguments are escaped to prevent SQL injection, SQL LIKE
wildcards (i.e., `%` and `_`) are not escaped. But there are no
description about SQL LIKE escape in the rails querying guide. So,
this adds a description about SQL LIKE escape to the guide.
2022-03-24 07:32:26 +09:00
Aaron Patterson 11451cee11
Merge pull request #44757 from donny741/fix-overlaps-for-beginless-range
Fix `Range#overlaps?` for beginless ranges
2022-03-23 14:50:13 -07:00
Donatas Povilaitis 49fa92bb32 Fix `Range#overlaps?` for beginless ranges
- `#first` raises an error when called on a beginless range.
  Using `#begin` to get the first element instead.
- Adding additional equality condition to cover the case when both
  ranges are beginless
2022-03-23 22:40:24 +02:00
Bob Farrell 0e1188b563 Receive a block to ActionController::Parameters.to_h
Allow `ActionController::Parameters.to_h` to receive a block to provide
parity with `Hash#to_h`. The provided block recieves `key, value` and
yields a two-element array/keypair which can be transformed in the
resulting Hash.

https://ruby-doc.org/core-2.7.5/Hash.html#method-i-to_h
2022-03-23 20:35:49 +00:00
Jonathan Hefner 659cdebc68 Fix sanitize_sql_like with escape_character doc [ci-skip]
When an `escape_character` is specified, `sanitize_sql_like` will escape
occurrences of it rather than `"\\"`.

This commit also modifies the examples to demonstrate that behavior.
2022-03-23 13:31:34 -05:00
Aaron Patterson 02ad57a46b
Merge pull request #44748 from ghousemohamed/remove-setting-session-host-in-console
Removed unwanted code which sets session host in console mode
2022-03-23 08:49:10 -07:00
Aaron Patterson d02cdf2ffd
Merge pull request #44749 from SkipKayhil/fix-params-to-yaml
fix serializing Parameters as yaml
2022-03-23 08:46:37 -07:00
Matthew Draper b30ae96534
Merge pull request #44750 from SkipKayhil/rm-hwia-old-psych-test
remove testing HWIA serialization for old psych
2022-03-23 15:14:56 +10:30
Hartley McGuire 535113e7e9 fix serializing Parameters as yaml
This has been broken since the logging context was added in
6be9c498bc

Also added a higher level test to ensure that this isn't broken again in
the future.
2022-03-22 23:43:46 -04:00
Hartley McGuire 458f4c48ec remove testing HWIA serialization for old psych
Psych 2.2.2 has been a default gem since Ruby 2.4.0, so testing against
< 2.0.9 is unnecessary
2022-03-22 23:31:45 -04: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