• Stars
    star
    1,221
  • Rank 38,417 (Top 0.8 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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 introduced API changes. Seed::Writer was been completely overhauled and will require you to update your scripts. Some other deprecations were introduced, and support is fully removed in 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.1, 3.2, 4.0, 4.1, 4.2, 5.0

Just add gem 'seed-fu', '~> 2.3' to your Gemfile

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 3.0

The current version is not backwards compatible with Rails 3.0. Please use gem 'seed-fu', '~> 2.0.0'.

Rails 2.3

The current version is not backwards compatible with Rails 2.3. Please use gem 'seed-fu', '~> 1.2.0'.

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. Now 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.

Capistrano deployment

SeedFu has included Capistrano deploy script, you just need require that in config/deploy.rb:

require 'seed-fu/capistrano'

# Trigger the task after update_code
after 'deploy:update_code', 'db:seed_fu'

If you use Capistrano3, you should require another file.

require 'seed-fu/capistrano3'

# Trigger the task before publishing
before 'deploy:publishing', 'db:seed_fu'

Bugs / Feature requests

Please report them on Github Issues.

Contributors

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

More Repositories

1

acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
Ruby
4,972
star
2

subdomain-fu

A new plugin approach to attempting to solve the usage of subdomains in linking and routing in Rails projects.
Ruby
590
star
3

twitter-auth

Standard authentication stack for Rails using Twitter to log in.
Ruby
358
star
4

princely

A simple Rails wrapper for the PrinceXML PDF generation library.
Ruby
233
star
5

uberkit

The UberKit is a Rails plugin with a set of UI tools to ease common development.
Ruby
103
star
6

omniauth-jwt

An OmniAuth strategy that uses JSON Web Token for Single Sign-On
Ruby
99
star
7

mbleigh.github.com

My blog and website.
Ruby
72
star
8

mash

Mash is a Hash with the ability to read, write, and test for the presence of arbitrary attributes using method calls.
Ruby
61
star
9

ruby-github

A Ruby library for getting information from the GitHub API.
Ruby
58
star
10

canonical-url

Rails plugin to take advantage of the new Canonical URL support of search engines.
Ruby
50
star
11

pwas-on-firebase

Demos and related material for Google I/O Progressive Web Apps on Firebase talk.
HTML
50
star
12

colorist

A Ruby library built to handle the easy conversion and manipulation of colors with a special emphasis on W3C standards and CSS-style hex color notation.
Ruby
48
star
13

acts-as-readable

A simple plugin that allows a user to mark anything as 'read.' Common usage would include forum posts, news items, etc.
Ruby
44
star
14

escapable-amp

TypeScript
30
star
15

conf_ask

A simple demonstration app built to show off Grape
JavaScript
26
star
16

fetches

A Rails plugin to simplify the fetching and memoization of records for parameter-based finds.
Ruby
25
star
17

persistence-smoothie

The source code (and slides in the Downloads section) for my talk "Persistence Smoothie: Blending SQL and NoSQL"
Ruby
21
star
18

marky

A Markdown rendering wrapper for the EtherPad collaborative editor.
JavaScript
17
star
19

twisteners

Who's listening to you? Find out with this Twitter app coded live at RailsConf 2009.
JavaScript
17
star
20

relates-to

Rails Plugin to provide simple polymorphic relationships between models.
Ruby
16
star
21

github-unfuddle

A web hook that takes GitHub commits and turns them into Unfuddle changesets posting using the Unfuddle API.
Ruby
15
star
22

from_param

Rails plugin that adds a from_param class method to ActiveRecord::Base for simple URL-based fetching.
Ruby
14
star
23

web-components-in-action

Examples and exercises for Web Components in Action workshop from Fluent 2014
CSS
13
star
24

cors-talk-example

Ruby
12
star
25

needy-controllers

Include stylesheets and scripts in a before_filter-esque fashion, and set up simple memoized methods for record fetches.
Ruby
12
star
26

browserized-styles

Automatic inclusion of browser and OS-specific stylesheets with a simple naming convention.
Ruby
11
star
27

bootstrap-polymer

An implementation of Bootstrap 3 components in Polymer.
JavaScript
10
star
28

polishing-rubies

Polishing Rubies: A Guide to Ruby Open Source Development
9
star
29

omniauth-box

Box.net strategy for OmniAuth
Ruby
9
star
30

railsconf-tweetups

Twitter app to see who's going to what at RailsConf!
Ruby
9
star
31

twitterdispatch

A simple Twitter API wrapper that gets out of your way.
6
star
32

commitbit

A web-based tool to help abandoned GitHub repos transfer to new maintainers.
CSS
6
star
33

present-future-of-oauth

Slides and code for "The Present Future of OAuth" given at RailsConf 2010.
Ruby
5
star
34

pictle

Drawing with Wordles
TypeScript
5
star
35

hooktastic

Merb-based application to make webhooks super-easy.
Ruby
5
star
36

jquery-pageselect

A jQuery plugin to handle selection of text on the page (not in a textarea).
4
star
37

jquery.navigable

A jQuery plugin for simple keyboard navigation.
JavaScript
4
star
38

css3-for-sass

A set of mixins to apply CSS3 rules to SASS.
4
star
39

cors-talk

JavaScript
4
star
40

bleightiful

The WordPress theme I hastily crafted for my personal site.
PHP
3
star
41

wedding-website

My wedding website!
JavaScript
2
star
42

rails-is-the-new-rails

Slides for a talk given at Ruby Midwest 2011
JavaScript
2
star
43

open-source-marketing

JavaScript
2
star
44

partay

Source for Partay games (for now, just Phraseology).
TypeScript
2
star
45

nodelin

Noodlin' around with Node.js to prep for the KO
JavaScript
2
star
46

callable-run-demo

HTML
2
star
47

schemer

Create JSON Schemas from Go data structures
Go
2
star
48

tinypage

A simple demo of the Firebase Hosting REST API.
JavaScript
2
star
49

omniauth-from-the-ground-up

OmniAuth: From the Ground Up Talk Content
Ruby
2
star
50

codemirror-element

A drop-in web component for source code editing. Utilizes the CodeMirror library.
HTML
2
star
51

dotfiles

Vim Script
1
star
52

puppetry

Ruby
1
star
53

i-am-open-source

I Am Open Source And So Can You (Talk)
1
star
54

let-us-lunch

Ruby
1
star
55

fiberthecrossroads.org

JavaScript
1
star