database

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: Apache-2.0 Imports: 20 Imported by: 8

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, gormConfig *gorm.Config, pgConfig PostgresConfig) (*gorm.DB, error)

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

func GetDB

func GetDB(ctx context.Context, dbConfig *DbConfig, logConfig *logger.Config) (
	*gorm.DB, error)

GetDB uses the dbConfig to create gorm 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 GetGormLogger

func GetGormLogger(ctx context.Context, logConfig *logger.Config) gormLogger.Interface

GetGormLogger converts between the kozmostdlib configured log level to the equivalent gorm log level and outputs a gorm/logger implementation accordingly configured.

func IsPgErrorWithCode

func IsPgErrorWithCode(err error, code string) bool

func Migrate

func Migrate(ctx context.Context, databaseConfig *DbConfig, migrations []*gormigrate.Migration) error

Migrate runs all configured migrations

func Rollback

func Rollback(ctx context.Context, databaseConfig *DbConfig, migrations []*gormigrate.Migration) error

Rollback rolls back the last migration

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"`

	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"`
	SQLite                                  SQLiteConfig    `json:"sqlite,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"`
	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

type SQLiteConfig

type SQLiteConfig struct {
	File string `` /* 167-byte string literal not displayed */
}

SQLiteConfig can be used to configure

func (SQLiteConfig) IsEmpty

func (s SQLiteConfig) IsEmpty() bool

Jump to

Keyboard shortcuts

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