db

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotConnected = errors.New("not connected to the database")
	ErrReadOnly     = errors.New("connected in readonly mode - only readonly transactions allowed")
	ErrNotfound     = errors.New("record not found or no rows returned")
)
View Source
var (
	ErrNotFound      = errors.New("object not found in database")
	ErrAlreadyExists = errors.New("object already exists in database")
)

Functions

func BeginTx

func BeginTx(ctx context.Context, opts *sql.TxOptions) (tx *sql.Tx, err error)

BeginTx creates a transaction with the connected dtabase; errors if not connected.

func Check

func Check(err error) error

func Close

func Close() (err error)

Close the database safely and allow reconnect after close.

func Connect

func Connect(conf config.DatabaseConfig) (err error)

Connect to the Postgres database specified by the DSN. Connecting in read-only mode is managed by the package, not by the database. Multiple or concurrent calls to Connect will be ignored even if a different configuration is passed.

func InitializeSchema

func InitializeSchema() (err error)

Initialize schema applies any unapplied migrations to the database and should be run when the database is first connected to. If empty is true then the migration table is created and all migrations are applied. If it is not true then the current migration of the database is fetched and all unapplied migrations are applied.

func Mock

func Mock() sqlmock.Sqlmock

Mock returns the sql mock object for use in unit testing

func Prep

func Prep(query string, args ...any) (string, []any)

Prep converts named query arguments into positional arguments for postgres queries.

Types

type Migration

type Migration struct {
	ID      int       // The unique sequence ID of the migration
	Name    string    // The human readable name of the migration
	Version string    // The package version when the migration was applied
	Created time.Time // The timestamp when the migration was applied
	Path    string    // The path of the migration in the filesystem
}

Migration is used to represent both a SQL migration from the embedded file system and a migration record in the database. These records are compared to ensure the database is as up to date as possible before the application starts.

func Migrations

func Migrations() (data []*Migration, err error)

Migrations returns the migration files from the embedded file system.

func (*Migration) SQL

func (m *Migration) SQL() (_ string, err error)

SQL loads the schema sql query from the embedded file on disk.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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