我正在尝试将 Rails 应用程序从使用 Paperclip 转换为 ActiveStorage,但我遇到了迁移指南中提供的 ConvertToActiveStorage 迁移脚本的问题。 https://github.com/thoughtbot/paperclip/blob/master/MIGRATING.md#copy-the-database-data-over
以下是我在尝试运行 Paperclip 到 ActiveStorage 迁移时遇到的错误。我不确定问题出在哪里ActiveRecord::Base.connection.execute_prepared
Paperclip 当前保存用户上传文件的路径是,ActiveStorage 应该在迁移后将/web/non-public/system/articles/documents/000/000/
它们保存到新位置。/web/storage
== 20190123165105 ConvertToActiveStorage: migrating ===========================
-- transaction()
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `execute_prepared' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x007fd7e9211128>
Did you mean? exec_delete
/web/db/migrate/20190123165105_convert_to_active_storage.rb:45:in `block (4 levels) in up'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:40:in `each'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:40:in `block (3 levels) in up'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:39:in `each'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:39:in `block (2 levels) in up'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:28:in `each'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:28:in `block in up'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:27:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
NoMethodError: undefined method `execute_prepared' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x007fd7e9211128>
Did you mean? exec_delete
此处列出了指南中提供的迁移脚本。
class ConvertToActiveStorage < ActiveRecord::Migration[5.2]
require 'open-uri'
def up
# postgres
get_blob_id = 'LASTVAL()'
# mariadb
# get_blob_id = 'LAST_INSERT_ID()'
# sqlite
# get_blob_id = 'LAST_INSERT_ROWID()'
active_storage_blob_statement = ActiveRecord::Base.connection.raw_connection.prepare('active_storage_blob_statement', <<-SQL)
INSERT INTO active_storage_blobs (
key, filename, content_type, metadata, byte_size, checksum, created_at
) VALUES ($1, $2, $3, '{}', $4, $5, $6)
SQL
active_storage_attachment_statement = ActiveRecord::Base.connection.raw_connection.prepare('active_storage_attachment_statement', <<-SQL)
INSERT INTO active_storage_attachments (
name, record_type, record_id, blob_id, created_at
) VALUES ($1, $2, $3, #{get_blob_id}, $4)
SQL
Rails.application.eager_load!
models = ActiveRecord::Base.descendants.reject(&:abstract_class?)
transaction do
models.each do |model|
attachments = model.column_names.map do |c|
if c =~ /(.+)_file_name$/
$1
end
end.compact
if attachments.blank?
next
end
model.find_each.each do |instance|
attachments.each do |attachment|
if instance.send(attachment).path.blank?
next
end
ActiveRecord::Base.connection.execute_prepared(
'active_storage_blob_statement', [
key(instance, attachment),
instance.send("#{attachment}_file_name"),
instance.send("#{attachment}_content_type"),
instance.send("#{attachment}_file_size"),
checksum(instance.send(attachment)),
instance.updated_at.iso8601
])
ActiveRecord::Base.connection.execute_prepared(
'active_storage_attachment_statement', [
attachment,
model.name,
instance.id,
instance.updated_at.iso8601,
])
end
end
end
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
private
def key(instance, attachment)
SecureRandom.uuid
# Alternatively:
# instance.send("#{attachment}_file_name")
# filename = instance.send("#{attachment}_file_name")
# klass = instance.class.table_name
# id = instance.id
# id_partition = ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
# "#{klass}/#{attachment.pluralize}/#{id_partition}/original/#{filename}"
end
def checksum(attachment)
# local files stored on disk:
url = attachment.path
Digest::MD5.base64digest(File.read(url))
# remote files stored on another person's computer:
# url = attachment.url
# Digest::MD5.base64digest(Net::HTTP.get(URI(url)))
end
end