database

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var Migrations embed.FS

Functions

func CloseAll

func CloseAll() error

CloseAll closes all connections

func CloseConnection

func CloseConnection(name string) error

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

func GetConnection(name string, cfg *config.PostgresConfig) (*gorm.DB, error)

GetConnection returns a database connection by name

func GetDB

func GetDB(name string) (*gorm.DB, bool)

GetDB returns a specific 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 Ready

func Ready(name string) bool

Ready checks if a specific connection is alive

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.

Jump to

Keyboard shortcuts

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