Created
November 23, 2008 12:31
-
-
Save xwmx/28104 to your computer and use it in GitHub Desktop.
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 | |
desc "Drop then recreate the dev database, migrate up, and load fixtures" | |
task :remigrate => :environment do | |
return unless %w[development test staging demo].include? Rails.env | |
ActiveRecord::Base.connection.tables.each { |t| ActiveRecord::Base.connection.drop_table t } | |
Rake::Task["db:migrate"].invoke | |
ENV['FIXTURES_PATH'] = "db/data/#{RAILS_ENV}" unless (ENV['FIXTURES_PATH'] || Rails.env.test?) | |
Rake::Task["db:fixtures:load"].invoke | |
end | |
namespace :fixtures do | |
desc 'Dump a database to yaml fixtures. Set environment variables DB | |
and DEST to specify the target database and destination path for the | |
fixtures. DB defaults to the current environment and DEST defaults to | |
db/data/#{Rails.env}.' | |
task :dump => :environment do | |
path = ENV['DEST'] || "#{RAILS_ROOT}/db/data/#{Rails.env}" | |
db = ENV['DB'] || Rails.env.to_s | |
sql = 'SELECT * FROM %s' | |
ActiveRecord::Base.establish_connection(db) | |
ActiveRecord::Base.connection.select_values('show tables').each do |table_name| | |
i = '000' | |
File.open("#{path}/#{table_name}.yml", 'wb') do |file| | |
file.write ActiveRecord::Base.connection.select_all(sql % table_name).inject({}) { |hash, record| | |
hash["#{table_name}_#{i.succ!}"] = record | |
hash | |
}.to_yaml | |
end | |
end | |
end | |
namespace :env do | |
desc 'Dump demo data to fixtures' | |
task :dump => :environment do | |
Dir.mkdir("#{Rails.root}/db/data/#{Rails.env}") unless File.exists?("#{Rails.root}/db/data/#{Rails.env}") | |
Rake::Task["db:fixtures:dump"].invoke | |
end | |
end | |
namespace :initial do | |
task :load => :environment do | |
env = Rails.env.eql?('demo') ? 'demo' : 'bootstrap' | |
ENV['FIXTURES_PATH'] = "db/data/#{env}" unless (ENV['FIXTURES_PATH'] || Rails.env.test?) | |
Rake::Task["db:fixtures:load"].invoke | |
end | |
end | |
end | |
namespace :mysql do | |
def sh_mysql(config, command) | |
returning '' do |mysql| | |
mysql << command << ' ' | |
mysql << "-u#{config['username']} " if config['username'] | |
mysql << "-p#{config['password']} " if config['password'] | |
mysql << "-h#{config['host']} " if config['host'] | |
mysql << "-P#{config['port']} " if config['port'] | |
mysql << "--socket=#{config['socket']} " if config['socket'] | |
mysql << "--no-create-info" if mysql_command == 'mysqldump' | |
mysql << config['database'] if config['database'] | |
end | |
end | |
def mysql_command() 'mysql' end | |
def mysqldump_command() 'mysqldump' end | |
desc "Launch mysql shell. Use with an environment task (e.g. rake production mysql)" | |
task :console do | |
system(sh_mysql(YAML.load(open(File.join('config', 'database.yml')))[RAILS_ENV], mysql_command)) | |
end | |
desc "Dump mysql table data. Use with an environment task (e.g. rake production mysqldump)" | |
task :dump do | |
ENV['save_path'] ||= "#{RAILS_ROOT}/db/data/#{Rails.env}_data.sql" | |
system("#{sh_mysql(YAML.load(open(File.join('config', 'database.yml')))[RAILS_ENV], mysqldump_command)} > #{ENV['save_path']}") | |
end | |
desc "Load table data dump into database. Use with an environment task (e.g. rake production mysql_import)" | |
task :import do | |
ENV['dump_path'] ||= "#{RAILS_ROOT}/db/data/#{Rails.env}_data.sql" | |
Rake::Task['db:purge'].invoke unless %w[production].include? RAILS_ENV | |
system("#{sh_mysql(YAML.load(open(File.join('config', 'database.yml')))[RAILS_ENV], mysql_command)} < #{ENV['dump_path']}") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment