Gem Of The Week: Seed Dump

Seed Dump

Generating seed data for testing and development purposes can be a time consuming frustration, especially if you’ve already got an app in production and need your hands on that data quickly.

Seed Dump is a gem that aims to solve that problem.

Seed Dump allows you to quickly create seed data files from the existing data within your database.

To get started, you need to install the gem (either with the terminal or through the Gemfile).

# Gemfile
gem 'seed_dump'

# Terminal
gem install seed_dump

Once installed you have access to a number of different ways to get your data into seed data files.

First method is a rake task that Seed Dump has made available post-installation:

rake db:seed:dump

this will dump all of your data directly into the db/seeds.rb file (overwriting any and all contents).

You can also specify different limitations on this rake task like so:

rake db:seed:dump MODELS=Product LIMIT=100 APPEND=true FILE=db/seeds/products.rb EXCLUDE=price

which will append only the first 100 records from the Product table onto the db/seeds/products.rb file (instead of overwriting it) and ensure that the resulting hash doesn’t include the “price” attribute.

For example, after running the above command you might see something like this in your db/seeds/products.rb file:

Product.create!([
    {name: "Product 1", brand: "Brand 1", description: "Product 1 is the hot new product from Brand 1!"},
    {name: "Product 2", brand: "Brand 1", description: "Product 2 is the hot new product from Brand 1 (even hotter than Product 1!)!"},
    …
    {name: "Product 100", brand: "Brand 1", description: "Product 100 is the hottest new product from Brand 1 (hotter than any other previous product!)!"}
])

Now that you’ve seen how to utilize the rake command it’s time to learn about the console features:

puts SeedDump.dump(Product)

this will list out, in the console, all of the necessary commands to recreate all of your Product data.

However, it may not be ideal to have all of that information in the console and you may rather have it written to a file — Seed Dump has your back.

SeedDump.dump(Product, file: 'db/seeds.rb')

which, similar to the rake command, will write all of your Product data to the db/seeds.rb file and overwrite its contents.

SeedDump.dump(Product, file: 'db/seeds/products.rb', append: true)

will write your Product information to the db/seeds/products.rb file and append the information instead of overwriting it.

Now, there are a few differences between the rake command and the console command.

First, the rake command requires a comma-separated list for the “exclude” flag whereas the console command requires an array

Second, the rake command can utilize the “limit” flag, but the console command cannot. However, the console can benefit from using ActiveRecord to achieve the same results:

SeedDump.dump(Product.limit(100), file: 'db/seeds/products.rb')

will limit the number of Product records to 100 in the output.

Lastly, the rake command can utilize the “models_exclude” flag, but the console command cannot.

And there you have it, a simple way of getting your data in a usable state so that you can test and develop easily.