Commit Graph

90761 Commits

Author SHA1 Message Date
Rafael Mendonça França
a9573eaa6b
Merge pull request #51035 from rails/rm-docs-actioncable
Transform actioncable documentation to Markdown
2024-02-13 17:55:42 -05:00
John Hawthorn
c730877c49
Merge pull request #51019 from jhawthorn/key-provider
Remove memoization to accept `key_provider` overridden by `with_encryption_context`
2024-02-13 13:10:38 -08:00
Rafael Mendonça França
154f7c2181
Register autoload for ActiveJob::Arguments
Since #50715, there are cases where `ActiveJob::Arguments` is used
but not required anymore in the test environment. This could be a
test for a custom argument serializer.

One solution could be to add a require "active_job/arguments" in
all the usages, but that isn't a convention we follow in Rails.

Instead, let's register the autoload for `ActiveJob::Arguments` in
`active_job.rb` so that it's always available when needed.
2024-02-13 20:34:10 +00:00
Eileen M. Uchitelle
8e20f5d3b2
Merge pull request #51072 from eileencodes/raise-error-for-trilogy-and-prepared-statements
Raise error for Trilogy when `prepared_statements` is `true`
2024-02-13 15:08:58 -05:00
eileencodes
847953dbaa
Raise error for Trilogy when prepared_statements is true
Trilogy doesn't currently support prepared statements. The error that
applications would see is a `StatementInvalid` error. This doesn't quite point
you to the fact this isn't supported. So raise a more appropriate error
pointing to what to change.
2024-02-13 14:47:43 -05:00
Yasuo Honda
6e7ef7d61c
Merge pull request #51037 from fatkodima/fix-deprecated-enum-syntax-tests
Fix deprecated `enum` syntax in tests
2024-02-13 21:37:23 +09:00
Yasuo Honda
ec684bbe51
Merge pull request #51060 from yahonda/workaround_thumbnail_height
Address `ActiveStorage::VariantTest#test_resized_variation_of_WEBP_blob` failure at Rails Nightly CI
2024-02-13 21:29:45 +09:00
Yasuo Honda
034398fdfe Address ActiveStorage::VariantTest#test_resized_variation_of_WEBP_blob failure at Rails Nightly CI
Managed to reproduce Rails Nightly CI failure
at https://buildkite.com/rails/rails-nightly/builds/149#018d9052-1b2d-48fa-9d74-a39df3f3f1d6/1251-1291

This commit allows both 33 and 34 as its height because this issue is isolated
that thedifference comes from libvips and/or ruby-vips behavior differences, not Active Storage.

* Steps to reprodude
Run this test on Ubuntu 22.04. It should not reproduce on Ubuntu 23.10.

```
git clone https://github.com/rails/rails
cd rails
rm Gemfile.lock
cd activestorage
bin/test test/models/variant_test.rb -n test_resized_variation_of_WEBP_blob
```

* Expected behavior
It should pass.

* Actual behavior
It fails because the height of the thumbnail is 34.

```
$ bin/test test/models/variant_test.rb -n test_resized_variation_of_WEBP_blob
F

Failure:
ActiveStorage::VariantTest#test_resized_variation_of_WEBP_blob [test/models/variant_test.rb:125]:
Expected: 33
  Actual: 34

bin/test test/models/variant_test.rb:117
```

