Commit Graph

8416 Commits

Author SHA1 Message Date
Akira Matsuda
4a9ef5e120 Use Regext#match? where MatchData is not needed 2019-09-17 07:59:04 +09:00
Gannon McGibbon
2ebac57a74
Merge pull request #37120 from gmcgibbon/fix_has_one_reflect_check_custom_pk
Fix has_one through reflection casting check
2019-09-16 12:43:07 -04:00
Eileen M. Uchitelle
9b6433bb82
Merge pull request #37199 from seejohnrun/reduce-surface-area-of-connection-specification
Reduce surface area of ConnectionSpecification
2019-09-16 09:35:22 -04:00
Ryuta Kamizono
306e7a796d Auto-correct rubocop offences 2019-09-16 05:46:26 +09:00
Akira Matsuda
97bc338162 define_attribute takes keyword arguments 2019-09-15 03:05:52 +09:00
Akira Matsuda
364449b252 Passing in a Hash instance as non-kwargs parameters has to be curly braced 2019-09-15 03:05:52 +09:00
Akira Matsuda
51a7422c9f Unify AR save method signature to take keyword arguments 2019-09-15 02:11:42 +09:00
eileencodes
b8fc0150d6 Reduce surface area of ConnectionSpecification
Eventually we'd like to get rid of this class altogether but for now
this PR reduces the surface area by removing methods from the class and
moving classes out into their own files.

* `adapter_method` was moved into database configurations
* `initialize_dup` was removed because it was only used in tests
* Resolver is now it's own class under connection adapters
* ConnectionUrlResolver, only used by the configurations, is in a class
under DatabaseConfigurations

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2019-09-13 22:05:02 -04:00
John Crepezzi
20c7bbd48f Pass db_config object around instead of hashes
This is part 1 of removing the need to pass hashes around inside
connection management. This PR creates database config objects every
time when passing a hash, string, or symbol and sends that object to
connection specification. ConnectionSpecification reaches into the
config hash on db_config when needed, but eventually we'll get rid of
that and ConnectionSpecification since it's doing duplicate work with
the DatabaseConfigurations.

We also chose to change the keys to strings because that's what the
database.yml would create and what apps currently expect. While symbols
are nicer, we'd end up having to deprecate the string behavior first.

Co-authored-by: eileencodes <eileencodes@gmail.com>
2019-09-13 16:55:01 -04:00
Gannon McGibbon
d739c835bc Fix has_one through reflection casting check
Fixes a bug where has_one through relations with non-integer primary
keys were incorrectly raising an implementation error even though a
reflectable association was present.
2019-09-13 13:07:01 -04:00
Eileen M. Uchitelle
42dea6b0f6
Merge pull request #37185 from seejohnrun/config-symbols
Use symbols everywhere for database configurations
2019-09-13 13:03:11 -04:00
eileencodes
ce9b197cc9 Use symbols everywhere for database configurations
Previously in some places we used symbol keys, and in some places we used
string keys. That made it pretty confusing to figure out in a particular
place what type of configuration object you were working with.

Now internally, all configuration hashes are keyed by symbols and
converted to such on the way in.

A few exceptions:

- `DatabaseConfigurations#to_h` still returns strings for backward compatibility
- Same for `legacy_hash`
- `default_hash` previously could return strings, but the associated
  comment mentions it returns symbol-key `Hash` and now it always does

Because this is a change in behavior, a few method renames have happened:

- `DatabaseConfig#config` is now `DatabaseConfig#configuration_hash` and returns a symbol-key `Hash`
- `ConnectionSpecification#config` is now `ConnectionSpecification#underlying_configuration_hash` and returns the `Hash` of the underlying `DatabaseConfig`
- `DatabaseConfig#config` was added back, returns `String`-keys for backward compatibility, and is deprecated in favor of the new `configuration_hash`

Co-authored-by: eileencodes <eileencodes@gmail.com>
2019-09-13 08:53:22 -04:00
Dylan Thacker-Smith
b94efe9f1b activerecord: Allow comment prefix in queries when preventing writes 2019-09-12 15:11:02 -04:00
John Crepezzi
e47f0da77b Fix error message for AdapterNotFound in spec
When this case was his previously, an error would be thrown like:
`<NoMethodError: "undefined method config for...">` because we were
erroneously calling `config` on a `Hash`.

This commit adds a test for this case, and fixes the hash access.
2019-09-12 09:59:03 -04:00
Eugene Kenny
36b639bd5f Skip insert all tests when features are unavailable
These tests are causing the Ruby master build to fail in CI. The Docker
image we use is based on Ubuntu Bionic which provides SQLite 3.22.0, and
the features required to use `insert_all` and `upsert_all` are not
available in that version.
2019-09-11 09:31:23 +01:00
Akira Matsuda
048436213e Workaround for kwargs 2.7 and 2.6 incompatibility
Current Ruby 2.7 implementation behaves differently from 2.6 (by design)
where receiving an empty keyword arguments.

Thus, for now we need this if branch everywhere we're asserting such a method call
in order for the CI to pass.

We could revert this workaround if Ruby 2.7 decides to revert back to the 2.6 behavior,
or maybe we'll need to fix Minitest::Mock once the 2.7 spec became stable.
2019-09-11 12:24:32 +09:00
Akira Matsuda
3796cd628f Fix keyword arguments warnings 2019-09-11 10:48:29 +09:00
Eugene Kenny
0cc13d3169 Accept columns passed with options in remove_index
Before this patch, column names could only be passed as a positional
argument when no other options were supplied:

    remove_index :reports, :report_id

Passing column names positionally along with other options would fail:

    remove_index :reports, :report_id, unique: true
    # => ArgumentError: wrong number of arguments (given 3, expected 1..2)
2019-09-10 22:21:20 +01:00
Eugene Kenny
edb23791d2 Allow bulk alter to drop and recreate named index
In 3809c80cd55ac2838f050346800889b6f8e041ef, adding an index with a
name that's already in use was changed from an error to a warning, to
allow other statements in the same migration to complete successfully.

In 55d0d57bfc72c0bdbc81ae5d95c99729f16899af this decision was reversed,
but instead of allowing the statement to execute and raise an adapter-
specific error as it did before, an `ArgumentError` was raised instead.

This interferes with a legitimate use case: on MySQL, it's possible to
drop an index and add another one with the same name in a single `ALTER`
statement. Right now an `ArgumentError` is raised when trying to do so,
even though the resulting statement would execute successfully.

There's no corresponding `ArgumentError` raised when attempting to add a
duplicate column, so I think we can safely remove the check and allow
the adapter to raise its own error about duplicate indexes again.
2019-09-10 00:26:14 +01:00
Eugene Kenny
fad44a6999 Clear query cache when insert all is used
The `InsertAll` class currently calls `exec_query`, which doesn't give
the query cache enough information to know that it needs to be cleared.

By adding an `exec_insert_all` method that calls `exec_query` internally
we can configure the query cache to clear when that method is called.
2019-09-09 00:55:16 +01:00
Eugene Kenny
2bdb58cc47 Allow inspected records to be marshaled
Since 32b03b46150b0161eba2321ccac7678511e3d58e, records which have had
`inspect` called on them can't be marshaled, because of this anonymous
`DelegateClass`. We can fix this by giving the class a concrete name.
2019-09-07 23:45:38 +01:00
Takayuki Nakata
a3a0c45a3e Fix comments to update mysql version in reference URL [ci skip]
Mysql versions 5.5.8 and up are supported in rails6 and MySQL 5.5 is
already EOL since December 31, 2018.
2019-09-06 09:54:16 +09:00
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
bogdanvlviv
39c6986ffc
Extend test_can_write_while_reading_from_replicas_if_explicit
- Ensure that explicit method call `connected_to` with `prevent_writes: false`
  turns off 'preventing writes' in the passed block.
- Ensure that after explicit call method call `connected_to` with `prevent_writes: false`
  'preventing writes' is retained

Related to https://github.com/rails/rails/pull/37065
2019-08-30 17:46:21 +03:00
eileencodes
66bc2ff6b3 Call while_preventing_writes from connected_to
If a user is using the middleware for swapping database connections and
manually calling `connected_to` in a controller/model/etc without
calling `while_preventing_writes(false)` there is potential for a race
condition where writes will be blocked.

While the user could _just_ call `while_preventing_writes` in the same
place they call `connected_to` this would mean that all cases need to
call two methods.

