Commit Graph

23671 Commits

Author SHA1 Message Date
eileencodes bd04126e79
Fix typo 2024-05-21 19:06:44 -04:00
eileencodes 227c590d02
Add test and fix changelog for `schema_cache_ignored_table?` 2024-05-21 15:37:38 -04:00
eileencodes e815c6663a
Make public method for `schema_cache_ignored_tables?`
Previously we only provided a method to set the ignored schema cache
tables, but there was no way to ask if a table was ignored by the schema
cache. Applications may want to implement their own schema cache, or at
least run this check. Rather than forcing them to implement an internal
method, this adds a way to ask whether a table is ignored by the schema
cache code.

Usage:

```ruby
ActiveRecord.schema_cache_ignored_tables = ["developers"]
ActiveRecord.schema_cache_ignored_tables?("developers")
```
2024-05-21 14:42:25 -04:00
Xavier Noria 1b534a4d10 ActiveRecord -> Active Record 2024-05-21 14:24:28 +02:00
Yasuo Honda b708a1b9dd Address `Layout/IndentationWidth` cop offenses
This commit addresses RuboCop offenses reported at
https://buildkite.com/rails/rails/builds/107320#018f8835-b770-4068-9b97-0aec28c7ddb3

- Offenses are addressed by RuboCop auto correction as follows
```
$ bundle exec rubocop -a activerecord/test/cases/connection_adapters/type_lookup_test.rb
Inspecting 1 file
C

Offenses:

activerecord/test/cases/connection_adapters/type_lookup_test.rb:91:9: C: [Corrected] Layout/IndentationWidth: Use 2 (not 3) spaces for indentation.
           %w{decimal(2) decimal(2,0) numeric(2) numeric(2,0) number(2) number(2,0)}.each do |type|
        ^^^
activerecord/test/cases/connection_adapters/type_lookup_test.rb:92:11: C: [Corrected] Layout/IndentationWidth: Use 2 (not 3) spaces for indentation.
             cast_type = @connection.send(:type_map).lookup(type)
          ^^^
activerecord/test/cases/connection_adapters/type_lookup_test.rb:94:14: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
             assert_equal :decimal, cast_type.type
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
activerecord/test/cases/connection_adapters/type_lookup_test.rb:95:14: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
             assert_equal 2, cast_type.cast(2.1)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 4 offenses detected, 4 offenses corrected
$
```

Follow up #51826
2024-05-18 07:49:08 +09:00
Rafael Mendonça França d0ce794e2d
Merge pull request #51442 from rosa/skip-deprecation-warning-for-invalid-reflections
Emit deprecation warning about inverse_of inference only for valid reflections
2024-05-17 16:38:06 -04:00
Rafael Mendonça França 59eb4f49a0
Merge pull request #51826 from Shopify/remove-oracle-sqlserver-from-tests
Remove oracle sqlserver from tests
2024-05-17 16:18:30 -04:00
Joé Dupuis 478874ae1b
Add `active_record.postgresql_adapter_decode_dates`
to toggle automatic decoding of dates column with the
PostgresqlAdapter.

PR #51483 is a breaking change and should have been gated behind a
config.
2024-05-15 18:38:08 +00:00
Andrew Novoselac de7bf12e46 Remove support for SQLServerAdapter, OracleAdapter and JBDC adapters in tests
As of rails/rails@8eae753 we are no longer supporting these and do not need to test them.
2024-05-14 14:56:16 -04:00
Rafael Mendonça França 52b417bca6
This will be released in 7.2, not 8 2024-05-14 18:05:40 +00:00
Carlos Antonio da Silva 0be846c893
Merge pull request #51758 from taketo1113/fix-changed-cidr2
PostgreSQL Cidr#change? raise an error of NoMethodError: undefined method `prefix' for nil, when creating a record with an empty value of inet/cidr column.
2024-05-14 09:59:51 -03:00
Ryuta Kamizono 1b6b1a9cb9
Merge pull request #51788 from fatkodima/batch_enumerator-destroy_all-returns-rows-number
Change `BatchEnumerator#destroy_all` to return the total number of affected rows
2024-05-14 21:13:55 +09:00
Jean Boussier 4ca838b899 Eliminate `lease_connection` call in `cached_find_by`
Fix: https://github.com/rails/rails/issues/51811
2024-05-14 15:12:12 +09:00
Jean Boussier db4dba78c9 Only serialize associations that were loaded
Fix: https://github.com/rails/rails/issues/51807

`association_cached?` only means the Association object was
created, not that the records were loaded.

Co-Authored-By: benk-gc <bkyriakou@gocardless.com>
2024-05-14 09:37:23 +09:00
Taketo Takashima c395b99f11 Fix PostgreSQL Cidr#change?: NoMethodError: undefined method `prefix` for nil 2024-05-14 08:43:51 +09:00
fatkodima 94af7c181a Change `BatchEnumerator#destroy_all` to return the total number of affected rows 2024-05-13 22:54:25 +03:00
Ryuta Kamizono 36885949a9
Merge pull request #51787 from fatkodima/fix-touch_all-with-aliased-attribute-for-update
Fix `ActiveRecord::Relation#touch_all` with custom attribute aliased as attribute for update
2024-05-14 03:55:58 +09:00
Andrew Novoselac 5b91084f20 Remove support for oracle, sqlserver and JRuby specific database adapters from the new and db:system:change commands. The supported options are sqlite3, mysql, postgresql and trilogy. 2024-05-13 14:07:47 -04:00
Rafael Mendonça França bf59d363fb
Clean CHANGELOG for 8.0 2024-05-13 16:55:52 +00:00
Rafael Mendonça França 37fd0e7fe4
Development of Rails 8.0 starts now
🎉
2024-05-13 16:45:20 +00:00
Yasuo Honda d2cf718093
Merge pull request #51806 from yahonda/follow_up_51571
Use `foreign_key` option for destroy_async test models
2024-05-13 22:18:31 +09:00
Yasuo Honda 9a77052dee Use `foreign_key` option for destroy_async test models
This pull request replaces the deprecated `query_constraints:` option with `foreign_key` for destroy_async test models.
Follow up #51571

- This commit addresses these two warnings below:

```
% bin/test test/activejob/destroy_association_async_test.rb
Using sqlite3
DEPRECATION WARNING: Setting `query_constraints:` option on `Cpk::BookDestroyAsync.has_many :chapters` is deprecated. To maintain current behavior, use the `foreign_key` option instead. (called from new at /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:19)
/Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:19:in `new'
  /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:19:in `create'
  /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/associations/builder/association.rb:50:in `create_reflection'
  /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/associations/builder/association.rb:32:in `build'
  /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/associations.rb:1531:in `has_many'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/models/cpk/book_destroy_async.rb:7:in `<class:BookDestroyAsync>'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/models/cpk/book_destroy_async.rb:4:in `<module:Cpk>'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/models/cpk/book_destroy_async.rb:3:in `<top (required)>'
  /Users/yahonda/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
  /Users/yahonda/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/activejob/destroy_association_async_test.rb:27:in `<top (required)>'
  /Users/yahonda/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
  /Users/yahonda/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
  /Users/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:61:in `block in load_tests'
  /Users/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:60:in `each'
  /Users/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:60:in `load_tests'
  /Users/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:52:in `run'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/support/tools.rb:37:in `<top (required)>'
  bin/test:11:in `require_relative'
  bin/test:11:in `<main>'
DEPRECATION WARNING: Setting `query_constraints:` option on `Cpk::ChapterDestroyAsync.belongs_to :book` is deprecated. To maintain current behavior, use the `foreign_key` option instead. (called from new at /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:19)
/Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:19:in `new'
  /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:19:in `create'
  /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/associations/builder/association.rb:50:in `create_reflection'
  /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/associations/builder/association.rb:32:in `build'
  /Users/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/associations.rb:1910:in `belongs_to'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/models/cpk/chapter_destroy_async.rb:8:in `<class:ChapterDestroyAsync>'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/models/cpk/chapter_destroy_async.rb:4:in `<module:Cpk>'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/models/cpk/chapter_destroy_async.rb:3:in `<top (required)>'
  /Users/yahonda/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
  /Users/yahonda/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/activejob/destroy_association_async_test.rb:28:in `<top (required)>'
  /Users/yahonda/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
  /Users/yahonda/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
  /Users/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:61:in `block in load_tests'
  /Users/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:60:in `each'
  /Users/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:60:in `load_tests'
  /Users/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:52:in `run'
  /Users/yahonda/src/github.com/rails/rails/activerecord/test/support/tools.rb:37:in `<top (required)>'
  bin/test:11:in `require_relative'
  bin/test:11:in `<main>'
Run options: --seed 24461

....................

Finished in 0.199597s, 100.2019 runs/s, 370.7471 assertions/s.
20 runs, 74 assertions, 0 failures, 0 errors, 0 skips
%
```
2024-05-13 21:55:43 +09:00
Jean Boussier ed2c15b524 Move `ActiveRecord::Base.insert` in `Relation`
Ref: https://github.com/rails/rails/pull/50396

As well as related methods.
2024-05-13 15:52:02 +09:00
Jean Boussier 8f419e482b Move `ActiveRecord::Base.destroy` in `Relation`
Ref: https://github.com/rails/rails/pull/50396
2024-05-13 15:52:02 +09:00
Jean Boussier 5e1d07dff4 Move `ActiveRecord::Base.delete` in `Relation`
Ref: https://github.com/rails/rails/pull/50396
2024-05-13 15:52:02 +09:00
Jean Boussier 135af7ba28
Merge pull request #51803 from akhilgkrishnan/fix-batch-test
Fix: batch test assertion order
2024-05-13 15:21:37 +09:00
Akhil G Krishnan 75bdf05d87 Fix: batch test assertion order
Co-authored-by: Chedli Bourguiba <chaadow@users.noreply.github.com>
2024-05-13 08:10:51 +05:30
Jean Boussier c08c2efda2 Refactor `ActiveRecord::TokenFor` to not rely on relation delegation
Ref: https://github.com/rails/rails/pull/50396
Ref: https://github.com/rails/rails/pull/51776

`ActiveRecord::Relation` automatically delegates missing methods
to the model class wrapped in a `scoping { }` block.

This is to support scoping in user defined class methods. The problem
however is that it's very error prone for the framework, because we
can mistakenly call model methods from inside `Relation` and not
realized we're applying a global scope.

In the best case scenario it's just a waste of performance, but
it can also lead to bugs like https://github.com/rails/rails/issues/51775

I'm planning to restrict this automatic delegation to methods defined
in childs of `ActiveRecord::Base` only: https://github.com/rails/rails/pull/50396
but for this to work we must first refactor any Rails code that rely on it.
2024-05-13 11:28:15 +09:00
Jean Boussier c6d0ef5974 Refactor `ActiveRecord::SignedId` to not rely on relation delegation
Ref: https://github.com/rails/rails/pull/50396
Ref: https://github.com/rails/rails/pull/51776

`ActiveRecord::Relation` automatically delegates missing methods
to the model class wrapped in a `scoping { }` block.

This is to support scoping in user defined class methods. The problem
however is that it's very error prone for the framework, because we
can mistakenly call model methods from inside `Relation` and not
realized we're applying a global scope.

In the best case scenario it's just a waste of performance, but
it can also lead to bugs like https://github.com/rails/rails/issues/51775

I'm planning to restrict this automatic delegation to methods defined
in childs of `ActiveRecord::Base` only: https://github.com/rails/rails/pull/50396
but for this to work we must first refactor any Rails code that rely on it.
2024-05-13 11:07:01 +09:00
fatkodima e057037c72 Support `touch_all` in batches 2024-05-13 09:23:57 +09:00
Sampat Badhe a44fbb11a3
Correct typo in activerecord changelog [ci skip]
Correct typo in activerecord changelog for -https://github.com/rails/rails/pull/50662
2024-05-12 10:43:00 +05:30
fatkodima 47f25b268a Add support for `:if_not_exists` and `:force` options to `create_schema` 2024-05-11 16:40:20 +03:00
fatkodima 5dcc35b6e1 Fix `ActiveRecord::Relation#touch_all` with custom attribute aliased as attribute for update 2024-05-11 14:14:44 +03:00
Jean Boussier 4bd4fcc85d Fix `.with_connection` to not set current scope
Fix: https://github.com/rails/rails/issues/51775

This us us being bitten by https://github.com/rails/rails/pull/50396
once more. We should really make this delegation much stricter.
2024-05-10 10:02:05 +02:00
lulalala 1c26a39f15 Fix test
It seems now attribute being base should always be part of the key
2024-05-09 23:45:29 +08:00
lulalala 0a36c36dd8 Add index_errors: :nested_attributes_order mode
which respects reject_if and is in nested_attributes order.

When in default index_errors:true mode,
fix #24390 and return index based on full association order.
2024-05-09 20:03:53 +08:00
Nikita Vasilevsky 9cadf61835
Warn about changing `query_constraints:` behavior
This commit adds a deprecation warning for the `query_constraints:`
association option. This option will change behavior in the future versions
of Rails and applications are encouraged to switch to `foreign_key:` to preserve the
current behavior.
2024-05-08 20:08:09 +00:00
Carlos Antonio da Silva 59064f88d8 Remove extraneous array wrapping
We're already conditioning `reflection_fk` based on whether it's an
`Array` or not, so no need to wrap it in this `Array()` call since in
this block it is sure to be an `Array`.

Ref.: d7980c6b10
2024-05-07 09:54:05 -03:00
Jean Boussier 06968f81b3 Reduce allocations in BelongsToAssociation#replace_keys
Using the same benchmark as in https://github.com/rails/rails/pull/51726

`replace_keys` always allocate a multiple arrays to support composite
primary keys, even though most primary keys likely aren't composite.

And even when they are, we can avoid needless allocations by not using
`Array#zip`.

This reduce allocations by 18% (8k) on that benchmark.

Before:

```
Total allocated: 4.88 MB (44495 objects)
Total retained: 4.16 MB (32043 objects)

allocated memory by file
-----------------------------------
...
 320.00 kB  activerecord/lib/active_record/associations/belongs_to_association.rb

allocated objects by file
-----------------------------------
...
      8000  activerecord/lib/active_record/associations/belongs_to_association.rb
```

After:

```
Total allocated: 4.56 MB (36495 objects)
Total retained: 4.16 MB (32041 objects)
```

NB: `belongs_to_association` doesn't show up in top files anymore
2024-05-07 12:43:17 +02:00
Jean Boussier bac70c3775
Merge pull request #51752 from Shopify/result-duplicated-column
Simplify `ActiveRecord::Result#hash_rows`
2024-05-07 12:07:01 +02:00
Jean Boussier 9652207c6b Simplify `ActiveRecord::Result#hash_rows`
Last big refactor was in https://github.com/rails/rails/pull/37614.
Somewhat extracted from https://github.com/rails/rails/pull/51744.

The concern about columns with the same name didn't make much sense
to me because in both code paths, the last one wins, so we can simplify
the whole methods.

