Commit Graph

20944 Commits

Author SHA1 Message Date
Jean Boussier
317547e0e7
Merge pull request #42729 from Shopify/fix-has-many-inversing-remove
Fix clearing the inverse relation when has_many_inversing is enabled
2021-07-08 12:18:14 +02:00
Jean Boussier
6d7235dd20 Fix clearing the inverse relation when has_many_inversing is enabled
https://github.com/rails/rails/pull/42601 fixed clearing the inverse relation,
but it didn't account for collection associations.

For these, just assigning `nil` isn't possible because we need the record to
remove it from the collection.

So this PR introduce an explicit method for this purpose rather than
reuse `inversed_from(nil)`.
2021-07-08 11:25:32 +02:00
Jean Boussier
197fb159ec
Merge pull request #42666 from justin808/patch-1
Improve logging of schema cache at startup.
2021-07-08 10:02:41 +02:00
Justin Gordon
9f9c1bf79e Improve logging of schema cache at startup.
This allows verification if the schema cache was used at startup.
2021-07-07 21:33:00 -10:00
Alex Ghiculescu
47467fe33d Verify foreign keys after loading fixtures
When writing fixtures, it's currently possible to define associations that don't exist, even if a foreign key exists. For example:

```yml
george:
  name: "Curious George"
  pirate: redbeard

blackbeard:
  name: "Blackbeard"
 ```

When the fixtures are created, `parrots(:george).pirate` will be nil, but it's not immediately clear why. This can make it hard to debug tests and can give false confidence in passing ones.

This can happen because Rails [disables referential integrity](f263530bf7/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb (L407)) when inserting fixtures. This makes the fixtures algorithm much simpler - it can just create the fixtures in alphabetical order and assume that the other side of a foreign key constraint will *eventually* be added.

Ideally we would check foreign keys once all fixtures have been loaded, so that we can be sure that the foreign key constraints were met. This PR introduces that. To enable it:

```ruby
config.active_record.verify_foreign_keys_for_fixtures = true
```

I'm proposing we enable this in 7.0 for new apps and have added it to new framework defaults. When run against our app, it found 3 fixture files with unmet FK constraints - turns out all those fixtures weren't being used and were safe to delete.
2021-07-07 15:41:05 -05:00
Jonathan del Strother
0c25a0baee
Micro-optimize ActiveRecord::Core#hash
Avoids calling _read_attribute("id") more than necessary

```ruby
require "active_record"
require "benchmark/ips"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database:  ":memory:")
ActiveRecord::Base.connection.create_table :users do |t|
  t.text :name
end

class User < ActiveRecord::Base
  # This is the current implementation from ActiveRecord::Core
  def hash
    if id
      self.class.hash ^ id.hash
    else
      super
    end
  end
end
class UserFastHash < ActiveRecord::Base
  self.table_name = "users"
  def hash
    if i = id
      self.class.hash ^ i.hash
    else
      super
    end
  end
end

1_000.times { |i| User.create(name: "test #{i}") }
slow_users = User.take(1000)
fast_users = UserFastHash.take(1000)

Benchmark.ips do |x|
  x.report("slowhash") {
    hash = {}
    slow_users.each { |u| hash[u] = 1 }
  }
  x.report("fasthash") {
    hash = {}
    fast_users.each { |u| hash[u] = 1 }
  }
  x.compare!
end
```

```
Warming up --------------------------------------
            slowhash   129.000  i/100ms
            fasthash   177.000  i/100ms
Calculating -------------------------------------
            slowhash      1.307k (± 0.7%) i/s -      6.579k in   5.033141s
            fasthash      1.764k (± 2.4%) i/s -      8.850k in   5.021749s

Comparison:
            fasthash:     1763.5 i/s
            slowhash:     1307.2 i/s - 1.35x  (± 0.00) slower
```
2021-07-07 19:07:22 +01:00
Roberto Miranda
7834a783b8
destroy_all_in_batches is off by default for newly generated Rails apps.
Following up https://github.com/rails/rails/pull/42673#issuecomment-874744582
2021-07-06 19:43:44 +01:00
Jordan
385c4a0771
Fix link to incorrect method in API docs 2021-07-06 10:21:32 +01:00
Ryuta Kamizono
32bac31cc0 Spell checking if record and association_name are provided for AssociationNotFoundError 2021-07-05 17:29:54 +09:00
Petrik
0409ed57ac Clean up checks to see if DidYouMean is defined
As of Ruby 2.7 DidYouMean is included as a default gem, so there is no
need to check if DidYouMean is defined in the test suite. We still need
to check if the DidYouMean modules are defined in the actual code, as
someone might run Rails with DidYouMean disabled by using the
`--disable-did_you_mean` flag. This is ussually done for performance
reasons.

