database

package
v2.0.15 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: Apache-2.0, Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const PgDuplicatedForeignKey = "23503"
View Source
const PgDuplicatedKey = "23505"
View Source
const PqDbAlreadyExistsCode = "42P04"
View Source
const PqInvalidDBCode = "3D000"

Variables

This section is empty.

Functions

func CreatePostgresDbIfNotExists

func CreatePostgresDbIfNotExists(ctx context.Context, pgConfig PostgresConfig) (*sqlx.DB, error)

CreatePostgresDbIfNotExists creates DB if it doesn't exist for the passed in config

func CreatePostgresReadOnlyDbConnection

func CreatePostgresReadOnlyDbConnection(ctx context.Context, pgConfig PostgresConfig) (*sqlx.DB, error)

CreatePostgresReadOnlyDbConnection creates readonly DB connection and returns the sqlx.DB object and error

func GetDB

func GetDB(ctx context.Context, dbConfig *DbConfig) (*sqlx.DB, error)

GetDB uses the dbConfig to create a sqlx DB object. If the db doesn't exist for the dbConfig then a new one is created using the default db for the provider. eg : postgres has default dbName as postgres

func GetPostgresDsn added in v2.0.8

func GetPostgresDsn(ctx context.Context, pgConfig PostgresConfig) string

Produces the DSN (data source name) for opening a postgres db connection.

func GetReadOnlyDB

func GetReadOnlyDB(ctx context.Context, dbConfig *DbConfig) (*sqlx.DB, error)

GetReadOnlyDB uses the dbConfig to create a sqlx DB object for the read replica passed via the config

func IsPgErrorWithCode

func IsPgErrorWithCode(err error, code string) bool

func Migrate

func Migrate(ctx context.Context, db *sqlx.DB, prefix string, migrations fs.FS) error

Migrate applies all unapplied SQL migration files from the provided fs.FS. Migration files are expected to be located under "sql/" and follow the naming convention "NNNN_description.sql". Files ending with "_down.sql" are skipped. Migrations are applied in lexicographic order, each within its own transaction.

The prefix parameter namespaces versions in the shared schema_migrations table so that multiple services sharing the same database do not collide (e.g. prefix "runs" records version "runs/001_init_schema").

func Rollback

func Rollback(ctx context.Context, db *sqlx.DB, prefix string, migrations fs.FS) error

Rollback rolls back the most recently applied migration for the given prefix by executing its corresponding _down.sql file from the provided fs.FS.

func RunTestMain added in v2.0.12

func RunTestMain(m *testing.M, port uint32, dbName string, db **sqlx.DB, migrate func(*sqlx.DB) error) int

RunTestMain starts an embedded PostgreSQL instance, runs migrate, executes m.Run(), and returns an exit code suitable for os.Exit. The connected *sqlx.DB is written to db. Intended for use in TestMain functions:

func TestMain(m *testing.M) {
    os.Exit(database.RunTestMain(m, 15432, "mydb", &testDB, func(db *sqlx.DB) error {
        return db.Exec(...)
    }))
}

Types

type DbConfig

type DbConfig struct {
	// deprecated: Please use Postgres.Host
	DeprecatedHost string `json:"host" pflag:"-,deprecated"`
	// deprecated: Please use Postgres.Port
	DeprecatedPort int `json:"port" pflag:"-,deprecated"`
	// deprecated: Please use Postgres.DbName
	DeprecatedDbName string `json:"dbname" pflag:"-,deprecated"`
	// deprecated: Please use Postgres.User
	DeprecatedUser string `json:"username" pflag:"-,deprecated"`
	// deprecated: Please use Postgres.Password
	DeprecatedPassword string `json:"password" pflag:"-,deprecated"`
	// deprecated: Please use Postgres.PasswordPath
	DeprecatedPasswordPath string `json:"passwordPath" pflag:"-,deprecated"`
	// deprecated: Please use Postgres.ExtraOptions
	DeprecatedExtraOptions string `json:"options" pflag:"-,deprecated"`
	// deprecated: Please use Postgres.Debug
	DeprecatedDebug bool `json:"debug" pflag:"-,deprecated"`

	// Deprecated: was GORM-specific, retained for config compatibility.
	EnableForeignKeyConstraintWhenMigrating bool            `json:"enableForeignKeyConstraintWhenMigrating" pflag:",Whether to enable gorm foreign keys when migrating the db"`
	MaxIdleConnections                      int             `json:"maxIdleConnections" pflag:",maxIdleConnections sets the maximum number of connections in the idle connection pool."`
	MaxOpenConnections                      int             `json:"maxOpenConnections" pflag:",maxOpenConnections sets the maximum number of open connections to the database."`
	ConnMaxLifeTime                         config.Duration `json:"connMaxLifeTime" pflag:",sets the maximum amount of time a connection may be reused"`
	Postgres                                PostgresConfig  `json:"postgres,omitempty"`
}

DbConfig is used to for initiating the database connection with the store that holds registered entities (e.g. workflows, tasks, launch plans...)

func GetConfig

func GetConfig() *DbConfig

func (DbConfig) GetPFlagSet

func (cfg DbConfig) GetPFlagSet(prefix string) *pflag.FlagSet

GetPFlagSet will return strongly types pflags for all fields in DbConfig and its nested types. The format of the flags is json-name.json-sub-name... etc.

type PostgresConfig

type PostgresConfig struct {
	Host            string `json:"host" pflag:",The host name of the database server"`
	ReadReplicaHost string `json:"readReplicaHost" pflag:",The host name of the read replica database server"`
	Port            int    `json:"port" pflag:",The port name of the database server"`
	DbName          string `json:"dbname" pflag:",The database name"`
	User            string `json:"username" pflag:",The database user who is connecting to the server."`
	// Either Password or PasswordPath must be set.
	Password     string `json:"password" pflag:",The database password."`
	PasswordPath string `json:"passwordPath" pflag:",Points to the file containing the database password."`
	ExtraOptions string `` /* 135-byte string literal not displayed */
	Debug        bool   `json:"debug" pflag:" Whether or not to start the database connection with debug mode enabled."`
}

PostgresConfig includes specific config options for opening a connection to a postgres database.

func (PostgresConfig) IsEmpty

func (s PostgresConfig) IsEmpty() bool

Jump to

Keyboard shortcuts

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