Documentation

Seed/fixture file

You may create seeds/fixtures using YAML and JSON files — along Ruby files. Following are some examples of valid, workable and applicable seed/fixture files in both YAML and JSON syntax. If you need to work with associations, Ruby Seeds would be a good starting point; YAML and JSON Seeds don't handle Model associations by now.

[
  {
    "environment": [ "test", "development" ],
    "user": [
      {
        "full_name": "Richard Feynman",
        "profession": "Theoretical physicist",
        "username": "rfeynman",
        "password": "271828"
      }
    ]
  },
  {
    "currency": [
      {
        "abbr": "USD",
        "name": "United States dollar"
      },
      {
        "abbr": "BRL",
        "name": "Brazilian real"
      }
    ]
  }
]
-
  environment:
    - :test
    - "development"
  user:
    -
      full_name: "Richard Feynman"
      profession: "Theoretical physicist"
      username: "rfeynman"
      password: "271828"
-
  currency:
    -
      abbr: 'USD'
      name: 'United States dollar'
    -
      abbr: 'BRL'
      name: 'Brazilian real'

By default, the Model class will be inferred by the Seed descriptor. If you need to override the default name and point to the proper Model class, you can do as following:

{
  "environment": "test",
  "model": {
    "class": "User",
    "entries": [
      {
        "full_name": "Richard Feynman",
        "profession": "Theoretical physicist",
        "username": "rfeynman",
        "password": "271828"
      }
    ]
  }
}
environment: "test"
model:
  class: "User"
  entries:
    -
      full_name: "Richard Feynman"
      profession: "Theoretical physicist"
      username: "rfeynman"
      password: "271828"

Wildcard Seeds are also available through YAML & JSON files.

{
  "user": {
    "full_name": "Richard Feynman",
    "profession": "Theoretical physicist",
    "username": "rfeynman",
    "password": "271828"
  }
}
user:
  full_name: "Richard Feynman"
  profession: "Theoretical physicist"
  username: "rfeynman"
  password: "271828"

Seeds in Ruby could be defined as following:

Sequel.seed(:development, :test) do # Applies only to "development" and "test" environments
  def run
    User.create \
      full_name: "Richard Feynman",
      profession: "Theoretical physicist",
      username: "rfeynman",
      password: "271828"
  end
end
Sequel.seed do # Wildcard Seed; applies to every environment
  def run
    [
      ['USD', 'United States dollar'],
      ['BRL', 'Brazilian real']
    ].each do |abbr, name|
      Currency.create abbr: abbr, name: name
    end
  end
end

Applying seeds/fixtures

In order to apply the seed/fixture files, you just need to load sequel and the sequel-seed extension, along with any constant (Class, Module etc.) used in a (Ruby) Seed file. The sequel-seed gem assumes that each environment has its own database; thus, each Sequel::Seed environment is associated with a different database instance. Following are the steps to apply a collection of seed/fixture files (all placed in one directory):

0. Set the environment (optional — if omitted, it will fulfil wildcard Seeds only)

Sequel::Seed.setup(:development)

1. Load the extension

require 'sequel'
require 'sequel/extensions/seed'

Sequel.extension :seed

2. Apply the seeds/fixtures

DB = Sequel.connect(...)
Sequel::Seeder.apply(DB, "/path/to/seeds")