seeder

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDuplicateSeeder    = errors.New("duplicate seeder name")
	ErrInvalidSeederName  = errors.New("invalid seeder name")
	ErrSeederNotFound     = errors.New("seeder not found")
	ErrCircularDependency = errors.New("circular seeder dependency")
)

Sentinel errors for the seeder system. Defined locally to avoid circular dependencies with pkg/migrator.

Functions

func AutoRegister

func AutoRegister(name string, s Seeder)

AutoRegister registers a seeder in the global auto-registry. Intended to be called from init() functions in seeder files. Panics if the name is empty/whitespace-only or duplicate (fail-fast at startup).

func CreateMany

func CreateMany(db *sql.DB, table string, records []map[string]any, chunkSize int) error

CreateMany inserts records into the specified table using multi-row INSERT statements, each containing at most chunkSize rows. Records are provided as []map[string]any where keys are column names and values are column values.

Column names are derived from the first record and sorted for deterministic SQL generation. All subsequent records must have the same set of keys.

If chunkSize is zero or negative, a default of 500 is used. Uses DialectPostgres by default. Use CreateManyWithDialect for other databases.

func CreateManyWithDialect

func CreateManyWithDialect(db *sql.DB, table string, records []map[string]any, chunkSize int, dialect Dialect) error

CreateManyWithDialect inserts records using the specified database dialect for placeholder and identifier quoting styles.

Supported dialects:

  • DialectPostgres: $1, $2 placeholders, "double-quoted" identifiers
  • DialectMySQL: ? placeholders, `backtick-quoted` identifiers
  • DialectSQLite: ? placeholders, "double-quoted" identifiers

func GetAutoRegistered

func GetAutoRegistered() map[string]Seeder

GetAutoRegistered returns a defensive copy of all auto-registered seeders.

func ResetAutoRegistry

func ResetAutoRegistry()

ResetAutoRegistry clears the global auto-registry (for testing only).

Types

type DependentSeeder

type DependentSeeder interface {
	Seeder
	DependsOn() []string
}

DependentSeeder extends Seeder with dependency declaration. DependsOn returns the names of seeders that must run before this one.

type Dialect

type Dialect int

Dialect represents a database dialect for SQL generation differences.

const (
	// DialectPostgres uses $1, $2, ... numbered placeholders and double-quoted identifiers.
	DialectPostgres Dialect = iota
	// DialectMySQL uses ? positional placeholders and backtick-quoted identifiers.
	DialectMySQL
	// DialectSQLite uses ? positional placeholders and double-quoted identifiers.
	DialectSQLite
)

type Logger

type Logger interface {
	Info(msg string, args ...any)
	Error(msg string, args ...any)
}

Logger defines a minimal logging interface for the seeder runner.

type Registry

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

Registry stores registered seeders by name.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty seeder registry.

func (*Registry) Get

func (r *Registry) Get(name string) (Seeder, error)

Get retrieves a seeder by name. Returns ErrSeederNotFound if not found.

func (*Registry) GetAll

func (r *Registry) GetAll() map[string]Seeder

GetAll returns a copy of all registered seeders keyed by name.

func (*Registry) Register

func (r *Registry) Register(name string, s Seeder) error

Register adds a seeder with the given name to the registry. Names must be non-empty and unique.

type RollbackableSeeder

type RollbackableSeeder interface {
	Seeder
	Rollback(db *sql.DB) error
}

RollbackableSeeder extends Seeder with rollback capability. Rollback undoes the data changes made by the seeder's Run method.

type Runner

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

Runner resolves seeder dependencies and executes seeders in the correct order.

func NewRunner

func NewRunner(registry *Registry, db *sql.DB, logger Logger) *Runner

NewRunner creates a new seeder Runner. The logger parameter may be nil, in which case logging is silently skipped.

func (*Runner) Rollback

func (r *Runner) Rollback(name string) error

Rollback executes the named seeder's Rollback method. Returns an error if the seeder is not found or does not implement RollbackableSeeder.

func (*Runner) Run

func (r *Runner) Run(name string) error

Run executes a specific seeder and its transitive dependencies in order. Returns ErrSeederNotFound if the named seeder is not registered.

func (*Runner) RunAll

func (r *Runner) RunAll() error

RunAll executes all registered seeders in dependency-resolved order. If any seeder fails, execution stops and the error is returned.

func (*Runner) RunByTag

func (r *Runner) RunByTag(tag string) error

RunByTag executes only seeders whose tags include the specified tag, in dependency-resolved order. Seeders that do not implement TaggedSeeder are skipped. Returns without error if no seeders match the tag.

func (*Runner) Truncate

func (r *Runner) Truncate(table string) error

Truncate deletes all rows from the specified table. Uses DELETE FROM for broad database compatibility (including SQLite).

type Seeder

type Seeder interface {
	Run(db *sql.DB) error
}

Seeder defines the contract for a database seeder. Implementations populate database tables with data.

type TaggedSeeder

type TaggedSeeder interface {
	Seeder
	Tags() []string
}

TaggedSeeder extends Seeder with tag-based grouping. Tags returns the list of tags (e.g., "development", "testing", "production") that this seeder belongs to. Seeders implementing this interface can be selectively executed via Runner.RunByTag.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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