Documentation
¶
Index ¶
- Variables
- func AutoRegister(name string, s Seeder)
- func CreateMany(db *sql.DB, table string, records []map[string]any, chunkSize int) error
- func CreateManyWithDialect(db *sql.DB, table string, records []map[string]any, chunkSize int, ...) error
- func GetAutoRegistered() map[string]Seeder
- func ResetAutoRegistry()
- type DependentSeeder
- type Dialect
- type Logger
- type Registry
- type RollbackableSeeder
- type Runner
- type Seeder
- type TaggedSeeder
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry stores registered seeders by name.
type RollbackableSeeder ¶
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 ¶
NewRunner creates a new seeder Runner. The logger parameter may be nil, in which case logging is silently skipped.
func (*Runner) Rollback ¶
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 ¶
Run executes a specific seeder and its transitive dependencies in order. Returns ErrSeederNotFound if the named seeder is not registered.
func (*Runner) RunAll ¶
RunAll executes all registered seeders in dependency-resolved order. If any seeder fails, execution stops and the error is returned.
type Seeder ¶
Seeder defines the contract for a database seeder. Implementations populate database tables with data.
type TaggedSeeder ¶
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.