This commit also includes some of the changes made by Yuki in:
https://github.com/rails/rails/pull/39555
These changes include replacing Jaro with the more accurate
SpellChecker, and using DidYouMean::Correctable for simplere
corrections.

The DidYouMean::SpellChecker does have a treshold for corrections.
If there is not enough similarity it might not return a suggestion.
To stop the tests from failing some test data had to be changed.

For example, `non_existent` does not meet the treshold for `hello`, but
`ello` does:

DidYouMean::SpellChecker.new(dictionary: %w[hello]).correct('non_existent')
=> []
DidYouMean::SpellChecker.new(dictionary: %w[hello]).correct('ello')
=> ["hello"]

The treshold makes sense for spelling errors. But maybe we should add a
different SpellChecker that helps to get a suggestion even if there is
little overlap. For example for when a model only has 2 attributes
(title and body), it's helpful to get a suggestion for `name`

Co-Authored-By: Yuki Nishijima <yk.nishijima@gmail.com>
2021-07-04 13:43:50 +02:00
Jean Boussier
ccb66572b1
Merge pull request #42601 from mdemare/has_one_cache
Clear cached has_one association after removing belongs_to association
2021-07-03 08:58:54 +02:00
Ryuta Kamizono
d7fce6c996 Fix odd closing parenthesis by enabling the Layout/ClosingParenthesisIndentation cop 2021-07-02 18:01:50 +09:00
Ryuta Kamizono
db767c332a Fix CI failure caused by error_highlight gem
Since https://github.com/ruby/ruby/pull/4586, error.message includes the
line of code that raised.

https://bugs.ruby-lang.org/issues/17930
2021-07-02 17:44:10 +09:00
Michiel de Mare
484303d698 Clear cached has_one association after setting belongs_to association to nil
Given an author that "has one" post, and setting the post's author to nil,
the author will still have a cached association to the post. Updating any
attribute of the author would erroneously restore the post's author. This
commit resets the association, preventing that behavior.
2021-07-01 22:38:54 +02:00
Abhay Nikam
5cf6898efb Fixes changelog entry after addition of #41722 2021-07-01 18:17:32 +05:30
eileencodes
ce7ff88a50
Fix changelog
Conflict left incorrect entry in changelog.
2021-07-01 08:38:16 -04:00
Eileen M. Uchitelle
6ecf1065da
Merge pull request #41722 from dbussink/openssl-constants
Always use OpenSSL constants for Digest operations
2021-07-01 07:49:39 -04:00
eileencodes
22cef24b6e
Fix migrations with the same timestamps in multiple databases
In #42604 we added functionality that would sort the migrations by
timestamp so that if there were 2 migrations in 2 databases that were
dependent on one another they would be run in timestamp order rather
than in cluster order.

While debugging another issue I realized that if the timestamps are the
same that only one of the migrations will get run. This fixes the issue
by collecting all the db configs for a version so that we get the entire
list.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2021-06-30 09:28:16 -04:00
Dirkjan Bussink
0523532a3c
Always use OpenSSL constants for Digest operations
As also previously discussed in
https://github.com/rails/rails/pull/40770#issuecomment-748347066, this
moves the usage of Digest constants to always use the OpenSSL version of
those Digest implementations.
2021-06-30 13:57:54 +02:00
Alex Kitchens
c3fe14c239
Merge pull request #42625 from intrip/36027-create-or-find-by-docs
Adjust docs of create_or_find_by
2021-06-29 15:44:46 -05:00
Eileen M. Uchitelle
b09c110aaf
Merge pull request #42198 from zetlanddk/remove-dangling-migrations
Handle duplicate migration names in multi db
2021-06-29 15:59:23 -04:00
John Hawthorn
fb3c5a15a3 Fix explain test on sqlite 3.36.0
Explain output changed in 3.36.0

