Commit Graph

18153 Commits

Author SHA1 Message Date
Stephen Margheim 77800d1920 Update documentation for ActiveRecord persistence methods that accept +returning+ option to note that this is available for SQLite3 adapter as well
Co-authored-by: OuYangJinTing <ouyangshi95@foxmail.com>
2023-12-28 09:54:50 +01:00
Jean Boussier 6b446bee63 Remove SQLite production warning but leave production config disabled
There are valid use cases for running SQLite in production, however it must be done
with care, so instead of a warning most users won't see anyway, it's preferable to
leave the configuration commented out to force them to think about having the database
on a persistent volume etc.

Co-Authored-By: Jacopo Beschi <beschi.jacopo@gmail.com>
2023-12-27 23:32:18 +01:00
zzak 16ff9afb2e
Merge pull request #50275 from seanpdoyle/polymorphic-rename
Provide guidance for renaming classes in polymorphic associations [ci skip]
2023-12-25 08:01:15 +09:00
Maple Ong 9572fcb4a0 Remove unused argument 2023-12-21 16:47:32 -05:00
OKURA Masafumi d9c63917b9
Doc: Prevent `Attributes` to be a link [ci-skip] (#50419)
This `Attributes` is not related to ActiveRecord's `attribute` API.
2023-12-21 16:51:20 +01:00
Jean Boussier 60df08920f
Merge pull request #50373 from fatkodima/improve-queries-assertions-matchers
Expose `assert_queries_match` and `assert_no_queries_match` assertions
2023-12-21 12:59:10 +01:00
fatkodima f48bbff32c Expose `assert_queries_match` and `assert_no_queries_match` assertions 2023-12-21 01:30:16 +02:00
fatkodima f68727dcd0 Fix async queries to work with query cache 2023-12-20 23:50:41 +02:00
Jonathan Hefner bf6c20edb7 Replace backticks with RDoc markup [ci-skip] 2023-12-19 13:16:47 -06:00
Jean Boussier 8c9ff7cdd8 Fix `Relation#transaction` to not apply a default scope
Fix: https://github.com/rails/rails/issues/50368

When called on a scope it would go through the default scoping
delegator that applies a default scope, which make no sense.
2023-12-19 11:29:39 +01:00
Petrik de Heus 462e8e8b1c
Merge pull request #50367 from mechanicles/update-api-doc-for-activerelation-none-any-one
Update API doc for ActiveRecord::Relation#none?/#any?/#one? [ci-skip]
2023-12-18 08:26:21 +01:00
Santosh Wadghule 45ce025274
Update API doc for ActiveRecord::Relation#none?/#any?/#one? [ci-skip]
I checked https://github.com/rails/rails/issues/50327 and noticed that
clear API docs are needed. Some developers are using these methods
incorrectly, thinking they're only for Active Record Relation, but they
actually work with Enumerable.
2023-12-18 11:42:57 +05:30
Stephen Margheim 4e7bdcf93a Add support for generated columns in SQLite3 adapter
Generated columns (both stored and dynamic) are supported since version 3.31.0 of SQLite.
This adds support for those to the SQLite3 adapter.

```ruby
create_table :users do |t|
  t.string :name
  t.virtual :name_upper, type: :string, as: 'UPPER(name)'
  t.virtual :name_lower, type: :string, as: 'LOWER(name)', stored: true
end
```
2023-12-14 18:19:02 +01:00
Jean Boussier 9517841bc3
Merge pull request #50334 from yykamei/add_doc_for_assert_queries
Add doc for `assert_queries` and `assert_no_queries`
2023-12-14 16:43:15 +01:00
Jean Boussier 970126e07e
Merge pull request #50336 from fatkodima/schema_dumper-dump-global-out
Change `SchemaDumper.dump` default stream to `$stdout`
2023-12-14 16:41:23 +01:00
Jean Boussier f8e842f13e Fix `Model.none.async_pluck` to return a Promise
Previously it would return a naked empty array, which is a break
of the interface.
2023-12-14 12:36:42 +01:00
Jean Boussier 5a1b2b8e50
Merge pull request #50353 from lucasmazza/lm/fix-load-async-null-relation
Fix `load_async` for null relations
2023-12-14 12:33:25 +01:00
Yutaka Kamei ccc512f6d6
Add doc for `assert_queries` and `assert_no_queries`
Follow-up to #50281

I think `assert_queries` and `assert_no_queries` are so useful that I
want to read the documentation for the assertions. This patch adds the
doc for them and updated the test case named `test_assert_queries` to
verify `:matcher` works as expected.

I also changed `assert_queries` to make sure the subscriber of
`"sql.active_record"` is unsubscribed without affecting other test
cases. Without this change, some test cases fail because of execution of
`String#match`, which may cause `ArgumentError: invalid byte sequence in UTF-8`.
2023-12-14 09:09:16 +09:00
Lucas Mazza c755144201
Fix `load_async` for null relations
Fixes https://github.com/rails/rails/issues/50351
2023-12-13 20:38:41 -03:00
Dustin Brown f5f48cb8a5 Prevent duplicate records when preloading has_many
When preloading a has_many association, we were simply concatenating
the preloaded records without regard to whether they were already
loaded on the owner. Even though there is a check for `loaded?` in this
part of the preloader, some persisted records may not be marked as such.
For example, if a record is created via `owner.association.create`. This
change reverts to the previous behavior of replacing the target
association while also preserving non-persisted records, which was the
goal of https://github.com/rails/rails/pull/50129.

Co-authored-by: John Hawthorn <john@hawthorn.email>
2023-12-13 21:59:53 +00:00
Jean Boussier ee65e97829 Fix a typo in a comment 2023-12-13 18:04:39 +01:00
Jean Boussier 0fe087eb56 TrilogyAdapter: ignore host if socket is set
Contrary to mysql2, trilogy will ignore the `socket` config
if `host` is set.

This makes it impossible to configure a UNIX domain socket connection
via `DATABASE_URL`, as the URL must include a host.
2023-12-13 17:35:09 +01:00
fatkodima 306b2fc22e Change `SchemaDumper.dump` default stream to `$stdout` 2023-12-12 16:39:22 +02:00
Jean Boussier 3881518c47
Merge pull request #50281 from p8/activerecord/assert-queries
Expose `assert_queries` and `assert_no_queries` assertions
2023-12-12 00:29:45 +01:00
Jean Boussier f5c6aa3349
Merge pull request #50163 from EduardoGHdez/interval-overflow-protection
Improve interval overflow protection
2023-12-11 18:51:04 +01:00
Eileen M. Uchitelle 0745043c7a
Merge pull request #50319 from fatkodima/fix-where-for-polymorphic-cpk
Fix predicate builder for polymorphic models referencing models with composite primary keys
2023-12-11 10:20:07 -05:00
Eileen M. Uchitelle 0a6532db99
Merge pull request #50321 from fatkodima/fix-polymorphic-belongs_to-with-query_constraints
Fix polymorphic `belongs_to` to correctly use parent's `query_constraints`
2023-12-11 10:12:44 -05:00
Petrik 8392c54e73 Expose `assert_queries` and `assert_no_queries` assertions
To assert the expected number of queries are made, Rails internally uses
`assert_queries` and `assert_no_queries`. These assertions can be
useful in applications as well.

By extracting these assertions to a module, the assertions can be
included where required.
These assertions are added to `ActiveSupport::TestCase` when
ActiveRecord is defined.

ActiveStorage, ActionView and ActionText are using this module now as
well, instead of duplicating the implementation.
The internal ActiveRecord::TestCase, used for testing ActiveRecord,
implements these assertions as well. However, these are slighlty more
advanced/complex and use the SQLCounter class. To keep things simple,
for now this implementation isn't used.
2023-12-11 12:31:16 +01:00
fatkodima 01cd9840a9 Fix `Preloader` to not generate a query for already loaded association with `query_constraints` 2023-12-11 12:28:49 +02:00
fatkodima f99d05e722 Fix polymorphic `belongs_to` to correctly use parent's `query_constraints` 2023-12-10 22:04:05 +02:00
fatkodima 0663b44bd9 Fix predicate builder for polymorphic models referencing models with composite primary keys 2023-12-10 15:26:23 +02:00
John Hawthorn c80fa6a069
Merge pull request #50190 from jhawthorn/retry_checkout_after_reap
Retry new connection on checkout after reap
2023-12-08 17:10:08 -08:00
Aaron Patterson ab8f5c9d93
Merge pull request #50079 from tttffff/mysql_null_first_last_consistency
Arel nulls first/last implementation Mysql
2023-12-08 15:44:02 -08:00
Sean Doyle 0d8b3f09af Provide guidance for renaming classes in polymorphic associations [ci skip]
Add guidance to the Association Basics and `.belongs_to` method
documentation to encourage the renaming of a model's Ruby class to
coincide with updates to the existing data in the database.

Since Action Text and Active Storage rely on polymorphic associations,
add similar warnings to their guides.

Co-authored-by: Petrik de Heus <petrik@deheus.net>
Co-authored-by: Stephen Hanson <s.hanson5@gmail.com>
Co-authored-by: zzak <zzakscott@gmail.com>
2023-12-08 09:19:21 -05:00
Aaron Patterson c5a8621c7f
Merge pull request #50129 from MaxLap/fix_preload_concat
Fix: Preloader should no longer overwrites built records
2023-12-07 14:21:26 -08:00
abeidahmed c8caf6d867 [Fix #49874] `has_secure_token` calls the setter method on initialize
Follow-up to #49146

The original behavior of `has_secure_token` was to use the
`send("#{attribute}=", some_value)` method so that the setter method, if
defined, was called. PR #49146 replaced the `send` method with
`write_attribute` which doesn't call the setter method and breaks
existing applications.
2023-12-03 14:35:19 +04:00
Adrianna Chang 2854e378c8
Revert "Add config for validating migration timestamps" 2023-12-01 11:58:53 -05:00
Eileen M. Uchitelle 831810c342
Merge pull request #50158 from fatkodima/fix-alias_attribute-sti
Fix defining `alias_attribute` for STI classes with abstract class in the inheritance chain
2023-12-01 09:31:25 -05:00
Eileen M. Uchitelle c60d064fd5
Merge pull request #50205 from rails/ac-validate-migration-timestamps
Add config for validating migration timestamps
2023-11-30 16:20:49 -05:00
Adrianna Chang 06575d1d75
Add `active_record.config.validate_migration_timestamps` option.
When set, validates that the timestamp prefix for a migration is in the form YYYYMMDDHHMMSS.
This is designed to prevent migration timestamps from being modified by hand.

It is turned off by default.
2023-11-30 16:04:06 -05:00
Kevin McPhillips 79fb41ddd9 Add deprecation and specific exceptions to warn when an adapter is using the legacy registration format in 7.2 2023-11-30 07:00:38 -05:00
Kevin McPhillips 0ccf300add Add an `ActiveRecord.protocol_adapters` configuration which maps the protocol in a `DATABASE_URL` to a database adapter. 2023-11-28 20:23:58 -05:00
Jean Boussier 4cbdffe459
Merge pull request #50196 from fatkodima/ensure-enable-trigger-all
Ensure triggers are enabled when operation fails in PostgreSQL
2023-11-28 20:37:48 +01:00
Jean Boussier ed2bc92b82
Merge pull request #50162 from misdoro/50160_allow_object_id_column_name
Allow `object_id` as a column name for ActiveRecord
2023-11-28 15:08:24 +01:00
Mikhail Doronin 622c58c287 Allow `object_id` as a column name for ActiveRecord
Fixes #50160

The `object_id` name may be used by polymorphic relations where `object` is the best name, like `notification.object`.
In that case two columns are created: `object_id` and `object_type`.

Update tests and comments to reference `__id__` instead of `object_id` for consistency.
2023-11-28 14:59:55 +01:00
fatkodima e28a609811 Ensure triggers are enabled when operation fails in PostgreSQL 2023-11-28 12:07:59 +02:00
John Hawthorn afefa5034b Retry new connection on checkout after reap
When we reap connections, we check if they are inactive (connected and
responding to ping in most adapters) and if so we remove the connection
instead of checking it back in.

However, in acquire_connection, we weren't checking after reaping
whether we were allowed to build a new connection, only whether an
existing one was in the available pool.

This still leaves a race condition where if the background reaper thread
runs while a thread is polling and finds a free inactive connection, it
could have the same issue and not wake the waiting thread. However we
don't really expect the reaper to solve this (it only runs every 60
seconds by default, far too slow to solve for a blocked thread). I think
this should be fixed, just separately.
2023-11-27 15:03:32 -08:00
Colin Knox 55ab5d51fb [ci skip] Fix small string typo in docs for ActiveRecord::AttributeMethods::Dirty 2023-11-27 12:53:26 -06:00
fatkodima 3bdd5954e8 Fix defining `alias_attribute` for STI classes with abstract class in the inheritance chain
Co-authored-by: Nikita Vasilevsky <nvasilevskii@gmail.com>
2023-11-24 23:41:54 +02:00
Eduardo Hernandez c528b347c2
Improve postgres interval overflow protection
> Sometimes, operations on Times returns just float numbers of seconds, so, we
need to handle that.
> Example: Time.current - (Time.current + 1.hour) # => -3600.000001776 (Float)

When this happens, it's possible to endup with a pretty large number of seconds,
making the ISO 8601 formatted duration to trigger an `interval field value out
range error`.

PostgreSQL 15 has already improved overflow detection when casting values to
interval

However, to further reduce the likelihood of such issues and ensure
better-formatted duration types, it now implements the construction of
`ActiveSupport::Duration` during the serialization step.

How to reproduce (at least in versions prior to PG 15+):

``` ruby

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
  gem "pg"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(
  adapter: 'postgresql',
  database: 'postgres',
  username: 'postgres',
  host: 'localhost'
)

ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :projects, force: true do |t|
    t.interval :duration
  end
end

class Project < ActiveRecord::Base; end

class IntervalBugTest < Minitest::Test
  def test_duration
    project = Project.create(duration: 70.years)
    assert_equal 70.years, project.duration
  end

  def test_duration_error
    duration = 70.years.ago - Time.now()

    assert_raises ActiveRecord::StatementInvalid do
      Project.create(duration: duration)
    end
  end
end
```
2023-11-24 12:20:47 -06:00