Permalink
Browse files

add the ability to specify which models you want to run the export fo…

…r via an environment variable
  • Loading branch information...
1 parent 89616c9 commit 7f22a97fb2626589f0e1df03a0c726f08e865c66 @jondkinney jondkinney committed with Mar 2, 2010
Showing with 60 additions and 2 deletions.
  1. +9 −0 README.rdoc
  2. +51 −2 tasks/db_translate.rake
View
@@ -71,6 +71,15 @@ haven't broken migrations at some point, which *will* happen to you at some poin
Dump your database to fixtures. Stores them in RAILS_ROOT/production_data. You can then use this to load the data back into another database, even one of a different type. We've used this to move
data from SQL Server to MySQL and back again.
+Optionally you can specify only the models you want to dump data for by passing a list of model names as a comma delimited list to the MODELS environment variable.
+
+ For example, if I wanted to dump data for the User, Role, and RoleUsers models and not the rest of the models in my app, any of the following would work:
+
+` rake db:to_yaml MODELS=User,Role,RoleUser #Model names
+ rake db:to_yaml MODELS=user.rb,role.rb,role_user.rb #File names
+ rake db:to_yaml MODELS=user,role,role_user #File names minus their extension
+ rake db:to_yaml MODELS=users,roles,role_users #DB table names
+
=== rake db:from_yaml
Load fixtures from RAILS_ROOT/production_data into your database. Loads fixtures dumped by using rake db:export
@@ -88,23 +88,72 @@ namespace :db do
desc "Dump all data to the production_data folder"
task :to_yaml => :environment do
+ ############ This is to give a time calculation at the end ##################
+ require 'time'
+ include ActionView::Helpers::TextHelper
+
+ def seconds_fraction_to_time(seconds)
+ hours = 0
+ mins = 0
+ if seconds >= 60
+ mins = (seconds / 60).to_i
+ seconds = (seconds % 60 ).to_i
+
+ if mins >= 60 then
+ hours = (mins / 60).to_i
+ mins = (mins % 60).to_i
+ end
+ else
+ mins = 0
+ hours = 0
+ seconds = seconds.floor
+ end
+
+ total_time = ""
+ if hours > 0
+ total_time << pluralize(hours, 'hour') + ", "
+ end
+
+ if mins > 0
+ total_time << pluralize(mins, 'minute') + " and "
+ end
+
+ if seconds > 0
+ total_time << pluralize(seconds, 'second')
+ else
+ total_time << "0 seconds"
+ end
+
+ total_time
+ end
+ ################################################################################
+
path = RAILS_ROOT + "/production_data"
- models= Dir.glob("#{RAILS_ROOT}/app/models/*.rb").collect{|c| c.gsub("#{RAILS_ROOT}/app/models/", "").gsub(".rb", "").camelize}
+ models = ENV["MODELS"].split(",").collect{ |m| m.camelize.singularize.gsub(".rb", "")} if ENV["MODELS"]
+ models ||= Dir.glob("#{RAILS_ROOT}/app/models/*.rb").collect{|c| c.gsub("#{RAILS_ROOT}/app/models/", "").gsub(".rb", "").camelize}
+
+ start_time = Time.now
+
FileUtils.mkdir_p path rescue nil
models.each do |m|
begin
+ this_models_start_time = Time.now
+
object = m.constantize
puts "[DB] Dumping data for #{object}"
str = object.to_yaml
write_file "#{path}/#{object.table_name}.yml", str
# get the association data for has_and_belongs_to_many
habtm_fixtures(object)
+
+ puts "[TIME] It took #{seconds_fraction_to_time(Time.now - this_models_start_time.to_time)} to export the data for the #{m} model"
rescue
- "skipping - not a model"
+ puts "[ERROR] skipping '#{m}' - not a model"
end
end
+ puts "[TIME] It took #{seconds_fraction_to_time(Time.now - start_time.to_time)} total to export the data your requested"
end

0 comments on commit 7f22a97

Please sign in to comment.