问题
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