Documentation
¶
Overview ¶
Package db also includes functions related to database management tasks like creating, dropping, and generating unique names for test databases within emberkit.
Package db provides functionality for managing the embedded PostgreSQL server instance used by emberkit, including starting, stopping, and port assignment.
Index ¶
- func AssignRandomPort(config *config.Config, logger *zap.Logger) error
- func BuildDSN(cfg config.Config) string
- func CreateDatabase(ctx context.Context, config config.Config, testDBName string, ...) (maintenanceDSN string, err error)
- func DropTestDatabaseFunc(maintenanceDSN, testDBName string, keepDatabase bool, logger *zap.Logger) cleanup.Func
- func GenerateUniqueDBName(prefix string) (string, error)
- func StartServer(ctx context.Context, config config.Config, instanceWorkDir string, ...) (*embeddedpostgres.EmbeddedPostgres, error)
- func StopEmbeddedServer(embeddedDBPtr **embeddedpostgres.EmbeddedPostgres, logger *zap.Logger) cleanup.Func
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssignRandomPort ¶
AssignRandomPort checks if the Port in the provided Config is 0. If it is, it finds a free TCP port on the specified host (or localhost by default) and updates the Config's Port field with the assigned port number. It modifies the provided config pointer directly.
func BuildDSN ¶
BuildDSN constructs a DSN suitable for connecting to a specific database (often 'postgres' for maintenance tasks) using the connection details from the provided config. It defaults to the 'postgres' database if cfg.Database is empty.
func CreateDatabase ¶
func CreateDatabase(ctx context.Context, config config.Config, testDBName string, logger *zap.Logger) (maintenanceDSN string, err error)
CreateDatabase connects to the administrative database (e.g., "postgres") specified in the config, using the provided maintenance DSN, and executes a `CREATE DATABASE` command to create the uniquely named test database (`testDBName`).
It ensures connectivity to the maintenance database before attempting creation. Returns the maintenance DSN used (for potential use in cleanup registration) and an error if connection or creation fails.
func DropTestDatabaseFunc ¶
func DropTestDatabaseFunc(maintenanceDSN, testDBName string, keepDatabase bool, logger *zap.Logger) cleanup.Func
DropTestDatabaseFunc returns a cleanup function suitable for use with `cleanup.Manager`. The returned function connects to the maintenance database using `maintenanceDSN`, terminates any active connections to the `testDBName`, and then drops the `testDBName` database.
It respects the `keepDatabase` flag; if true, it logs that the drop is skipped and returns nil. It includes timeouts and logging for robustness during cleanup.
func GenerateUniqueDBName ¶
GenerateUniqueDBName creates a unique, sanitized database or runtime directory name suitable for use with PostgreSQL. It combines the given prefix with a random hexadecimal string.
The name is lowercased, hyphens are replaced with underscores, and it's truncated to a maximum length of 63 characters to comply with PostgreSQL identifier limits.
func StartServer ¶
func StartServer(ctx context.Context, config config.Config, instanceWorkDir string, logger *zap.Logger) (*embeddedpostgres.EmbeddedPostgres, error)
StartServer initializes and starts an embedded PostgreSQL server instance using the configuration provided in `config`. It uses the `instanceWorkDir` for storing runtime data specific to this server instance.
It configures the embedded server (version, port, credentials, paths, logger, timeout) based on the `config` and then attempts to start it. Returns a pointer to the started `embeddedpostgres.EmbeddedPostgres` instance or an error if startup fails.
func StopEmbeddedServer ¶
func StopEmbeddedServer(embeddedDBPtr **embeddedpostgres.EmbeddedPostgres, logger *zap.Logger) cleanup.Func
StopEmbeddedServer returns a cleanup function suitable for use with `cleanup.Manager`. The returned function stops the embedded PostgreSQL server instance pointed to by `embeddedDBPtr`.
It takes a pointer-to-a-pointer (`**embeddedpostgres.EmbeddedPostgres`) to the server instance. This allows the cleanup function to set the original variable to `nil` after successfully stopping the server, preventing potential issues with attempting to stop an already stopped server.
Types ¶
This section is empty.