aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/tasks/databases.rake33
2 files changed, 24 insertions, 11 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index bd90449127..c624a1b41a 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -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:
diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake
index a761689932..ef2c25663a 100644
--- a/railties/lib/tasks/databases.rake
+++ b/railties/lib/tasks/databases.rake
@@ -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