1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

This commit is contained in:
Yves Senn 2014-06-16 19:28:38 +02:00
parent b4b5af0342
commit e2f232aba1
4 changed files with 55 additions and 0 deletions

View file

@ -1,3 +1,7 @@
* Add `bin/rake db:purge` task to empty the current database.
*Yves Senn*
* Deprecate `serialized_attributes` without replacement. You can access its * Deprecate `serialized_attributes` without replacement. You can access its
behavior by going through the column's type object. behavior by going through the column's type object.

View file

@ -28,6 +28,17 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.drop_current ActiveRecord::Tasks::DatabaseTasks.drop_current
end 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)." desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
task :migrate => [:environment, :load_config] do task :migrate => [:environment, :load_config] do
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true

View file

@ -143,6 +143,18 @@ module ActiveRecord
class_for_adapter(configuration['adapter']).new(configuration).purge class_for_adapter(configuration['adapter']).new(configuration).purge
end 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) def structure_dump(*arguments)
configuration = arguments.first configuration = arguments.first
filename = arguments.delete_at 1 filename = arguments.delete_at 1

View file

@ -285,6 +285,34 @@ module ActiveRecord
end end
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 class DatabaseTasksCharsetTest < ActiveRecord::TestCase
include DatabaseTasksSetupper include DatabaseTasksSetupper