The create_database method of databases.rake creates a mysql
database with default charset utf8 and default collation of
utf8_unicode_ci. These values can be overridden by providing a
:charset resp. :collation key/values in database.yml.
When connecting to the database though by default it will use
these values:
{"Variable_name"=>"character_set_client", "Value"=>"latin1"},
{"Variable_name"=>"character_set_connection", "Value"=>"latin1"},
{"Variable_name"=>"character_set_database", "Value"=>"utf8"},
{"Variable_name"=>"character_set_results", "Value"=>"latin1"},
{"Variable_name"=>"character_set_server", "Value"=>"utf8"},
{"Variable_name"=>"character_set_system", "Value"=>"utf8"},
{"Variable_name"=>"collation_connection", "Value"=>"latin1_swedish_ci"}
{"Variable_name"=>"collation_database", "Value"=>"utf8_unicode_ci"},
{"Variable_name"=>"collation_server", "Value"=>"utf8_unicode_ci"}
The only way to get some control over this by explicitly
defining encoding:utf8 in database.yml. You'd then get:
{"Variable_name"=>"character_set_client", "Value"=>"utf8"},
{"Variable_name"=>"character_set_connection", "Value"=>"utf8"},
{"Variable_name"=>"character_set_database", "Value"=>"utf8"},
{"Variable_name"=>"character_set_results", "Value"=>"utf8"},
{"Variable_name"=>"character_set_server", "Value"=>"utf8"},
{"Variable_name"=>"character_set_system", "Value"=>"utf8"},
{"Variable_name"=>"collation_connection", "Value"=>"utf8_general_ci"}
{"Variable_name"=>"collation_database", "Value"=>"utf8_unicode_ci"},
{"Variable_name"=>"collation_server", "Value"=>"utf8_unicode_ci"}
I.e., the collation_connection value is still using a wrong
value in this case (it's value is always the default collation for
the character set of the connection, in this case of the utf8
character set).
The only solution I see is by patching the
+configure_connection+ method of the mysql adapter.
There are two options:
-
Instead of doing SET NAMES 'utf8' it should be possible to do
SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'. For this I propose the
+configure_connection+ method should (re)use the :charset and
:collation values from database.yml (in favor of :encoding), just
like the databases rake tasks do.
-
Instead of doing SET NAMES 'utf8' it should do SET CHARACTER SET
'utf8'. In that case the collation_connection value will be set to
the value of collation_database.
Which option is preferable?