This PR changes `connected_to` to call `while_preventing_writes`
directly. By default we'll assume you don't want to prevent writes, but
if called with `connected_to(role: :writing, prevent_writes: true)` or
from the middleware (which calls `connected_to` this way) the writes
will be blocked.

For replicas, apps should use readonly users to enforce not writing
rather than `while_preventing_writes` directly.

Should fix the remaining issues in
https://github.com/rails/rails/issues/36830
2019-08-28 13:44:51 -04:00
Takayuki Nakata
37b4a3c4f0 Fix docs to replace http with https in activerecord [ci skip] 2019-08-26 22:34:33 +09:00
Guo Xiang Tan
97fe59990c Fix ConnectionPool::Reaper reaping parent connection pools after fork.
After forking, the connection handler will discard any connection pools that belongs to the parent process. However, it will continue to hold a reference to the connection pools of the parent process which is used for retrieval of the connection pool in the child process. Therefore, the `weakref_alive?` check in the connection pool reaper is insufficient since the connection pools of the parent process is never GCed.
2019-08-24 14:26:50 +08:00
Guo Xiang Tan
755da80063 Move tests that does not rely on forking out of fork conditional. 2019-08-21 15:27:41 +08:00
John Hawthorn
95cf3d9a57
Merge pull request #36999 from rails/reaper_fork2
Fix error on reap/flush for closed connection pool
2019-08-20 14:00:36 -07:00
John Hawthorn
3a8eeacf63 Fix error on reap/flush for closed connection pool 2019-08-20 13:20:56 -07:00
Eileen M. Uchitelle
d06dbe9432
Merge pull request #36930 from eugeneius/active_record_base_connection_specification_name
Fix setting connection_specification_name on ActiveRecord::Base
2019-08-20 15:15:37 -04:00
John Hawthorn
c72b77bc54 Ensure reaper threads respawn in forks
This broke in 3e2e8ee where it switched to one reaper thread per
process. However, the implementation will only spawn the reaping thread
if the reaping frequency has not been seen.  Since the reaping
frequencies are stored in a class instance variable, that variable is
copied when the process is forked. As such, a forked process will not
spawn a new reaping thread since the class instance variable would have
contain the reaping frequency that was seen in the parent process.

This commit tracks threads separately and checks that they both have
been spawned and are currently alive.

This adds a failing test to reaper_test.rb, based on the previous test
without forking. It also improves the test to return an failure instead
of hanging forever if the reaper is not started.

Co-authored-by: Guo Xiang Tan <tgx_world@hotmail.com>
2019-08-20 10:42:48 -07:00
Anmol Arora
2a65310890 Clear ActiveRecord object memoized by take 2019-08-20 00:54:03 +05:30
Jon Zeppieri
d553213cfb Make prepared statement status thread and instance-specific
This fixes a race condition in system tests where prepared
statements can be incorrectly parameterized when multiple
threads observe the mutation of the @prepared_statements
instance variable on the connection.

Fixes #36763
2019-08-16 02:11:19 -04:00
Kir Shatrov
c99c572d37 Improve detection of ActiveRecord::StatementTimeout with mysql2 adapter
in the edge case when the query is terminated by MySQL server during filesort.
See https://bugs.mysql.com/bug.php?id=96537 for more details.
2019-08-15 21:00:07 +01:00
Eugene Kenny
92d31b1f81 Fix setting connection_specification_name on ActiveRecord::Base
Since 2f8b397258b66581409b0e6537f98ea9b56e9f19, `ActiveRecord::Base` and
`ApplicationRecord` use the same default `connection_specification_name`
so that database connections configured on `ApplicationRecord` also
apply to `ActiveRecord::Base`.

However, when resolving the connection for `ApplicationRecord` we should
continue to fall back to `ActiveRecord::Base` when there's no connection
explicitly configured, so that setting `connection_specification_name`
on `ActiveRecord::Base` affects `ApplicationRecord` and its descendants
in this case as it did in previous versions.
2019-08-14 02:54:13 +01:00
Gannon McGibbon
9899697794 Stop trying to read yaml file fixtures when loading Active Record fixtures 2019-08-09 14:24:59 -04:00
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
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
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
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
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