https://www.sqlite.org/releaselog/3_36_0.html

Co-authored-by: Dinah Shi <dinahshi@github.com>
2021-06-29 10:34:22 -07:00
Jean Boussier
8be6e7af27
Merge pull request #42444 from OuYangJinTing/fix-ar-sanitization
Improve disallow_raw_sql error msg and fix typo
2021-06-29 14:18:24 +02:00
Jacopo
562972caa8 Adjust docs of create_or_find_by
- Mention explicitly in the docs that `create_of_find_by` requires a DB
constraint.

Closes #36027

Co-authored-by: Eileen M. Uchitelle <eileencodes@users.noreply.github.com>
2021-06-29 11:04:10 +02:00
Guillermo Iguaran
1cdee90d38
Merge pull request #40445 from robertomiranda/destroy_all-in_batcches
ActiveRecord::Relation#destroy_all perform its work in batches
2021-06-28 22:49:30 -07:00
Roberto Miranda
4e25cedf7f ActiveRecord::Relation#destroy_all perform its work in batches
💇‍♀️

Fix specs

Update activerecord/lib/active_record/relation.rb

Co-authored-by: Eugene Kenny <elkenny@gmail.com>

Update activerecord/lib/active_record/relation.rb

Co-authored-by: Viktar Basharymau <6alliapumob@gmail.com>

Use class attribute instead of mattr_accessor

Add destroy all test case around value returned

add Rails.application.config.active_record.destroy_all_in_batches too new defaults framework

Reset Active Record Relation

Document ActiveRecord::Relation#destroy_all

Add changelog entry and update docs

💇‍♀️

Update method signature and argument docs

Apply suggestions from code review

Co-authored-by: Viktar Basharymau <6alliapumob@gmail.com>
Co-authored-by: Alberto Almagro <albertoalmagro@gmail.com>
2021-06-28 21:37:24 +01:00
Roberto Miranda
06b026f024 Fix migration compatibility for default precision value on datetime columns (Round 2)
add test cases on default precision from 4.2 to 6.1

Fix compatibility on rails 6.0

Fix compatibility from 5.2 to 4.2
2021-06-28 21:27:43 +01:00
Zachary Scott
0ffdf6dfe7
Merge pull request #42615 from zzak/zzak/42572
Add CHANGELOG for #42572
2021-06-28 19:54:52 +09:00
Zachary Scott
6caaec74bb Add CHANGELOG for #42572 2021-06-28 19:40:07 +09:00
OuYangJinTing
d01ab84b41 [AR] Fix typo of disallow_raw_sql! exception msg 2021-06-28 10:22:30 +08:00
Roberto Miranda
63c2efaa16 Add support for if_exists/if_not_exists on remove_foreign_key/add_foreign_key
Applications can set their migrations to ignore exceptions raised when adding a foreign key that already exists or when removing a foreign key that does not exist.

Add test cases

💇‍♀️
2021-06-27 18:55:17 +01:00
alkeshghorpade
e4cb51d8ff typo fix in error raised 2021-06-26 23:26:59 +05:30
Guillermo Iguaran
de737ea267
Merge pull request #42606 from robertomiranda/r/test-compatibility
Fix migration compatibility for default precision value on datetime columns
2021-06-25 17:32:33 -07:00
Samuel Cochran
8478b26485
Prevent polluting ENV with postgres vars 2021-06-26 08:27:53 +10:00
Roberto Miranda
365acb675d Add test case for add_column on rails 5.0 migrations 2021-06-25 17:47:25 +01:00
Roberto Miranda
9e3320ad26 Fix migrations compatibility for default precision value on datetime columns 2021-06-25 17:47:15 +01:00
Roberto Miranda
9b9d9c7bf9 Test default precision against 6.1 2021-06-25 17:47:02 +01:00
Eileen M. Uchitelle
32488129e9
Merge pull request #42604 from eileencodes/fix-migration-ordering-bug
Fix migration ordering across databases
2021-06-25 12:35:37 -04:00
Eileen M. Uchitelle
246bac42a0
Merge pull request #42605 from Tonkpils/tonkpils/revert-disable-automatic-write-protection
Revert "Disable automatic write protection on replicas"
2021-06-25 12:05:17 -04:00
Guillermo Iguaran
5a26648c3c
Merge pull request #42297 from robertomiranda/r/default-precision-datime
Set precision 6 by default for datetime columns
2021-06-25 08:59:14 -07:00
eileencodes
45eb0f3bec
Fix migration ordering across databases
Previously if there were 2 migrations in one db and 1 migration in the
other db all the migrations for db one would run and then all migrations
for db two would run. If a migration in one database depended on a
migration in another database then it could fail. This is probably
pretty rare, however in a multi-db application that's moving tables from
one db to another, running them out of order could result in a migration
error.

