use "CONVERT TO" to change the encoding for the table and all character columns

see: https://dev.mysql.com/doc/refman/5.6/en/charset-conversion.html
This commit is contained in:
Benjamin Neff 2015-04-18 17:54:15 +02:00
parent 2613896cfd
commit f4fd77fa7c

View file

@ -25,24 +25,8 @@ class SetMysqlToUnicodeMb4 < ActiveRecord::Migration
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET #{encoding} COLLATE #{collation};" execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET #{encoding} COLLATE #{collation};"
tables.each do |table| tables.each do |table|
execute "ALTER TABLE `#{table}` CHARACTER SET = #{encoding} COLLATE #{collation}" execute "ALTER TABLE `#{table}` CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation}"
end end
character_columns.each do |table, columns|
columns.each do |column|
execute "ALTER TABLE `#{table}` CHANGE `#{column.name}` `#{column.name}` #{column.sql_type} CHARACTER SET #{encoding} COLLATE #{collation} #{column.null ? 'NULL' : 'NOT NULL'} #{"DEFAULT '#{column.default}'" if column.has_default?};"
end
end
end
def character_columns
# build a hash with all the columns that contain characters
@character_columns ||= Hash[tables.map {|table|
col = columns(table)
.select {|column| column.type == :string || column.type == :text }
next if col.empty?
[table, col]
}.compact]
end end
def shorten_indexes def shorten_indexes