Additionally by implementing `columns_index`, we have a decent template
always available.
2024-05-07 11:45:38 +02:00
David Heinemeier Hansson e8e077dd16
Add ENV["SKIP_TEST_DATABASE_TRUNCATE"] flag to speed up multi-process test runs (#51686) 2024-05-06 17:04:01 -07:00
maniSHarma7575 fd94a129d1 [FIX] Sqlite3 Should create db for missing parent directories as well 2024-05-06 16:04:20 +02:00
Jean Boussier 743763b860 Avoid allocating a column_type Hash when unnecessary
Only the Postgres provide these types, so no point allocating
a hash every time for other adapters.
2024-05-06 12:46:18 +02:00
Rafael Mendonça França 9ddb9e2f2d
Fix documentation
This method still returns string keys.
2024-05-03 17:06:06 +00:00
Jean Boussier d9a54e41ab Use symbols as keys for `_reflections`
Using a simple benchmark such as:

```ruby
Post.transaction do
  100.times do
    post = Post.create!(author_id: 42, title: "A" * 50, body: "a" * 400, tags: "blah blah blah", published_at: Time.now, comments_count: 20)
    20.times do
      post.comments.create!(author_id: 42, body: "a" * 120, tags: "blah blah blah", published_at: Time.now)
    end
  end
end

posts = Post.includes(:comments).to_a
```

This allocate `43,077` objects, and out of these `2,200` are caused by association
names being stored as strings internally:

```
Allocated String Report
-----------------------------------
  80.00 kB    2000  "post"
              2000  activerecord/lib/active_record/reflection.rb:123

   8.00 kB     200  "comments"
               200  activerecord/lib/active_record/reflection.rb:123
```

This is because many API accept either symbol or string, and blindly
call `to_s` on it. We could avoid this with sprinkling the code with
`Symbol == association ? association.name : association.to_s`, but it's
ugly.

This issue may be entirely solved in a future Ruby version, but
it will take years: https://bugs.ruby-lang.org/issues/20350

By using symbols, we both save allocations, and marginally speed up
lookups and comparisons, reducing this particular benchmark allocations
by 5%.

It's a bit unclear why these were ever made strings. Historically
symbols were immortal and using them for user supplied data could lead
to DOS vulnerability, so this may have been why, but it's not longer
a concern since Ruby 2.2.
2024-05-03 14:43:33 +02:00
Jean Boussier fa048105d1 Eliminate missed `lease_connection` calls
Followup: https://github.com/rails/rails/pull/51353
Fix: https://github.com/rails/rails/issues/51722

Not too sure what happened, but I somehow missed quite a number
of `lease_connection` calls inside Active Record.
2024-05-03 13:32:33 +02:00
Jean Boussier 087260d0fc
Merge pull request #51713 from Shopify/stale-state-casting
Don't cast `stale_state` to String
2024-05-02 16:59:02 +02:00
Jean Boussier 8ba2b7f92f
Merge pull request #51409 from fatkodima/fix-destroy_async-job-for-cpk
Fix `destroy_async` job for owners with composite primary keys
2024-05-02 16:57:50 +02:00
Jean Boussier 2cadcb2718 Don't cast `stale_state` to String
I'm looking at reducing the allocations and memory footprint
of Active Record models, and based on a small benchmark I saw
a lot of numeric strings being created from `stale_state`.

I tracked this cast all the way down to 1c07b84df9
but unfortunately the commit message doesn't explain why it was added.

I can't think of a reason why this would be needed.

The benchmark is essentially just: `Post.includes(:comments).to_a` with
`100` posts and `20` comments each.

```
Total allocated: 4.69 MB (45077 objects)
Total retained: 3.84 MB (29623 objects)

retained objects by location
-----------------------------------
...
      2000  activerecord/lib/active_record/associations/belongs_to_association.rb:152

retained memory by location
-----------------------------------
...
  80.00 kB  activerecord/lib/active_record/associations/belongs_to_association.rb:152
```

NB: This break the final assertion of the Rails 6.1 Marshal backward compatibility
test, but I think it's an acceptable tradeoff.
2024-05-02 14:36:34 +02:00