module Sequel
Extension based upon Sequel::Migration and Sequel::Migrator
Adds the Sequel::Seed
module and the Sequel::Seed::Base
and Sequel::Seeder
classes, which allow the user to easily group entity changes and seed/fixture the database to a newer version only (unlike migrations, seeds are not directional).
To load the extension:
Sequel.extension :seed
It is also important to set the environment:
Sequel::Seed.setup(:development)
Public Class Methods
seed(*env_labels, &block)
click to toggle source
Creates a Seed
subclass according to the given block
.
The env_labels
lists on which environments the seed should be applicable. If the current environment is not applicable, the seed is ignored. On the other hand, if it is applicable, it will be listed in Seed.descendants
and subject to application (if it was not applied yet).
Expected seed call:
Sequel.seed(:test) do # seed is only applicable to the test environment def run Entity.create attribute: value end end
Wildcard seed:
Sequel.seed do # seed is applicable to every environment, or no environment def run Entity.create attribute: value end end
# File lib/sequel/extensions/seed.rb, line 49 def seed(*env_labels, &block) return if env_labels.length > 0 && !env_labels.map(&:to_sym).include?(Seed.environment) seed = Class.new(Seed::Base) seed.class_eval(&block) if block_given? Seed::Base.inherited(seed) unless Seed::Base.descendants.include?(seed) seed end