Documentation
¶
Overview ¶
Package database manages PostgreSQL connection lifecycle and runs schema migrations.
Connections are named and cached process-wide. GetConnection returns the existing *gorm.DB for a name or creates one with double-checked locking; the underlying pool is sized at 10 idle / 100 open connections with a one-hour max lifetime. Ready pings a named connection under a 1 s timeout and is the hook the health package uses for its readiness probe. GetDB, ListConnections, CloseConnection, and CloseAll cover lookup and graceful shutdown.
Migrations are embedded at build time from migrations/*.sql (see migrations.go) and driven by golang-migrate. RunMigrations applies all pending up migrations and treats migrate.ErrNoChange as success; RollbackMigration steps back one version; MigrationVersion reports the current version and dirty flag; ForceMigrationVersion pins the version without running migrations — use it only to recover a dirty state.
Index ¶
- Variables
- func CloseAll() error
- func CloseConnection(name string) error
- func ForceMigrationVersion(cfg *config.PostgresConfig, version int) error
- func GetConnection(name string, cfg *config.PostgresConfig) (*gorm.DB, error)
- func GetDB(name string) (*gorm.DB, bool)
- func ListConnections() []string
- func MigrationVersion(cfg *config.PostgresConfig) (uint, bool, error)
- func Ready(name string) bool
- func RollbackMigration(cfg *config.PostgresConfig) error
- func RunMigrations(cfg *config.PostgresConfig) error
Constants ¶
This section is empty.
Variables ¶
var Migrations embed.FS
Functions ¶
func CloseConnection ¶
CloseConnection safely closes and removes a connection
func ForceMigrationVersion ¶
func ForceMigrationVersion(cfg *config.PostgresConfig, version int) error
ForceMigrationVersion sets the migration version without running migrations Use with caution - this is for fixing dirty state issues
func GetConnection ¶
GetConnection returns a database connection by name
func ListConnections ¶
func ListConnections() []string
ListConnections returns a list of all connection names
func MigrationVersion ¶
func MigrationVersion(cfg *config.PostgresConfig) (uint, bool, error)
MigrationVersion returns the current migration version
func RollbackMigration ¶
func RollbackMigration(cfg *config.PostgresConfig) error
RollbackMigration rolls back the last migration
func RunMigrations ¶
func RunMigrations(cfg *config.PostgresConfig) error
RunMigrations executes all pending database migrations
Types ¶
This section is empty.