Commit Graph

79965 Commits

Author SHA1 Message Date
Jean Boussier
de3d1dd8f9
Merge pull request #42015 from Shopify/refactor-inheritance-column
Make ModelSchema.inheritance_column a class_attribute
2021-04-20 19:48:38 +02:00
Eileen M. Uchitelle
153a19f07a
Merge pull request #41897 from ricardotk002/allow-parallel-tests-to-fail-fast
Allow test parallelization with processes to fail fast
2021-04-20 12:17:13 -04:00
Ryuta Kamizono
5d7b86b698
Merge pull request #42023 from caffkane/remove-unused-ar-disable-join-constant
Remove unused constant from disable_joins_association_relation
2021-04-20 17:51:45 +09:00
Ryuta Kamizono
f02a136f89 Merge pull request #41990 from p8/autosave-association-callbacks-get-called-once
Ensure has_one autosave association callbacks get called once
2021-04-20 17:50:16 +09:00
Logan Kane
6f23b69a1e Remove unused constant from disable_joins_association_relation 2021-04-19 22:26:33 -07:00
Ryuta Kamizono
dd30498b1e
Merge pull request #42022 from yahonda/diag76751
Address non-deteristic CI failure of HasManyThroughDisableJoinsAssoci ationsTest
2021-04-20 12:18:14 +09:00
Yasuo Honda
662f7c14c6 Address non-deteristic CI failure of HasManyThroughDisableJoinsAssociationsTest
Addressed CI failure at https://buildkite.com/rails/rails/builds/76751#fa683e3c-b213-4474-8faf-dd37e6444b76
and modified two tests to avoid similar failures.

- HasManyThroughDisableJoinsAssociationsTest#test_to_a_on_disable_joins_through

```
Failure:
HasManyThroughDisableJoinsAssociationsTest#test_to_a_on_disable_joins_through [/rails/activerecord/test/cases/associations/has_many_through_disable_joins_associations_test.rb:71]:
--- expected
+++ actual
@@ -1 +1 @@
-[#<SpecialComment id: 11, post_id: 7, body: "go crazy", type: "SpecialComment", label: "default", tags_count: 0, children_count: 0, parent_id: nil, author_type: nil, author_id: nil, resource_id: nil, resource_type: nil, origin_id: nil, origin_type: nil, developer_id: nil, updated_at: "2021-04-19 22:54:11.359941000 +0000", deleted_at: nil, comments: nil, company: nil>, #<Comment id: 71, post_id: 65, body: "text", type: nil, label: "default", tags_count: 0, children_count: 0, parent_id: nil, author_type: nil, author_id: nil, resource_id: nil, resource_type: nil, origin_id: 70, origin_type: "Member", developer_id: nil, updated_at: "2021-04-19 22:54:12.138620000 +0000", deleted_at: nil, comments: nil, company: nil>, #<Comment id: 70, post_id: 64, body: "text", type: nil, label: "default", tags_count: 0, children_count: 0, parent_id: nil, author_type: nil, author_id: nil, resource_id: nil, resource_type: nil, origin_id: 69, origin_type: "Member", developer_id: nil, updated_at: "2021-04-19 22:54:12.131832000 +0000", deleted_at: nil, comments: nil, company: nil>]
+[#<SpecialComment id: 11, post_id: 7, body: "go crazy", type: "SpecialComment", label: "default", tags_count: 0, children_count: 0, parent_id: nil, author_type: nil, author_id: nil, resource_id: nil, resource_type: nil, origin_id: nil, origin_type: nil, developer_id: nil, updated_at: "2021-04-19 22:54:11.359941000 +0000", deleted_at: nil, comments: nil, company: nil>, #<Comment id: 70, post_id: 64, body: "text", type: nil, label: "default", tags_count: 0, children_count: 0, parent_id: nil, author_type: nil, author_id: nil, resource_id: nil, resource_type: nil, origin_id: 69, origin_type: "Member", developer_id: nil, updated_at: "2021-04-19 22:54:12.131832000 +0000", deleted_at: nil, comments: nil, company: nil>, #<Comment id: 71, post_id: 65, body: "text", type: nil, label: "default", tags_count: 0, children_count: 0, parent_id: nil, author_type: nil, author_id: nil, resource_id: nil, resource_type: nil, origin_id: 70, origin_type: "Member", developer_id: nil, updated_at: "2021-04-19 22:54:12.138620000 +0000", deleted_at: nil, comments: nil, company: nil>]
```

