Skip to content

Instantly share code, notes, and snippets.

@ayamomiji
Last active February 13, 2017 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayamomiji/66696d7fea669f9e8d6e288b9ccdf3df to your computer and use it in GitHub Desktop.
Save ayamomiji/66696d7fea669f9e8d6e288b9ccdf3df to your computer and use it in GitHub Desktop.
Add emoji support for mysql
default: &default
adapter: mysql2
encoding: utf8mb4
charset: utf8mb4
collation: utf8mb4_unicode_ci
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
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