Commit Graph

467 Commits

Author SHA1 Message Date
Takayuki Nakata
246417c74e Add a test for select argument error with block
This added test is for select with arguments and block.
2019-09-04 10:25:05 +09:00
Ryuta Kamizono
d05f1f036f Merge pull request #37103 from giraffate/add_tests_for_no_or_blank_like_arguments_to_query_methods
Add tests for no or blank like arguments to query methods
2019-09-02 10:49:58 +09:00
Takayuki Nakata
2f308e7071 Add tests for no or blank like arguments to query methods
The methods with tests added here is for the query methods that uses
`check_if_method_has_arguments!` in `ActiveRecord::QueryMethods`.
However, some of these query methods had no tests.
2019-09-02 09:39:41 +09:00
Anmol Arora
2a65310890 Clear ActiveRecord object memoized by take 2019-08-20 00:54:03 +05:30
Ryuta Kamizono
eec562dbcf Deprecate .reorder(nil) with .first / .first! taking non-deterministic result
To continue taking non-deterministic result, use `.take` / `.take!`
instead.
2019-08-08 19:14:24 +09:00
Ryuta Kamizono
49b617f68c Restore an ability that reorder with first taking non-deterministic result
Actually, `first` taking non-deterministic result is considered a bug to
me. But changing the behavior should not be happened in rc2.

I've restored the behavior for 6.0, and then will deprecate that in 6.1.

Fixes #36802.
2019-08-08 18:34:10 +09:00
Akira Matsuda
af2129b4c7 Use try only when we're unsure if the receiver would respond_to the method 2019-08-01 17:58:00 +09:00
Ryuta Kamizono
f6db8b8d82 length(title) is a safe SQL string since #36448 2019-06-26 08:56:00 +09:00
Ryuta Kamizono
cb0299c9eb PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute
GROUP BY with virtual count attribute is invalid for almost all
databases, but it is valid for PostgreSQL, and it had worked until Rails
5.2.2, so it is a regression for Rails 5.2.3 (caused by 311f001).

I can't find perfectly solution for fixing this for now, but I would not
like to break existing apps, so I decided to allow referencing virtual
count attribute in ORDER BY clause when GROUP BY aggrigation (it partly
revert the effect of 311f001) to fix the regression #36022.

Fixes #36022.
2019-06-17 22:14:29 +09:00
Ryuta Kamizono
64d8c54e16 Allow column name with function (e.g. length(title)) as safe SQL string
Currently, almost all "Dangerous query method" warnings are false alarm.
As long as almost all the warnings are false alarm, developers think
"Let's ignore the warnings by using `Arel.sql()`, it actually is false
alarm in practice.", so I think we should effort to reduce false alarm
in order to make the warnings valuable.

This allows column name with function (e.g. `length(title)`) as safe SQL
string, which is very common false alarm pattern, even in the our
codebase.

Related 6c82b6c99, 6607ecb2a, #36420.

Fixes #32995.
2019-06-10 07:36:58 +09:00
Ryuta Kamizono
7cc67a368c NULLS { FIRST | LAST } is safe SQL string since 6c82b6c99d86f37e61f935fb342cccd725d6c7d4
There is no need to be wrapped by `Arel.sql()`.
2019-06-07 17:17:51 +09:00
Ryuta Kamizono
7696f44f6f Allow quoted identifier string as safe SQL string
Currently `posts.title` is regarded as a safe SQL string, but
`"posts"."title"` (it is a result of `quote_table_name("posts.title")`)
is regarded as an unsafe SQL string even though a result of
`quote_table_name` should obviously be regarded as a safe SQL string,
since the column name matcher doesn't respect quotation, it is a little
annoying.

This changes the column name matcher to allow quoted identifiers as safe
SQL string, now all results of the `quote_table_name` are regarded as
safe SQL string.
2019-06-06 03:57:24 +09:00
Ryuta Kamizono
95e952626d Use capture_sql instead of assert_sql with no pattern
And no longer need to except SCHEMA SQLs manually since 0810c07.
2019-05-22 09:09:18 +09:00
Ryuta Kamizono
060e09dfb3 Fix count(:all) with eager loading and explicit select and order
This follows up ebc09ed9ad9a04338138739226a1a92c7a2707ee.

