Create database in `utf8` even if DATABASE_ENCODING is `utf8mb4`.

This is because we specify the `utf8mb4` charset in some of the columns
and expect others to be defaulted to `utf8`.

I'd like to make this a feature of `ar_mysql_column_charset`. If you
want to set a table charset, you can always pass a keyword option
`options: "default charset=utf8mb4"` to `create_table`.

Complements #414.

Akinori MUSHA 10 lat temu
rodzic
commit
41eb277475
1 zmienionych plików z 9 dodań i 0 usunięć
  1. 9 0
      lib/ar_mysql_column_charset.rb

+ 9 - 0
lib/ar_mysql_column_charset.rb

@@ -63,6 +63,15 @@ module ActiveRecord::ConnectionAdapters
63 63
           end
64 64
         }
65 65
       end
66
+
67
+      def create_database(name, options = {})
68
+        # utf8mb4 is used in column definitions; use utf8 for
69
+        # databases.
70
+        if options[:charset] == 'utf8mb4'
71
+          options = options.merge(charset: 'utf8')
72
+        end
73
+        super(name, options)
74
+      end
66 75
     end
67 76
 
68 77
     prepend CharsetSupport