diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-06-19 07:41:44 -0700 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-06-19 07:41:44 -0700 |
commit | 1aa052c6889bf5371ec92e5bfc461070d7d99761 (patch) | |
tree | 8f70dd921b3d88140ad1ee884ab6bee6d7f789c8 | |
parent | 4d69cdbdd75f5a9594055099770c4c3d0ffeb98d (diff) | |
parent | 363ab882165dc89a642c4fed01417bd64cdce0a8 (diff) | |
download | rails-1aa052c6889bf5371ec92e5bfc461070d7d99761.tar.gz rails-1aa052c6889bf5371ec92e5bfc461070d7d99761.tar.bz2 rails-1aa052c6889bf5371ec92e5bfc461070d7d99761.zip |
Merge pull request #6779 from simonjefford/db-rake-charset
Refactor db:charset task
9 files changed, 118 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 539836e9ed..d513dca1f5 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -143,20 +143,7 @@ db_namespace = namespace :db do # desc "Retrieves the charset for the current environment's database" task :charset => [:environment, :load_config] do - config = ActiveRecord::Base.configurations[Rails.env || 'development'] - case config['adapter'] - when /mysql/ - ActiveRecord::Base.establish_connection(config) - puts ActiveRecord::Base.connection.charset - when /postgresql/ - ActiveRecord::Base.establish_connection(config) - puts ActiveRecord::Base.connection.encoding - when /sqlite/ - ActiveRecord::Base.establish_connection(config) - puts ActiveRecord::Base.connection.encoding - else - $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' - end + puts ActiveRecord::Tasks::DatabaseTasks.charset_current end # desc "Retrieves the collation for the current environment's database" diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 24fe4134e0..5101871cc0 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -47,6 +47,15 @@ module ActiveRecord } end + def charset_current(environment = Rails.env) + charset ActiveRecord::Base.configurations[environment] + end + + def charset(*arguments) + configuration = arguments.first + class_for_adapter(configuration['adapter']).new(*arguments).charset + end + def purge(configuration) class_for_adapter(configuration['adapter']).new(configuration).purge end diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index 646034f313..361437fdc7 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -40,6 +40,10 @@ module ActiveRecord connection.recreate_database configuration['database'], creation_options end + def charset + connection.charset + end + private def configuration @@ -90,4 +94,4 @@ IDENTIFIED BY '#{configuration['password']}' WITH GRANT OPTION; end end end -end
\ No newline at end of file +end diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index ddc5b9fc5c..8d4249fd79 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -23,6 +23,10 @@ module ActiveRecord connection.drop_database configuration['database'] end + def charset + connection.encoding + end + def purge clear_active_connections! drop @@ -47,4 +51,4 @@ module ActiveRecord end end end -end
\ No newline at end of file +end diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb index 85f4c12829..d9332eaf1b 100644 --- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb @@ -27,6 +27,10 @@ module ActiveRecord end alias :purge :drop + def charset + connection.encoding + end + private def configuration @@ -38,4 +42,4 @@ module ActiveRecord end end end -end
\ No newline at end of file +end diff --git a/activerecord/test/cases/database_tasks_test.rb b/activerecord/test/cases/database_tasks_test.rb index 503e26f549..a5329073ed 100644 --- a/activerecord/test/cases/database_tasks_test.rb +++ b/activerecord/test/cases/database_tasks_test.rb @@ -294,4 +294,38 @@ module ActiveRecord ActiveRecord::Tasks::DatabaseTasks.purge 'adapter' => 'sqlite3' end end + + class DatabaseTasksCharsetTest < ActiveRecord::TestCase + def setup + @mysql_tasks, @postgresql_tasks, @sqlite_tasks = stub, stub, stub + ActiveRecord::Tasks::MySQLDatabaseTasks.stubs(:new).returns @mysql_tasks + ActiveRecord::Tasks::PostgreSQLDatabaseTasks.stubs(:new). + returns @postgresql_tasks + ActiveRecord::Tasks::SQLiteDatabaseTasks.stubs(:new).returns @sqlite_tasks + end + + def test_mysql_charset + @mysql_tasks.expects(:charset) + + ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => 'mysql' + end + + def test_mysql2_charset + @mysql_tasks.expects(:charset) + + ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => 'mysql2' + end + + def test_postgresql_charset + @postgresql_tasks.expects(:charset) + + ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => 'postgresql' + end + + def test_sqlite_charset + @sqlite_tasks.expects(:charset) + + ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => 'sqlite3' + end + end end diff --git a/activerecord/test/cases/mysql_rake_test.rb b/activerecord/test/cases/mysql_rake_test.rb index 1dfd28df54..39d0ae428c 100644 --- a/activerecord/test/cases/mysql_rake_test.rb +++ b/activerecord/test/cases/mysql_rake_test.rb @@ -176,4 +176,22 @@ module ActiveRecord ) end end -end
\ No newline at end of file + + class MysqlDBCharsetTest < ActiveRecord::TestCase + def setup + @connection = stub(:create_database => true) + @configuration = { + 'adapter' => 'mysql', + 'database' => 'my-app-db' + } + + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_retrieves_charset + @connection.expects(:charset) + ActiveRecord::Tasks::DatabaseTasks.charset @configuration + end + end +end diff --git a/activerecord/test/cases/postgresql_rake_test.rb b/activerecord/test/cases/postgresql_rake_test.rb index 58168e4948..a9e0d913fa 100644 --- a/activerecord/test/cases/postgresql_rake_test.rb +++ b/activerecord/test/cases/postgresql_rake_test.rb @@ -132,4 +132,22 @@ module ActiveRecord ActiveRecord::Tasks::DatabaseTasks.purge @configuration end end -end
\ No newline at end of file + + class PostgreSQLDBCharsetTest < ActiveRecord::TestCase + def setup + @connection = stub(:create_database => true) + @configuration = { + 'adapter' => 'postgresql', + 'database' => 'my-app-db' + } + + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_retrieves_charset + @connection.expects(:encoding) + ActiveRecord::Tasks::DatabaseTasks.charset @configuration + end + end +end diff --git a/activerecord/test/cases/sqlite_rake_test.rb b/activerecord/test/cases/sqlite_rake_test.rb index f68809bfda..cfca937a24 100644 --- a/activerecord/test/cases/sqlite_rake_test.rb +++ b/activerecord/test/cases/sqlite_rake_test.rb @@ -103,4 +103,24 @@ module ActiveRecord ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' end end -end
\ No newline at end of file + + class SqliteDBCharsetTest < ActiveRecord::TestCase + def setup + @database = 'db_create.sqlite3' + @connection = stub :connection + @configuration = { + 'adapter' => 'sqlite3', + 'database' => @database + } + + File.stubs(:exist?).returns(false) + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_retrieves_charset + @connection.expects(:encoding) + ActiveRecord::Tasks::DatabaseTasks.charset @configuration, '/rails/root' + end + end +end |