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)`.
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.
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>
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.
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>
- 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>
💇♀️
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>
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
💇♀️
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>
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
💇♀️
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
💇♀️
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.
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>