Added db:drop:all to drop all databases declared in config/database.yml [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7489 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 148202d4
*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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册