Add warning about MySQL collation Fix database index length This allows new databases to be created with utf8mb4, on MySQL. The maximum column size is 767 bytes. Each character is 4 bytes long -> 767 / 4 = 191 characters for the column.
40 lines
1.2 KiB
Ruby
40 lines
1.2 KiB
Ruby
class SetMysqlToUnicodeMb4 < ActiveRecord::Migration
|
|
|
|
UTF8_PAIRS = {}
|
|
tables = ActiveRecord::Base.connection.tables
|
|
|
|
tables.each do |table|
|
|
ActiveRecord::Base.connection.columns(table).each do |column|
|
|
# build a hash with all the columns that contain text
|
|
UTF8_PAIRS[table] = column.name if (column.type == :string) || (column.type == :text)
|
|
end
|
|
end
|
|
|
|
def self.up
|
|
if AppConfig.mysql?
|
|
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET utf8mb4;"
|
|
|
|
tables.each do |table|
|
|
execute "ALTER TABLE `#{table}` CHARACTER SET = utf8mb4;"
|
|
end
|
|
|
|
UTF8_PAIRS.each do |table, col|
|
|
execute "ALTER TABLE `#{table}` CHANGE `#{col}` `#{col}` TEXT CHARACTER SET utf8mb4 NULL;"
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.down
|
|
if AppConfig.mysql?
|
|
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET utf8;"
|
|
|
|
tables.each do |table|
|
|
execute "ALTER TABLE `#{table}` CHARACTER SET = utf8;"
|
|
end
|
|
|
|
UTF8_PAIRS.each do |table, col|
|
|
execute "ALTER TABLE `#{table}` CHANGE `#{col}` `#{col}` TEXT CHARACTER SET utf8 NULL;"
|
|
end
|
|
end
|
|
end
|
|
end
|