config

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: 10 Imported by: 0

Documentation

Overview

Package config provides configuration loading and validation for go-migration. It supports YAML files, JSON files, and environment variables.

Index

Constants

This section is empty.

Variables

View Source
var ErrConfigValidation = errors.New("configuration validation failed")

ErrConfigValidation is returned when configuration validation fails.

Functions

func InterpolateEnv

func InterpolateEnv(data []byte) ([]byte, error)

InterpolateEnv replaces ${VAR_NAME} placeholders in data with the corresponding environment variable values. Escaped sequences ($${VAR_NAME}) are replaced with the literal text ${VAR_NAME}. Returns an error listing all unresolved variables.

When a quoted JSON string consists entirely of a single placeholder (e.g. "${DB_PORT}") and the resolved value is a number, boolean, or null, the surrounding quotes are removed so that the JSON remains valid. For example, "port": "${DB_PORT}" with DB_PORT=5433 becomes "port": 5433.

Types

type Config

type Config struct {
	Connections    map[string]ConnectionConfig `yaml:"connections" json:"connections"`
	DefaultConn    string                      `yaml:"default" json:"default"`
	MigrationTable string                      `yaml:"migration_table" json:"migration_table"`
	MigrationDir   string                      `yaml:"migration_dir" json:"migration_dir"`
	SeederDir      string                      `yaml:"seeder_dir" json:"seeder_dir"`
	FactoryDir     string                      `yaml:"factory_dir" json:"factory_dir"`
	LogLevel       string                      `yaml:"log_level" json:"log_level"`
	LogOutput      string                      `yaml:"log_output" json:"log_output"`
}

Config holds the top-level configuration for go-migration.

func Load

func Load(path string) (*Config, error)

Load reads configuration from a file at the given path. It detects the format by file extension (.yaml/.yml for YAML, .json for JSON). After loading, it applies defaults.

func LoadFromEnv

func LoadFromEnv() (*Config, error)

LoadFromEnv builds a Config from environment variables with the GOMIGRATE_ prefix. It creates a single "default" connection from the DB-related env vars. After loading, it applies defaults.

func (*Config) ApplyDefaults

func (c *Config) ApplyDefaults()

ApplyDefaults sets sensible default values for optional settings.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks that the configuration has all required fields and valid values. Returns an error wrapping ErrConfigValidation listing all violations.

type ConnectionConfig

type ConnectionConfig struct {
	Driver          string            `yaml:"driver" json:"driver"`
	Host            string            `yaml:"host" json:"host"`
	Port            int               `yaml:"port" json:"port"`
	Database        string            `yaml:"database" json:"database"`
	Username        string            `yaml:"username" json:"username"`
	Password        string            `yaml:"password" json:"password"`
	MaxOpenConns    int               `yaml:"max_open_conns" json:"max_open_conns"`
	MaxIdleConns    int               `yaml:"max_idle_conns" json:"max_idle_conns"`
	ConnMaxLifetime time.Duration     `yaml:"conn_max_lifetime" json:"conn_max_lifetime"`
	Options         map[string]string `yaml:"options" json:"options"`
}

ConnectionConfig holds the configuration for a single database connection.

Jump to

Keyboard shortcuts

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