Documentation ¶
Index ¶
- Constants
- Variables
- func CloseAllConnections(ctx context.Context, conn *db.Connection, databaseName string) error
- func CreateDatabase(ctx context.Context, name string, opts ...db.Option) error
- func CreateDatabaseIfNotExists(ctx context.Context, serviceEnv, database string, opts ...db.Option) error
- func DatabaseExists(ctx context.Context, name string, opts ...db.Option) (bool, error)
- func DropDatabase(ctx context.Context, name string, opts ...db.Option) error
- func OpenManagementConnection(options ...db.Option) (*db.Connection, error)
- func ValidateDatabaseName(name string) error
- type BaseManager
Constants ¶
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" )
const (
ErrDatabaseDoesntExist ex.Class = "dbutil; database doesnt exist"
)
Error constant
Variables ¶
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 ¶
CloseAllConnections closes all other connections to a database.
func CreateDatabase ¶
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 ¶
DatabaseExists returns if a database exists or not.
func DropDatabase ¶
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
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.