Skip to content

Instantly share code, notes, and snippets.

@zbelzer
Created March 24, 2010 19:05
Show Gist options
  • Save zbelzer/342654 to your computer and use it in GitHub Desktop.
Save zbelzer/342654 to your computer and use it in GitHub Desktop.
namespace :db do
desc 'Dumps your current schema and data from the database into a timestamped .sql dumpfile'
task :dump => :environment do
puts "Dumping schema and data from #{@data_reload_helper.db_name} to #{@data_reload_helper.dump_path}"
# -c Includes the column names in the insert statement. Important when rails reorders your columns on different machines
@data_reload_helper.mysqldump('--add-drop-table -c')
end
desc 'Dumps your current schema and data from the database into a timestamped, gzipped .sql dumpfile'
task :dump_deflated => ['dump', 'dump:deflate']
namespace :dump do
desc 'Dumps your current data from the database into a timestamped .sql dumpfile'
task :data => :environment do
puts "Dumping data from #{@data_reload_helper.db_name} to #{@data_reload_helper.dump_path}"
# -c Includes the column names in the insert statement. Important when rails reorders your columns on different machines
# -n Drops the 'create db' portions of the dump file
# -t Drops creation info from the dump
@data_reload_helper.mysqldump('-n -t -c')
end
desc 'Dumps your current data from the database into a timestamped, gzipped .sql dumpfile'
task :data_deflated => [:data, :deflate]
desc "Gzip's your most recent dump"
task :deflate => :environment do
latest = @data_reload_helper.latest_dump_file_name
compressed_latest = @data_reload_helper.gzipify(latest)
if latest.nil?
puts "No files to compress"
elsif File.exists?(compressed_latest)
puts "#{latest} already compressed to #{compressed_latest}"
else
puts "Compressing #{latest} to #{compressed_latest}"
writer = Zlib::GzipWriter.open(compressed_latest)
writer.write File.read(latest)
writer.close
end
end
end
desc 'Reloads your database from dump file dump.out if it exists, and migrates up'
task :reload => [:drop, :create] do
@data_reload_helper.do_reload { |latest_file_name| @data_reload_helper.mysql("#{@data_reload_helper.db_name} < #{latest_file_name}") }
end
desc 'Reloads your database data from a dump file'
task :reload_data => :environment do
@data_reload_helper.do_reload { |latest_file_name| @data_reload_helper.mysql("#{@data_reload_helper.db_name} < #{latest_file_name}") }
end
end
class DataReloadHelper
def db; ActiveRecord::Base.configurations[RAILS_ENV] end
def db_name; db["database"] end
def db_user; db["username"] end
def db_pass; db["password"] end
def gzipify(name)
"#{name}.gz"
end
# Get the file name as blah_dev_dump_YYYYMMDDHHMMSS
def dump_path; File.join("db", "#{db_name}_dump_#{Time.now.strftime('%Y%m%d%H%M%S')}.sql"); end
def compressed_dump_path; gzipify(dump_path); end
def dump_query(path=nil, name_format=nil)
path ||= 'db'
name_format ||= "#{db_name}_dump"
File.join(path, "#{name_format}*.sql")
end
def mysql(arg_string)
system("mysql #{credential_args} #{arg_string}")
end
def mysqldump(options='')
system("mysqldump #{db_name} #{options} #{credential_args} > #{dump_path}")
dump_path
end
def credential_args
"-u #{db_user} --password=#{db_pass}"
end
def latest_dump_file_name(path=nil, name_format=nil)
Dir[dump_query(path, name_format)].sort.last
end
def do_reload(path=nil, file_name_format=nil, &block)
latest = ENV['dump_file'] || latest_dump_file_name(path, file_name_format)
unless latest.blank?
puts "Reloading #{db_name} from #{latest}"
block.call(latest)
else
puts "Dump file for #{db_name} not found at #{dump_query(path, file_name_format)}"
end
end
end
@data_reload_helper = DataReloadHelper.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment