From f4fd77fa7c8b04e0bbd12b016f54f5fb1ba7d933 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 18 Apr 2015 17:54:15 +0200 Subject: [PATCH 1/2] 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 --- .../20150106050733_set_mysql_to_unicode_mb4.rb | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb b/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb index 9495e7f80..0f5181eb2 100644 --- a/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb +++ b/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb @@ -25,24 +25,8 @@ class SetMysqlToUnicodeMb4 < ActiveRecord::Migration execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET #{encoding} COLLATE #{collation};" 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 - - 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 def shorten_indexes From 4be8a0bed96239cf117cce2a4dc03efc3ce4cb77 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 19 Apr 2015 03:19:40 +0200 Subject: [PATCH 2/2] change TEXT columns back to TEXT mysql changes the type of TEXT columns to MEDIUMTEXT with "CONVERT TO", but we don't want this, so change this columns back to TEXT. see: https://bugs.mysql.com/bug.php?id=31291 --- db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb b/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb index 0f5181eb2..37dd9373e 100644 --- a/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb +++ b/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb @@ -25,7 +25,12 @@ class SetMysqlToUnicodeMb4 < ActiveRecord::Migration execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET #{encoding} COLLATE #{collation};" tables.each do |table| - execute "ALTER TABLE `#{table}` CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation}" + + modify_text_columns = columns(table).select {|column| column.type == :text }.map {|column| + "MODIFY `#{column.name}` TEXT #{'NOT' unless column.null } NULL#{" DEFAULT '#{column.default}'" if column.has_default?}" + }.join(", ") + + execute "ALTER TABLE `#{table}` CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation}#{", #{modify_text_columns}" unless modify_text_columns.empty?};" end end