Commit Graph

20918 Commits

Author SHA1 Message Date
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
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
Kevin Sjöberg
f0f067af78 Ignore file fixtures on db:fixtures:load 2021-06-23 20:42:28 +02:00
Eileen M. Uchitelle
97b494fefb
Merge pull request #42579 from eileencodes/fix-nil-pool_config-in-legacy-handling
Fix nil pool_config in legacy connection handling
2021-06-23 10:53:02 -04:00
eileencodes
3f3ec51e0d
Fix nil pool_config in legacy connection handling
This is a followup to #42537 because that PR only fixed the bug for the
new connection handling but not legacy connection handling.

In #41549 a user was getting an error that the `pool_config` was `nil`
so the `all_connection_pools` method would raise an error. After getting
a reproduction application I saw that if the application is
misconfigured this can happen. For example, if an application uses
`:all` for the writing role but does not set
`config.active_record.writing_role = :all` then the
`setup_shared_connection_pool` pool method will get a `nil` value for
the `writing_pool_config` and set that in `set_pool_config`. I
considered fixing this in `setup_shared_connection_pool` directly and
raising an error there, but there's a possibility this _can_ happen in
an external gem or application code if they're using these private APIs
and realistically we never want any code, Rails or otherwise, to be able
to set a `nil` pool config in the pools.
2021-06-23 10:29:03 -04:00
eileencodes
b56e5c1fa1
Move test to more correct file
This test is about connection swapping but not nested connections so it
fits better in the connection handler test. Additionally I renamed the
tests to make more sense and removed unnecessary parts like using
ApplicationRecord, the bug is reproducible on ActiveRecord::Base alone.
2021-06-23 10:25:29 -04:00
Zachary Scott
d4d560bc4b
Merge pull request #42018 from benoror/patch-2
[ci skip] Enhance delegated types example & schema
2021-06-23 19:15:05 +09:00
Ben Orozco
85a2e5fdcc
[ci skip] Enhance delegated types example & schema
Adding a bit more context to the documented examples & schema
2021-06-22 20:49:51 -05:00
Zachary Scott
3b99ee96ba
Merge pull request #38198 from eikes/migrations-guides-references-section
Adds 'References' to migrations guide and adds 'index' column modifier documentation [ci skip]
2021-06-23 10:40:55 +09:00
Rafael França
b8ef890a5a
Merge pull request #41513 from kassio/include-associated-record-in-activerecord-recordnotsaved
Include record in ActiveRecord::RecordNotSaved
2021-06-22 18:38:39 -04:00
alkeshghorpade
7463afae45 removed unused function param 2021-06-23 00:25:35 +05:30
John Hawthorn
5383f6782f Update CHANGELOG.md 2021-06-22 11:15:03 -07:00
John Hawthorn
b3d77575c5 Allow preloads on instance dependent associations
Instance dependent associations are associations which are defined with
a scope taking an argument which makes the scope they are selected from
dependent on the owner record.

This presented a challenge for preloading and eager_loading, since each
record's association being loaded could require a different scope.
Previously this raised an exception if attempted.

This commit adds the ability to preload instance dependent scopes (eager
loading is still unsupported). This is done by detecting instance
dependent scopes in the preloader and building the scope and
Preloader::Association for each record. Because we now merge similar
scopes later in the preloading process, any records which end up with
the same instance dependent scope will be loaded from a single query.

In the case that there's a different scope for each record, this will
perform N+1 queries. I think it's up to the app developer to consider
this (this is essentially the same as preloading polymorphic scopes).

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
Co-authored-by: Adam Hess <HParker@github.com>
Co-authored-by: Eileen M. Uchitelle <eileencodes@gmail.com>
Co-authored-by: Dinah Shi <dinahshi@github.com>
2021-06-22 11:14:53 -07:00
Alex Ghiculescu
80e240b94d Fix: duplicate objects stored in has many association after save
Fixes https://github.com/rails/rails/issues/42549
2021-06-21 10:55:43 -05:00
eileencodes
d2761cfd29
Raise an error if pool_config is nil in set_pool_config
In #41549 a user was getting an error that the `pool_config` was `nil`
so the `all_connection_pools` method would raise an error. After getting
a reproduction application I saw that if the application is
misconfigured this can happen. For example, if an application uses
`:all` for the writing role but does not set
`config.active_record.writing_role = :all` then the
`setup_shared_connection_pool` pool method will get a `nil` value for
the `writing_pool_config` and set that in `set_pool_config`. I
considered fixing this in `setup_shared_connection_pool` directly and
raising an error there, but there's a possibility this _can_ happen in
an external gem or application code if they're using these private APIs
and realistically we never want any code, Rails or otherwise, to be able
to set a `nil` pool config in the pools.

Note: In the test I made a new connection handler so that we have
isolated pools to test against. Otherwise we'll be testing against the
existing pools but we don't want to mess those up.

Closes #41549
2021-06-18 12:03:20 -04:00
HParker
cb699045a7 [ci skip] Expand on disable-prevent-writes changelog entry 2021-06-17 16:06:25 -07:00
Rafael França
9ceb940375
Merge pull request #42524 from ghiculescu/has-many-build-perf-regression
Performance regression in CollectionAssocation#build
2021-06-17 15:38:48 -04:00
Rafael França
58ed26f616
Revert "Extract methods assert_queries and assert_no_queries" 2021-06-17 13:35:25 -04:00
Alex Ghiculescu
0d1993d352 Performance regression in CollectionAssocation#build 2021-06-17 10:22:17 -05:00
Rafael Mendonça França
a34eb120f1
Don't reuse the same connection to drop the tables
The test can disconnect the connection so we can't reuse.
2021-06-17 15:09:57 +00:00
Rafael Mendonça França
115d4a3994
Simplify the implementation by only calling the checks in the right places 2021-06-17 15:09:54 +00:00
Jamie McCarthy
d9f2453827
Clear connections in this test too 2021-06-17 15:09:23 +00:00
Jamie McCarthy
9eb4b8972a
Re-allow committing transactions already rolled back 2021-06-17 15:09:21 +00:00
Jamie McCarthy
5e4d3a3252
When state is invalidated, skip rollbacks and raise on commits 2021-06-17 15:09:20 +00:00
Jamie McCarthy
bc5d5e2770
Add an "invalidated" TransactionState 2021-06-17 15:09:18 +00:00
Jamie McCarthy
4e30f98ced
Rubocop: remove empty line 2021-06-17 15:09:17 +00:00
Jamie McCarthy
a93482c739
Add Mysql2NestedDeadlockTest, PostgresqlTransactionNestedTest 2021-06-17 15:09:15 +00:00