Refer to https://github.com/libvips/ruby-vips/issues/383
2024-02-13 21:04:08 +09:00
Petrik de Heus
a42ca9cf14
Merge pull request #51049 from sobstel/main
[DOCS] Warn against uniqueness validator when create_or_find_by is used [ci-skip]
2024-02-13 07:45:59 +01:00
Przemek Sobstel
a42eda12da doc: Warn against having uniqueness validator when create_or_find_by is used
Co-authored-by: Petrik de Heus <petrik@deheus.net>
2024-02-13 07:23:58 +01:00
Yasuo Honda
e4f1c8758c
Merge pull request #50962 from joshuay03/update-has-one-create-test-descriptions
Update some create `has_one` failure test descriptions
2024-02-13 10:07:04 +09:00
Eileen M. Uchitelle
631429056b
Merge pull request #50606 from maximerety/active-record-encryption-eager-load
[Fix #50604] Restore compatibility of Active Record Encryption configs with eager loading mode
2024-02-12 16:13:23 -05:00
maximerety
d997c554b3
[Fix #50604] Restore compatibility of ARE configs with eager loading mode
Configure ActiveRecord::Encryption (ARE) on ActiveRecord::Base (AR)
loading, so that ARE configs are ready before AR models start using
`encrypts` to declare encrypted attributes.

This means that you can add ARE configurations in initializers, as long
as you don't trigger the loading of ActiveRecord::Base or your AR models
in prior initializers.
2024-02-12 17:57:56 +01:00
Jean Boussier
f5910f74d4
Merge pull request #51050 from Shopify/show-exception-report-error
ActionDispatch::Executor: report errors handled by ShowExceptions
2024-02-12 17:51:02 +01:00
Jean Boussier
4067c9565a ActionDispatch::Executor: report errors handled by ShowExceptions
Fix: https://github.com/rails/rails/issues/51002

In the default middleware stack, the `ShowExceptions` middleware is
lower than `ActionDispatch::Execturor` and will handle most exceptions
causing `Executor` not to witness any.

Instead we need to rely on `action_dispatch.exception` being added
into the request env.
2024-02-12 17:40:24 +01:00
Jean Boussier
9940dc879d docker-entrypoint: export LD_PRELOAD
Otherwise it won't applied to the execed process.

Fix: https://github.com/rails/rails/pull/50943#issuecomment-1936443319
2024-02-12 14:15:16 +01:00
Jean Boussier
a8d6d477c7
Merge pull request #50999 from Shopify/refactor-transactional-fixtures
Decouple transactional fixtures and active connections
2024-02-12 09:54:39 +01:00
Joshua Young
e61234dac7 Update create has_one failure test descriptions 2024-02-11 19:42:23 +10:00
Xavier Noria
0f9aaa5ca9
Merge pull request #51039 from rails/update-release-notes
Update 7.0 release notes re inflections and the once autoloader
2024-02-10 12:51:56 +01:00
Xavier Noria
5876939a5a Update 7.0 release notes re inflections and the once autoloader 2024-02-10 12:42:40 +01:00
fatkodima
93884c66d5 Fix deprecated enum syntax in tests 2024-02-10 12:42:54 +02:00
Rafael Mendonça França
1a92af2b51
Merge pull request #50987 from skipkayhil/hm-deprecate-kw-enum
Deprecate defining enums with keywords args
2024-02-09 20:32:13 -05:00
Hartley McGuire
8c5425197c
Deprecate defining enums with keywords args
Enums have historically been defined using keyword arguments:

```ruby
class Function > ApplicationRecord
  enum color: [:red, :blue],
       type: [:instance, :class],
       _scopes: false
```

This has the advantage of being able to define multiple enums at once
with the same options. However, it also has a downside that enum options
must be prefixed with an underscore to separate them from the enum
definitions (to enable models to have enums with the same name as an
option).

In Rails 7, a new syntax was [introduced][1] to instead define enums with
positional arguments:

```ruby
class Function > ApplicationRecord
  enum :color, [:red, :blue], scopes: false
  enum :type, [:instance, :class], scopes: false
```

This new syntax eliminates the need to prefix options with an underscore,
and the docs were updated to recommend this new syntax.

However, both versions of the API have been supported since, and it has
started to cause some problems:

The first issue is that the available options have drifted. In Rails
7.1, an option was added to make assigning an invalid enum value use
validation errors instead of runtime errors. However, the equivalent
underscored prefix option was not added for the original enum syntax

Articles have been created that describe the new option in Rails 7.1,
but the examples in the articles use un-prefixed options with the old
syntax. This confusion has also lead to issues opened asking why that
incorrect syntax is not working.

Additionally, the presence of underscored options is just generally
confusing because it tends to imply an option is for internal use.

This commit aims to fix all of these issues by deprecating the old enum
syntax. With only one way to define enums, options cannot drift and
there will be less confusion around how enums should be defined.

[1]: 0618d2d84a501aea93c898aec504ff9a0e09d6f2
2024-02-10 00:20:45 +00:00
Rafael Mendonça França
6fdd31d7db
Transform actioncable documentation to Markdown 2024-02-10 00:11:26 +00:00
Rafael Mendonça França
f0adde935e
Fix configuration order 2024-02-09 23:50:00 +00:00
Rafael Mendonça França
fdc5ec228e
Merge PR #50953 2024-02-09 23:46:44 +00:00
Lewis Buckley
a15f5b837f
Add note about imagemagick / libvips support being required 2024-02-09 23:45:18 +00:00
Lewis Buckley
f3e3f82e7d
Add WebP as a new framework default image type
Follows https://github.com/rails/rails/pull/38918 and
https://github.com/rails/rails/pull/38988

At the time, webp browser support was limited. Now 96% of browsers
support webp: https://caniuse.com/?search=webp
2024-02-09 23:44:51 +00:00
Rafael Mendonça França
cd053bcedc
Merge pull request #50992 from KJTsanaktsidis/ktsanaktsidis/persist_logger_tags_until_as_event
Don't pop logger tags in Rails::Rack::Logger until request is finished
2024-02-09 18:38:55 -05:00
Rafael Mendonça França
5c0286dcf7
Merge pull request #50950 from amkisko/patch-1
Add class name to enum validation exception message
2024-02-09 18:35:26 -05:00
Rafael Mendonça França
efa8889ad6
Merge pull request #51017 from p8/activesupport/document-time-zone-create
Improve documentation of ActiveSupport::TimeZone.create [ci-skip]
2024-02-09 18:31:27 -05:00
Rafael Mendonça França
fbb230eb95
Merge pull request #51030 from searls/queue-approach-to-preview-image-race-condition
Fixes race condition for multiple preprocessed video variants
2024-02-09 18:15:53 -05:00
Rafael Mendonça França
6929568774
Merge pull request #50879 from rails/rm-docs-actionpack
Transform actionpack documentation to Markdown
2024-02-09 18:14:12 -05:00
Andrei Makarov
b55b5584d8
Add class name to enum validation exception message
Unify enum without column exception message

Update enum_test for new exception message
2024-02-09 22:57:52 +00:00
Rafael Mendonça França
1352bd79aa
Merge pull request #49897 from glaucocustodio/main
add a note on the postgres limit [ci skip]
2024-02-09 17:53:40 -05:00
Rafael Mendonça França
0f9638ebb1
Update activestorage/app/models/active_storage/blob/representable.rb 2024-02-09 17:46:39 -05:00
Rafael Mendonça França
d6bf4de7dc
Transform actionpack documentation to Markdown 2024-02-09 22:28:15 +00:00
Rafael Mendonça França
3079e8b0f8
Merge pull request #51033 from jasonkarns/patch-3
.gitignore template references git's default instead of custom config
2024-02-09 17:08:14 -05:00
Eileen M. Uchitelle
45ecdce495
Merge pull request #51006 from kddnewton/parse-with-prism
Parse tests with prism
2024-02-09 16:43:22 -05:00
Hartley McGuire
6cfd5682f8
Merge pull request #50892 from Takahashi-Seiji/main
Documentation: Expand documentation on has_one_attached method [ci-skip].

Co-authored-by: Petrik <petrik@deheus.net>
2024-02-09 16:14:57 -05:00
Takahashi-Seiji
2f5e052521
docs:expand documentation on has_one_attached method.
Accepted PR's suggestions and added the information to has_many_attached method.

Co-authored-by: Salo Charabati <salochara@gmail.com>
2024-02-09 16:07:34 -05:00
Hartley McGuire
a7c68d46a2
Merge pull request #51032 from DoodlingDev/layout-and-rendering-in-rails--redirect-to-disambiguation
Disambiguate language around when controller code stops re: `redirect_to` [ci skip]
2024-02-09 15:49:57 -05:00
Aji Slater
380094920a
Disambiguate language around when code stops re: redirect_to
This paragraph tends to cause confustion with Rails developers who have just learned the correct control flow of `redirect_to` and `render`.

A phrase above it in the "Avoiding Double Render Errors" section clears up a common misunderstanding that `render` will stop execution within a controller action. That misunderstanding is further corrected with the following phrase.

> But this will _not_ stop the rest of the code in the `show` action from running

This section is often enough to get the point across for newer developers who might often be running into double render errors, but soon after the phrase which attempts the same illustration of `redirect_to`'s execution confuses the matter by using the same language as the earlier statement to mean the opposite.

> Sometimes inexperienced developers think of `redirect_to` as a sort of `goto` command, moving execution from one place to another in your Rails code. This is _not_ correct. Your code stops running and waits for a new request from the browser.

Here "Your code stops running" means that the action will run to completion and not jump to another site in controller code. This often re-confuses the concept they've just learned and undermines their understanding. By being more specific in the second phrase, we can reinforce the concept instead.
2024-02-09 13:43:37 -06:00
Jason Karns
ff9248a4d4
Prefer to reference git's default configuration, not bespoke
Prefer references to git's default configuration, rather than encouraging customized configurations.

`git config core.excludesFile` defaults to `${XDG_CONFIG_HOME:-~/.config}/git/ignore`. This updates the .gitignore template to suggest the default location for git's global gitignore rather than a customized location.
2024-02-09 14:37:39 -05:00
Justin Searls
131fd59ff6 Fixes race condition when multiple preprocessed variants are defined for a Previewable file is attached
This fixes race condition in Active Storage when multiple preprocessed variants are defined for a `Previewable` file is attached.

When a variant is specified for a "previewable" file type (e.g. video  or PDF) attachment, a `preview_image` attachment is first created and attached on the original blob and then any user-specified variants are derived from _that_ preview image. When those variants are named and have `preprocessed: true`, the jobs to create those variants are queued simultaneously.

Example from my case:

```ruby
  has_one_attached :file, dependent: :purge_later do |attachable|
    attachable.variant :preview, resize_to_fill: [400, 400], preprocessed: true
    attachable.variant :still, format: "jpg", saver: {quality: 85}, preprocessed: true
  end
```

When a `Previewable` attachment is created (a video, in my case), `TransformJob.perform_later` is called for each named variant with `preprocessed: true`. Unless your queue adapter is synchronous (e.g. :inline or :test), this results in a race condition in which every such variant's worker will check `processed?`, see that no `preview_image` attachment exists yet on the `ActiveStorage::Blob`, and:

1. Redundantly download the file from storage
2. Create duplicative ActiveStorage::Attachment and `ActiveStorage::Blob` records for the `preview_image` attachment (all but one of which will be orphaned from the original blob's `has_one_attached :preview_image`)
3. Create variant blobs (and associated `ActiveStorage::VariantRecord`) that are similarly orphaned (by virtue of being a variant of an orphaned `preview_image` blob)

As a result, if the video is ever purged, `PurgeJob` will only find the current `has_one_attached :preview_image` and whatever variant demanded it into existence, then leave the rest as orphaned records in the database and in storage.

Pretty simple: wrap the first step of the job in `blob.with_lock {}`. By pessimistically locking on the blob, we can prevent processing the preview image multiple times by multiple `TransformJob` jobs running concurrently.

Alternate approaches would all be more work:

* Queuing a `PreviewImage` job instead of N `TransformJob` and have it, only after `preview_image` is attached, enqueue those `TransformJob` jobs
* Batching up all the named variant transformations into a single meta-job

Writing a test for this inside Rails would be difficult because it would require running the resulting TransformJob jobs concurrently. I [started a test](https://github.com/searls/rails/blob/fix-video-duplicate-preview-variants/activestorage/test/models/variant_with_record_test.rb#L348-L367) but failed to reproduce, in part because the test queue adapter will perform enqueued jobs inline instead of concurrently. In order to write a test that replicated the issue appropriately, we might first need a new option for `perform_enqueued_jobs(async: true) { … }`

If you're interested, [this gist](https://gist.github.com/searls/5b8298abe88b3206f670ea3c6d574aab) includes a driver script and output before and after the patch showing it working.

I only found this because I'm a total cheapskate and was literally counting records in my S3 bucket to ensure `PurgeJob` worked. Then I wasted the next two days trying to figure out why before landing on this. I strongly suspect that ActiveStorage users who host video and take advantage of `preprocessed: true` named variants will have a lot of orphaned stuff floating around their buckets.

To see if you have any such "zombie" preview_images (and presumably, associated variants) floating around your application that would survive calls to `purge` on the owning attachment, you could write a query like this:

```
ActiveStorage::Attachment
  .joins("INNER JOIN active_storage_attachments as other_attachments ON
          active_storage_attachments.record_id = other_attachments.record_id AND
          active_storage_attachments.id != other_attachments.id")
  .where(
    :name => "preview_image",
    :record_type => "ActiveStorage::Blob",
    "other_attachments.name" => "preview_image",
    "other_attachments.record_type" => "ActiveStorage::Blob"
  )
  .distinct
```

Clearing out one's production database and backend storage to get this all right-sized should be a fun exercise for the reader.

Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
2024-02-09 13:22:35 -05:00
Kevin Newton
105e05e891
Parse tests with prism
This changes TestParser to parse with prism instead of ripper if it
is available for the current version of Ruby. It's within the margin
for the speed, and its significantly less code that is easier to
read and should be easier to maintain.
2024-02-09 10:54:20 -05:00
Jean Boussier
931c5ef379
Merge pull request #51025 from Shopify/dont-clone-proc
Workaround a Ruby bug that can cause a VM crash
2024-02-09 16:46:54 +01:00
Jean Boussier
7025c84eda Workaround a Ruby bug that can cause a VM crash
See: https://bugs.ruby-lang.org/issues/20250

The bug exist all the way since Ruby 2.7, if you `clone` a `Proc`
object on which you already accessed `object_id`, when its clone
is GCed Ruby will crash.

By accessing the clone's `object_id` right away, we prevent the
crash.
2024-02-09 16:29:46 +01:00
Jean Boussier
3b8222ccd0
Merge pull request #51023 from Shopify/av-non-string-template
Allow template to return any kind of objects
2024-02-09 15:10:28 +01:00
Jean Boussier
f62ff52c65 Allow template to return any kind of objects
Fix: https://github.com/rails/rails/issues/50930

While Action View is predominantly meant to render text,
in sime case it's used to render more complex object.

So we shouldn't assume `#_run` returns a buffer.
2024-02-09 14:52:16 +01:00