In this this change we collect all the versions for each migration and
the corresponding db_config so we can run them in the order they are
created rather than per-db.

Closes #41664
Related #41538

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
Co-authored-by: Kiril Dokh <dsounded@gmail.com>
2021-06-25 11:54:00 -04:00
Leo Correa
69bc201929
Revert "Disable automatic write protection on replicas"
This reverts commit 951deecc52f191ca85bd5c0416b382f4852c6f72.

This change prevents applications from testing replicas and would
require explicitly setting `prevent_writes` when connecting to reading
roles in `connected_to`. For now we'll revert this until there's a
longer term fix in place
2021-06-25 11:35:51 -04:00
Jean Boussier
99913f6a5e Report async queries lock wait duration
This duration is very important to figure wether the `load_async`
actually improved something.
2021-06-25 13:59:47 +02:00
Roberto Miranda
c2a6f618d2 Set precision 6 by default for datetime
💇‍♀️

Set precision 6 by detauls for datime columns when using sqlite3_adapter

 Set precision 6 by detauls for datime columns when using postgresql_adapter

Update activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Co-authored-by: Zachary Scott <zzakscott@gmail.com>

Update tests precision

Add datetime precision by using the migration compatibility layer

Add default precision when adding new column for a table

Fix compatibility tests

Add changelog entry

Update postgres test cases

Custom type does not support precision

Typo ⌨️

Fix specific mysql2 schema

💇‍♀️
2021-06-25 09:23:01 +01:00
Rafael França
b56e6afcfd
Merge pull request #42591 from dinahshi/strict-loading-mode-ivar
Convert strict_loading_mode from class attr to ivar
2021-06-24 16:55:53 -04:00
Eileen M. Uchitelle
3c4d2172b8
Merge pull request #42590 from eileencodes/fix-disable-joins-with-sti-type
Fix disable joins with sti type
2021-06-24 15:50:13 -04:00
eileencodes
207747ec2d
Fix disable_joins when using an enum STI type
When a model has an STI enum type, the type wasn't properly applied when
disable joins was set to true. In this case we need to apply the scope
from the association in `add_constraints` so that `type` is included in
the query. Otherwise in a `has_one :through` with `type` all records
will be returned because we're not filtering on type.

Long term I think this might be in the wrong place and that we want to
do this in a new definition of `target_scope` but that's a future
refactoring that needs to be done.
2021-06-24 14:30:00 -04:00
John Hawthorn
18b0135e18
Merge pull request #42553 from jhawthorn/preload_instance_scope
Support preloads on instance dependent associations
2021-06-24 10:25:47 -07:00
Dinah Shi
cec55aafb9 Convert strict_loading_mode from class attr to ivar
Strict loading mode `:n_plus_one_only` is only
supported on single records, not associations or
models. Using an ivar instead of class_attribute
ensures that this cannot be set globally. This
fixes a bug where setting `strict_loading_mode`
caused errors to be silent when
`strict_loading_by_default` is true.

Fixes #42576

Co-Authored-By: John Hawthorn <john@hawthorn.email>
2021-06-24 11:54:40 -04:00
eileencodes
25f2341968
Move find_target to more correct place for disable joins
Instead of redefining this in has_one_association we can define this in
singular association and reuse the existing `find_target` method.
2021-06-24 10:08:04 -04:00