Intro
Phases 3 and 4 at Flatiron School's software engineering program are based on Ruby and Rails, respectively. During the past couple of phases, we've learned a lot about gems and how beneficial they can be.
A Ruby gem is basically a small program you can insert into the application you're creating in order to make your life easier. For example, installing the BCrypt gem allows you to use incredible password encryption nearly effortlessly. If you were to build this yourself, it would be its own, large program. Since it's a gem, we don't need to reinvent the wheel, we can install the gem and use the program effectively with a few lines of code.
One of the gems that was covered in class is Faker. Faker is fantastic because it allows you to create and show off fake records with real-looking data. It's helpful for both practical testing, but also for taking screenshots and/or video for your amazing readme or demo.
Seed Dump
I want to highlight another gem I found while working on my phase 4 project, which I had previously never heard of. This gem is called Seed Dump.
What makes Seed Dump different from Faker is that, instead of using the gem to create fictional data, which has its place, Seed Dump allows you to boost your seed file with some of the data you've already been testing on.
In my most recent project, I have full CRUD on one object, CRD on two objects, and CR on another. Since there are a lot of interactions that take place between these objects, testing is actually easiest while using my own app. It's easiest to test while actually creating a new user, a new post, viewing a community, deleting a comment, etc.
During the testing of this functionality, you're adding data to your database. Rather than resetting your database, what if you utilized the information you entered? What if this is how you could build your seed file, rather than creating it from scratch? Or what if you do some testing and just want to add a couple of live examples to your seed file? Enter Seed Dump.
Installation
Installing Seed Dump is as easy as installing other gems you're used to. Simply add it to your Gemfile by adding:
gem 'seed_dump'
and then running bundle install
.
You may also install it on the CLI by typing:
gem install seed_dump
Utilization
There are a few things you can do with Seed Dump that I'll discuss here. Most notably, you can dump your entire current database into the seed file, you can add the current database entries to your existing seed file, you can dump the existing database to a separate file, you can exclude certain tables, and you can limit the results.
Let's look at a just a few examples of how easy Seed Dump is to use.
Dumping the Current DB to Seed File
To use the following commands, ensure your server is currently shut down.
If you simply want to dump the entire existing database directly into your db/seeds.rb file, you can run the following command:
rake db:seed:dump
Note: If your seed file already has data in it, it will be overwritten. Only use this option if your db/seeds.rb file is completely empty.
Adding the Current DB to Seed File
This is my favorite use case for Seed Dump. Generally speaking, when I start building, I'll have to create a few custom entries in db/seeds.rb just to get started. However, after setting up a few models, I'm ready to do testing on a larger scale, and the new database entries come fast. This is where adding more entries to your seed file becomes helpful.
If you've already created the entries, and you know you'll need to reset the database for x or y reason(s), why not incorporate the current test database you've created directly to your seed file? I found this incredibly useful. In order to do so, you simply run:
rake db:seed:dump APPEND=true
Note: Seed Dump will create objects alphabetically, so if you have objects that use foreign keys, you may need to rearrange the order in which objects are being created.
Saving Current DB to Separate File
If you'd prefer to save the database you've been testing to a separate seed file instead of db/seeds.rb, there's a short two step process for you to follow.
First, you need to create a new file that you want to save the existing database to. For example, if I wanted to save my database to create db/second-db.rb, I just need to create the second-db.rb file within the db folder. After that, I just need to run the following command to dumb the entire existing database into that file:
rake db:seed:dump FILE=db/second-db.rb
Voila, the current database has been saved into a separate file.
Conclusion
When I was working on this project, I came across this gem naturally. I had the feeling that some other coder out there in the world had the same idea as I had -- to easily create a seed file based on the existing database. Sure enough, after a quick Google search, I found Seed Dump. I immediately installed and utilized it myself.
There are multiple ways to create a good seed file, including both Faker and Seed Dump. For me, Seed Dump was too easy not to use, and worked so well for my latest project. Now that I'm about to be working on my capstone project, I can guarantee you that Seed Dump will be installed early on to help me with my testing. I hope it helps you too.
Happy coding.
Top comments (0)