padrino-framework icon indicating copy to clipboard operation
padrino-framework copied to clipboard

Add db tasks for dumping the database

Open postmodern opened this issue 8 years ago • 9 comments

I think it would be handy to add a db:dump task for creating a database dump.

postmodern avatar Dec 08 '15 23:12 postmodern

@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.

Quintasan avatar Oct 02 '16 19:10 Quintasan

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.

wakatara avatar Jun 04 '17 09:06 wakatara

@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

wakatara avatar Jun 04 '17 09:06 wakatara

Whoops, sorry those comments should have copied @Quintasan . Sorry for the extra traffic.

wakatara avatar Jun 04 '17 09:06 wakatara

@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 avatar Jun 04 '17 09:06 Quintasan

@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

wakatara avatar Jun 04 '17 09:06 wakatara

@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

wakatara avatar Jun 04 '17 09:06 wakatara

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 avatar Jun 14 '17 19:06 postmodern

@postmodern Post PoC? Just curious as to how it's different... =]

wakatara avatar Jun 15 '17 02:06 wakatara