-
-
Save beatep/2851207 to your computer and use it in GitHub Desktop.
Rake task for MySQL data import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :db do | |
namespace :data do | |
desc "Load data from sql script file (FILENAME=[data file]) into offices table" | |
task :load_mysql_dump => 'environment' do | |
environment = (ENV.include?("RAILS_ENV")) ? (ENV["RAILS_ENV"]) : 'development' | |
ENV["RAILS_ENV"] = RAILS_ENV = environment | |
database = Rails.configuration.database_configuration[Rails.env]["database"] | |
user = Rails.configuration.database_configuration[Rails.env]["username"] | |
password = Rails.configuration.database_configuration[Rails.env]["password"] | |
filename = ENV['FILENAME'] | |
raise "Please specify a source file (FILENAME=[source.sql])" if filename.blank? | |
puts "Connecting to #{environment}..." | |
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym) | |
puts "Truncating table offices..." | |
ActiveRecord::Base.connection.execute("truncate table offices") | |
puts "Importing data from #{filename}..." | |
sh "mysql -u#{user} -p#{password} #{database} < #{RAILS_ROOT}/#{filename}" | |
puts "Completed loading #{filename} into #{environment} environment." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment