Permalink
Browse files

Rails is the default (but now override able) source for environment a…

…nd path.
  • Loading branch information...
1 parent 8d5710c commit 55f8dfd99e20d991dee2d948120ce8955054acbf @pat pat committed Jun 17, 2012
@@ -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 @@ def self.create_all
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 @@ def self.drop_all
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 @@ def self.class_for_adapter(adapter)
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|
@@ -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 @@ def create
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 @@ def drop
def configuration
@configuration
end
+
+ def root
+ @root
+ end
end
@@ -106,32 +106,33 @@ def setup
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 @@ def setup
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
@@ -19,15 +19,15 @@ def setup
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
File.stubs(:exist?).returns(true)
$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 @@ def test_db_create_with_file_does_nothing
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 @@ def test_db_create_with_error_prints_message
$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 @@ def setup
'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,23 +73,32 @@ def setup
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
@path.stubs(:absolute?).returns(true)
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
@path.stubs(:absolute?).returns(false)
FileUtils.expects(:rm).with('/former/relative/path')
- ActiveRecord::Tasks::DatabaseTasks.drop @configuration
+ ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root'
end
end
end

0 comments on commit 55f8dfd

Please sign in to comment.