Skip to content

Commit 9555470

Browse files
committed
add migration to standard as documents plugin is enabled by default
1 parent c229868 commit 9555470

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class MoveDocumentsToActiveStorage < ActiveRecord::Migration[7.0]
2+
def up
3+
change_table :documents do |t|
4+
t.boolean :folder, default: false, null: false
5+
end
6+
7+
Document.find_each do |document|
8+
if document.data.present? && document.mime.present?
9+
document.attachment.attach(create_blob_from_document(document))
10+
else
11+
document.update(folder: true)
12+
end
13+
end
14+
15+
change_table :documents, bulk: true do |t|
16+
t.remove :data
17+
t.remove :mime
18+
end
19+
end
20+
21+
def down
22+
change_table :documents, bulk: true do |t|
23+
t.binary :data, limit: 16.megabyte
24+
t.string :mime
25+
t.remove :folder
26+
end
27+
28+
Document.find_each do |document|
29+
next unless document.attachment.attached?
30+
31+
document.update(
32+
data: document.attachment.download,
33+
mime: document.attachment.blob.content_type
34+
)
35+
end
36+
end
37+
38+
def create_blob_from_document(document)
39+
ActiveStorage::Blob.create_and_upload!(
40+
io: StringIO.new(document.data),
41+
filename: document.name,
42+
content_type: document.mime
43+
)
44+
end
45+
end

0 commit comments

Comments
 (0)