Skip to content

Instantly share code, notes, and snippets.

@brianmario
Created July 21, 2009 18:50
Show Gist options
  • Save brianmario/151513 to your computer and use it in GitHub Desktop.
Save brianmario/151513 to your computer and use it in GitHub Desktop.
# Override the default db:drop and db:create Rake tasks to fix bug with using a database driver
# other than mysql, postgres or sqlite.
namespace :db do
task :create do
@charset = ENV['CHARSET'] || 'utf8'
@collation = ENV['COLLATION'] || 'utf8_general_ci'
config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
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)
rescue Exception => e
puts "#{config['database']} already exists"
end
end
task :drop do
config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
begin
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.drop_database config['database']
rescue Exception => e
puts "Couldn't drop #{config['database']} : #{e.inspect}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment