Coder Social home page Coder Social logo

pguegan / seedbank Goto Github PK

View Code? Open in Web Editor NEW

This project forked from james2m/seedbank

0.0 2.0 0.0 430 KB

Seedbank gives your Rails seed data a little structure. Create seeds for each environment, share seeds between environments and specify dependencies to load your seeds in order. All nicely integrated with simple rake tasks.

License: MIT License

Ruby 100.00%

seedbank's Introduction

Seedbank

Seedbank allows you to structure your Rails seed data instead of having it all dumped into one large file. I find my seed data tended to fall into two categories. 1. Stuff that the entire application requires. 2. Stuff to populate my development and staging environments.

Seedbank assumes common seed data is under db/seeds and any directories under db/seeds/ are specific to an environment, so db/seeds/development is contains all your development only seed data.

The reason behind Seedbank is laziness. When I checkout or re-visit a project I don't want to mess around getting my environment setup I just want the code and a database loaded with data in a known state. Since the Rails core team were good enough to give us rake db:setup it would be rude not to use it.

rake db:setup  # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)

To achieve this slothful aim Seedbank renames the original db:seed rake task to db:seed:original, makes it a dependency for all the Seedbank seeds and adds a new db:seed task that loads all the common seeds in db/seeds plus all the seeds for the current Rails environment.

Example

Seedbank seeds follow this structure;

db/seeds/
  bar.seeds.rb
  development/
    users.seeds.rb
  foo.seeds.rb

This would generate the following Rake tasks

rake db:seed                    # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the current environment in Rails.env.
rake db:seed:bar                # Load the seed data from db/seeds/bar.seeds.rb
rake db:seed:common             # Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb.
rake db:seed:development        # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/development/*.seeds.rb.
rake db:seed:development:users  # Load the seed data from db/seeds/development/users.seeds.rb
rake db:seed:foo                # Load the seed data from db/seeds/foo.seeds.rb
rake db:seed:original           # Load the seed data from db/seeds.rb

Therefor assuming RAILS_ENV is not set or is 'development'

$ rake db:seed

would load the seeds in db/seeds.rb, db/seeds/bar.seeds.rb, db/seeds/foo.seeds/rb and db/seeds/development/users.seeds.rb. Whereas

$ RAILS_ENV=production db:seed

would load the seeds in db/seeds.rb, db/seeds/bar.seeds.rb and db/seeds/foo.seeds/rb

Installation

Rails 3.x

Add the seedbank gem to your Gemfile. In Gemfile:

gem "seedbank"

That's it!

Rails 2.x

Add to your config/environment.rb

config.gem 'seedbank'

Install the gem;

$ rake gems:install

Then in the bottom of your application's Rakefile:

require 'seedbank'
Seedbank.load_tasks if defined?(Seedbank)

If you vendor the gem you'll need to change the require to the specific path.

Usage

Seeds files are just plain old Ruby executed in your rails application environment so anything you could type into the rails console will work in your seeds. Seeds files have to be named with the '.seeds.rb' extension.

db/seeds/companies.seeds.rb

Company.find_or_create_by_name('Hatch', :url => 'http://thisishatch.co.uk' )

The seed files under db/seeds are run first in alphanumeric order followed by the ones in the db/seeds/RAILS_ENV. You can add dependencies to your seed files to enforce the run order. for example;

db/seeds/users.seeds.rb

after :companies do
  company = Company.find_by_name('Hatch')
  company.users.create(:first_name => 'James', :last_name => 'McCarthy')
end

db/seeds/projects.seeds.rb

after :companies do
  company = Company.find_by_name('Hatch')
  company.projects.create(:title => 'Seedbank')
end

db/seeds/tasks.seeds.rb

after :projects, :users do
  project = Project.find_by_name('Seedbank')
  user = User.find_by_first_name_and_last_name('James', 'McCarthy')
  project.tasks.create(:owner => user, :title => 'Document seed dependencies in the README.md')
end

If the dependencies are in one of the environment folders, you need to namespace the parent task:

db/seeds/development/users.seeds.rb

after "development:companies" do
  company = Company.find_by_name('Hatch')
  company.users.create(:first_name => 'James', :last_name => 'McCarthy')
end

Note - If you experience any errors like Don't know how to build task 'db:seed:users'. Ensure your specifying after 'development:companies' like the above example. This is the usual culprit (YMMV).

Contributors

git log | grep Author | sort | uniq
  • Ahmad Sherif
  • Andy Triggs
  • Corey Purcell
  • James McCarthy
  • Joost Baaij
  • Justin Smestad
  • Peter Suschlik
  • Philip Arndt
  • Tim Galeckas
  • lulalala
  • pivotal-cloudplanner
  • vkill

Note on Patches/Pull Request

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore it when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2011 James McCarthy, released under the MIT license

seedbank's People

Contributors

james2m avatar pivotal-cloudplanner avatar andyt avatar coreypurcell avatar tilsammans avatar jsmestad avatar splattael avatar parndt avatar pguegan avatar timgaleckas avatar lulalala avatar vkill avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.