GithubHelp home page GithubHelp logo

myobie / seed-fu Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mbleigh/seed-fu

1.0 2.0 0.0 299 KB

Advanced seed data handling for Rails, combining the best practices of several methods together.

License: MIT License

seed-fu's Introduction

Seed Fu

Seed Fu is an attempt to once and for all solve the problem of inserting and maintaining seed data in a database. It uses a variety of techniques gathered from various places around the web and combines them to create what is hopefully the most robust seed data system around.

Warning: API Changes

Version 2.0.0 of Seed Fu introduces API changes. Seed::Writer has been completely overhauled and will require you to update your scripts. Some other deprecations have been introduced, but the methods will not be removed until version 2.1.0. Please see the CHANGELOG for details.

The API documentation is available in full at http://rubydoc.info/github/mbleigh/seed-fu/master/frames.

Basic Example

In db/fixtures/users.rb

User.seed do |s|
  s.id    = 1
  s.login = "jon"
  s.email = "[email protected]"
  s.name  = "Jon"
end

User.seed do |s|
  s.id    = 2
  s.login = "emily"
  s.email = "[email protected]"
  s.name  = "Emily"
end

To load the data:

$ rake db:seed_fu
== Seed from /path/to/app/db/fixtures/users.rb
 - User {:id=>1, :login=>"jon", :email=>"[email protected]", :name=>"Jon"}
 - User {:id=>2, :login=>"emily", :email=>"[email protected]", :name=>"Emily"}

Installation

Rails 3

Just add gem 'seed-fu' to your Gemfile

Active Record 3

Seed Fu depends on Active Record, but doesn't have to be used with a full Rails app. Simply load and require the seed-fu gem and you're set.

Rails 2.3

The current version is not backwards compatible with Rails 2.3. Please use Seed Fu version 1.2.3.

Rails 3.1 (beta)

As above, but install the gem which has '.rails31' prepended to its version string. For example, instead of using seed-fu version 2.0.1, use seed-fu version 2.0.1.rails31.

Constraints

Constraints are used to identify seeds, so that they can be updated if necessary. For example:

Point.seed(:x, :y) do |s|
  s.x = 4
  s.y = 7
  s.name = "Home"
end

The first time this seed is loaded, a Point record will be created. No suppose the name is changed:

Point.seed(:x, :y) do |s|
  s.x = 4
  s.y = 7
  s.name = "Work"
end

When this is run, Seed Fu will look for a Point based on the :x and :y constraints provided. It will see that a matching Point already exists and so update its attributes rather than create a new record.

If you do not want seeds to be updated after they have been created, use seed_once:

Point.seed_once(:x, :y) do |s|
  s.x = 4
  s.y = 7
  s.name = "Home"
end

The default constraint just checks the id of the record.

Where to put seed files

By default, seed files are looked for in the following locations:

  • Rails.root/db/fixtures and Rails.root/db/fixtures/Rails.env in a Rails app
  • db/fixtures when loaded without Rails

You can change these defaults by modifying the SeedFu.fixture_paths array.

Seed files can be named whatever you like, and are loaded in alphabetical order.

Terser syntax

When loading lots of records, the above block-based syntax can be quite verbose. You can use the following instead:

User.seed(:id,
  { :id => 1, :login => "jon",   :email => "[email protected]",   :name => "Jon"   },
  { :id => 2, :login => "emily", :email => "[email protected]", :name => "Emily" }
)

Rake task

Seed files can be run automatically using rake db:seed_fu. There are two options which you can pass:

  • rake db:seed_fu FIXTURE_PATH=path/to/fixtures -- Where to find the fixtures
  • rake db:seed_fu FILTER=users,articles -- Only run seed files with a filename matching the FILTER

You can also do a similar thing in your code by calling SeedFu.seed(fixture_paths, filter).

Disable output

To disable output from Seed Fu, set SeedFu.quiet = true.

Handling large seed files

Seed files can be huge. To handle large files (over a million rows), try these tricks:

  • Gzip your fixtures. Seed Fu will read .rb.gz files happily. gzip -9 gives the best compression, and with Seed Fu's repetitive syntax, a 160M file can shrink to 16M.
  • Add lines reading # BREAK EVAL in your big fixtures, and Seed Fu will avoid loading the whole file into memory. If you use SeedFu::Writer, these breaks are built into your generated fixtures.
  • Load a single fixture at a time with the FILTER environment variable
  • If you don't need Seed Fu's ability to update seed with new data, then you may find that activerecord-import is faster

Generating seed files

If you need to programmatically generate seed files, for example to convert a CSV file into a seed file, then you can use SeedFu::Writer.

Bugs / Feature requests

Please report them on Github Issues.

Contributors

Copyright © 2008-2010 Michael Bleigh, released under the MIT license

seed-fu's People

Contributors

jonleighton avatar jcnetdev avatar zdennis avatar queso avatar ayqazi avatar darcy avatar hmans avatar fd avatar

Stargazers

Nathan avatar

Watchers

James Cloos avatar  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.