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 ¶
- Variables
- func Close() error
- func Default() *gorm.DB
- func DriverName() string
- func HealthCheck(ctx context.Context, timeout time.Duration) error
- func Migrate(ctx context.Context) error
- func Open(ctx context.Context, cfg Config) (*gorm.DB, error)
- func Register(d Driver)
- func SQL() *sql.DB
- type Config
- type Driver
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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.