Commit Graph

79282 Commits

Author SHA1 Message Date
Ryuta Kamizono
e450a4a497 Fix average on decimal and enum attributes
Follow up to #40351 and #41418.

This fixes `average` on decimal and enum (on integer in general)
attributes to not do `type.deserialize`.

The precision and the scale on the column might be lower than the
calculated result.

And also, mapping the calculated result to enum label is quite
meaningless, the mapping result is almost nil.
2021-02-13 15:07:48 +09:00
Ryuta Kamizono
8dc7439058
Merge pull request #41428 from jonathanhefner/enumerable-test-empty-maximum-minimum
Test #maximum and #minimum with empty enumerable
2021-02-13 08:46:46 +09:00
Jonathan Hefner
5df979dd35 Test #maximum and #minimum with empty enumerable
Follow-up to #41404.

These tests will prevent regressions if we decide to change the
implementations of `maximum` or `minimum` in the future (for example,
calling `max_by` or `min_by` followed by `send`).
2021-02-12 16:20:51 -06:00
Rafael França
1acde22bf2
Merge pull request #41427 from dylanahsmith/dry-primary-key-constraints-hash
activerecord: Extract primary key constraints hash to a method
2021-02-12 15:38:38 -05:00
Kasper Timm Hansen
e0f95b44ae
Merge pull request #41404 from ayrton/maximum
Enumerable#maximum
2021-02-12 21:29:39 +01:00
Dylan Thacker-Smith
1d3ef1abb2 Extract primary key constraints hash to a method.
Since this hash will need to change in the future to support composite
primary keys.
2021-02-12 15:20:32 -05:00
Ayrton De Craene
2526938b32 Calculate the minimum/maximum from an enumerable's extracted elements
```
payments = [Payment.new(5), Payment.new(15), Payment.new(10)]
payments.minimum(:price) # => 5
payments.maximum(:price) # => 20
```
2021-02-12 17:37:53 +01:00
Ryuta Kamizono
1efa175b3b
Merge pull request #41423 from kamipo/fix_nested_unprepared_statements
Fix `unprepared_statement` to work it when nesting
2021-02-13 01:25:04 +09:00
Ryuta Kamizono
da26910fbc Fix unprepared_statement to work it when nesting
I've been reported a problem by @osyo-manga, preloading queries in
`unprepared_statement` doesn't work as expected in main branch (queries
are executed as prepared statements).

It is caused by #41385, due to `scope.to_sql` in `grouping_key` depends
on `unprepared_statement` which has an issue when nesting.

To fix the issue, don't add/delete object_id in the prepared statements
disabled cache if that is already disabled.
2021-02-12 22:47:27 +09:00
Ryuta Kamizono
a82df4c5e0 Refactor type_cast_calculated_value to look up cast_type before 2021-02-12 21:52:27 +09:00
Ryuta Kamizono
a19c9b3e18
Merge pull request #41418 from schmijos/regression-for-40351
Harmonize average aggregation with minimum and maximum
2021-02-12 21:35:39 +09:00
Josua Schmid
1a9b84391f
Harmonize average aggregation with minimum and maximum
The real problem behind the previous implementation of average
aggregation was not that float columns returned `BigDecimal` but that
average skipped `ActiveModel` type casting.

