seed

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package seed provides Lucid-style factories and seeders.

Index

Constants

View Source
const RootSeeder = "DatabaseSeeder"

RootSeeder is the name of the seeder `db:seed` starts from when one is registered.

Variables

This section is empty.

Functions

func Call

func Call(ctx context.Context, db *migro.DB, classes ...Class) error

Call runs seeders in the order given, stopping at the first error (Laravel's $this->call([...])). Use it from a DatabaseSeeder so dependent tables are filled in foreign-key order; the calls share the caller's transaction and context, so a failure rolls the whole DatabaseSeeder back.

func Register

func Register(c Class)

Register records a seeder named after its type (so db:seed --class=UserSeeder finds it).

func ResetRegistry

func ResetRegistry()

ResetRegistry clears the registry (for tests).

Types

type Class

type Class interface {
	Run(ctx context.Context, db *migro.DB) error
}

Class is a Lucid-style seeder: a type with a Run method, registered from init():

func init() { seed.Register(&UserSeeder{}) }

Add `func (UserSeeder) Environments() []string` to limit where it runs.

type Factory

type Factory[T any] struct {
	// contains filtered or unexported fields
}

Factory builds and persists models. Configure it once with Define/State/Relation/After, then derive variants with Merge/Apply/With/Seed — each returns a copy, so a shared factory is never mutated.

func Define

func Define[T any](define func(f *Faker) T) *Factory[T]

Define creates a factory from a function returning default attribute values.

func (*Factory[T]) After

func (f *Factory[T]) After(fn func(ctx context.Context, db *migro.DB, m *T) error) *Factory[T]

After registers a hook run after each Create (Lucid's .after('create')). Call during setup.

func (*Factory[T]) AfterMaking

func (f *Factory[T]) AfterMaking(fn func(m *T)) *Factory[T]

AfterMaking registers a hook run after each model is built, by Make and Create alike (Laravel's afterMaking).

func (*Factory[T]) Apply

func (f *Factory[T]) Apply(names ...string) *Factory[T]

Apply activates named states in order (Lucid's .apply()).

func (*Factory[T]) Create

func (f *Factory[T]) Create(ctx context.Context, db *migro.DB) (*T, error)

Create builds and inserts one model, then runs With relations and After hooks.

func (*Factory[T]) CreateMany

func (f *Factory[T]) CreateMany(ctx context.Context, db *migro.DB, n int) ([]T, error)

CreateMany inserts n models in a single transaction: either all of them exist afterwards or none.

func (*Factory[T]) For

func (f *Factory[T]) For(name string) *Factory[T]

For creates the named parent before each insert and links the child to it (Laravel's for()). It has no effect on Make, which never touches the database.

func (*Factory[T]) Make

func (f *Factory[T]) Make() (T, error)

Make builds one model without persisting it.

func (*Factory[T]) MakeMany

func (f *Factory[T]) MakeMany(n int) ([]T, error)

MakeMany builds n models without persisting them.

func (*Factory[T]) Merge

func (f *Factory[T]) Merge(fn func(*T)) *Factory[T]

Merge overrides attributes on every generated model (Lucid's .merge()).

func (*Factory[T]) Parent

func (f *Factory[T]) Parent(name string, fn ParentFn[T]) *Factory[T]

Parent registers a named belongs-to builder. Call during setup; activate it with For.

func (*Factory[T]) Relation

func (f *Factory[T]) Relation(name string, fn RelationFn[T]) *Factory[T]

Relation registers a named relation builder. Call during setup.

func (*Factory[T]) Seed

func (f *Factory[T]) Seed(seed int64) *Factory[T]

Seed makes generated data reproducible.

func (*Factory[T]) Sequence

func (f *Factory[T]) Sequence(fns ...func(i int, m *T)) *Factory[T]

Sequence alternates attribute changes across the generated models: model i gets fns[i % len(fns)] (Laravel's sequence()). The index counts from 0 within one Make/Create call.

func (*Factory[T]) State

func (f *Factory[T]) State(name string, fn func(*T)) *Factory[T]

State registers a named variant (Lucid's .state()). Call during setup.

func (*Factory[T]) With

func (f *Factory[T]) With(relation string, n int) *Factory[T]

With creates n related rows per created model (Lucid's .with()).

type Faker

type Faker struct {
	// contains filtered or unexported fields
}

Faker generates plausible fake values. It is deterministic for a given seed.

func NewFaker

func NewFaker(seed int64) *Faker

func (*Faker) Bool

func (f *Faker) Bool() bool

func (*Faker) Date

func (f *Faker) Date(from, to time.Time) time.Time

Date returns a time between from and to.

func (*Faker) Email

func (f *Faker) Email() string

Email uses reserved example domains so generated data never reaches real mailboxes.

func (*Faker) FirstName

func (f *Faker) FirstName() string

func (*Faker) Float

func (f *Faker) Float(min, max float64) float64

Float returns a float in [min, max).

func (*Faker) Int

func (f *Faker) Int(min, max int) int

Int returns an integer in [min, max].

func (*Faker) LastName

func (f *Faker) LastName() string

func (*Faker) Name

func (f *Faker) Name() string

func (*Faker) Paragraph

func (f *Faker) Paragraph() string

func (*Faker) Pick

func (f *Faker) Pick(options ...string) string

Pick returns one of the options; the empty string when none are given.

func (*Faker) Sentence

func (f *Faker) Sentence() string

func (*Faker) UUID

func (f *Faker) UUID() string

UUID returns a random version-4 UUID.

func (*Faker) Username

func (f *Faker) Username() string

Username is lowercase with a numeric suffix to reduce collisions.

func (*Faker) Word

func (f *Faker) Word() string

func (*Faker) Words

func (f *Faker) Words(n int) string

type Option

type Option func(*Runner)

func Once

func Once() Option

Once records executed seeders in a table so re-running skips them. Without it seeders run every time, like Lucid — write them idempotently (e.g. with FirstOrCreate).

func WithEnvironment

func WithEnvironment(env string) Option

WithEnvironment sets the environment (default: MIGRO_ENV, then APP_ENV, then "development").

func WithoutEvents

func WithoutEvents() Option

WithoutEvents runs seeders with model events muted (Laravel's WithoutModelEvents trait), so observers and event handlers do not fire for seeded rows.

type ParentFn

type ParentFn[T any] func(ctx context.Context, db *migro.DB, child *T, f *Faker) error

ParentFn creates a parent row for a child about to be inserted and points the child at it (Laravel's ->for(User::factory())).

type RelationFn

type RelationFn[T any] func(ctx context.Context, db *migro.DB, parent *T, n int, f *Faker) error

RelationFn creates n related rows for a freshly created parent (Lucid's factory.with()).

type Result

type Result struct {
	Ran     []string
	Skipped []string
}

Result lists what a run did.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner executes seeders in name order, each in its own transaction.

func NewRunner

func NewRunner(db *migro.DB, opts ...Option) *Runner

func (*Runner) Add

func (r *Runner) Add(ss ...Seeder) error

Add registers seeders; names must be unique and Run set.

func (*Runner) AddRegistered

func (r *Runner) AddRegistered() error

AddRegistered adds every seeder registered through Register.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) (Result, error)

Run executes every eligible seeder, in name order.

func (*Runner) RunOnly

func (r *Runner) RunOnly(ctx context.Context, names ...string) (Result, error)

RunOnly executes only the named seeders (still subject to environment rules).

func (*Runner) Seed

func (r *Runner) Seed(ctx context.Context) (Result, error)

Seed is `php artisan db:seed`: it runs the DatabaseSeeder when one is registered (which calls the others in the order it needs, see Call), and otherwise every seeder in name order (Adonis's db:seed).

type Seeder

type Seeder struct {
	Name         string
	Environments []string
	Run          func(ctx context.Context, db *migro.DB) error
}

Seeder is one unit of seed data. Like Lucid's, it may be limited to environments: with no Environments it runs everywhere except "production"; to seed production, list "production" explicitly.

func FromFS

func FromFS(fsys fs.FS, dir string) ([]Seeder, error)

FromFS loads every *.sql file in dir as a seeder named after the file. Statements are split like migration files.

func Registered

func Registered() []Seeder

Registered returns the registered seeders sorted by name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL