pgtestdb

package module
v0.0.0-...-341c3e5 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: AGPL-3.0 Imports: 9 Imported by: 0

README

PostgreSQL database migrator for Golang tests

Overview

The library creates a test database and runs migrations or fixtures on it.
The newly database name is provided together with a cleanup function.
In case test run fails, the cleanup function could not be run, thus the database could not deleted and be available for more analysis. The database naming convention is "t" plus unix time seconds plus the test name.

The migration info is written in a table which name can be overriden.

How to use

Migration files

Migration files naming convention should be "V0001__name.sql".
This naming convention can be overridden by passing the desired regex.

Fixture files

On top of migration files, fixture files can be applied by concentrating their paths in MigrationFilePaths.

Template Fixture files

On top of normal fixture files, template fixtures can be used. This should come handy when specific IDs should be used across fixture files.
The template fixture files should be added in TemplateFilePaths together with a render function.
Any render engine could be used as long as the render function signature is met.

How to integrate with your code

Database could be created as in the makefile target.
For files and render function see external test file example.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PGTestDB

type PGTestDB struct {
	ConnectionURL                string
	RegexValidationMigrationFile sql.NullString // default is `^V\d{4}__[a-zA-Z0-9_]+\.sql$`
	MigrationsTableName          sql.NullString // overrides default name

	TemplateRenderFunction func(string) (string, error)
	TemplateFilePaths      []string

	MigrationDirectories []fs.FS
	MigrationFilePaths   []string

	T *testing.T
}

Connection in form of "postgres://%s:%s@%s:%s/%s?", db user, db password, host, port, db name.

func (*PGTestDB) Execute

func (pg *PGTestDB) Execute() (string, func())

Execute performs migrations and returns created db name and cleanup function that should be run on test exit.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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