From db2bd738e9d4a7435580ed2c0ba59bc3a470b3cb Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 24 Jul 2014 14:02:12 -0300 Subject: [PATCH] Fix encoding for columns serialized with YAML --- db/migrate/20140724134601_fix_yaml_encoding.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+), 0 deletions(-) create mode 100644 db/migrate/20140724134601_fix_yaml_encoding.rb diff --git a/db/migrate/20140724134601_fix_yaml_encoding.rb b/db/migrate/20140724134601_fix_yaml_encoding.rb new file mode 100644 index 0000000..d9e182b --- /dev/null +++ b/db/migrate/20140724134601_fix_yaml_encoding.rb @@ -0,0 +1,30 @@ +class FixYamlEncoding < ActiveRecord::Migration + def self.up + fix_encoding(Block, 'settings') + fix_encoding(Product, 'data') + fix_encoding(Environment, 'settings') + fix_encoding(Profile, 'data') + fix_encoding(ActionTracker::Record, 'params') + fix_encoding(Article, 'setting') + end + + def self.down + puts "Warning: cannot restore original encoding" + end + + private + + def self.fix_encoding(model, param) + result = model.find(:all, :conditions => "#{param} LIKE '%!binary%'") + puts "Fixing #{result.count} rows of #{model} (#{param})" + result.each {|r| r.update_column(param, deep_fix(r.send(param)).to_yaml)} + end + + def self.deep_fix(hash) + hash.each do |value| + value.force_encoding('UTF-8') if value.is_a?(String) && !value.frozen? && value.encoding == Encoding::ASCII_8BIT + deep_fix(value) if value.respond_to?(:each) + end + end + +end -- libgit2 0.21.2