Rails 3: Change charset and collation of an existing mysql database

◇◆丶佛笑我妖孽 提交于 2020-05-29 09:05:18

问题


Is it possible to change the charset and collation of an existing Mysql database using Rails migrations or other options ?!

What's the best way to initially configure database charset & collation ?!


回答1:


For latest version of Rails you can use this gist:

Change charset and collation of an existing mysql

Modified from here:




回答2:


Native query could be executed in rails migration:

def self.up
  execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET charset_here COLLATE collation_here;"
end

And for initial configuration you can predefine charset and collation in your database.yml file, like this:

production:
  adapter: mysql2
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci



回答3:


I used Migrating your Rails application to Unicode and changed:

when 'MySQL'

to

when 'Mysql2'



来源:https://stackoverflow.com/questions/8200699/rails-3-change-charset-and-collation-of-an-existing-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!