Rails 4 ActiveRecord Sql Server - Unable to save binary into image column

619 views Asked by At

We are working to upgrade our application to a more current version of Ruby & Rails. Our app integrates with a legacy database (SQL Server 2008 R2) that has a table with a column of image data type (we are unable to change this column to varbinary(max)). Previously we were able to save a binary into the image column. However now we are getting conversion errors.

We are working to upgrade to the following (among others):

  • Rails 4.2.1
  • ActiveRecord_SQLServer_Adapter (4.2.4)
  • tiny_tds (0.6.3.rc1)
  • freeTDS (v0.91.112)

When we now attempt to save into the image column, we get errors similar to:

TinyTds::Error: Unclosed quotation mark after the character string

Researching various issues within tiny_tds & activerecord_sqlserver_adapter, we decided to create a second table that matched the first but change the data type from image to varbinary(max). We can save a binary into the column.

The code causing the challenge is in a background job where we grab images from s3, store them locally and then push the image into the database. Again, we don't control the legacy database and thus can't change the data type (or confront the issue of why we are storing the image in the db in the first place).

...
@d = Doc.new
...
open("#{Rails.root}/cache/pictures/image.png", "wb") do |file|
    file << open(r.image.url).read
end

@d.document = File.binread("#{Rails.root}/cache/pictures/image.png")
@d.save!

Given the upgrade has broken our saving images, we are trying to figure out how best to determine a fix. We could obviously roll back until we find a version that works. However we hope to find a fix. Anyone have any ideas?

Update: We added the following configuration as we had triggers on the table being inserted: ActiveRecord::ConnectionAdapters::SQLServerAdapter.use_output_inserted = true

When we remove this configuration we get the following error: TinyTds::Error: The target table 'doc' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.

Note: We are unable to make any modifications to the triggers.

1

There are 1 answers

0
Alan Ruth On

Per feedback on the ActiveRecord_SQLServer_Adapter site, we rolled back to 4.1.11 and we are now able to save into the image column.

We also had to add this snippet to overcome the issue with the triggers.