udatabase

package
v0.0.0-...-ebe5785 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PqErrors = map[string]*uerr.UError{
	"unique_violation":   uerr.NewError(uerr.ResourceAlreadyExistsError, "Resource already exists."),
	"not_null_violation": uerr.NewError(uerr.WrongInputParameterError, "Missing required value."),
}

Functions

func BeginTx

func BeginTx(ctx context.Context, r Transactional) (context.Context, error)

func EndTx

func EndTx(ctx context.Context, r Transactional, rErr *error)

nolint:gocritic // rErr *error is required to be pointer to capture properly the errors returned by functions

func RunMigrations

func RunMigrations(db *sql.DB, cfg *DBConfig) error

RunMigrations runs SQL migrations found in the folder specified by DBConfig.MigrationsDir

Types

type DBConfig

type DBConfig struct {
	Host                 string `env:"POSTGRES_HOST" envDefault:"localhost"`
	Port                 string `env:"POSTGRES_PORT" envDefault:"5432"`
	User                 string `env:"POSTGRES_USER" envDefault:"postgres"`
	Password             string `env:"POSTGRES_PASSWORD" envDefault:"postgres"`
	DatabaseName         string `env:"POSTGRES_DATABASE" envDefault:"postgres"`
	SchemaName           string `env:"POSTGRES_SCHEMA" envDefault:"public"`
	MigrationsDir        string `env:"POSTGRES_MIGRATIONS_DIR" envDefault:"./migrations"`
	RunMigrationsOnReset bool   `env:"POSTGRES_RUN_MIGRATIONS" envDefault:"false"`
}

func NewDBConfigFromEnv

func NewDBConfigFromEnv() *DBConfig

NewDBConfigFromEnv returns a *DBConfig initialized by env variables

func (*DBConfig) CreateSchema

func (c *DBConfig) CreateSchema(db *sql.DB)

func (*DBConfig) GetConnectionString

func (c *DBConfig) GetConnectionString() string

func (*DBConfig) SetEmptyValuesToDefaults

func (c *DBConfig) SetEmptyValuesToDefaults()

func (*DBConfig) SetSearchPath

func (c *DBConfig) SetSearchPath(db *sql.DB)

type ResourcePage

type ResourcePage[T any] struct {
	Total     int64 `json:"total"`
	Limit     int64 `json:"limit"`
	Offset    int64 `json:"offset"`
	Resources []T   `json:"resources"`
}

type Transactional

type Transactional interface {
	Begin(ctx context.Context) (context.Context, error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context)
}

type TransactionalMock

type TransactionalMock struct {
	mock.Mock
}

func (*TransactionalMock) Begin

func (*TransactionalMock) Commit

func (m *TransactionalMock) Commit(ctx context.Context) error

func (*TransactionalMock) Rollback

func (m *TransactionalMock) Rollback(ctx context.Context)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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