Last active
February 13, 2017 09:21
-
-
Save ayamomiji/66696d7fea669f9e8d6e288b9ccdf3df to your computer and use it in GitHub Desktop.
Add emoji support for mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
default: &default | |
adapter: mysql2 | |
encoding: utf8mb4 | |
charset: utf8mb4 | |
collation: utf8mb4_unicode_ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record/connection_adapters/abstract_mysql_adapter' | |
module ActiveRecord | |
module ConnectionAdapters | |
class AbstractMysqlAdapter | |
NATIVE_DATABASE_TYPES[:string] = { name: 'varchar', limit: 191 } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddEmojiSupport < ActiveRecord::Migration | |
def up | |
conn = ActiveRecord::Base.connection | |
execute "ALTER DATABASE `#{conn.current_database}` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;" | |
conn.tables.each do |table| | |
conn.columns(table).each do |column| | |
case column.type | |
when :string, :text | |
change_column table, column.name, column.type | |
end | |
end | |
execute "ALTER TABLE `#{table}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" | |
end | |
end | |
def down | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment