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 ?!
You can change the default with an alter table set default charset but that won't change the charset of the existing columns. To change that you need to use a alter table modify column . Changing the charset of a column only means that it will be able to store a wider range of characters.
To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. Replace dbname with the database name: Copy ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci; To exit the mysql program, type \q at the mysql> prompt.
A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With