database

package
v0.0.0-...-e4d8600 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const SUBSYTEM = "database"

Variables

View Source
var (
	ErrNoHealthyConn            = errors.New("no healthy mysql connection available")
	ErrUnsupportedBalancingMode = errors.New("unsupported balancing mode")
)

Functions

func GetMysqlDsn

func GetMysqlDsn(connection *MysqlConnectionOptions, multiStatements bool) string

func IsTx

func IsTx(executor boil.ContextExecutor) bool

IsTx checks whether the given executor is a transaction

func Migrate

func Migrate(conn *MysqlConnectionOptions, config *MigrationConfig) error

func MustCommit

func MustCommit(tx *sql.Tx)

MustCommit commits the given transaction or panics if an error occurs

func MustRollbackTx

func MustRollbackTx(tx *sql.Tx)

MustRollbackTx rolls back the given transaction or panics if an error occurs

Types

type BalancingMode

type BalancingMode string
const (
	Random  BalancingMode = "random"  // the connection pool will return a random database node each time
	Ordered BalancingMode = "ordered" // the connection pool will always return the first available database node
)

type ClusterConfig

type ClusterConfig struct {
	IsGaleraCluster *bool               `yaml:"is_galera_cluster" json:"is_galera_cluster" mapstructure:"is_galera_cluster" validate:"required"`
	BalancingMode   BalancingMode       `yaml:"balancing_mode" json:"balancing_mode" mapstructure:"balancing_mode" validate:"required,oneof=random ordered"`
	User            string              `yaml:"user" json:"user" mapstructure:"user" validate:"required"`
	Password        string              `yaml:"password" json:"password" mapstructure:"password" validate:"required"`
	Database        string              `yaml:"database" json:"database" mapstructure:"database" validate:"required"`
	Connections     []*ConnectionConfig `yaml:"connections" json:"connections" mapstructure:"connections" validate:"gte=1,dive,required"`
}

type ConnectionConfig

type ConnectionConfig struct {
	Host string `yaml:"host" json:"host" mapstructure:"host" validate:"required"`
	Port int    `yaml:"port" json:"port" mapstructure:"port" validate:"required"`
}

type MigrationConfig

type MigrationConfig struct {
	MigrationDir string `yaml:"migration_dir" json:"migration_dir" mapstructure:"migration_dir" validate:"required"` // Directory for SQL migration files
}

type MysqlConnection

type MysqlConnection struct {
	Name     string
	Dsn      string
	DB       *sql.DB
	Config   *MysqlConnectionOptions
	IsActive bool
}

type MysqlConnectionOptions

type MysqlConnectionOptions struct {
	User     string
	Password string
	Database string
	Host     string
	Port     int
}

type MysqlConnectionPool

type MysqlConnectionPool struct {
	Connections []*MysqlConnection
	Mutex       *sync.Mutex
	PingsTicker *time.Ticker
	PingsDone   chan bool
	Config      *ClusterConfig
}

func NewMysqlConnectionPool

func NewMysqlConnectionPool(config *ClusterConfig) (*MysqlConnectionPool, error)

func (*MysqlConnectionPool) BeginTx

func (m *MysqlConnectionPool) BeginTx() (*sql.Tx, error)

BeginTx starts a database transaction

func (*MysqlConnectionPool) Close

func (m *MysqlConnectionPool) Close()

Close closes all database connections from the pool

func (*MysqlConnectionPool) GetActiveConfig

func (m *MysqlConnectionPool) GetActiveConfig() (*MysqlConnectionOptions, error)

func (*MysqlConnectionPool) GetConnection

func (m *MysqlConnectionPool) GetConnection() (*sql.DB, error)

GetConnection returns an active connection or ErrNoHealthyConn if none of the connections from the pool is healthy

func (*MysqlConnectionPool) MustBeginTx

func (m *MysqlConnectionPool) MustBeginTx() *sql.Tx

MustBeginTx starts a database transaction or panics if an error occurs

func (*MysqlConnectionPool) MustGetConnection

func (m *MysqlConnectionPool) MustGetConnection() *sql.DB

MustGetConnection returns an active connection of panics if none of the connections from the pool is healthy

Jump to

Keyboard shortcuts

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