Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[feature-request] db seed files for each environment.

#4908

I keep running into scenarios where I need to seed the database of the current environment differently. It would be very useful, and somewhat trivial to support seed files for each environment. One could still have the usual db/seeds.rb file for all environments. Additionally, one could also have db/seeds/production.rb, db/seeds/development.rb and db/seeds/test.rb.

Reported by Postmodern · June 20th, 2010 @ 12:33 AM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Postmodern
  2. Ryan Bigg
    Ryan Bigg
    • State changed from new to needs-more-info

    You'll need to add tests if you want this in. I'm on the fence about this. The data I load to test things in development is exactly the same data I use in production. What use case is there for having two separate seed files?

    June 21st, 2010 @ 04:04 AM

  3. Postmodern
    Postmodern

    Alright, I will get on writing tests.

    One common scenario for having multiple seeds files, is using a very large and arbitrary data-set for development, in order to test the performance of complex queries.

    The other scenario I recently ran into was designing an app that used a shared production database, where certain tables were populated by other backend python scripts; maintained by another developer. In my testing and development databases, which I controlled, I still needed to load in a canned snapshot of the data from the production database.

    Finally, this would mirror the same file organization used by config/environment.rb and config/environments.

    June 21st, 2010 @ 04:15 AM

  4. Postmodern
    Postmodern

    Where would I write tests against active_record/railties/database.rake? Most of the tests in activerecord appear to be testing the ORM code.

    June 21st, 2010 @ 04:21 AM

  5. Robert Pankowecki
    Robert Pankowecki
    • Importance changed from to Low

    What use case is there for having two separate seed files?

    Keeping seed.rb with data that must be in application to run properly ex. some dictionaries. Keeping data that makes writing tests easier in seeds/test.rb (works like fixtures for me). Keeping data that makes using the application easier in seeds/development.rb ex. few more users, some objects already created, permissions already set and so on...

    I hope that this feature will be implemented :-)

    August 9th, 2010 @ 03:42 PM

  6. CodeOfficer
    CodeOfficer

    I'd also love to see an environment based solution as there are 2 ways that I tend to use seeds:

    1) seeding my development environment with lots of dummy data to flush out views
    2) seeding production on deploy with data the app needs to run

    I dont use seed files at all in the testing environment.

    September 30th, 2010 @ 04:26 AM

  7. David Trasbo
    David Trasbo
    • State changed from needs-more-info to wontfix

    My personal opinion: -1

    Why not just use if statements instead?

    if Rails.env.development?
     # ...
    end
    
    if Rails.env.production?
      # ...
    end
    
    # So on, so forth
    

    If scalability is the issue, you could move environment specific seeds into a separate file and require it:

    if Rails.env.development?
      require 'db/seeds/development'
    end
    
    # So on.
    

    Will mark this as won't fix for now. It's not really my decision, but I'll reopen if necessary.

    October 10th, 2010 @ 06:25 PM

  8. Postmodern
    Postmodern

    Hmm, or we could use:

    require File.join('db','seeds',Rails.env)
    

    October 11th, 2010 @ 12:14 AM

  9. james2m
    james2m

    @postmodern If it's useful to you, I put together a little gem to do this Seedbank. It lets you have seeds for different environments.

    April 4th, 2011 @ 04:58 PM