Skip to content
This repository
Browse code

Merge pull request #6906 from kennyj/extract_collation

Extract collation tasks to database tasks.
  • Loading branch information...
commit cca614bc37d00abd791c27c404682682ad73af59 2 parents f021467 + bece59e
Rafael Mendonça França authored June 30, 2012
11  activerecord/lib/active_record/railties/databases.rake
@@ -148,14 +148,9 @@ db_namespace = namespace :db do
148 148
 
149 149
   # desc "Retrieves the collation for the current environment's database"
150 150
   task :collation => [:environment, :load_config] do
151  
-    config = ActiveRecord::Base.configurations[Rails.env || 'development']
152  
-    case config['adapter']
153  
-    when /mysql/
154  
-      ActiveRecord::Base.establish_connection(config)
155  
-      puts ActiveRecord::Base.connection.collation
156  
-    else
157  
-      $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
158  
-    end
  151
+    puts ActiveRecord::Tasks::DatabaseTasks.collation_current
  152
+  ensure NoMethodError
  153
+    $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
159 154
   end
160 155
 
161 156
   desc 'Retrieves the current schema version number'
9  activerecord/lib/active_record/tasks/database_tasks.rb
@@ -56,6 +56,15 @@ def charset(*arguments)
56 56
         class_for_adapter(configuration['adapter']).new(*arguments).charset
57 57
       end
58 58
 
  59
+      def collation_current(environment = Rails.env)
  60
+        collation ActiveRecord::Base.configurations[environment]
  61
+      end
  62
+
  63
+      def collation(*arguments)
  64
+        configuration = arguments.first
  65
+        class_for_adapter(configuration['adapter']).new(*arguments).collation
  66
+      end
  67
+
59 68
       def purge(configuration)
60 69
         class_for_adapter(configuration['adapter']).new(configuration).purge
61 70
       end
4  activerecord/lib/active_record/tasks/mysql_database_tasks.rb
@@ -44,6 +44,10 @@ def charset
44 44
         connection.charset
45 45
       end
46 46
 
  47
+      def collation
  48
+        connection.collation
  49
+      end
  50
+
47 51
       def structure_dump(filename)
48 52
         establish_connection configuration
49 53
         File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
4  activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
@@ -29,6 +29,10 @@ def charset
29 29
         connection.encoding
30 30
       end
31 31
 
  32
+      def collation
  33
+        connection.collate
  34
+      end
  35
+
32 36
       def purge
33 37
         clear_active_connections!
34 38
         drop
11  activerecord/test/cases/tasks/database_tasks_test.rb
@@ -251,6 +251,17 @@ class DatabaseTasksCharsetTest < ActiveRecord::TestCase
251 251
     end
252 252
   end
253 253
 
  254
+  class DatabaseTasksCollationTest < ActiveRecord::TestCase
  255
+    include DatabaseTasksSetupper
  256
+ 
  257
+    ADAPTERS_TASKS.each do |k, v|
  258
+      define_method("test_#{k}_collation") do
  259
+        eval("@#{v}").expects(:collation)
  260
+        ActiveRecord::Tasks::DatabaseTasks.collation 'adapter' => k
  261
+      end
  262
+    end
  263
+  end
  264
+
254 265
   class DatabaseTasksStructureDumpTest < ActiveRecord::TestCase
255 266
     include DatabaseTasksSetupper
256 267
 
18  activerecord/test/cases/tasks/mysql_rake_test.rb
@@ -195,6 +195,24 @@ def test_db_retrieves_charset
195 195
     end
196 196
   end
197 197
 
  198
+  class MysqlDBCollationTest < ActiveRecord::TestCase
  199
+    def setup
  200
+      @connection    = stub(:create_database => true)
  201
+      @configuration = {
  202
+        'adapter'  => 'mysql',
  203
+        'database' => 'my-app-db'
  204
+      }
  205
+
  206
+      ActiveRecord::Base.stubs(:connection).returns(@connection)
  207
+      ActiveRecord::Base.stubs(:establish_connection).returns(true)
  208
+    end
  209
+
  210
+    def test_db_retrieves_collation
  211
+      @connection.expects(:collation)
  212
+      ActiveRecord::Tasks::DatabaseTasks.collation @configuration
  213
+    end
  214
+  end
  215
+
198 216
   class MySQLStructureDumpTest < ActiveRecord::TestCase
199 217
     def setup
200 218
       @connection    = stub(:structure_dump => true)
18  activerecord/test/cases/tasks/postgresql_rake_test.rb
@@ -159,6 +159,24 @@ def test_db_retrieves_charset
159 159
     end
160 160
   end
161 161
 
  162
+  class PostgreSQLDBCollationTest < ActiveRecord::TestCase
  163
+    def setup
  164
+      @connection    = stub(:create_database => true)
  165
+      @configuration = {
  166
+        'adapter'  => 'postgresql',
  167
+        'database' => 'my-app-db'
  168
+      }
  169
+
  170
+      ActiveRecord::Base.stubs(:connection).returns(@connection)
  171
+      ActiveRecord::Base.stubs(:establish_connection).returns(true)
  172
+    end
  173
+
  174
+    def test_db_retrieves_collation
  175
+      @connection.expects(:collate)
  176
+      ActiveRecord::Tasks::DatabaseTasks.collation @configuration
  177
+    end
  178
+  end
  179
+
162 180
   class PostgreSQLStructureDumpTest < ActiveRecord::TestCase
163 181
     def setup
164 182
       @connection    = stub(:structure_dump => true)
21  activerecord/test/cases/tasks/sqlite_rake_test.rb
@@ -124,6 +124,27 @@ def test_db_retrieves_charset
124 124
     end
125 125
   end
126 126
 
  127
+  class SqliteDBCollationTest < ActiveRecord::TestCase
  128
+    def setup
  129
+      @database      = 'db_create.sqlite3'
  130
+      @connection    = stub :connection
  131
+      @configuration = {
  132
+        'adapter'  => 'sqlite3',
  133
+        'database' => @database
  134
+      }
  135
+
  136
+      File.stubs(:exist?).returns(false)
  137
+      ActiveRecord::Base.stubs(:connection).returns(@connection)
  138
+      ActiveRecord::Base.stubs(:establish_connection).returns(true)
  139
+    end
  140
+
  141
+    def test_db_retrieves_collation
  142
+      assert_raise NoMethodError do
  143
+        ActiveRecord::Tasks::DatabaseTasks.collation @configuration, '/rails/root'
  144
+      end
  145
+    end
  146
+  end
  147
+
127 148
   class SqliteStructureDumpTest < ActiveRecord::TestCase
128 149
     def setup
129 150
       @database      = "db_create.sqlite3"

0 notes on commit cca614b

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