We've still experienced a regression for `size` (`count(:all)`) with
eager loading and explicit select and order when upgrading Rails to 5.1.

In that case, the eager loading enforces `distinct` to subselect but
still keep the custom select, it would cause the ORDER BY with DISTINCT
issue.

```
% ARCONN=postgresql bundle exec ruby -w -Itest test/cases/relations_test.rb -n test_size_with_eager_loading_and_custom_select_and_order
Using postgresql
Run options: -n test_size_with_eager_loading_and_custom_select_and_order --seed 8356

# Running:

E

Error:
RelationTest#test_size_with_eager_loading_and_custom_select_and_order:
ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ..." ON "comments"."post_id" = "posts"."id" ORDER BY comments.i...
                                                             ^
```

As another problem on `distinct` is enforced, the result of `count`
becomes fewer than expected if `select` is given explicitly.

e.g.

```ruby
Post.select(:type).count
# => 11

Post.select(:type).distinct.count
# => 3
```

As long as `distinct` is enforced, we need to care to keep the result of
`count`.

This fixes both the `count` with eager loading problems.
2019-04-04 16:36:49 +09:00
David Heinemeier Hansson
4e076b03b6
Add ActiveRecord::Relation#extract_associated for extracting associated record (#35784)
* Add `ActiveRecord::Relation#extract_associated` for extracting associated records from a relation
2019-03-29 16:01:45 -07:00
Ryuta Kamizono
8309706239 Delegate only query method to relation as with except
I've found the skewness of delegation methods between `except` and
`only` in a88b6f2.

The `only` method is closely similar with `except` as `SpawnMethods`.

e056b9bfb0/activerecord/lib/active_record/relation/spawn_methods.rb (L53-L67)

It is preferable both behaves the same way.
2019-03-07 19:11:43 +09:00
Ryuta Kamizono
bff5e799c0 Relax table name detection in from to allow any extension like INDEX hint
#35360 allows table name qualified if `from` has original table name.
But that is still too strict. We have a valid use case that `from` with
INDEX hint (e.g. `from("comments USE INDEX (PRIMARY)")`).
So I've relaxed the table name detection in `from` to allow any
extension like INDEX hint.

Fixes #35359.
2019-03-01 22:41:46 +09:00
Ryuta Kamizono
aa9bc1e8f0 Fix random CI failure due to non-deterministic sorting order
https://buildkite.com/rails/rails/builds/59106#596284a1-4692-4640-8a50-c4286e173bbb/115-126
2019-02-27 01:38:04 +09:00
jvillarejo
fa2c61fc63 fixes different count calculation when using size manual select with DISTINCT
When using `select` with `'DISTINCT( ... )'` if you use method `size` on a non loaded relation it overrides the column selected by passing `:all` so it returns different value than count.

This fixes #35214
2019-02-26 12:21:14 -03:00
Ryuta Kamizono
257564d65a Add test case for unscope with merge 2019-02-24 18:30:20 +09:00
Ryuta Kamizono
15da1fb35b Add test case for unscope with unknown column 2019-02-24 08:23:43 +09:00
yuuji.yaginuma
619ae03f60 Make test_select_with_subquery_in_from_uses_original_table_name work with old SQLite3
It seems that the reason why the `test_select_with_subquery_in_from_uses_original_table_name`
does not pass is that the return value of `sqlite3_column_name()` is
wrong due to subquery flattening.

