using COPY FROM in a Rails app on Heroku with the Postgresql backend

前端 未结 2 740
春和景丽
春和景丽 2021-01-06 14:44

I want to give users the option of uploading files in my Ruby on Rails 3.2 application, with the data going into the db. I wanted to use the COPY FROM command s

相关标签:
2条回答
  • 2021-01-06 15:00

    You can use the gem https://github.com/diogob/postgres-copy Assuming data is an IO object.

    User.copy_from data, columns: [:name, :taxon_id, :updated_at, :created_at]
    

    Or using the path to the uploaded file directly:

    User.copy_from "/tmp/uploaded_file.csv", columns: [:name, :taxon_id, :updated_at, :created_at]
    
    0 讨论(0)
  • 2021-01-06 15:12

    Thanks to @PhilipHallstrom's link, I used COPY FROM STDIN like this:

    rc = User.connection.raw_connection
    rc.exec("COPY users (name, taxon_id, updated_at, created_at) FROM STDIN")
    begin
      until rc.put_copy_data( data )
        $stderr.puts "  waiting for connection to be writable..."
        sleep 0.1
      end
    rescue Errno => err
      @errmsg = @errmsg + "%s while reading copy data: %s" % [ err.class.name, err.message ]
      error = true
    else
      rc.put_copy_end
      while res = rc.get_result
        if (res.result_status != 1)
          error = true
          @errmsg = @errmsg + "Result of COPY is: %s" % [ res.res_status(res.result_status) ]
        end
      end
    end
    
    0 讨论(0)
提交回复
热议问题