22
33class User ::AvatarTest < ActiveSupport ::TestCase
44 test "avatar_thumbnail returns variant for variable images" do
5- users ( :david ) . avatar . attach ( io : File . open ( file_fixture ( "moon.jpg" ) ) , filename : "moon.jpg" , content_type : "image/jpeg" )
5+ blob = ActiveStorage ::Blob . create_and_upload! ( io : File . open ( file_fixture ( "moon.jpg" ) ) , filename : "moon.jpg" , content_type : "image/jpeg" )
6+ users ( :david ) . avatar . attach ( blob )
67
78 assert users ( :david ) . avatar . variable?
89 assert_equal users ( :david ) . avatar . variant ( :thumb ) . blob , users ( :david ) . avatar_thumbnail . blob
@@ -16,7 +17,8 @@ class User::AvatarTest < ActiveSupport::TestCase
1617 end
1718
1819 test "allows valid image content types" do
19- users ( :david ) . avatar . attach ( io : File . open ( file_fixture ( "moon.jpg" ) ) , filename : "test.jpg" )
20+ blob = ActiveStorage ::Blob . create_and_upload! ( io : File . open ( file_fixture ( "moon.jpg" ) ) , filename : "test.jpg" , content_type : "image/jpeg" )
21+ users ( :david ) . avatar . attach ( blob )
2022
2123 assert users ( :david ) . valid?
2224 end
@@ -27,4 +29,22 @@ class User::AvatarTest < ActiveSupport::TestCase
2729 assert_not users ( :david ) . valid?
2830 assert_includes users ( :david ) . errors [ :avatar ] , "must be a JPEG, PNG, GIF, or WebP image"
2931 end
32+
33+ test "thumb variant is processed immediately on attachment" do
34+ # Create blob separately to ensure file is uploaded before variant processing.
35+ #
36+ # Root cause: When ActiveStorage::Record uses `connects_to` for read replica support
37+ # (as in SAAS mode), it creates a separate connection pool from application models.
38+ # Since after_commit callbacks are tracked per connection pool, the callback order
39+ # between User's pool (upload) and Attachment's pool (create_variants) isn't guaranteed.
40+ # In MySQL/SAAS mode, the Attachment callback fires before the file is uploaded.
41+ blob = ActiveStorage ::Blob . create_and_upload! (
42+ io : File . open ( file_fixture ( "avatar.png" ) ) ,
43+ filename : "avatar.png" ,
44+ content_type : "image/png"
45+ )
46+ users ( :david ) . avatar . attach ( blob )
47+
48+ assert users ( :david ) . avatar . variant ( :thumb ) . processed?
49+ end
3050end
0 commit comments