Skip to content

Commit

Permalink
Connect to writing database during thumbnail generation
Browse files Browse the repository at this point in the history
In a multi db setup where you have a reading and a writing database
we need to make sure that we are using the writing database.

This is necessary, because we are writing (caching) picture
thumbnails in a GET request. Usually Rails would handle this for us
on POST, PATCH/PUT and DELETE requests.

ActiveStorage does the same thing to support multi database
setups.

Closes #2428
  • Loading branch information
tvdeyen committed Feb 26, 2023
1 parent 6113974 commit d47d2ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/alchemy/picture/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def uid
thumb.uid
else
uid = PictureThumb::Uid.call(signature, variant)
PictureThumb.generator_class.call(variant, signature, uid)
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
PictureThumb.generator_class.call(variant, signature, uid)
end
uid
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/models/alchemy/picture/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@
it "returns the url to the thumbnail" do
is_expected.to match(/\/pictures\/\d+\/.+\/image\.png/)
end

it "connects to writing database" do
writing_role = ActiveRecord::Base.writing_role
expect(ActiveRecord::Base).to receive(:connected_to).with(role: writing_role)
subject
end
end
end

0 comments on commit d47d2ef

Please sign in to comment.