def create_local_database(config)
if config[:host] == 'localhost' || config[:host].blank?
begin
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection
rescue
case config[:adapter]
when 'mysql'
@charset = ENV['CHARSET'] || 'utf8'
@collation = ENV['COLLATION'] || 'utf8_general_ci'
begin
ActiveRecord::Base.establish_connection(config.merge({:database => nil}))
ActiveRecord::Base.connection.create_database(config[:database], {:charset => (config[:charset] || @charset), :collation => (config[:collation] || @collation)})
ActiveRecord::Base.establish_connection(config)
puts "MySQL #{config[:database]} database successfully created"
rescue
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config[:charset] || @charset}, collation: #{config[:collation] || @collation} (if you set the charset manually, make sure you have a matching collation)"
end
when 'postgresql'
`createdb "#{config[:database]}" -E utf8`
when 'sqlite'
`sqlite "#{config[:database]}"`
when 'sqlite3'
`sqlite3 "#{config[:database]}"`
end
else
puts "#{config[:database]} already exists"
end
else
puts "This task only creates local databases. #{config[:database]} is on a remote host."
end
end