Skip to content
This repository
Browse code

Fix charset/collate in mysql recreate_database

See new method mysql_creation_options. It is used by both
create_database and recreate_database so they are consistent.
  • Loading branch information...
commit 3ba3125b24b532876c95c8d9d00c9b69faab5a60 1 parent c314203
Simon Baird authored July 04, 2011
13  activerecord/lib/active_record/railties/databases.rake
@@ -35,6 +35,12 @@ namespace :db do
35 35
     create_database(ActiveRecord::Base.configurations[Rails.env])
36 36
   end
37 37
 
  38
+  def mysql_creation_options(config)
  39
+    @charset   = ENV['CHARSET']   || 'utf8'
  40
+    @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
  41
+    {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
  42
+  end
  43
+
38 44
   def create_database(config)
39 45
     begin
40 46
       if config['adapter'] =~ /sqlite/
@@ -58,9 +64,6 @@ namespace :db do
58 64
     rescue
59 65
       case config['adapter']
60 66
       when /^(jdbc)?mysql/
61  
-        @charset   = ENV['CHARSET']   || 'utf8'
62  
-        @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
63  
-        creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
64 67
         if config['adapter'] =~ /jdbc/
65 68
           #FIXME After Jdbcmysql gives this class
66 69
           require 'active_record/railties/jdbcmysql_error'
@@ -71,7 +74,7 @@ namespace :db do
71 74
         access_denied_error = 1045
72 75
         begin
73 76
           ActiveRecord::Base.establish_connection(config.merge('database' => nil))
74  
-          ActiveRecord::Base.connection.create_database(config['database'], creation_options)
  77
+          ActiveRecord::Base.connection.create_database(config['database'], mysql_creation_options(config))
75 78
           ActiveRecord::Base.establish_connection(config)
76 79
         rescue error_class => sqlerr
77 80
           if sqlerr.errno == access_denied_error
@@ -435,7 +438,7 @@ namespace :db do
435 438
       case abcs["test"]["adapter"]
436 439
       when /^(jdbc)?mysql/
437 440
         ActiveRecord::Base.establish_connection(:test)
438  
-        ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"], abcs["test"])
  441
+        ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"], mysql_creation_options(abcs["test"]))
439 442
       when /^(jdbc)?postgresql$/
440 443
         ActiveRecord::Base.clear_active_connections!
441 444
         drop_database(abcs['test'])

0 notes on commit 3ba3125

Please sign in to comment.
Something went wrong with that request. Please try again.