Predictable seed

Hey there,

In many projects, I guess like everyone, we’re using the seed file to generate a bunch of models with random data.

Obviously, after running db:seed task, each developer has a “different” database. Not cool.

If we need to sync those data, we prefix seed.rb code with something like


generator = Rand.new(1234567890)

and replace all “rand” calls by “generator.rand”

As we do that in many projects, I could write a gem which would override the Railties load_seed method and insert a fixed srand call before seed.rb load, so all rand calls during seed will be “predictable”.

But is it make sense to make a Railties pull request with this (with a config options to enable/disable “predictable” seed) ?

Something like :


def load_seed

srand(config.predictable_seed) if config.predictable_seed

seed_file = paths["db/seeds.rb"].existent.first

load(seed_file) if seed_file

ensure

srand if config.predictable_seed

end

Thoughts ?

Thank you

Ben

Interesting idea. If what you’re after is consistent data, why are you randomizing anything?

A consistent database … but with some random fields :wink:

I guess, it’s simpler to run a seed with

10000.times { User.create! :first_name => Faker::Name.first_name }

than loading an hardcoded file with 10000 users.

Ben

I hope this does not feel like a plug:

https://github.com/metaskills/named_seeds

There is a talk I have around this gem/concept I have been using for several years. The general idea is:

  • Factories generate Valid Garbage™ which is 100% random.

  • Test fixtures take advantage of said factories but are explicit in their IDs and properties.

This idea is to have a minimal fixture set (not driven by YAML) allowing ad-hoc tests to create valid garbage as needed for testing purposes. NamedSeeds even allows these fixture personas to populate your development DB.

I realize this is slightly tangential to the way you are doing things, but I wanted to share. Either way I kind of believe that Rails is doing it right now and solutions like these are best done in gems and projects like ours. Hope it is helpful.

  • Cheers,

Ken