Database, Migrations & Seeders
github.com/mmycin/goforge/core/database manages GORM database drivers, safe DSN formatting, connection pooling, Atlas/GORM migrations, SQLC configuration, and a transactional seeder framework.
Supported Drivers & Registry
Each driver under core/database/drivers/ registers itself via database.Register(name, factory) in init(). Importing _ "github.com/myorg/myapp/core/database/drivers" activates all compiled drivers (goforge new deletes unselected driver files):
DB_CONNECTION | GORM Driver | Notes |
|---|---|---|
sqlite | gorm.io/driver/sqlite (mattn/go-sqlite3) | Requires CGO_ENABLED=1 (gcc) |
postgres | gorm.io/driver/postgres (pgx/v5) | Pure Go |
mysql | gorm.io/driver/mysql | Pure Go |
sqlserver | gorm.io/driver/sqlserver | Pure Go |
Safe DSN Construction (core/database/dsn.go)
DSNs are constructed with proper credential escaping to prevent injection or parsing issues when passwords contain special characters (@, :, /, ?):
mysql:mysqldriver.NewConfig()(Net: "tcp",ParseTime: true,Loc: time.Local,charset=utf8mb4) →FormatDSN().postgres:url.URL{Scheme: "postgres", User: url.UserPassword(...), Host: net.JoinHostPort(...)}withsslmode(DB_SSL_MODE, default"disable").sqlserver:url.URL{Scheme: "sqlserver", User: url.UserPassword(...), Host: net.JoinHostPort(...)}with?database=<name>.sqlite: For file-backed DBs (not"",:memory:,file:..., or?), automatically appends?_busy_timeout=5000&_journal_mode=WAL&_foreign_keys=on.
Connection Pooling & Database API
database.Connect(cfg config.DBConfig) (*Database, error) opens GORM, applies pool settings (DB_MAX_OPEN_CONNS=25, DB_MAX_IDLE_CONNS=5, DB_CONN_MAX_LIFETIME=5m), and pings with a 5-second timeout (closing the pool on failure):
type Database struct {
Gorm *gorm.DB
Sqlc any
}db.GetGorm() (*gorm.DB, error)— nil-safe accessor.db.Ping(ctx context.Context) error— used by HTTP/readyreadiness probe.db.Close() error— closes the underlying*sql.DBpool (invoked bydi.Lifecycle.OnStop).
Migrations: Atlas vs GORM AutoMigrate
- Atlas (
DB_MIGRATOR=atlas, default):core/database/atlas.hclinvokesgo run ./app/main.go loader(//go:build !prod) viaariga.io/atlas-provider-gorm/gormschemato diff yourservices.Model()structs againstinternal/database/migrations/. (Note:atlas.hclsupportssqlite,mysql, andpostgres;sqlserveris not mapped inatlas.hcl— useDB_MIGRATOR=gormfor SQL Server.) - GORM AutoMigrate (
DB_MIGRATOR=gorm): runsgormDB.AutoMigrate(services.Model()...).
SQLC (core/database/sqlc.yaml)
SQLC v2 is configured to read schemas from ../../internal/database/migrations and queries from ../../internal/database/queries (e.g. todo_query.sql), generating Go code into ../../internal/database/gen (package sqlc) with emit_json_tags, emit_interface, emit_exact_table_names, emit_empty_slices, and omit_unused_structs.
Seeder Framework (core/database/seeder.go)
type Seeder interface {
Name() string
Run(db *gorm.DB) error
}database.RegisterSeeder(s Seeder)— registers a seeder globally (panics on duplicate name).database.ResolveSeeder(name string) (Seeder, bool)/database.AllSeederNames() []string.database.Call(db *gorm.DB, seeders ...Seeder) error— runs all seeders sequentially inside a singledb.Transaction, rolling back everything if any seeder fails.
Built-in seeders in internal/database/seeders/:
DatabaseSeeder— default root seeder (db:seedwith no--classflag).TodoSeeder— idempotentFirstOrCreateseeder inserting 3 initial todos.RegisterAll()— blank-import anchor ensuringinit()functions run.
