Skip to content

Rails 3 task to dump (parts) of your database to db/seeds.rb

License

Notifications You must be signed in to change notification settings

razenha/seed_dump

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SeedDump

Seed dump is a simple plugin that adds a rake task named db:seed:dump.

It allows you to create a db/seeds.rb from your existing data in the database. When there is no data in the database it will generate empty create statements.

It mainly exists for people who are too lazy writing create statements in db/seeds.rb themselves and need something (seed_dump) to dump data from the table(s) into seeds.rb

Example Usage

Dump all data directly to db/seeds.rb:

rake db:seed:dump

Dump only data from the users and products table and dump a maximum amount of 2 records:

$ rake db:seed:dump MODELS=User,Product LIMIT=2

Result:

$ cat db/seeds.rb 
# Autogenerated by the db:seed:dump task
# Do not hesitate to tweak this to your needs

products = Product.create([
  { :category_id => 1, :description => "Long Sleeve Shirt", :name => "Long Sleeve Shirt" },
  { :category_id => 3, :description => "Plain White Tee Shirt", :name => "Plain T-Shirt" }
])

users = User.create([
  { :id => 1, :password => "123456", :username => "test_1" },
  { :id => 2, :password => "234567", :username => "tes2" }
])

Append to db/seeds.rb instead of overwriting it:

rake db:seed:dump APPEND=true

Use another output file instead of db/seeds.rb

rake db:seed:dump FILE=db/categories.rb

By default the :id column will not be added to the generated Create statements If you do want the :id to be included use WITH_ID:

rake db:seed:dump WITH_ID=1

If you don’t want seed_dump to dump any data allready available in the database use NO_DATA.

This will generate the dump with only 1 empty create statement. It’s up to you to edit these and change the values into something meaningful:

rake db:seed:dump MODEL=User NO_DATA=1 APPEND=true

Here is a full example using all of the options above:

rake db:seed:dump MODELS=Category LIMIT=10 APPEND=true FILE=db/categories.rb WITH_ID=1 NO_DATA=1

All environment variables

APPEND:

Append the data to db/seeds.rb instead of overwriting it.

FILE:

Use a different output file, default: db/seeds.rb

LIMIT:

Dump no more then this amount of data, default: no limit

MODEL(S):

A model name or a comma seperated list of models, default: all models

NO_DATA:

Don't dump any data from the db, instead generate empty Create options

WITH_ID:

Inlcude the +:id+ in the create options

Copyright © 2010 Rob Halff, released under the MIT license

About

Rails 3 task to dump (parts) of your database to db/seeds.rb

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%