This change introduces handling for the only needed special case
of average: integers. Now any fields based on
`ActiveRecord::Type::Integer` will be casted to `BigDecimal` when
aggregated with average.
2021-02-12 13:01:47 +01:00
Jonathan Hefner
092713ffb0
Merge pull request #41411 from abhaynikam/typo-in-migration-doc
Fixes a typo and some cosmetic changes for migration documentation [ci skip]
2021-02-11 22:44:37 -06:00
Abhay Nikam
f2d05a213c Fixes a typo and some cosmetic changes for migration documentation added in #40806 [ci skip] 2021-02-12 10:04:45 +05:30
Ryuta Kamizono
94554514fe Use +/<tt> instead of ` in the rdoc [ci skip] 2021-02-12 12:56:44 +09:00
Ryuta Kamizono
9984c3f8fa
Merge pull request #41421 from websebdev/add-missing-view-helper-to-guides
Add missing ActionView helper number_to_human in guides [ci skip]
2021-02-12 12:22:16 +09:00
Sebastien Auriault
24d1615789 Add missing ActionView helper number_to_human in guides 2021-02-11 22:02:30 -05:00
Rafael França
afa58dc832
Merge pull request #41419 from smartygus/activerecord-findermethod-include-with-having
Fix #41417 Handle Relations with having referencing aliased selects in #include?
2021-02-11 19:19:09 -05:00
Michael Smart
644d694dea Handle Relations with having clauses in #include?
- skips optimised exists? query for relations that have a
  having clause

Relations that have aliased select values AND a having clause that
references an aliased select value would generate an error when
#include? was called, due to an optimisation that would generate
call #exists? on the relation instead, which effectively alters
the select values of the query (and thus removes the aliased select
values), but leaves the having clause intact. Because the having
clause is then referencing an aliased column that is no longer
present in the simplified query, an ActiveRecord::InvalidStatement
error was raised.

An sample query affected by this problem:

    Author.select('COUNT(*) as total_posts', 'authors.*')
          .joins(:posts)
          .group(:id)
          .having('total_posts > 2')
          .include?(Author.first)

This change adds an addition check to the condition that skips the
simplified #exists? query, which simply checks for the presence of
a having clause.
2021-02-12 01:04:39 +01:00
Rafael Mendonça França
5b0fe373f3
Merge PR #41356
Closes #41356
Closes #41034
2021-02-11 23:51:53 +00:00
Rafael França
84e115acba
Merge pull request #41394 from afrase/recursive-association-fix
Avoid stack level too deep in predicate builder
2021-02-11 18:32:52 -05:00
Rafael França
a751d7d922
Merge pull request #41416 from tjschuck/fix_highlight_docs
Fix formatting in highlight docs
2021-02-11 17:57:23 -05:00
Rafael Mendonça França
b544150c5e
Fix string quotes 2021-02-11 22:52:20 +00:00
Rafael Mendonça França
1ba87a80b6
Make we always type cast TimeWithZone objects before passing to mysql2
mysql2 knows how to handle Time and Date objects but doesn't know
about TimeWithZone. This was causing failures when prepared statements
were enabled since we were passing TimeWithZone objects that mysql2 didn't
know how to deal with.

An new environment variable to enable prepared statements were added to
config.example.yml, so we can test in our CI and prevent regressions.

Fixes #41368.
2021-02-11 22:45:14 +00:00
T.J. Schuck
c80df59b25 Fix formatting in highlight docs
Right now, the `\1` in the plain text gets escaped to just "1" in the rendered docs.  Wrap it in `tt` tags to keep the \ there as intended.

[ci skip]
2021-02-11 16:48:45 -05:00
Eileen M. Uchitelle
9eedc375d9
Merge pull request #41397 from eileencodes/allow-scoping-to-apply-to-all-queries
Enable scoping to apply to all queries
2021-02-11 16:04:22 -05:00
eileencodes
d6e12ffb7d
Enable scoping to apply to all queries
Similar to https://github.com/rails/rails/pull/40720
and https://github.com/rails/rails/pull/40805 this change allows for the
`scoping` method to apply to all queries in the block. Previously this
would only apply to queries on the class and not the instance. Ie
`Post.create`, Post.all`, but not `post.update`, or `post.delete`.

The change here will create a global scope that is applied to all
queries for a relation for the duration of the block.

Benefits:

This change allows applications to add a scope to any query for the
duration of a block. This is useful for applications using sharding to
be able to control the query without requiring a `default_scope`. This
is useful if you want to have more control over when a `scoping` is used
on a relation. This also brings `scoping` in parity with the behavior of
`default_scope` so there are less surprises between the behavior of
these two methods.

There are a caveats to this behavior:

1) The `scoping` only applies to objects of the same type. IE you cannot
scope `Post.where(blog_id: 1).scoping` and then expect `post.comments`
will apply `blog_id = 1` to the `Comment` query. This is not possible
because the scope is `posts.blog_id = 1` and we can't apply the `posts`
scope to a `comments` query. To solve this, scopes must be nested.
2) If a block is scoped to `all_queries` it cannot be unscoped without
exiting the block. I couldn't find a way around this but ActiveRecord
scoping is a bit complex and turning off `all_queries` when it's already
on in nested scoping blocks had interesting behavior that I decided was
best left out.
2021-02-11 16:03:55 -05:00
Eileen M. Uchitelle
bddb2c9b19
Merge pull request #41410 from eileencodes/define-empty-result
Add ActiveRecord::Result.empty
2021-02-11 11:37:09 -05:00
Aaron Frase
91d583420b
Avoid stack level too deep in predicate builder
Check if `query` is different from `attributes` before recursively calling `expand_from_hash`.
Updated cases to account for new company and comment records.
2021-02-11 11:32:20 -05:00
eileencodes
12fd40887f
Add ActiveRecord::Result.empty
This change is extracted from #39547 which we never finished. This minor
refactoring makes a new `empty` method to clean up places where we want
to specifically return an empty result.

