improve UTF-8 detection migration refs #6035

* use a single DJ instead of a DJ per applicable attachment
 * don't hold a transaction for any length of time
 * do direct updates, instead of through AR, to avoid 7 extra
   queries per update from before_save callbacks

test plan:
 * run the migration; it should run relatively quickly, and not
   have any errors

Change-Id: I6af6f0ddbcedbb69ad6afb3688d94feb32d0f201
Reviewed-on: https://gerrit.instructure.com/8905
Tested-by: Hudson <hudson@instructure.com>
Reviewed-by: Zach Wily <zach@instructure.com>
This commit is contained in:
Cody Cutrer 2012-02-22 09:59:34 -07:00
parent d375fc36fb
commit 7489de2d5c
4 changed files with 30 additions and 14 deletions

View File

@ -127,9 +127,11 @@ class Attachment < ActiveRecord::Base
end
iconv.iconv(nil)
end
self.update_attribute(:encoding, 'UTF-8')
self.encoding = 'UTF-8'
Attachment.update_all({:encoding => 'UTF-8'}, {:id => self.id})
rescue Iconv::Failure
self.update_attribute(:encoding, '')
self.encoding = ''
Attachment.update_all({:encoding => ''}, {:id => self.id})
return
end
end

View File

@ -0,0 +1,10 @@
class DetectAttachmentEncoding < ActiveRecord::Migration
tag :postdeploy
def self.up
DataFixup::DetectAttachmentEncoding.send_later_if_production(:run)
end
def self.down
end
end

View File

@ -1,12 +0,0 @@
class ScheduleAttachmentEncodingDetection < ActiveRecord::Migration
def self.up
Attachment.transaction do
Attachment.find_each(:conditions => "encoding IS NULL AND content_type LIKE '%text%'") do |a|
a.send_later_if_production(:infer_encoding)
end
end
end
def self.down
end
end

View File

@ -0,0 +1,16 @@
module DataFixup::DetectAttachmentEncoding
def self.run
begin
attachments = Attachment.find(:all, :conditions => "encoding IS NULL AND content_type LIKE '%text%'", :limit => 5000)
attachments.each do |a|
begin
a.infer_encoding
rescue
# some old attachments may have been cleaned off disk, but not out of the db
Rails.logger.warn "Unable to detect encoding for attachment #{a.id}: #{$!}"
Attachment.update_all({:encoding => ''}, {:id => a.id})
end
end
end until attachments.empty?
end
end