提交 e2f232ab 编写于 作者: Y Yves Senn

add `bin/rake db:purge` task to empty the current database.

上级 b4b5af03
* Add `bin/rake db:purge` task to empty the current database.
*Yves Senn*
* Deprecate `serialized_attributes` without replacement. You can access its
behavior by going through the column's type object.
......
......@@ -28,6 +28,17 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.drop_current
end
namespace :purge do
task :all => :load_config do
ActiveRecord::Tasks::DatabaseTasks.purge_all
end
end
# desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV it defaults to purging the development and test databases."
task :purge => [:load_config] do
ActiveRecord::Tasks::DatabaseTasks.purge_current
end
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
task :migrate => [:environment, :load_config] do
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
......
......@@ -143,6 +143,18 @@ def purge(configuration)
class_for_adapter(configuration['adapter']).new(configuration).purge
end
def purge_all
each_local_configuration { |configuration|
purge configuration
}
end
def purge_current(environment = env)
each_current_configuration(environment) { |configuration|
purge configuration
}
end
def structure_dump(*arguments)
configuration = arguments.first
filename = arguments.delete_at 1
......
......@@ -285,6 +285,34 @@ class DatabaseTasksPurgeTest < ActiveRecord::TestCase
end
end
class DatabaseTasksPurgeCurrentTest < ActiveRecord::TestCase
def test_purges_current_environment_database
configurations = {
'development' => {'database' => 'dev-db'},
'test' => {'database' => 'test-db'},
'production' => {'database' => 'prod-db'}
}
ActiveRecord::Base.stubs(:configurations).returns(configurations)
ActiveRecord::Tasks::DatabaseTasks.expects(:purge).
with('database' => 'prod-db')
ActiveRecord::Tasks::DatabaseTasks.purge_current('production')
end
end
class DatabaseTasksPurgeAllTest < ActiveRecord::TestCase
def test_purge_all_local_configurations
configurations = {:development => {'database' => 'my-db'}}
ActiveRecord::Base.stubs(:configurations).returns(configurations)
ActiveRecord::Tasks::DatabaseTasks.expects(:purge).
with('database' => 'my-db')
ActiveRecord::Tasks::DatabaseTasks.purge_all
end
end
class DatabaseTasksCharsetTest < ActiveRecord::TestCase
include DatabaseTasksSetupper
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册