- HasManyThroughDisableJoinsAssociationsTest#test_pluck_on_disable_joins_through
Reproduced one time at local environment.

```ruby
...........F

Failure:
HasManyThroughDisableJoinsAssociationsTest#test_pluck_on_disable_joins_through [/home/yahonda/src/github.com/rails/rails/activerecord/test/cases/associations/has_many_through_disable_joins_associations_test.rb:47]:
Expected: [31, 11, 32]
  Actual: [11, 31, 32]

bin/test test/cases/associations/has_many_through_disable_joins_associations_test.rb:46

```

-  HasManyThroughDisableJoinsAssociationsTest#test_pluck_on_disable_joins_through_using_custom_foreign_key

Were not reproduced locally but it could also fail due to
non-determistic order.
2021-04-20 11:56:34 +09:00
John Hawthorn
70470d7eed
Merge pull request #41998 from jhawthorn/template_cache_clear_lock
Don't clear view cache during concurrent requests
2021-04-19 15:46:35 -07:00
John Hawthorn
faa84aa2f1
Merge pull request #41989 from HParker/improve-template-did-you-mean
Improve `did_you_mean` partial name correction
2021-04-19 15:37:39 -07:00
Adam Hess
f80038ae7d Improve did_you_mean partial name correction
Before this change did_you_mean shows partial paths like `animals/_partial`,
but adding that to your render call in a view like `<%= render 'animals/_partial' %>`
 will still be missing as rails will search for the template `animals/__partial`.
We can provide the user a easier copy/paste correction if we don't tell them about the underscore.

This also reduces the candidates to only return partials if you are
looking for a partial and only look for non-templates when you are not
looking for a template.
2021-04-19 15:01:00 -07:00
John Hawthorn
9a4c1e205e Don't clear view cache during concurrent requests
This updates ActionView::CacheExpiry to hold a lock while inside the
executor (ie. inside a request) and to only clear caches when that is
done.

This is done using Concurrent::ReadWriteLock. This allows any number
of parallel requests to hold the read lock, but once we detect a change
and begin to acquire the write lock, all future requests will be
blocked.
2021-04-19 09:36:38 -07:00
Eileen M. Uchitelle
6ebd134a9a
Merge pull request #41937 from eileencodes/has_many_through_skipping_joins
Add option to skip joins for associations.
2021-04-19 12:08:23 -04:00
eileencodes
de6b4efa3e
Add option to skip joins for associations.
In a multiple database application, associations can't join across
databases. When set, this option tells Rails to make 2 or more queries
rather than using joins for associations.

Set the option on a has many through association:

```ruby
class Dog
  has_many :treats, through: :humans, disable_joins: true
  has_many :humans
end
```

Then instead of generating join SQL, two queries are used for `@dog.treats`:

```
SELECT "humans"."id" FROM "humans" WHERE "humans"."dog_id" = ?  [["dog_id", 1]]
SELECT "treats".* FROM "treats" WHERE "treats"."human_id" IN (?, ?, ?)  [["human_id", 1], ["human_id", 2], ["human_id", 3]]
```

This code is extracted from a gem we use internally at GitHub which
means the implementation here is used in production daily and isn't
experimental.

I often get the question "why can't Rails do this automatically" so I
figured I'd include the answer in the commit. Rails can't do this
automatically because associations are lazily loaded. `dog.treats` needs
to load `Dog`, then `Human` and then `Treats`. When `dog.treats` is
called Rails pre-generates the SQL that will be run and puts that
information into a reflection object. Because the SQL parts are pre-generated,
as soon as `dog.treats` is loaded it's too late to skip a join. The join
is already available on the object and that join is what's run to load
`treats` from `dog` through `humans`. I think the only way to avoid setting
an option on the association is to rewrite how and when the SQL is
generated for associations which is a large undertaking. Basically the
way that Active Record associations are designed, it is currently
impossible to have Rails figure out to not join (loading the association
will cause the join to occur, and that join will raise an error if the
models don't live in the same db).

The original implementation was written by me and Aaron. Lee helped port
over tests, and I refactored the extraction to better match Rails style.

Co-authored-by: Lee Quarella <leequarella@gmail.com>
Co-authored-by: Aaron Patterson <aaron@rubyonrails.org>
2021-04-19 11:17:31 -04:00
Asherah Connor
2e14c53fc6
Add Enumerable#sole (#40914)
* Add `Enumerable#sole`, from `ActiveRecord::FinderMethods#sole`

* distinguish single-item Enumerable and two-item with nil last

Add a test for same.

* add symmetry, against rubocop's wishes
2021-04-19 11:26:34 +02:00
Jean Boussier
5877562f56 Make ModelSchema.inheritance_column a class_attribute
Ultimately it's exactly the same semantic that was implemented
using `superclass`, except it avoid recursion and benefit from
method cache, so much faster the longer the inheritance chain.

The use of an alias_method chain is unfortunate, but I don't
see any alternative to cast the provided value to string.
2021-04-19 11:17:39 +02:00
Ryuta Kamizono
9a263e9a0f
Merge pull request #42001 from ricardotk002/fix-encrypted-fixtures-test
Fix test `EncryptableFixtureTest` that fails intermittently
2021-04-17 13:10:01 +09:00
Ryuta Kamizono
10f031f704
Merge pull request #41992 from ashiksp/update-sti-abbreviation
[ci skip]Updated abbreviation for single table inheritance.
2021-04-17 13:04:38 +09:00
Ricardo Díaz
cbec080dc4 Allow test parallelization with processes to fail fast
Running tests in parallel with processes wasn't failing fast when the
option was enabled. The problem was that even when the reporter raised
the `Interrupt` exception, the queue was not emptied, so workers keep
processing jobs as if nothing happened.

This patch basically intercepts the `Interrupt` exception that may
come from the reporter, and tells the server to clear the jobs queue,
so that it can continue the shutdown process as usual. The exception
must then continue its journey so that the backtrace is displayed.
2021-04-16 19:23:57 -05:00
Ricardo Díaz
edc481a66d Fix test EncryptableFixtureTest that fails intermittently
Reference:

https://buildkite.com/rails/rails/builds/76506#f42a5ab3-1923-4a9f-b843-56c4098b6bef
https://buildkite.com/rails/rails/builds/76590#04721d73-9505-4c29-82bf-0a1be1f41649
https://buildkite.com/rails/rails/builds/76556#aa284ad0-0eab-48e9-a50b-60e3b5c7ccac

Apparently when loading different fixtures that reference the same
table, the files that are loaded last run a `DELETE FROM` statement
that removes previous data.

Reproduction command using `minitest_bisect`:

```
activerecord $ bin/test -a mysql2 --seed 22031 -n "/^(?:ActiveRecord::Encryption::EncryptableRecordTest#(?:test_when_downcase:_true_it_creates_content_downcased)|ActiveRecord::Encryption::EncryptableFixtureTest#(?:test_fixtures_get_encrypted_automatically))$/"
```
2021-04-16 13:06:05 -05:00
Ashik Salman
8e1b191b37 Updated abbreviation for single table inheritance. 2021-04-16 11:53:05 +05:30
Zachary Scott
0d91d4aa07
Merge pull request #41984 from jbampton/fix-case-of-css-and-javascript
chore: fix case of CSS and JavaScript
2021-04-16 14:49:18 +09:00
John Hawthorn
8e2aac6c34
Merge pull request #41993 from jhawthorn/convert_type_klass_to_class_ivar
Convert Template::Types.type_klass to instance ivar
2021-04-15 18:29:00 -07:00
John Hawthorn
33e054edbc Convert Template::Types.type_klass to instance ivar 2021-04-15 18:05:07 -07:00
Rafael Mendonça França
6d624c648c
Make sure the config values is set back to the original value
If the test fails the config should be reset as well.
2021-04-15 23:36:26 +00:00
Rafael Mendonça França
7b198e6024
Copy edit #41718 2021-04-15 23:34:30 +00:00
Rafael França
7ce853ecb0
Merge pull request #41718 from dzunk/enumerate_columns
Add setting for enumerating column names in SELECT statements
2021-04-15 19:28:41 -04:00
Rafael Mendonça França
8f07a124ef
Eager load ActiveRecord::StatementCache 2021-04-15 22:09:21 +00:00
Petrik
2a786431e2 Ensure has_one autosave association callbacks get called once
When saving a record, autosave adds callbacks to save its' associations.
Since the associations can have similar callbacks for the inverse,
endless loops could occur.

To prevent these endless loops, the callbacks for `has_many` and
`belongs_to` are defined as methods that only execute once.
This is implemented in the `define_non_cyclic_method` method.
However, this wasn't used for the `has_one` callbacks.

While `has_one` association callbacks didn't result in endless loops,
they could execute multiple times.
For example for a bidirectional `has_one` with autosave enabled,
the `save_has_one_association` gets called twice:

    class Pirate < ActiveRecord::Base
      has_one :ship, autosave: true

      def save_has_one_association(reflection)
        @count ||= 0
        @count += 1 if reflection.name == :ship
        super
      end
    end

    class Ship < ActiveRecord::Base
      belongs_to :pirate, autosave: true
    end

    pirate = Pirate.new(catchphrase: "Aye")
    pirate.build_ship(name: "Nights Dirty Lightning")
    pirate.save!
    # this returns 2 instead of 1.
    assert_equal 1, pirate.instance_variable_get(:@count)

This commit changes `has_one` autosave callbacks to be non-cyclic as
well. By doing this the autosave callback are made more consistent for
all 3 cases: `has_many`, `has_one` and `belongs_to`.
2021-04-15 22:30:59 +02:00
Eileen M. Uchitelle
80a23227ea
Merge pull request #41839 from abhaynikam/fix-return-type-for-strict-loading
ActiveRecord#strict_loading! should return boolean instead of current mode set.
2021-04-15 08:49:33 -04:00
John Bampton
eef63e05fb chore: fix case of CSS and JavaScript 2021-04-15 21:52:58 +10:00
Zachary Scott
95cd5148dd
Merge pull request #41981 from jbampton/fix-grammar-and-spelling 2021-04-15 19:17:34 +09:00
Zachary Scott
a79798e464
Merge pull request #41977 from abhaynikam/add-documentation-for-strict-loading-mode 2021-04-15 18:44:45 +09:00
Zachary Scott
63cc045401
Merge pull request #41975 from itsaphel/patch-1 2021-04-15 18:37:48 +09:00
John Bampton
7260f79d7a chore: fix grammar and spelling in Markdown and Ruby files 2021-04-15 19:32:27 +10:00
Ryuta Kamizono
01fcf11f45
Merge pull request #41980 from jbampton/fix-grammar-and-spelling
docs: fix grammar and spelling [ci skip]
2021-04-15 18:08:25 +09:00
John Bampton
c02068bad8 docs: fix grammar and spelling [ci skip] 2021-04-15 18:51:29 +10:00
Ryuta Kamizono
be22eab1d0
Merge pull request #41979 from jbampton/fix-grammar-and-spelling
chore: fix grammar and spelling
2021-04-15 16:56:59 +09:00
John Bampton
de3b207aa6 chore: fix grammar and spelling 2021-04-15 16:57:01 +10:00
Ryuta Kamizono
e525dd2490
Merge pull request #41978 from jbampton/fix-spelling
chore: fix spelling
2021-04-15 15:15:11 +09:00
John Bampton
1825c2b6c5 chore: fix spelling 2021-04-15 15:49:48 +10:00
Abhay Nikam
b841b2a84e Adds documentation for strict_loading_n_plus_one_only? [ci skip] 2021-04-15 10:35:11 +05:30
Abhay Nikam
2629f48ced ActiveRecord#strict_loading! should return boolean instead of mode set.
The return type was changed in the PR #41704 after addition of mode
option. The current documentation is misleading since
documentation puropose strict_loading! would return boolean whereas
it returns the current mode set.

I can across this issue while debugging issue: #41827 and thought
this should be brought to the attention.

PR fixes the issue and would always return boolean based on
strict_loading is enabled or disabled.

```
user.strict_loading! # => true
user.strict_loading!(false) # => false
user.strict_loading!(mode: :n_plus_one_only) # => true
```
2021-04-15 10:22:18 +05:30
aphel
011180e56c
Document input format for multiple arguments 2021-04-15 05:47:03 +01:00
Ryuta Kamizono
56f376c753
Merge pull request #41974 from jbampton/fix-spelling
chore: fix spelling
2021-04-15 13:35:39 +09:00
John Bampton
cf05afc990 chore: fix spelling 2021-04-15 14:14:59 +10:00
Rafael França
ac170aa8a7
Merge pull request #41973 from jbampton/fix-spelling
chore: fix spelling
2021-04-14 23:13:29 -04:00
John Bampton
5afc11f897 chore: fix spelling 2021-04-15 12:32:57 +10:00
Matt Duszynski
726abeaab4 Add setting for enumerating column names in SELECT statements 2021-04-14 15:48:17 -07:00
Zachary Scott
9e1cdf27eb
Merge pull request #41894 from braingourmets/task/getting-started-6.1
Update the "getting started" guide to Rails 6.1
2021-04-15 06:39:10 +09:00
John Hawthorn
ec1735b708
Merge pull request #41969 from jhawthorn/combine_file_system_resolvers
Combine FileSystemResolver classes
2021-04-14 13:42:31 -07:00