config

package
v0.2025.3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package config defines the configuration structures and functional options used to customize the behavior of the emberkit testing toolkit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyOptions

func ApplyOptions(initialConfig *Config, options ...Option) (*Settings, Config)

ApplyOptions takes an initial base Config and a slice of Option functions. It initializes a Settings struct with defaults, applies all provided Option functions to populate the Settings, and then merges the initial Config with the populated Settings to produce the final, resolved Config.

It returns the populated Settings struct (containing hooks, migrator, etc.) and the final merged Config struct used for running the EmberKit instance.

Types

type Config

type Config struct {
	Version  PostgresVersion // e.g., V16_8
	Host     string          // Host for the DB to listen on. Defaults to "localhost".
	Port     uint32          // Port for the DB to listen on. 0 means select a random free port.
	Database string          // Initial database to create and connect to. Must not be empty.
	Username string          // Database user. Must not be empty.
	Password string          // Database password. Must not be empty.
	// RuntimePath removed - now handled internally by EmberKit
	BinariesPath string        // Optional: Path to existing postgres binaries. If empty, downloads.
	StartTimeout time.Duration // How long to wait for Postgres to start. Default 15s.
	Logger       *os.File      // Where to log raw Postgres output. Default os.Stderr. Set to nil to discard.
	// SlogLogger and SlogLevel removed, zap logger is now created internally or passed via options.
	StartupParams map[string]string // Additional server parameters for postgresql.conf
	DSNParams     map[string]string // Additional parameters to append to the DSN (e.g., "search_path=public").
	// UseAtlas removed - Atlas usage is now implicit based on migrator configuration
	KeepDatabase bool           // If true, do not drop the database on cleanup. Default false.
	SQLTxOptions *sql.TxOptions // Custom transaction options for database/sql. Default nil.
	PgxTxOptions pgx.TxOptions  // Custom transaction options for pgx. Default empty struct.
}

Config defines the configuration for the embedded PostgreSQL instance.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default configuration for the embedded database.

func (*Config) DSN

func (c *Config) DSN() string

DSN constructs a DSN string from the Config struct. Note: Assumes config.Port has been assigned (either initially or randomly).

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the essential configuration fields are set correctly.

type Option

type Option func(*Settings)

Option defines the functional option type used to configure an EmberKit instance. Functions matching this signature (e.g., WithKeepDatabase, WithAtlas) are passed to NewEmberKit to customize its setup and behavior.

func WithAfterConnectionHook

func WithAfterConnectionHook(hook func(ctx context.Context, db *sql.DB, pool *pgxpool.Pool, logger *zap.Logger) error) Option

WithAfterConnectionHook registers a function to run after database connections (sql.DB, pgxpool.Pool) are established.

func WithAtlasHCLPath

func WithAtlasHCLPath(path string) Option

WithAtlasHCLPath specifies the path to the atlas.hcl configuration file.

func WithBeforeMigrationHook

func WithBeforeMigrationHook(hook func(ctx context.Context, dsn string, logger *zap.Logger) error) Option

WithBeforeMigrationHook registers a function to run before migrations are applied.

func WithDSNParams

func WithDSNParams(params map[string]string) Option

WithDSNParams provides additional parameters to be appended to the DSN.

func WithKeepDatabase

func WithKeepDatabase() Option

WithAtlas configures the EmberKit to use Atlas for migrations. It uses the HCL path specified by WithAtlasHCLPath (or the default "atlas.hcl"). The logger passed to NewAtlasMigrator here is temporary; the actual logger WithKeepDatabase prevents the test database from being dropped during cleanup.

func WithPgxTxOptions

func WithPgxTxOptions(txsts pgx.TxOptions) Option

WithPgxTxOptions provides custom transaction options for pgx tests.

func WithSQLTxOptions

func WithSQLTxOptions(txsts *sql.TxOptions) Option

WithSQLTxOptions provides custom transaction options for database/sql tests.

