-
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: move document data from document plugin to active storage (#1050)
* move document data from document plugin to active storage * implement rubocoop recommendations * add migration * add migration to standard as documents plugin is enabled by default --------- Co-authored-by: Philipp Rothmann <[email protected]>
- Loading branch information
Showing
6 changed files
with
124 additions
and
30 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
db/migrate/20240306141647_move_documents_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,45 @@ | ||
class MoveDocumentsToActiveStorage < ActiveRecord::Migration[7.0] | ||
def up | ||
change_table :documents do |t| | ||
t.boolean :folder, default: false, null: false | ||
end | ||
|
||
Document.find_each do |document| | ||
if document.data.present? && document.mime.present? | ||
document.attachment.attach(create_blob_from_document(document)) | ||
else | ||
document.update(folder: true) | ||
end | ||
end | ||
|
||
change_table :documents, bulk: true do |t| | ||
t.remove :data | ||
t.remove :mime | ||
end | ||
end | ||
|
||
def down | ||
change_table :documents, bulk: true do |t| | ||
t.binary :data, limit: 16.megabyte | ||
t.string :mime | ||
t.remove :folder | ||
end | ||
|
||
Document.find_each do |document| | ||
next unless document.attachment.attached? | ||
|
||
document.update( | ||
data: document.attachment.download, | ||
mime: document.attachment.blob.content_type | ||
) | ||
end | ||
end | ||
|
||
def create_blob_from_document(document) | ||
ActiveStorage::Blob.create_and_upload!( | ||
io: StringIO.new(document.data), | ||
filename: document.name, | ||
content_type: document.mime | ||
) | ||
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
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
45 changes: 45 additions & 0 deletions
45
plugins/documents/db/migrate/20240306141647_move_documents_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,45 @@ | ||
class MoveDocumentsToActiveStorage < ActiveRecord::Migration[7.0] | ||
def up | ||
change_table :documents do |t| | ||
t.boolean :folder, default: false, null: false | ||
end | ||
|
||
Document.find_each do |document| | ||
if document.data.present? && document.mime.present? | ||
document.attachment.attach(create_blob_from_document(document)) | ||
else | ||
document.update(folder: true) | ||
end | ||
end | ||
|
||
change_table :documents, bulk: true do |t| | ||
t.remove :data | ||
t.remove :mime | ||
end | ||
end | ||
|
||
def down | ||
change_table :documents, bulk: true do |t| | ||
t.binary :data, limit: 16.megabyte | ||
t.string :mime | ||
t.remove :folder | ||
end | ||
|
||
Document.find_each do |document| | ||
next unless document.attachment.attached? | ||
|
||
document.update( | ||
data: document.attachment.download, | ||
mime: document.attachment.blob.content_type | ||
) | ||
end | ||
end | ||
|
||
def create_blob_from_document(document) | ||
ActiveStorage::Blob.create_and_upload!( | ||
io: StringIO.new(document.data), | ||
filename: document.name, | ||
content_type: document.mime | ||
) | ||
end | ||
end |