thoughtbot / paperclip

Easy file attachment management for ActiveRecord
https://thoughtbot.com
Other
9.01k stars 2.43k forks source link

Trouble creating the migration converting to Active Storage #2603

Open hazmatzo opened 6 years ago

hazmatzo commented 6 years ago

I have this code, which I copied from the ActiveStorage migration guide:

    active_storage_blob_statement = ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL)
      INSERT INTO active_storage_blobs (
        `key`, filename, content_type, metadata, byte_size, checksum, created_at
      ) VALUES (?, ?, ?, '{}', ?, ?, ?)
    SQL

And it's giving me this error when I attempt to migrate:

StandardError: An error has occurred, this and all later migrations canceled:

wrong number of arguments (given 1, expected 2..3)
project/db/migrate/20180523202013_convert_to_active_storage.rb:16:in `prepare'
project/db/migrate/20180523202013_convert_to_active_storage.rb:16:in `up'
project/bin/rails:9:in `<top (required)>'
project/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ArgumentError: wrong number of arguments (given 1, expected 2..3)
project/db/migrate/20180523202013_convert_to_active_storage.rb:16:in `prepare'
project/db/migrate/20180523202013_convert_to_active_storage.rb:16:in `up'
project/bin/rails:9:in `<top (required)>'
project/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
rnbronstein commented 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!

twest7 commented 6 years ago

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
colinpetruno commented 6 years ago

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

mike-burns commented 6 years ago

I've opened a PR for this: https://github.com/thoughtbot/paperclip/pull/2613 .