9

I have a Rails 3.2 project using Mysql 5.5.34, with utf8 encoding. Now I found that with utf8 encoding Mysql could not save unicode characters which represent emoji.

So is it OK for me to convert the whole database to use utf8mb4 encoding that I found on the web that could hold 4 byte unicode include emoji?

Is all the information I have in the database covered by utf8mb4 encoding? Will I face data loses if I do that?

Is there any way that Rails provide to do that?

Thanks a lot for helping.

3
  • I am kinda facing the same situation.. What did you end up doing? Commented Apr 24, 2015 at 5:20
  • @Hari Sorry for the late response. I've done that following this blog: blog.xdite.net/posts/2013/12/19/mysql-with-utf8mb4 . Unfortunately it's written in Chinese, would you try Google translate to see if it helps? Commented May 5, 2015 at 14:57
  • blog.arkency.com/2015/05/… Commented Sep 4, 2015 at 19:21

1 Answer 1

1

Actually you just need to migrate the column you want to encode with utf8mb4.

execute("ALTER TABLE yourtablename MODIFY yourcolumnname TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;")

If you plan to migrate the data itself it might not be possible, since the common utf8 consists out of 3 byte chars and the utf8mb4 out of 4 byte. So you might already have corrupt data in your db.

Furthermore Rails 3.2 has an encoding issue within ActiveSupports JSON encoding. In case you plan to work with json and emojis, you will need to add a patch like the following (based on the solution in rails 4 https://github.com/rails/rails/blob/4-0-stable/activesupport/lib/active_support/json/encoding.rb) or just simply upgrade to rails 4.

module ActiveSupport
  module JSON
    module Encoding
      class << self
        def escape(string)
          if string.respond_to?(:force_encoding)
            string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
          end
          json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }
          json = %("#{json}")
          json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding)
          json
        end
      end
    end
  end
end
Sign up to request clarification or add additional context in comments.

3 Comments

I did the alter table query above.. the smilies still end up as a bunch of question marks like this "????" .. Any idea?
Anything I should add to the answer?
@Hari What did you do to solve the database connection?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.