From f4fd77fa7c8b04e0bbd12b016f54f5fb1ba7d933 Mon Sep 17 00:00:00 2001
From: Benjamin Neff <benjamin@coding4coffee.ch>
Date: Sat, 18 Apr 2015 17:54:15 +0200
Subject: [PATCH] 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 9495e7f80d..0f5181eb21 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
-- 
GitLab