diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2012-09-21 12:03:11 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2012-09-21 12:03:16 -0700 |
commit | 78b30fed9336336694fb2cb5d2825f95800b541c (patch) | |
tree | b3bb786dfd966d42988b87dee9ec8c56c45a4e44 /activerecord | |
parent | 68bd4db190fea5aad213886898e3d541f8de6fa5 (diff) | |
download | rails-78b30fed9336336694fb2cb5d2825f95800b541c.tar.gz rails-78b30fed9336336694fb2cb5d2825f95800b541c.tar.bz2 rails-78b30fed9336336694fb2cb5d2825f95800b541c.zip |
Correct default charset/collation for mysql dbs
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/tasks/mysql_database_tasks.rb | 15 | ||||
-rw-r--r-- | activerecord/test/cases/tasks/mysql_rake_test.rb | 30 |
2 files changed, 30 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index 7abbf08571..3d27c97254 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -73,13 +73,16 @@ module ActiveRecord configuration.merge('database' => nil) end - # If neither encoding nor collation is specified, use the utf-8 defaults. def creation_options - options = configuration.slice('encoding', 'collation').symbolize_keys - if options.empty? - { charset: DEFAULT_CHARSET, collation: DEFAULT_COLLATION } - else - options + Hash.new.tap do |options| + options[:charset] = configuration['encoding'] if configuration.include? 'encoding' + options[:collation] = configuration['collation'] if configuration.include? 'collation' + + # Set default charset only when collation isn't set. + options[:charset] ||= DEFAULT_CHARSET unless options[:collation] + + # Set default collation only when charset is also default. + options[:collation] ||= DEFAULT_COLLATION if options[:charset] == DEFAULT_CHARSET end end diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb index 46b97a1274..97d283bf14 100644 --- a/activerecord/test/cases/tasks/mysql_rake_test.rb +++ b/activerecord/test/cases/tasks/mysql_rake_test.rb @@ -20,20 +20,32 @@ module ActiveRecord ActiveRecord::Tasks::DatabaseTasks.create @configuration end - def test_creates_database_with_default_options + def test_creates_database_with_default_encoding_and_collation @connection.expects(:create_database). - with('my-app-db', {:charset => 'utf8', :collation => 'utf8_unicode_ci'}) + with('my-app-db', charset: 'utf8', collation: 'utf8_unicode_ci') ActiveRecord::Tasks::DatabaseTasks.create @configuration end - def test_creates_database_with_given_options + def test_creates_database_with_given_encoding_and_default_collation @connection.expects(:create_database). - with('my-app-db', {:charset => 'latin', :collation => 'latin_ci'}) + with('my-app-db', charset: 'utf8', collation: 'utf8_unicode_ci') - ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge( - 'encoding' => 'latin', 'collation' => 'latin_ci' - ) + ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge('encoding' => 'utf8') + end + + def test_creates_database_with_given_encoding_and_no_collation + @connection.expects(:create_database). + with('my-app-db', charset: 'latin1') + + ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge('encoding' => 'latin1') + end + + def test_creates_database_with_given_collation_and_no_encoding + @connection.expects(:create_database). + with('my-app-db', collation: 'latin1_swedish_ci') + + ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge('collation' => 'latin1_swedish_ci') end def test_establishes_connection_to_database @@ -166,14 +178,14 @@ module ActiveRecord def test_recreates_database_with_the_default_options @connection.expects(:recreate_database). - with('test-db', {:charset => 'utf8', :collation => 'utf8_unicode_ci'}) + with('test-db', { :charset => 'utf8', :collation => 'utf8_unicode_ci' }) ActiveRecord::Tasks::DatabaseTasks.purge @configuration end def test_recreates_database_with_the_given_options @connection.expects(:recreate_database). - with('test-db', {:charset => 'latin', :collation => 'latin_ci'}) + with('test-db', { :charset => 'latin', :collation => 'latin_ci' }) ActiveRecord::Tasks::DatabaseTasks.purge @configuration.merge( 'encoding' => 'latin', 'collation' => 'latin_ci' |