This seems to have been fixed with SQLite 3.20.0(https://sqlite.org/changes.html#version_3_20_0).
But CI uses the old version(maybe 3.11.0), I added `DISTINCT` to avoid
optimization by subquery flattening.

Ref: https://sqlite.org/optoverview.html#flattening
2019-02-23 13:35:27 +09:00
Ryuta Kamizono
fcbf5f4003 Skip test_select_with_subquery_in_from_uses_original_table_name on CI
Somehow `ENV["BUILDKITE"]` didn't work as expected.
2019-02-23 05:20:20 +09:00
Ryuta Kamizono
a333ba3f7f Skip test_select_with_subquery_in_from_uses_original_table_name on Buildkite as well
https://buildkite.com/rails/rails/builds/58981#2423c707-7c56-4639-a76e-8db4fd1e5cf3/102-111
2019-02-22 18:51:51 +09:00
Ryuta Kamizono
04a4789848 Just skip test_select_with_subquery_in_from_uses_original_table_name on Travis
I'm not sure why the test is failed on Travis, it passed on locally.

I suspect that failure is a bug on SQLite3, so just skip the test for
now, since it was not covered by before.

https://travis-ci.org/rails/rails/jobs/496726410#L1198-L1208
2019-02-22 07:04:52 +09:00
Ryuta Kamizono
6e599ee099 Fix pluck and select with from if from has original table name
This is caused by 0ee96d1.

Since #18744, `select` columns doesn't be qualified by table name if
using `from`. 0ee96d1 follows that for `pluck` as well.

But people depends that `pluck` columns are qualified even if using
`from`.

So I've fixed that to be qualified if `from` has the original table name
to keep the behavior as much as before.

Fixes #35359.
2019-02-22 06:14:33 +09:00
Abhay Nikam
5c8d4c3466 Introduce delete_by and destroy_by methods to ActiveRecord::Relation 2019-02-19 16:57:49 +05:30
Ryuta Kamizono
5ca19efafe
Merge pull request #35274 from AlexBrodianoi/fix_does_not_support_reverse
Raise ActiveRecord::IrreversibleOrderError if nulls first/last is not a single ordering argument.
2019-02-17 12:39:44 +09:00
Finn Young
db930ec0fd Raise ActiveRecord::IrreversibleOrderError if nulls first/last is not a single ordering argument. 2019-02-17 00:41:30 +00:00
Ryuta Kamizono
311f001167 Fix order with custom attributes
This follows up 0ee96d13de29680e148ccb8e5b68025f29fd091c.
2019-02-17 02:44:37 +09:00
Ryuta Kamizono
4c6171d605 Deprecate using class level querying methods if the receiver scope regarded as leaked
This deprecates using class level querying methods if the receiver scope
regarded as leaked, since #32380 and #35186 may cause that silently
leaking information when people upgrade the app.

We need deprecation first before making those.
2019-02-15 17:40:15 +09:00
Ryuta Kamizono
cdb8697b4a Revert "Merge pull request #35186 from kamipo/fix_leaking_scope_on_relation_create"
This reverts commit b67d5c6dedbf033515a96a95d24d085bf99a0d07, reversing
changes made to 2e018361c7c51e36d1d98bf770b7456d78dee68b.

Reason: #35186 may cause that silently leaking information when people
upgrade the app.

We need deprecation first before making this.
2019-02-15 12:06:45 +09:00
Ryuta Kamizono
0ee96d13de Fix pluck and select with custom attributes
Currently custom attributes are always qualified by the table name in
the generated SQL wrongly even if the table doesn't have the named
column, it would cause an invalid SQL error.

Custom attributes should only be qualified if the table has the same
named column.
2019-02-13 02:47:46 +09:00
Ryuta Kamizono
22360534ac Fix relation.create to avoid leaking scope to initialization block and callbacks
`relation.create` populates scope attributes to new record by `scoping`,
it is necessary to assign the scope attributes to the record and to find
STI subclass from the scope attributes.

But the effect of `scoping` is class global, it was caused undesired
behavior that pollute all class level querying methods in initialization
block and callbacks (`after_initialize`, `before_validation`,
`before_save`, etc), which are user provided code.

To avoid the leaking scope issue, restore the original current scope
before initialization block and callbacks are invoked.

Fixes #9894.
Fixes #17577.
Closes #31526.
2019-02-07 21:04:01 +09:00
Ryuta Kamizono
0f3e8e1ebb Relation no longer respond to Arel methods
This follows up d97980a16d76ad190042b4d8578109714e9c53d0.
2019-02-06 04:58:45 +09:00
Bogdan
18f83faf71 #create_or_find_by/!: add more tests and fix docs (#34653)
* `#create_or_find_by/!`: add more tests

* Fix docs of `create_or_find_by`

This method uses `find_by!` internally.
2018-12-08 07:46:30 +09:00
Ryuta Kamizono
136b738cd2 Generate delegation methods to named scope in the definition time
The delegation methods to named scope are defined when `method_missing`
is invoked on the relation.

Since #29301, the receiver in the named scope is changed to the relation
like others (e.g. `default_scope`, etc) for consistency.

Most named scopes would be delegated from relation by `method_missing`,
since we don't allow scopes to be defined which conflict with instance
methods on `Relation` (#31179). But if a named scope is defined with the
same name as any method on the `superclass` (e.g. `Kernel.open`), the
`method_missing` on the relation is not invoked.

To address the issue, make the delegation methods to named scope is
generated in the definition time.

Fixes #34098.
2018-10-09 13:03:08 +09:00
Ryuta Kamizono
68d6c1353a Extract {update,delete}_all_test.rb from persistence_test.rb and relations_test.rb
`persistence_test.rb` and `relations_test.rb` have too many lines, so
I'd like to extract relation around tests to dedicated files before
newly test added.
2018-09-16 08:41:08 +09:00
Darwin D Wu
5291044a1d Fixes #33610
In order to avoid double assignments of nested_attributes for `has_many`
relations during record initialization, nested_attributes in `create_with`
should not be passed into `klass.new` and have them populate during
`initialize_internals_callback` with scope attributes.

However, `create_with` keys should always have precedence over where
clauses, so if there are same keys in both `create_with` and
`where_values_hash`, the value in `create_with` should be the one that's
used.
2018-09-11 00:13:09 -07:00
Ryuta Kamizono
5c656889a6 Just delegate update with ids on a relation to klass.update
This restores an ability that `update` with ids on a relation which is
described at https://github.com/rails/rails/issues/33470#issuecomment-411203013.

I personally think that the `update` with two arguments on a relation is
not a designed feature, since that is totally not using a relation
state, and also is not documented.

But removing any feature should not be suddenly happened in a stable
version even if that is not documented.
2018-08-31 02:56:32 +09:00
Ryuta Kamizono
c83e30da27 Avoid extra scoping when using Relation#update
Since 9ac7dd4, class level `update`, `destroy`, and `delete` were placed
in the `Persistence` module as class methods.

But `Relation#update` without passing ids which was introduced at #11898
is not a class method, and it was caused the extra scoping regression
#33470.

I moved the relation method back into the `Relation` to fix the
regression.

Fixes #33470.
2018-07-31 08:31:46 +09:00
Rafael França
7bdfc63cc2
Merge pull request #31513 from fatkodima/relation-touch_all
Add `touch_all` method to `ActiveRecord::Relation`
2018-04-20 16:45:38 -04:00
Daniel Colson
a1ac18671a Replace assert ! with assert_not
This autocorrects the violations after adding a custom cop in
3305c78dcd.
2018-04-19 08:11:33 -04:00
fatkodima
73aa40034a Add touch_all method to ActiveRecord::Relation 2018-04-13 11:09:01 +03:00
Ryuta Kamizono
f1878fa06e Merge pull request #32005 from maschwenk/ar-distinct-order-count-regression
Active Record distinct & order #count regression
2018-02-27 22:22:04 +09:00
David Heinemeier Hansson
fe6adf43e1
Add #create_or_find_by to lean on unique constraints (#31989)
Add #create_or_find_by to lean on unique constraints
2018-02-14 16:51:15 -08:00
Daniel Colson
fda1863e1a Remove extra whitespace 2018-01-25 23:32:59 -05:00
Daniel Colson
82c39e1a0b Use assert_empty and assert_not_empty 2018-01-25 23:32:59 -05:00
Daniel Colson
94333a4c31 Use assert_predicate and assert_not_predicate 2018-01-25 23:32:59 -05:00
Daniel Colson
0d50cae996 Use respond_to test helpers 2018-01-25 23:32:58 -05:00