seed

package
v0.5.0-dev.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package seed loads reference data into a database from seed files. The consumer supplies the parts only it knows — the seed file system, a typed load function per table, and the step order — and the runner owns the mechanics: format selection by file extension, decoding, one transaction per step, and the outcome log. The package is stdlib-only, so it lives in the base library beside database.

Steps

Table binds one seed file to a typed load function, producing an opaque Step; Runner.Run executes the steps in the order given, each in its own transaction, stopping at the first failure with the step's path in the error. Idempotency belongs to the load function's SQL — INSERT … ON CONFLICT DO NOTHING against the table's natural key — because the conflict target is knowledge the consumer holds, not the runner.

Formats

Format decodes one seed file and owns the file extensions it claims. Formats are passed to New at construction — typed registration, no registry, no init side effects — and no two may claim the same extension. JSON ships with the package and decodes strictly: an unknown field or trailing content is a defect in a curated seed file, not data to ignore. Another format is one Format implementation, supplied by a sub-module or by the consumer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Format

type Format interface {
	// Extensions lists the file extensions this format owns, leading dot
	// included, as ".json".
	Extensions() []string
	// Decode reads a whole seed file from r into v.
	Decode(r io.Reader, v any) error
}

Format decodes one seed file into the shape a Step expects. A format owns one or more file extensions, and the Runner selects it by the extension of the step's path. Formats are passed at construction (New); there is no registry.

type JSON

type JSON struct{}

JSON is the Format for .json seed files: one JSON document per file, decoded strictly. An unknown field or content after the document is a data defect in a curated seed file, so both fail the decode rather than pass silently.

func (JSON) Decode

func (JSON) Decode(r io.Reader, v any) error

Decode reads one JSON document from r into v.

func (JSON) Extensions

func (JSON) Extensions() []string

Extensions reports ".json".

type Runner

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

Runner executes seed steps against a database, decoding each step's seed file with the format that owns its extension.

func New

func New(db *sql.DB, logger *slog.Logger, formats ...Format) (*Runner, error)

New builds a Runner over db, logging each step's outcome to logger. At least one Format is required, and no two formats may claim the same extension.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, data fs.FS, steps ...Step) error

Run executes the steps in order against the seed files in data. Each step runs in its own transaction; the first failure stops the run with the step's path in the error.

type Step

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

Step is one ordered unit of a seed run: a seed file bound to the load function that inserts its rows. Construct steps with Table.

func Table

func Table[T any](
	p string,
	load func(ctx context.Context, tx *sql.Tx, rows []T) error,
) Step

Table binds the seed file at p to a typed load function. The runner decodes the file into []T and passes the rows to load inside the step's transaction; load owns the insert SQL and its idempotency, where the conflict target is known.

Jump to

Keyboard shortcuts

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