From 66d1ac07c81a8965f0ba2cf36386ec1831ca5d25 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Thu, 21 Aug 2014 19:43:04 +0900 Subject: [PATCH] Fall back utf8mb4 to utf8 if the server does not support it. --- lib/ar_mysql_column_charset.rb | 35 ++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/ar_mysql_column_charset.rb b/lib/ar_mysql_column_charset.rb index 6b91790d..354f2f3f 100644 --- a/lib/ar_mysql_column_charset.rb +++ b/lib/ar_mysql_column_charset.rb @@ -38,6 +38,31 @@ module ActiveRecord::ConnectionAdapters def migration_keys super + [:charset, :collation] end + + def utf8mb4_supported? + if @utf8mb4_supported.nil? + @utf8mb4_supported = !select("show character set like 'utf8mb4'").empty? + else + @utf8mb4_supported + end + end + + def charset_collation(charset, collation) + [charset, collation].map { |name| + case name + when nil + nil + when /\A(utf8mb4(_\w*)?)\z/ + if utf8mb4_supported? + $1 + else + "utf8#{$2}" + end + else + name.to_s + end + } + end end prepend CharsetSupport @@ -52,12 +77,14 @@ module ActiveRecord::ConnectionAdapters end def add_column_options!(sql, options) - if options[:charset] - sql << " CHARACTER SET #{options[:charset]}" + charset, collation = @conn.charset_collation(options[:charset], options[:collation]) + + if charset + sql << " CHARACTER SET #{charset}" end - if options[:collation] - sql << " COLLATE #{options[:collation]}" + if collation + sql << " COLLATE #{collation}" end super