Skip to content

Commit

Permalink
add bin/rake db:purge task to empty the current database.
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed Jun 17, 2014
1 parent b4b5af0 commit e2f232a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions activerecord/test/cases/tasks/database_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e2f232a

Please sign in to comment.