dao

package
v0.0.0-...-b28247c Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: Apache-2.0, BSD-2-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package dao provides a thin, driver-agnostic data-access facade for CubeMaster. It owns the database/sql connection lifecycle, hands a GORM handle to business packages, and orchestrates schema migration via goose.

The package is deliberately "thin": business code keeps using *gorm.DB directly. Swapping the underlying engine (MySQL → PostgreSQL/SQLite/…) requires only adding a Driver implementation and a sibling migrations sub-directory; no business code needs to change.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotOpened = errors.New("dao: Open has not been called yet")

ErrNotOpened is returned by Default() before any successful Open() call. It exists so callers can fail gracefully in init-order bugs instead of nil-deref'ing a *gorm.DB.

Functions

func Close

func Close() error

Close releases the underlying *sql.DB. Subsequent Default()/SQL() calls panic; callers must Open() again before use. Intended for tests.

func Default

func Default() *gorm.DB

Default returns the global GORM handle established by Open. It panics only when called before Open; this lets business packages keep their "fail loudly on misconfiguration" stance without burying nil checks.

func DriverName

func DriverName() string

DriverName returns the name of the currently opened driver (e.g. "mysql" or "postgres"). It returns "" if Open has not been called. Business code uses this to select dialect-specific SQL fragments.

func HealthCheck

func HealthCheck(ctx context.Context, timeout time.Duration) error

HealthCheck pings the database with a short timeout. It is used by the startup sequence in main.go to fail-fast if the DB is unreachable.

func Migrate

func Migrate(ctx context.Context) error

Migrate runs every pending schema migration under the configured driver's dialect, taking the driver-provided cluster-wide SessionLocker so multiple instances starting up simultaneously serialize safely. Returns nil when the database is already at HEAD.

func Open

func Open(ctx context.Context, cfg Config) (*gorm.DB, error)

Open establishes the shared database connection. It is safe to call multiple times; subsequent calls with the same Driver+DSN are no-ops (idempotent), while a different DSN/driver returns an error rather than silently clobbering the global handle.

func Register

func Register(d Driver)

Register makes a driver available by name. Re-registering the same name panics; this is a programmer error caught at process start, not a recoverable runtime condition.

func SQL

func SQL() *sql.DB

SQL returns the raw *sql.DB. Reserved for the migration package and integration tests; business code should not use it.

Types

type Config

type Config struct {
	Driver string

	Addr   string
	User   string
	Pwd    string
	DBName string

	ConnTimeoutSeconds     int
	ReadTimeoutSeconds     int
	WriteTimeoutSeconds    int
	MaxIdleConns           int
	MaxOpenConns           int
	MaxConnLifeTimeSeconds int

	// MigrationLockTimeoutSeconds bounds GET_LOCK / pg_advisory_lock waits.
	// Defaults to 60s when zero.
	MigrationLockTimeoutSeconds int

	Extra map[string]string
}

Config captures the minimal data every driver needs. Concrete drivers may read additional, engine-specific knobs out of the Extra map. The shape is stable; new engines extend via Extra rather than by growing required fields, so swapping engines never silently invalidates an old yaml.

type Driver

type Driver interface {
	// Name returns a short, stable identifier (e.g. "mysql"). It is used as
	// the sub-directory name under migrations/ and as the goose dialect.
	Name() string

	// Open returns an open *sql.DB and a GORM handle wrapping it. The
	// returned *sql.DB MUST be the same handle that backs *gorm.DB so that
	// goose can run its migrations on the very same connection pool.
	Open(ctx context.Context, cfg Config) (*sql.DB, *gorm.DB, error)

	// SessionLocker returns a SessionLocker that wraps the entire
	// Up()/Down() run with an engine-native cluster-wide lock. It is the
	// "outer" half of the two-layer locking scheme; the "inner" per-file
	// lock is asserted from inside each migration SQL via
	// CALL cubemaster_acquire_migration_lock(...) (see migrations).
	SessionLocker(cfg Config) lock.SessionLocker
}

Driver abstracts everything an engine-specific package must provide so the generic facade in dao.go can stay engine-agnostic.

Directories

Path Synopsis
driver
mysql
Package mysql plugs the MySQL engine into CubeDB/dao.
Package mysql plugs the MySQL engine into CubeDB/dao.
postgres
Package postgres plugs the PostgreSQL engine into CubeDB/dao.
Package postgres plugs the PostgreSQL engine into CubeDB/dao.

Jump to

Keyboard shortcuts

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