Skip to content

Commit e2f232a

Browse files
committed
add bin/rake db:purge task to empty the current database.
1 parent b4b5af0 commit e2f232a

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add `bin/rake db:purge` task to empty the current database.
2+
3+
*Yves Senn*
4+
15
* Deprecate `serialized_attributes` without replacement. You can access its
26
behavior by going through the column's type object.
37

activerecord/lib/active_record/railties/databases.rake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ db_namespace = namespace :db do
2828
ActiveRecord::Tasks::DatabaseTasks.drop_current
2929
end
3030

31+
namespace :purge do
32+
task :all => :load_config do
33+
ActiveRecord::Tasks::DatabaseTasks.purge_all
34+
end
35+
end
36+
37+
# 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."
38+
task :purge => [:load_config] do
39+
ActiveRecord::Tasks::DatabaseTasks.purge_current
40+
end
41+
3142
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
3243
task :migrate => [:environment, :load_config] do
3344
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ def purge(configuration)
143143
class_for_adapter(configuration['adapter']).new(configuration).purge
144144
end
145145

146+
def purge_all
147+
each_local_configuration { |configuration|
148+
purge configuration
149+
}
150+
end
151+
152+
def purge_current(environment = env)
153+
each_current_configuration(environment) { |configuration|
154+
purge configuration
155+
}
156+
end
157+
146158
def structure_dump(*arguments)
147159
configuration = arguments.first
148160
filename = arguments.delete_at 1

activerecord/test/cases/tasks/database_tasks_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,34 @@ class DatabaseTasksPurgeTest < ActiveRecord::TestCase
285285
end
286286
end
287287

288+
class DatabaseTasksPurgeCurrentTest < ActiveRecord::TestCase
289+
def test_purges_current_environment_database
290+
configurations = {
291+
'development' => {'database' => 'dev-db'},
292+
'test' => {'database' => 'test-db'},
293+
'production' => {'database' => 'prod-db'}
294+
}
295+
ActiveRecord::Base.stubs(:configurations).returns(configurations)
296+
297+
ActiveRecord::Tasks::DatabaseTasks.expects(:purge).
298+
with('database' => 'prod-db')
299+
300+
ActiveRecord::Tasks::DatabaseTasks.purge_current('production')
301+
end
302+
end
303+
304+
class DatabaseTasksPurgeAllTest < ActiveRecord::TestCase
305+
def test_purge_all_local_configurations
306+
configurations = {:development => {'database' => 'my-db'}}
307+
ActiveRecord::Base.stubs(:configurations).returns(configurations)
308+
309+
ActiveRecord::Tasks::DatabaseTasks.expects(:purge).
310+
with('database' => 'my-db')
311+
312+
ActiveRecord::Tasks::DatabaseTasks.purge_all
313+
end
314+
end
315+
288316
class DatabaseTasksCharsetTest < ActiveRecord::TestCase
289317
include DatabaseTasksSetupper
290318

0 commit comments

Comments
 (0)