Commit Graph

18939 Commits

Author SHA1 Message Date
John Hawthorn
ec2dcddf79
Merge pull request #36848 from jhawthorn/type_error_on_resolve_connection
Raise TypeError instead of infinite loop in resolve_connection
2019-08-07 14:51:26 -07:00
Gannon McGibbon
dacfa5b792
Merge pull request #36847 from gmcgibbon/fix_custom_pk_through_reflect
Ensure custom PK types are casted in through reflection queries
2019-08-07 16:48:21 -04:00
Gannon McGibbon
5ec2f35f27 Ensure custom PK types are casted in through reflection queries
Make sure primary keys with non-integer types can be correctly type
casted in through reflections.
2019-08-07 16:27:33 -04:00
Carlos Antonio da Silva
683e1c7d18 Fix typo in test name 2019-08-07 13:46:15 -03:00
Ryuta Kamizono
e62195e259 Fix GROUP BY aggregation alias to not duplicate "_" chars
c9e4c848 has one performance optimization for `aggregate_alias` to early
returning by `aggregate_alias.match?(/\A\w+\z/)`, but it is caused a
regression that failing deduplication for non word chars #36867.

I've quited the optimization and add a test to prevent a future
regression.

Fixes #36867.
2019-08-07 23:05:23 +09:00
John Hawthorn
6bc88355ea
Merge pull request #36870 from jhawthorn/schema_sha
Sync test DB from schema using its SHA1
2019-08-06 15:47:44 -07:00
John Hawthorn
ba093a5ada Sync test DB from schema using its SHA1
Previously, we used the migration status to determine whether the test
database(s) needed to be reloaded from the schema. This worked in most
cases, but if a schema.rb was modified outside of migrations or if a
migration was rolled back, it would require a manual db:test:prepare.

This commit updates load_schema to record the SHA1 of the loaded schema
file inside of the ar_internal_metadata table. We can then use this SHA
to determine whether we should reload the schema.

This ensures that the test DB stays exactly in sync with the schema
file, including rollbacks which fixes a test marked TODO.
2019-08-06 12:44:22 -07:00
eileencodes
ff70c1764d Fix thread safety of prevent_writes
As demonstrated in the test added and in #36830 the code that prevents
writes wasn't thread safe. If one thread does a read, then another does
a write, and then another does a read the second read will cause the
first write to be unwriteable.

This change removes the instance variable and instead uses a
getter/setter on Thread.current[:prevent_writes] for the connection
handler to set whether writes are allowed.

Fixes #36830
2019-08-06 13:07:19 -04:00
OKURA Masafumi
e7ce8d2653 [ci skip] Mention context option of save in documentation
`ActiveRecord::Validations#save` takes `context` option
which can change validation context.
This fact should be mentioned in the documentation.
2019-08-06 20:10:11 +09:00
Lachlan Sylvester
1af44e4aee
handle passing in primary key to unique_by, and handle primary keys missing indexes 2019-08-05 02:39:52 +02:00
Eileen M. Uchitelle
2c0b67c281
Merge pull request #36845 from eileencodes/fix-pg-closed-connection
Fix `PG::ConnectionBad` error when running fixtures
2019-08-04 15:25:05 -04:00
Yasuo Honda
20772f6c53 Address DEPRECATED: Use assert_nil if expecting nil
```ruby
$ cd activerecord
$ bin/test test/cases/dirty_test.rb:494
... snip ...
DEPRECATED: Use assert_nil if expecting nil from /home/yahonda/git/rails/activerecord/test/cases/dirty_test.rb:494. This will fail in Minitest 6.
DEPRECATED: Use assert_nil if expecting nil from /home/yahonda/git/rails/activerecord/test/cases/dirty_test.rb:511. This will fail in Minitest 6.
.

Finished in 0.061593s, 16.2356 runs/s, 795.5428 assertions/s.
1 runs, 49 assertions, 0 failures, 0 errors, 0 skips
$
```

Refer seattlerb/minitest#666 rails/rails#27712
2019-08-03 02:27:56 +00:00
John Hawthorn
d0e95f45f3 Raise TypeError instead of infinite looping 2019-08-02 15:58:42 -07:00
eileencodes
bfec23aecb Fix PG::ConnectionBad error when running fixtures
At first this appeared to be a multi-db bug but after some invesitgation
it was clear that this can occur just by calling `establish_connection`
from ApplicationRecord.

After some investigation we found that this only occurred when using
fixtures. The console boots fine, the server runs fine, and the tests
even run fine if we used paralellization or eager loading in the tests.

I tracked the issue down to the line that calls
`self.connection_specification_name = name` in the SchemaMigration
changes for Rails 6.0. But how can this be? That is not that major of a
change? How could `connection_specification_name` be a problem?

