ruby PG::语法错误:错误:“active_storage_blob_statement”或其附近存在语法错误

rm5edbpk  于 2023-03-17  发布在  Ruby
关注(0)|答案(1)|浏览(78)

我正在尝试将Rails应用程序从使用Paperclip转换为使用Active Storage来存储文件附件。我正在遵循一个指南/教程,该指南/教程概述了如何通过迁移来完成此操作。但是,当我运行迁移时,我收到以下错误:

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

PG::SyntaxError: ERROR:  syntax error at or near "active_storage_blob_statement"
LINE 1: active_storage_blob_statement
        ^
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:61:in `block (4 levels) in up'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:54:in `each'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:54:in `block (3 levels) in up'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:53:in `each'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:53:in `block (2 levels) in up'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:33:in `each'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:33:in `block in up'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:32:in `up'

我不确定导致此错误的原因或修复方法。以下是我的迁移中的相关代码:

class ConvertToActiveStorage < ActiveRecord::Migration[6.1]
  require 'open-uri'

  def up
    # postgres
    get_blob_id = 'LASTVAL()'
    # mariadb
    # get_blob_id = 'LAST_INSERT_ID()'
    # sqlite
    # get_blob_id = 'LAST_INSERT_ROWID()'

    # Prepare two insert statements for the new ActiveStorage tables
    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
    

    # Eager load the application so that all Models are available
    Rails.application.eager_load!

    # Get a list of all the models in the application
    models = ActiveRecord::Base.descendants.reject(&:abstract_class?)

    transaction do
      models.each do |model|
        # If the model has a column or columns named *_file_name,
        # We are assuming this is a column added by Paperclip.
        # Store the name of the attachment(s) found (e.g. "avatar") in an array named attachments
        attachments = model.column_names.map do |c|
          if c =~ /(.+)_file_name$/
            $1
          end
        end.compact

        # If no Paperclip columns were found in this model, go to the next model
        if attachments.blank?
          puts '  No Paperclip attachment columns found for [' + model.to_s + '].'
          puts ''
          next
        end

        puts '  Paperclip attachment columns found for [' + model.to_s + ']: ' + attachments.to_s

        # Loop through the records of the model, and then through each attachment definition within the model
        model.find_each.each do |instance|
          attachments.each do |attachment|
            # If the model record doesn't have an uploaded attachment, skip to the next record
            if instance.send(attachment).path.blank?
              next
            end

            # Otherwise, we will convert the Paperclip data to ActiveStorage records
            ActiveRecord::Base.connection.execute(
              '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(
              'active_storage_attachment_statement', [
                attachment,
                model.name,
                instance.id,
                instance.updated_at.iso8601,
              ])
          end
        end
      end
    end

    ActiveRecord::Base.connection.execute('DEALLOCATE PREPARE active_storage_attachment_statement')
    ActiveRecord::Base.connection.execute('DEALLOCATE PREPARE active_storage_blob_statement')
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end

  private

  def key(instance, attachment)
    # SecureRandom.uuid
    # Alternatively:
    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

我尝试使用exec_prepared而不是execute

ActiveRecord::Base.connection.exec_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.exec_prepared(
  'active_storage_attachment_statement', [
    attachment,
    model.name,
    instance.id,
    instance.updated_at.iso8601,
  ])

但这会导致以下错误:

Did you mean?  exec_update
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:61:in `block (4 levels) in up'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:54:in `each'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:54:in `block (3 levels) in up'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:53:in `each'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:53:in `block (2 levels) in up'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:33:in `each'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:33:in `block in up'
/myapp/db/migrate/20230313230800_convert_to_active_storage.rb:32:in `up'

Caused by:
NoMethodError: undefined method `exec_prepared'

任何人都可以帮助我了解是什么原因导致这个错误以及如何修复它吗?提前感谢。

but5z9lq

but5z9lq1#

要准备语句,您需要使用原始postgres连接的prepare(正如您所做的那样),然后执行语句,您需要再次使用原始postgres连接的exec_prepared
execute是AR连接上的一个方法,您可以使用它来执行普通的SQL语句:-(

Something.connection.raw_connection.prepare('some-name', "select count(*) from somethings")
=> #<PG::Result:0x000000401f888f38 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>
irb(main):003:0> Something.connection.raw_connection.exec_prepared("some-name")
=> #<PG::Result:0x000000401fcc63c0 status=PGRES_TUPLES_OK ntuples=1 nfields=1 cmd_tuples=1>
irb(main):004:0>

简而言之:
变更
ActiveRecord::Base.connection.execute

ActiveRecord::Base.connection.raw_connection.exec_prepared

相关问题