diaspora/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb
2015-01-19 02:18:19 +02:00

38 lines
1.1 KiB
Ruby

class SetMysqlToUnicodeMb4 < ActiveRecord::Migration
UTF8_PAIRS = {
'comments': 'text',
'messages': 'text',
'poll_answers': 'answer',
'polls': 'question',
'posts': 'text',
}
def self.up
if ENV['DB'] == 'mysql'
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET utf8mb4;"
ActiveRecord::Base.connection.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 ENV['DB'] == 'mysql'
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET utf8;"
ActiveRecord::Base.connection.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