-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use active storage for invoice attachments
until now invoice attachments were stored in the database directly, this led to performance issues see #1037. This commit migrates the invoice attachments to use active storage. Additionally multiple attachments are now supported, a small preview is rendered and the delete flow was adapted.
- Loading branch information
Showing
14 changed files
with
85 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$("#attachment_<%= params[:attachment_id] %>").remove(); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
40 changes: 40 additions & 0 deletions
40
db/migrate/20240126111615_move_attachment_to_active_storage.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class MoveAttachmentToActiveStorage < ActiveRecord::Migration[7.0] | ||
def up | ||
Invoice.find_each do |invoice| | ||
if invoice.attachment_data.present? && invoice.attachment_mime.present? | ||
blob = ActiveStorage::Blob.create_and_upload!( | ||
io: StringIO.new(invoice.attachment_data), | ||
filename: 'attachment', | ||
content_type: invoice.attachment_mime | ||
) | ||
|
||
invoice.attachments.attach(blob) | ||
end | ||
end | ||
|
||
change_table :invoices, bulk: true do |t| | ||
t.remove :attachment_data | ||
t.remove :attachment_mime | ||
end | ||
end | ||
|
||
def down | ||
change_table :invoices, bulk: true do |t| | ||
t.binary :attachment_data, limit: 16.megabyte | ||
t.string :attachment_mime | ||
end | ||
|
||
Invoice.find_each do |invoice| | ||
if invoice.attachments.attached? | ||
attachment = invoice.attachments.first # Will only migrate the first attachment back, as multiple were not supported before | ||
attachment_data = attachment.download | ||
attachment_mime = attachment.blob.content_type | ||
|
||
invoice.update( | ||
attachment_data: attachment_data, | ||
attachment_mime: attachment_mime | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains 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