padrino-framework
padrino-framework copied to clipboard
Add db tasks for dumping the database
I think it would be handy to add a db:dump
task for creating a database dump.
@postmodern I'd like to try implementing that but I'm not sure where to start. At first I considered making a per-ORM Rake task but maintaining that seems like a pain.
Actually, I may be able to help with this since I have a rake task that already does this, so turning it into a generator should be fairly trivial... you just need to iterate over each model in the application. Wanna tag team on it @postmodern ? Example code here in my Rakefile:
require 'bundler/setup'
require 'padrino-core/cli/rake'
PadrinoTasks.use(:database)
PadrinoTasks.use(:activerecord)
PadrinoTasks.init
task :default => :test
namespace :export do
desc "Dumps all models to seed format"
task :seeds_dump => :environment do
Account.order(:id).all.each do |account|
puts "Account.create(#{account.serializable_hash.delete_if {|key, value| ['created_at','updated_at'].include?(key)}.to_s.gsub(/[{}]/,'')})"
end
Species.order(:id).all.each do |species|
puts "Species.create(#{species.serializable_hash.delete_if {|key, value| ['created_at','updated_at'].include?(key)}.to_s.gsub(/[{}]/,'')})"
end
Org.order(:id).all.each do |org|
puts "Org.create(#{org.serializable_hash.delete_if {|key, value| ['created_at','updated_at'].include?(key)}.to_s.gsub(/[{}]/,'')})"
end
Program.order(:id).all.each do |tmp|
puts "Program.create(#{tmp.serializable_hash.delete_if {|key, value| ['created_at','updated_at'].include?(key)}.to_s.gsub(/[{}]/,'')})"
end
Tracking.order(:id).all.each do |tracking|
puts "Tracking.create(#{tracking.serializable_hash.delete_if {|key, value| ['created_at','updated_at'].include?(key)}.to_s.gsub(/[{}]/,'')})"
end
Assessment.order(:id).all.each do |assessment|
puts "Assessment.create(#{assessment.serializable_hash.delete_if {|key, value| ['created_at','updated_at'].include?(key)}.to_s.gsub(/[{}]/,'')})"
end
Country.order(:id).all.each do |country|
puts "Country.create(#{country.serializable_hash.delete_if {|key, value| ['created_at','updated_at'].include?(key)}.to_s.gsub(/[{}]/,'')})"
end
Photo.order(:id).all.each do |photo|
puts "Photo.create(#{country.serializable_hash.delete_if {|key, value| ['created_at','updated_at'].include?(key)}.to_s.gsub(/[{}]/,'')})"
end
end
end
Anyhow, you get the idea... should not be too tricky with that code. it's the same thing over and over again for each model, so easy to throw into a generator (or have as part of a starting rakefile... I just didn't have time to refactor it when I needed it done and therafter I was mostly grabbing prod data off heroku and just overwriting my local postgres after we launched.
@postmodern I think the tricky bit off the top if my head is that I'm not sure how you make AR give you a list of all models to iterate through, but after that it's pretty much what you have in the code example I just gave you. // @ujifgc
Whoops, sorry those comments should have copied @Quintasan . Sorry for the extra traffic.
@wakatara I totally forgot about this. And yeah, the main problem is how to get a list of all models to iterate through. While AR may have a list of these somewhere I'm not sure about other ORMs
@Quintasan Actually, there is a pretty simple way to get it out of ActiveRecord, so yeah... I think this works easy for AR with ActiveRecord::Base.send(:subclasses).map(&:name)
to get all models (try it in padrino console
- works the charm.).
I have to embarrassingly admit, I was thinking... oh, it works for AR! And forgot about all the other DBs.
Good news is, we already have a way to do this now for ActiveRecord. What are the other ORMs we need to support again?... =p
// @postmodern
@Quintasan Sequel supports db_schema() as a call, so that seems like it is just a simple matter of iterating over the returned hash for the top level key names.).
In any case, let's start with AR and add that in as a "bonus" task for people using AR and figure out how to add in the rest as we go along. =]
// @postmodern
I've been tinkering with a different approach: generic rake db tasks that call out to mysql/psql/sqlite3. This way we can dump/restore the full DB and can reuse them across any project.
@postmodern Post PoC? Just curious as to how it's different... =]