Permalink
Browse files

Correct default charset/collation for mysql dbs

  • Loading branch information...
1 parent 68bd4db commit 78b30fed9336336694fb2cb5d2825f95800b541c @jeremy jeremy committed Sep 21, 2012
View
15 activerecord/lib/active_record/tasks/mysql_database_tasks.rb
@@ -73,13 +73,16 @@ def configuration_without_database
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
View
30 activerecord/test/cases/tasks/mysql_rake_test.rb
@@ -20,20 +20,32 @@ def test_establishes_connection_without_database
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 @@ def test_establishes_connection_to_test_database
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'

0 comments on commit 78b30fe

Please sign in to comment.