undefined method `execute_prepared' Rails Paperclip to ActiveStorage migration
Asked Answered
P

1

13

I'm trying to convert a Rails app over from using Paperclip to ActiveStorage and I'm running into an issue with the ConvertToActiveStorage migration script provided in the migration guide. https://github.com/thoughtbot/paperclip/blob/master/MIGRATING.md#copy-the-database-data-over

Below is the error I get while trying to run the Paperclip to ActiveStorage migration. I'm not sure what the issue is with ActiveRecord::Base.connection.execute_prepared

the path to where Paperclip is currently saving the users uploaded files is /web/non-public/system/articles/documents/000/000/ and ActiveStorage is supposed to save them to a new location at /web/storage after the migration.

    == 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

the migration script provided in the guide is listed here.

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
Potentiate answered 23/1, 2019 at 18:56 Comment(0)
D
19

I faced the same issue when running the migration. I had to change the two execute_prepared statements to use exec_prepared instead:

ActiveRecord::Base.connection.raw_connection.exec_prepared(
    'active_storage_blob_statement', [
        ...
])

ActiveRecord::Base.connection.raw_connection.exec_prepared(
    'active_storage_attachment_statement', [
        ...
])

I don't quite remember why, but I also had to pass the ID of the last blob record inserted to the attachment insert statement. You can skip this if you don't need this:

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, #{"(SELECT max(id) from active_storage_blobs)"}, $4)
SQL

Here is my complete migration.

Diella answered 24/1, 2019 at 13:29 Comment(1)
changing ActiveRecord::Base.connection.execute_prepared to ActiveRecord::Base.connection.raw_connection.exec_prepared got the migration to work. thanks!Potentiate

© 2022 - 2024 — McMap. All rights reserved.