Permalink
Browse files

Added db:drop:all to drop all databases declared in config/database.y…

…ml [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7489 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Sep 15, 2007
1 parent 148202d commit 694f15758c5af88f6d288d72650e2fcd2cfccfe7
Showing with 24 additions and 11 deletions.
  1. +2 −0 railties/CHANGELOG
  2. +22 −11 railties/lib/tasks/databases.rake
View
@@ -1,5 +1,7 @@
*SVN*
+* Added db:drop:all to drop all databases declared in config/database.yml [DHH]
+
* Use attribute pairs instead of the migration name to create add and remove column migrations. Closes #9166 [lifofifo]
For example:
@@ -28,7 +28,6 @@ namespace :db do
ActiveRecord::Base.establish_connection(config.merge({'database' => nil}))
ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation})
ActiveRecord::Base.establish_connection(config)
- p "MySQL #{config['database']} database succesfully created"
rescue
$stderr.puts "Couldn't create database for #{config.inspect}"
end
@@ -47,19 +46,20 @@ namespace :db do
end
end
- desc 'Drops the database for the current environment'
- task :drop => :environment do
- config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
- case config['adapter']
- when 'mysql'
- ActiveRecord::Base.connection.drop_database config['database']
- when /^sqlite/
- FileUtils.rm_f File.join(RAILS_ROOT, config['database'])
- when 'postgresql'
- `dropdb "#{config['database']}"`
+ namespace :drop do
+ desc 'Drops all the local databases defined in config/database.yml'
+ task :all => :environment do
+ ActiveRecord::Base.configurations.each_value do |config|
+ drop_database(config)
+ end
end
end
+ desc 'Drops the database for the current RAILS_ENV'
+ task :drop => :environment do
+ drop_database(ActiveRecord::Base.configurations[RAILS_ENV || 'development'])
+ end
+
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
task :migrate => :environment do
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
@@ -266,6 +266,17 @@ namespace :db do
end
end
+def drop_database(config)
+ case config['adapter']
+ when 'mysql'
+ ActiveRecord::Base.connection.drop_database config['database']
+ when /^sqlite/
+ FileUtils.rm_f(File.join(RAILS_ROOT, config['database']))
+ when 'postgresql'
+ `dropdb "#{config['database']}"`
+ end
+end
+
def session_table_name
ActiveRecord::Base.pluralize_table_names ? :sessions : :session
end

0 comments on commit 694f157

Please sign in to comment.