First `connection_specification_name` caches the name of the connection
specificatio. Second, fixtures were incorrectly holding onto a reference
to that connection.

So when you went to run the tests the models wouldn't be connected and
when the fixtures tried to load the data it would choke on that
unconnected database.

The changes here move the connection into a lambda so we can call it
when we need it rather than blowing up before the model is connected.

Fixes #36743

Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
2019-08-02 15:22:11 -04:00
eileencodes
f2de448106 Add ability to unset preventing writes
Previously if an app attempts to do a write inside a read request it will be
impossilbe to switch back to writing to the primary. This PR adds an
argument to the `while_preventing_writes` so that we can make sure to
turn it off if we're doing a write on a primary.

Fixes #36830

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2019-08-02 12:10:32 -04:00
Eileen M. Uchitelle
f3c68c59ed
Merge pull request #36814 from eileencodes/introduce-invalid-configuration-error
Introduce InvalidConfigurationError
2019-08-02 08:50:27 -04:00
Rafael Mendonça França
967beb7229
Revert "MethodCallAssertions is a regular player of the team ActiveSupport::TestCase now"
This reverts commit 98d0f7ebd34b858f12a12dcf37ae54fdbb5cab64.
2019-08-02 00:24:21 -04:00
David Heinemeier Hansson
a0bb19fbfa
Add *_previously_was attribute methods when dirty tracking (#36836) 2019-08-01 15:38:03 -07:00
Ryuta Kamizono
89399ed88c
Merge pull request #36821 from rails/virtual-attribute-type
Add tests for selecting aggregrates
2019-08-02 05:54:01 +09:00
Akira Matsuda
98d0f7ebd3 MethodCallAssertions is a regular player of the team ActiveSupport::TestCase now
It's used everywhere, clean and mature enough
2019-08-02 05:36:15 +09:00
eileencodes
dde03a9234 Introduce InvalidConfigurationError
In our app at work we had a faked config like this:

```
{ "foo" => :bar, "bar" => { "adapter" => "memory" } }
```

This config is invalid. You can't say for foo env just have a symbol,
nor would this work if you had fa foo env with just a string. A
configuration must be a url or an adapter or a database. Otherwise it's
invalid and we can't parse it.

When this was just yaml turned into hashes you could get away with
passing whatever. It wouldn't work but it wouldn't blow up either.

Now that we're using objects we were returning `nil` for these but that
just means we either blow up on `for_current_env` or compact the
`nil`'s.

I think it's a better user experience to not build the configs and raise
an appropriate error.

This is also an invalid config because if you do pass a string here it
should be a URL.

```
{ "foo" => "bar", "bar" => { "adapter" => "memory" } }
```
2019-08-01 16:33:58 -04:00
Ryuta Kamizono
e6f953fc39 Deduplicate joins values
#36805 have one possible regression that failing deduplication if
`joins_values` have complex order (e.g. `joins_values = [join_node_a,
:comments, :tags, join_node_a]`).

This fixes the deduplication to take it in the first phase before
grouping.
2019-08-02 03:35:44 +09:00
Godfrey Chan
05300be635 Add tests for selecting aggregates
These tests verifies that aggregates like `AVG` can be selected as
"virtual attributes" on Active Record models and have the correct
column type.
2019-08-01 10:56:10 -07:00
Rafael Mendonça França
d40c88ae05
No private def 2019-08-01 11:18:42 -04:00
Akira Matsuda
530f7805ed It may be better to explicitly require 'object/try' where we call try
In most cases it works now without explicit require because it's accidentally required through
active_support/core_ext/date_and_time/calculations.rb where we still call `try`,
but that would stop working if we changed the Calculations implementation and remove the require call there.
2019-08-01 18:51:51 +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
Vipul A M
dbf3e4882f
Merge pull request #36827 from akshaymohite/fix-documentation-typo-in-example
Fixed a typo in documentation example of activerecord database configuration. [ci skip]
2019-08-01 10:05:04 +08:00
Akshay Mohite
b5950b8ee8
Fixed a typo in documentation example of activerecord database configuration. [ci skip]
- The example has sqlite3 adpater in database.yml, whereas configuration object had incorrectly specified mysql2 in documentation.
2019-08-01 07:23:51 +05:30
eileencodes
cd148be072 Fix db:seed
The `rake db:seed` command was broken for the primary environment if the
application is using multiple databases. We never implemented `rake
db:seed` for other databases (coming soon), but that shouldn't break the
default case.

The reason this was broken was because `abort_if_pending_migrations`
would loop through the configs for all databases and check for
migrations but would leave the last established connection. So `db:seed`
was looking in the wrong database for the table to seed.

This PR doesn't fix the fact that `db:seed` doesn't work for multiple
databases but does fix the default case.

Fixes #36817

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2019-07-31 12:22:01 -04:00
Ryuta Kamizono
4c8c8c87b0 Address to rubocop offences 2019-07-31 17:48:14 +09:00
Akira Matsuda
34a7e68bb9 Reduce Array allocations 2019-07-31 17:41:34 +09:00
Kasper Timm Hansen
40fc31c103
Merge pull request #36708 from rails/has-one-polymorphic-touch-dont-cache-association-result-inside-create-transaction
Polymorphic has_one touch: Don't cache association result inside crea…
2019-07-31 06:31:50 +02:00
yamato-payforward
b8309e85b9 fix a typo 2019-07-31 13:17:32 +09:00
Kasper Timm Hansen
cea392eb96
Polymorphic has_one touch: Reset association cache result after create transaction
In case of a polymorphic association there's no automatic inverse_of to assign the
inverse record. So to get the record there needs to be a query executed,
however, if the query fires within the transaction that's trying to create
the associated record, no record can be found. And worse, the nil result is cached
on the association so after the transaction commits the record can't be found.

That's what happens if touch is enabled on a polymorphic has_one association.

Consider a Comment with a commentable association that needs to be touched.

For `Comment.create(commentable: Post.new)`, the existing code essentially
does `commentable.send(:comment)` within the create transaction for the comment
and thus not finding the comment.

Now we're purposefully clearing the cache in case we've tried accessing
the association within the transaction and found no object.

Before:

```
kaspth-imac 2.6.3 ~/code/rails/activerecord master *= ARCONN=postgresql bin/test test/cases/associations/has_one_associations_test.rb -n /commit/
Using postgresql
Run options: -n /commit/ --seed 46022

D, [2019-07-19T03:30:37.864537 #96022] DEBUG -- :   Chef Load (0.2ms)  SELECT "chefs".* FROM "chefs" WHERE "chefs"."employable_id" = $1 AND "chefs"."employable_type" = $2 LIMIT $3  [["employable_id", 1], ["employable_type", "DrinkDesignerWithPolymorphicTouchChef"], ["LIMIT", 1]]
D, [2019-07-19T03:30:37.865013 #96022] DEBUG -- :   Chef Create (0.2ms)  INSERT INTO "chefs" ("employable_id", "employable_type") VALUES ($1, $2) RETURNING "id"  [["employable_id", 1], ["employable_type", "DrinkDesignerWithPolymorphicTouchChef"]]
D, [2019-07-19T03:30:37.865201 #96022] DEBUG -- :   TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
D, [2019-07-19T03:30:37.874136 #96022] DEBUG -- :   TRANSACTION (0.1ms)  ROLLBACK
D, [2019-07-19T03:30:37.874323 #96022] DEBUG -- :   TRANSACTION (0.1ms)  ROLLBACK
F

Failure:
HasOneAssociationsTest#test_polymorphic_has_one_with_touch_option_on_create_wont_cache_assocation_so_fetching_after_transaction_commit_works [/Users/kaspth/code/rails/activerecord/test/cases/associations/has_one_associations_test.rb:716]:
--- expected
+++ actual
@@ -1 +1 @@
-#<Chef id: 1, employable_id: 1, employable_type: "DrinkDesignerWithPolymorphicTouchChef", department_id: nil, employable_list_type: nil, employable_list_id: nil>
+nil
```

After:

```
kaspth-imac 2.6.3 ~/code/rails/activerecord master *= ARCONN=postgresql bin/test test/cases/associations/has_one_associations_test.rb -n /commit/
Using postgresql
Run options: -n /commit/ --seed 46022

D, [2019-07-19T03:30:22.479387 #95973] DEBUG -- :   Chef Create (0.3ms)  INSERT INTO "chefs" ("employable_id", "employable_type") VALUES ($1, $2) RETURNING "id"  [["employable_id", 1], ["employable_type", "DrinkDesignerWithPolymorphicTouchChef"]]
D, [2019-07-19T03:30:22.479574 #95973] DEBUG -- :   TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
D, [2019-07-19T03:30:22.482051 #95973] DEBUG -- :   Chef Load (0.1ms)  SELECT "chefs".* FROM "chefs" WHERE "chefs"."employable_id" = $1 AND "chefs"."employable_type" = $2 LIMIT $3  [["employable_id", 1], ["employable_type", "DrinkDesignerWithPolymorphicTouchChef"], ["LIMIT", 1]]
D, [2019-07-19T03:30:22.482317 #95973] DEBUG -- :   TRANSACTION (0.1ms)  ROLLBACK
D, [2019-07-19T03:30:22.482437 #95973] DEBUG -- :   TRANSACTION (0.1ms)  ROLLBACK
.

Finished in 0.088498s, 11.2997 runs/s, 22.5994 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
```

Notice the select now fires after the commit.
2019-07-31 05:59:23 +02:00
Akira Matsuda
6dbfc67734 Accessing ivar with Symbols might be just a very little bit better than with fstrings 2019-07-31 12:18:44 +09:00
Ryuta Kamizono
1e5c0a7836
Merge pull request #36805 from kamipo/user_supplied_joins_order_should_be_preserved
Preserve user supplied joins order as much as possible
2019-07-30 12:39:50 +09:00
Ryuta Kamizono
fff120c752 Add silence_warnings for defining 'not_' prefix enum elements
To suppress the following warnings in tests.

```
~/rails/activerecord/lib/active_record/scoping/named.rb:190: warning: method redefined; discarding old not_sent
~/rails/activerecord/lib/active_record/scoping/named.rb:190: warning: previous definition of not_sent was here
```
2019-07-30 04:54:55 +09:00
Ryuta Kamizono
b4478ae8bc Preserve user supplied joins order as much as possible
Currently, string joins are always applied as last joins part, and Arel
join nodes are always applied as leading joins part (since #36304), it
makes people struggled to preserve user supplied joins order.

To mitigate this problem, preserve the order of string joins and Arel
join nodes either before or after of association joins.

Fixes #36761.
Fixes #34328.
Fixes #24281.
Fixes #12953.
2019-07-30 04:02:58 +09:00
John Hawthorn
8b0b591296
Merge pull request #36782 from jhawthorn/move_database_exists_to_adapter
Move DatabaseAlreadyExists detection to DB adapter
2019-07-29 11:06:27 -07:00
Ryuta Kamizono
e0b19a3622 Expand CHANGELOG for #36800 [ci skip] 2019-07-30 02:50:11 +09:00
John Hawthorn
69700c9ee7 Move DatabaseAlreadyExists detection to DB adapter
Previously it was the responsibility of the database tasks to translate
the invalid statement from creating a duplicate database into an
ActiveRecord::Tasks::DatabaseAlreadyExists error.

It's actually easier for us to do this detection inside of the adapter,
where we already do a case statement on the return code to translate the
error.

This commit introduces ActiveRecord::DatabaseAlreadyExists, a subclass
of StatementInvalid, and updates both AbstractMysqlAdapter and
PostgresqlAdapter to return this more specific exception in that case.

Because this is a subclass of the old exception, StatementInvalid, it
should be backwards compatible with any code expecting that from
create_database.

This works for both create_database and exectute("CREATE DATABASE")
2019-07-29 08:40:57 -07:00
James Pearson
92c265b3ad Enabled matches_regex for MySql
Previously matches_regex was only availble on PostgreSql, this will enable it for MySql

Usage example:

    users = User.arel_table;
    users = User.arel_table; User.where(users[:email].matches_regexp('(.*)\@gmail.com'))

Update activerecord/test/cases/arel/visitors/mysql_test.rb

Co-Authored-By: Ryuta Kamizono <kamipo@gmail.com>
2019-07-29 15:53:29 +01:00
Akira Matsuda
0196551e60 Use match? where we don't need MatchData 2019-07-29 14:23:10 +09:00
Ryuta Kamizono
715dad107b
Merge pull request #36787 from st0012/refactor-sql-tests
Use capture_sql helper method in tests
2019-07-28 16:34:24 +09:00
st0012
6fbf52d580 Use capture_sql helper method in tests 2019-07-28 14:47:57 +08:00
Rafael França
481714dd06
Merge pull request #36303 from gaotongfei/feature/ignore-specified-fixtures
Allow specifying fixtures to be ignored in "_fixture" section
2019-07-27 21:12:06 -04:00
Carlos Antonio da Silva
2e9176c04a Update links to new MySQL server errors reference page [ci skip] 2019-07-27 16:06:50 -03:00
Tongfei Gao
c09a4fd23d Allow specify fixtures to be ignored
Allow specifying what fixtures can be ignored by setting
`ignore` in fixtures YAML file:

    # users.yml
    _fixture:
      ignore:
        - base

    base: &base
      admin: false
      introduction: "This is a default description"

    admin:
      <<: *base
      admin: true

    visitor:
      <<: *base

In the above example, "base" fixture will be ignored when creating
users fixture. This is helpful when you want to inherit attributes
and it makes your fixtures more "DRY".
2019-07-27 16:40:16 +08:00
yuuji.yaginuma
121551b80a Avoid to use a method that acts on the hash
To avoid a deprecation warning.
2019-07-27 17:26:42 +09:00
Akira Matsuda
d1ffe59ab5 Use match? where we don't need MatchData
We're already running Performance/RegexpMatch cop, but it seems like the cop is not always =~ justice
2019-07-27 13:06:49 +09:00