diff options
author | Pat Allan <pat@freelancing-gods.com> | 2012-06-17 22:16:45 +0200 |
---|---|---|
committer | Pat Allan <pat@freelancing-gods.com> | 2012-06-17 22:16:45 +0200 |
commit | 55f8dfd99e20d991dee2d948120ce8955054acbf (patch) | |
tree | 0c67d3c91f495287fda6458cae159707b5c4d216 /activerecord | |
parent | 8d5710c8987b9d624e276f4f47f91130b46b0440 (diff) | |
download | rails-55f8dfd99e20d991dee2d948120ce8955054acbf.tar.gz rails-55f8dfd99e20d991dee2d948120ce8955054acbf.tar.bz2 rails-55f8dfd99e20d991dee2d948120ce8955054acbf.zip |
Rails is the default (but now override able) source for environment and path.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/tasks/database_tasks.rb | 30 | ||||
-rw-r--r-- | activerecord/lib/active_record/tasks/sqlite_database_tasks.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/database_tasks_test.rb | 29 | ||||
-rw-r--r-- | activerecord/test/cases/sqlite_rake_test.rb | 31 |
4 files changed, 57 insertions, 43 deletions
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index e49af454b5..10cc679a05 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -6,8 +6,9 @@ class ActiveRecord::Tasks::DatabaseTasks } LOCAL_HOSTS = ['127.0.0.1', 'localhost'] - def self.create(configuration) - class_for_adapter(configuration['adapter']).new(configuration).create + def self.create(*arguments) + configuration = arguments.first + class_for_adapter(configuration['adapter']).new(*arguments).create rescue Exception => error $stderr.puts error, *(error.backtrace) $stderr.puts "Couldn't create database for #{configuration.inspect}" @@ -17,13 +18,16 @@ class ActiveRecord::Tasks::DatabaseTasks each_local_configuration { |configuration| create configuration } end - def self.create_current - each_current_configuration { |configuration| create configuration } - ActiveRecord::Base.establish_connection Rails.env + def self.create_current(environment = Rails.env) + each_current_configuration(environment) { |configuration| + create configuration + } + ActiveRecord::Base.establish_connection environment end - def self.drop(configuration) - class_for_adapter(configuration['adapter']).new(configuration).drop + def self.drop(*arguments) + configuration = arguments.first + class_for_adapter(configuration['adapter']).new(*arguments).drop rescue Exception => error $stderr.puts error, *(error.backtrace) $stderr.puts "Couldn't drop #{configuration['database']}" @@ -33,8 +37,10 @@ class ActiveRecord::Tasks::DatabaseTasks each_local_configuration { |configuration| drop configuration } end - def self.drop_current - each_current_configuration { |configuration| drop configuration } + def self.drop_current(environment = Rails.env) + each_current_configuration(environment) { |configuration| + drop configuration + } end def self.purge(configuration) @@ -48,9 +54,9 @@ class ActiveRecord::Tasks::DatabaseTasks TASKS_PATTERNS[key] end - def self.each_current_configuration - environments = [Rails.env] - environments << 'test' if Rails.env.development? + def self.each_current_configuration(environment) + environments = [environment] + environments << 'test' if environment.development? configurations = ActiveRecord::Base.configurations.values_at(*environments) configurations.compact.each do |configuration| diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb index 8f8021f47c..4d3cba8a50 100644 --- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb @@ -1,8 +1,8 @@ class ActiveRecord::Tasks::SQLiteDatabaseTasks delegate :connection, :establish_connection, :to => ActiveRecord::Base - def initialize(configuration) - @configuration = configuration + def initialize(configuration, root = Rails.root) + @configuration, @root = configuration, root end def create @@ -18,7 +18,7 @@ class ActiveRecord::Tasks::SQLiteDatabaseTasks def drop require 'pathname' path = Pathname.new configuration['database'] - file = path.absolute? ? path.to_s : File.join(Rails.root, path) + file = path.absolute? ? path.to_s : File.join(root, path) FileUtils.rm(file) end @@ -30,4 +30,8 @@ class ActiveRecord::Tasks::SQLiteDatabaseTasks def configuration @configuration end + + def root + @root + end end diff --git a/activerecord/test/cases/database_tasks_test.rb b/activerecord/test/cases/database_tasks_test.rb index 3cb82a8683..503e26f549 100644 --- a/activerecord/test/cases/database_tasks_test.rb +++ b/activerecord/test/cases/database_tasks_test.rb @@ -106,32 +106,33 @@ module ActiveRecord end def test_creates_current_environment_database - Rails.stubs(:env).returns ActiveSupport::StringInquirer.new('production') - ActiveRecord::Tasks::DatabaseTasks.expects(:create). with('database' => 'prod-db') - ActiveRecord::Tasks::DatabaseTasks.create_current + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new('production') + ) end def test_creates_test_database_when_environment_is_database - Rails.stubs(:env).returns ActiveSupport::StringInquirer.new('development') - ActiveRecord::Tasks::DatabaseTasks.expects(:create). with('database' => 'dev-db') ActiveRecord::Tasks::DatabaseTasks.expects(:create). with('database' => 'test-db') - ActiveRecord::Tasks::DatabaseTasks.create_current + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new('development') + ) end def test_establishes_connection_for_the_given_environment - Rails.stubs(:env).returns ActiveSupport::StringInquirer.new('development') ActiveRecord::Tasks::DatabaseTasks.stubs(:create).returns true ActiveRecord::Base.expects(:establish_connection).with('development') - ActiveRecord::Tasks::DatabaseTasks.create_current + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new('development') + ) end end @@ -239,23 +240,23 @@ module ActiveRecord end def test_creates_current_environment_database - Rails.stubs(:env).returns ActiveSupport::StringInquirer.new('production') - ActiveRecord::Tasks::DatabaseTasks.expects(:drop). with('database' => 'prod-db') - ActiveRecord::Tasks::DatabaseTasks.drop_current + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new('production') + ) end def test_creates_test_database_when_environment_is_database - Rails.stubs(:env).returns ActiveSupport::StringInquirer.new('development') - ActiveRecord::Tasks::DatabaseTasks.expects(:drop). with('database' => 'dev-db') ActiveRecord::Tasks::DatabaseTasks.expects(:drop). with('database' => 'test-db') - ActiveRecord::Tasks::DatabaseTasks.drop_current + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new('development') + ) end end diff --git a/activerecord/test/cases/sqlite_rake_test.rb b/activerecord/test/cases/sqlite_rake_test.rb index ad7146e2a9..f96a161df1 100644 --- a/activerecord/test/cases/sqlite_rake_test.rb +++ b/activerecord/test/cases/sqlite_rake_test.rb @@ -19,7 +19,7 @@ module ActiveRecord def test_db_checks_database_exists File.expects(:exist?).with(@database).returns(false) - ActiveRecord::Tasks::DatabaseTasks.create @configuration + ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' end def test_db_create_when_file_exists @@ -27,7 +27,7 @@ module ActiveRecord $stderr.expects(:puts).with("#{@database} already exists") - ActiveRecord::Tasks::DatabaseTasks.create @configuration + ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' end def test_db_create_with_file_does_nothing @@ -36,18 +36,13 @@ module ActiveRecord ActiveRecord::Base.expects(:establish_connection).never - ActiveRecord::Tasks::DatabaseTasks.create @configuration + ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' end def test_db_create_establishes_a_connection ActiveRecord::Base.expects(:establish_connection).with(@configuration) - ActiveRecord::Tasks::DatabaseTasks.create @configuration - end - - def test_db_create_returns_the_connection - assert_equal ActiveRecord::Tasks::DatabaseTasks.create(@configuration), - @connection + ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' end def test_db_create_with_error_prints_message @@ -57,7 +52,7 @@ module ActiveRecord $stderr.expects(:puts). with("Couldn't create database for #{@configuration.inspect}") - ActiveRecord::Tasks::DatabaseTasks.create(@configuration) + ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' end end @@ -70,7 +65,6 @@ module ActiveRecord 'database' => @database } - Rails.stubs(:root).returns('/rails/root') Pathname.stubs(:new).returns(@path) File.stubs(:join).returns('/former/relative/path') FileUtils.stubs(:rm).returns(true) @@ -79,7 +73,7 @@ module ActiveRecord def test_creates_path_from_database Pathname.expects(:new).with(@database).returns(@path) - ActiveRecord::Tasks::DatabaseTasks.drop @configuration + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' end def test_removes_file_with_absolute_path @@ -87,7 +81,16 @@ module ActiveRecord FileUtils.expects(:rm).with('/absolute/path') - ActiveRecord::Tasks::DatabaseTasks.drop @configuration + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' + end + + def test_generates_absolute_path_with_given_root + @path.stubs(:absolute?).returns(false) + + File.expects(:join).with('/rails/root', @path). + returns('/former/relative/path') + + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' end def test_removes_file_with_relative_path @@ -95,7 +98,7 @@ module ActiveRecord FileUtils.expects(:rm).with('/former/relative/path') - ActiveRecord::Tasks::DatabaseTasks.drop @configuration + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' end end end |