Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
rake db:test:purge creates mysql database with wrong charset & collation #1952
In db:test:purge ActiveRecord::Base.connection.recreate_database is called with the full database config options hash instead of the :charset and :collation hash that is expected.
This causes my test database to be created with the collation set incorrectly (I need utf8_bin) and my fixtures don't load and my tests fail. (Further explanation in comments).
See new method mysql_creation_options. It is used by both create_database and recreate_database so they are consistent.
An alternative fix might be to change the options param here:
but that would ignore ENV['CHARSET'] and ENV['COLLATION']
Imagine if you set charset and collation in config/database.yml. For this example lets use the test db:
it calls create_database with these options:
This is correct and creates the database with the correct charset and collation.
Now if you run
it calls recreate_database which then calls create_database with the following options, (ie the entire test db config from database.yml):
Notice that it does have the collation and charset values there BUT the hash keys are symbols so there is no options[:collation] and no options[:charset] present. This causes the database to be created with the default charset of utf8 and an unspecified collation.
(See create_database method here:
In my particular case using the default collation value causes the primary key constraint to consider primary keys as case insensitive, which means I can't load fixtures since I have some data where primary key constraints need to be case sensitive.
It's not hard to imagine other scenarios where having the test database created with a different charset and collation when running tests could lead to weirdness and problems.
It would be simple to patch recreate_database to work-around this (see comment above), but ideally we should respect the ENV['CHARSET'] and ENV['COLLATE'] values in the same way that rake db:create would do. So the fix is done in database.rake and involves a small refactor so create_database and recreate_database can work the same way.
In Rails 3.1
I have not confirmed it but from looking at the code I believe this problem exists in the 3.1 stable branch also. A port of this fix to 3.1 might be required if this doesn't merge cleanly.