Repository navigation
fix(assets): extract metadata after the upload on Rails 8.1.4+ - #266
Open
TheRealAnubis wants to merge 1 commit into
Open
TheRealAnubis wants to merge 1 commit into
TheRealAnubis wants to merge 1 commit into
Conversation
Maglev::ActiveStorage registered `save_metadata_now` with `prepend: true` so that, with the legacy (reverse) after_commit order, it ran after the upload callback added by `has_one_attached`. Rails 8.1.4 fixed `prepend: true` for after_commit callbacks when `run_after_transaction_callbacks_in_order_defined` is enabled (the default since load_defaults 7.1): a prepended callback now really runs first. So the metadata hook ran before the file was uploaded, and creating an asset raised ActiveStorage::FileNotFoundError. Only prepend the callback when the legacy order is in use, so it runs after the upload whatever the Rails version and configuration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Creating an asset raises
ActiveStorage::FileNotFoundErroron Rails 8.1.4+ (with the defaultload_defaults≥ 7.1).Root cause:
Maglev::ActiveStorageregisterssave_metadata_nowwithprepend: trueso that it runs after the upload callback thathas_one_attachedadds (attachment_changes.delete(...).upload). That relied on the legacy, reverse order of after_commit callbacks.Rails 8.1.4 fixed
prepend: trueforafter_commit/after_rollbackwhenrun_after_transaction_callbacks_in_order_definedis enabled (see the "Fix incorrect callback execution order…" entry in the activerecord 8.1.4 CHANGELOG): a prepended callback now really runs first. So the metadata hook analyzes a blob that hasn't been uploaded yet.Fix: only prepend the callback when the legacy order is in use:
Tests:
spec/models/maglev/asset_spec.rb("sets the width and height of the image") reproduces the bug as soon as Rails is bumped to 8.1.4.1 (bundle update rails --conservative): red before the fix, green after.run_after_transaction_callbacks_in_order_definedboth enabled and disabled.Gemfile.lockbump to this PR if you'd like.Found while running Maglev 3.1.0 on Rails 8.1.4 in production, where we've been carrying a monkey-patch for it.