Skip to content

Commit

Permalink
Add TABLES param to db:seed:dump task
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Sep 4, 2013
1 parent 7b4cd2f commit 8021a74
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/tasks/seed_dumper.rake
@@ -1,9 +1,14 @@
namespace :db do
namespace :seed do
desc 'Dump records from the database into db/seeds.rb'
desc 'Dump records from the database into db/seeds.rb (param TABLES e.g.: TABLES=people,roles)'
task :dump => :environment do

all_tables = ActiveRecord::Base.connection.tables
tables = ENV['TABLES'].split(",") if ENV['TABLES'].present?
tables_to_dump = all_tables && (tables.nil? ? all_tables : tables)

Dir.glob(Rails.root + '/app/models/*.rb').each { |file| require file }
models = Rails::VERSION::MAJOR >= 3 ? ActiveRecord::Base.connection.tables.map {|t| t.classify.constantize rescue nil}.compact : Object.subclasses_of(ActiveRecord::Base)
models = Rails::VERSION::MAJOR >= 3 ? tables_to_dump.map {|t| t.classify.constantize rescue nil}.compact : Object.subclasses_of(ActiveRecord::Base)

models.each { |model| SeedDumper::Writer.write_data(model.name, SeedDumper::Fetcher.fetch_data(model) ) }
end
Expand Down

0 comments on commit 8021a74

Please sign in to comment.