Co-authored-by: Aaron Patterson tenderlove@ruby-lang.org
2021-02-11 10:55:01 -05:00
Jonathan Hefner
e9de0f51c6
Merge pull request #41407 from calleluks/patch-1
Improve AJAX example [ci skip]
2021-02-11 09:23:33 -06:00
Calle Luks
6dbd73d826
Improve AJAX example [ci skip]
The example uses `fetch` to retrieve HTML from the server and append it to an element on the page. This commit updates the example to append the response HTML rather than the full response object.
2021-02-11 16:07:18 +01:00
Bill Harding
2416da9975 Increment @counter of prepared postgres statements prior to running the query.
If the next query or the prepared statement itself get interrupted, this prevents the database session getting stuck perpetually retrying to recreate the same prepared statement.
2021-02-11 12:41:21 +01:00
Ryuta Kamizono
56ea638290 A value acts like time object should respond to getutc and getlocal 2021-02-11 15:05:25 +09:00
Rafael França
3b9a877c69
Merge pull request #41399 from jonathanhefner/always-overwrite-bin-yarn
Overwrite bin/yarn when running app:binstub:yarn
2021-02-10 19:08:29 -05:00
Rafael Mendonça França
f3d703967e
Remove broken test 2021-02-10 23:48:12 +00:00
Rafael França
08ef83b7cf
Merge pull request #41401 from schmijos/addendum-40351
Add missing CHANGELOG entry for #40351
2021-02-10 18:01:35 -05:00
Rafael Mendonça França
a6a121163c
Remove unnessary escape char in Regexp
Fix the test by defining a valid host on the mocked requests.
2021-02-10 22:59:17 +00:00
Jonathan Hefner
6e78549d2c Overwrite bin/yarn when running app:binstub:yarn
This ensures that commands which delegate to `app:binstub:yarn`, such as
`action_text:install`, will update `bin/yarn`.
2021-02-10 15:41:03 -06:00
Josua Schmid
fed2689fb0
Add missing CHANGELOG entry for #40351 2021-02-10 22:37:18 +01:00
Aaron Patterson
83a6ac3fee
Prevent open redirect when allowed host starts with a dot
[CVE-2021-22881]

Thanks to @tktech (https://hackerone.com/tktech) for reporting this
issue and the patch!
2021-02-10 20:47:49 +00:00
Aaron Patterson
eddda4d8fb
Fix possible DoS vector in PostgreSQL money type
Carefully crafted input can cause a DoS via the regular expressions used
for validating the money format in the PostgreSQL adapter.  This patch
fixes the regexp.

Thanks to @dee-see from Hackerone for this patch!

[CVE-2021-22880]
2021-02-10 20:47:43 +00:00
Eileen M. Uchitelle
142745fecc
Merge pull request #40806 from eileencodes/make-migration-context-public
Make migration context public
2021-02-10 15:14:43 -05:00
Rafael França
0dda445645
Merge pull request #41395 from ghiculescu/pg-schema-dump-timestamptz
PostgreSQL: handle `timestamp with time zone` columns correctly in `schema.rb`
2021-02-10 14:55:12 -05:00
Rafael França
030acf5d41
Merge pull request #41385 from dinahshi/preloader-grouped-by-scope
Batch Preloader::Association queries having similar queries
2021-02-10 14:30:44 -05:00
Rafael França
de630cdac8
Merge pull request #41104 from f6p/skip-spring-when-locked-gems-empty
Prevent webpacker:install failures on missing Gemfile
2021-02-10 14:19:43 -05:00
Alex Ghiculescu
25e8a581a5 PostgreSQL: handle timestamp with time zone columns correctly in schema.rb
Found this issue while working on https://github.com/rails/rails/pull/41084

Currently if you have a `timestamp with time zone` column, and you run `rake db:schema:dump`, it will be output as a `t.datetime`. This is wrong because when you run `rake db:schema:load`, the column will be recreated as a `timestamp without time zone`.

The fix is to add a new column type, `t.timestamptz`. This behaves exactly the same as before, the only change is what native type it is converted to during schema loads.
2021-02-10 10:59:31 -07:00
Eileen M. Uchitelle
a27567596b
Merge pull request #41349 from eileencodes/add-primary_abstract_class-to-generator
Set `ApplicationRecord` to `primary_abstract_class`
2021-02-10 10:42:44 -05:00
Ryuta Kamizono
8d3e6ce1f3
Merge pull request #40351 from schmijos/avg-decimal-float
Return BigDecimal **OR** Float from "average" operations
2021-02-10 20:44:29 +09:00
Filip Pyda
217dc41355 Pass spring if there is no Gemfile.lock 2021-02-10 07:52:25 +01:00