Permalink
Browse files

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

  • Loading branch information...
senny committed Jun 16, 2014
1 parent b4b5af0 commit e2f232aba15937a4b9d14bd91e0392c6d55be58d
@@ -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
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

0 comments on commit e2f232a

Please sign in to comment.