func WithSharedServer

func WithSharedServer(dsn string, cfg Config) Option

WithSharedServer configures the EmberKit to use a pre-existing shared server instance. It provides the necessary maintenance DSN and the configuration that was used to start the shared server. When this option is used, NewEmberKit will skip starting/stopping its own server instance.

func WithStartupParams

func WithStartupParams(params map[string]string) Option

WithStartupParams provides additional parameters for the PostgreSQL server startup.

func WithZapOptions

func WithZapOptions(zapsts ...zap.Option) Option

WithZapOptions provides additional options for the zap logger.

func WithZapTestLevel

func WithZapTestLevel(level zapcore.Level) Option

WithZapTestLevel sets the minimum log level specifically for the zaptest logger.

type PostgresVersion

type PostgresVersion string

Supported PostgreSQL Versions Source: https://www.postgresql.org/support/versioning/ (Add more versions as needed and supported by embedded-postgres)

const (
	// PostgreSQL 17
	V17_0 PostgresVersion = "17.0.0"
	V17_1 PostgresVersion = "17.1.0"
	V17_2 PostgresVersion = "17.2.0"
	V17_3 PostgresVersion = "17.3.0"
	V17_4 PostgresVersion = "17.4.0"

	// PostgreSQL 16
	V16_0 PostgresVersion = "16.0.0"
	V16_1 PostgresVersion = "16.1.0"
	V16_2 PostgresVersion = "16.2.0"
	V16_3 PostgresVersion = "16.3.0"
	V16_4 PostgresVersion = "16.4.0"
	V16_5 PostgresVersion = "16.5.0"
	V16_6 PostgresVersion = "16.6.0"
	V16_7 PostgresVersion = "16.7.0"
	V16_8 PostgresVersion = "16.8.0"

	// PostgreSQL 15
	V15_0  PostgresVersion = "15.0.0"
	V15_1  PostgresVersion = "15.1.0"
	V15_2  PostgresVersion = "15.2.0"
	V15_3  PostgresVersion = "15.3.0"
	V15_4  PostgresVersion = "15.4.0"
	V15_5  PostgresVersion = "15.5.0"
	V15_6  PostgresVersion = "15.6.0"
	V15_7  PostgresVersion = "15.7.0"
	V15_8  PostgresVersion = "15.8.0"
	V15_9  PostgresVersion = "15.9.0"
	V15_10 PostgresVersion = "15.10.0"
	V15_11 PostgresVersion = "15.11.0"
	V15_12 PostgresVersion = "15.12.0"
)

type Settings

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

Settings holds configuration values provided via functional Option functions. These settings customize various aspects of the EmberKit instance, such as migration behavior, transaction options, logging, and hooks. It is distinct from the final `Config` struct, which holds the resolved values used to run the database instance.

func (*Settings) AfterConnectionHook

func (sts *Settings) AfterConnectionHook() func(ctx context.Context, db *sql.DB, pool *pgxpool.Pool, logger *zap.Logger) error

func (*Settings) AtlasHCLPath

func (sts *Settings) AtlasHCLPath() string

func (*Settings) BeforeMigrationHook

func (sts *Settings) BeforeMigrationHook() func(ctx context.Context, dsn string, logger *zap.Logger) error

func (*Settings) DSN

func (sts *Settings) DSN() string

func (*Settings) Migrator

func (sts *Settings) Migrator() migration.Migrator

func (*Settings) SetMigrator

func (sts *Settings) SetMigrator(m migration.Migrator)

func (*Settings) SharedConfig

func (sts *Settings) SharedConfig() Config

func (*Settings) UseSharedServer

func (sts *Settings) UseSharedServer() bool

func (*Settings) ZapOptions

func (sts *Settings) ZapOptions() []zap.Option

func (*Settings) ZapTestLevel

func (sts *Settings) ZapTestLevel() *zap.AtomicLevel

Jump to

Keyboard shortcuts

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