Open hazmatzo opened 6 years ago
I am having this same error and I'm having trouble debugging.
Edit: You may have some luck following this comment. I still am having a lot of errors but I actually don't need our old attachments so I'm just moving on!
This is my workaround since I only have one model to migrate:
Change NAME_OF_ATTACHMENT_TYPE per your app, ie: logo, etc. mine are just named attachment Change YOUR_MODEL_NAME to the Model name your migrating ie: Organization, Document, etc.
For Postgres use, LASTVAL() for Mariadb use LAST_INSERT_ID()
def up
connection = ActiveRecord::Base.connection.raw_connection
transaction do
YOUR_MODEL_NAME.find_each.each do |instance|
connection.exec(
"INSERT INTO active_storage_blobs (key, filename, content_type, metadata, byte_size, checksum, created_at) VALUES (
'#{SecureRandom.uuid}',
'#{instance.send("NAME_OF_ATTACHMENT_TYPE_file_name")}',
'#{instance.send("NAME_OF_ATTACHMENT_TYPE_content_type")}',
'{}',
'#{instance.send("NAME_OF_ATTACHMENT_TYPE_file_size")}',
'0',
'#{instance.updated_at.iso8601}'
)"
)
connection.exec(
"INSERT INTO active_storage_attachments (name, record_type, record_id, blob_id, created_at) VALUES (
'NAME_OF_ATTACHMENT_TYPE',
'YOUR_MODEL_NAME',
'#{instance.id}',
LASTVAL(),
'#{instance.updated_at.iso8601}'
)"
)
end
end
end
I ran into this as well since the example doesn't work for postgres. Here's a completed gist of a rake task with a few changes to ensure it would run with some spotty seed data.
https://gist.github.com/colinpetruno/037de4fafa4cff695b1d7905cd6fd7c2
I've opened a PR for this: https://github.com/thoughtbot/paperclip/pull/2613 .
I have this code, which I copied from the ActiveStorage migration guide:
And it's giving me this error when I attempt to migrate: