dbutil

package
v1.20210104.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// ErrDatabaseNameReserved is a validation failure.
	ErrDatabaseNameReserved ex.Class = "dbutil; database name is reserved"

	// ErrDatabaseNameEmpty is a validation failure.
	ErrDatabaseNameEmpty ex.Class = "dbutil; database name is empty"

	// ErrDatabaseNameInvalidFirstRune is a validation failure.
	ErrDatabaseNameInvalidFirstRune ex.Class = "dbutil; database name must start with a letter or underscore"

	// ErrDatabaseNameInvalid is a validation failure.
	ErrDatabaseNameInvalid ex.Class = "dbutil; database name must be composed of (in regex form) [a-zA-Z0-9_]"

	// ErrDatabaseNameTooLong is a validation failure.
	ErrDatabaseNameTooLong ex.Class = "dbutil; database name must be 63 characters or fewer"
)
View Source
const (
	ErrDatabaseDoesntExist ex.Class = "dbutil; database doesnt exist"
)

Error constant

Variables

View Source
var (
	// ReservedDatabaseNames are names you cannot use to create a database with.
	ReservedDatabaseNames = []string{
		"postgres",
		"defaultdb",
		"template0",
		"template1",
	}

	// DatabaseNameMaxLength is the maximum length of a database name.
	DatabaseNameMaxLength = 63
)

Functions

func CloseAllConnections

func CloseAllConnections(ctx context.Context, conn *db.Connection, databaseName string) error

CloseAllConnections closes all other connections to a database.

func CreateDatabase

func CreateDatabase(ctx context.Context, name string, opts ...db.Option) error

CreateDatabase creates a database with a given name.

Note: the `name` parameter is passed to the statement directly (not via. a parameter). You should use extreme care to not pass user submitted inputs to this function.

func CreateDatabaseIfNotExists added in v1.20210103.1

func CreateDatabaseIfNotExists(ctx context.Context, serviceEnv, database string, opts ...db.Option) error

CreateDatabaseIfNotExists creates a databse if it doesn't exist.

It will check if a given `serviceEnv` is prodlike, and if the database doesn't exist, and the `serviceEnv` is prodlike, an `ErrDatabaseDoesntExist` will be returned.

If a given `serviceEnv` is not prodlike, the database will be created with a management connection.

func DatabaseExists

func DatabaseExists(ctx context.Context, name string, opts ...db.Option) (bool, error)

DatabaseExists returns if a database exists or not.

func DropDatabase

func DropDatabase(ctx context.Context, name string, opts ...db.Option) error

DropDatabase drops a database.

func OpenManagementConnection

func OpenManagementConnection(options ...db.Option) (*db.Connection, error)

OpenManagementConnection creates a database connection to the default database (typically postgres).

func ValidateDatabaseName added in v1.20210103.1

func ValidateDatabaseName(name string) error

ValidateDatabaseName validates a database name.

Types

type BaseManager added in v1.20210103.1

type BaseManager struct {
	Conn    *db.Connection
	Options []db.InvocationOption
}

BaseManager is the manager for database tasks.

It is a base type you can use to build your own models that provides an `Invoke` method that will add default invocation options to a given invocation.

func NewBaseManager added in v1.20210103.1

func NewBaseManager(conn *db.Connection, opts ...db.InvocationOption) BaseManager

NewBaseManager creates a new manager.

func (BaseManager) Invoke added in v1.20210103.1

func (m BaseManager) Invoke(ctx context.Context, opts ...db.InvocationOption) *db.Invocation

Invoke runs a command with a given set of options merged with the manager defaults.

Jump to

Keyboard shortcuts

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