Skip to content

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_CONNECTIONGORM DriverNotes
sqlitegorm.io/driver/sqlite (mattn/go-sqlite3)Requires CGO_ENABLED=1 (gcc)
postgresgorm.io/driver/postgres (pgx/v5)Pure Go
mysqlgorm.io/driver/mysqlPure Go
sqlservergorm.io/driver/sqlserverPure 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(...)} with sslmode (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):

go
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 /ready readiness probe.
  • db.Close() error — closes the underlying *sql.DB pool (invoked by di.Lifecycle.OnStop).

Migrations: Atlas vs GORM AutoMigrate ​

  • Atlas (DB_MIGRATOR=atlas, default): core/database/atlas.hcl invokes go run ./app/main.go loader (//go:build !prod) via ariga.io/atlas-provider-gorm/gormschema to diff your services.Model() structs against internal/database/migrations/. (Note: atlas.hcl supports sqlite, mysql, and postgres; sqlserver is not mapped in atlas.hcl — use DB_MIGRATOR=gorm for SQL Server.)
  • GORM AutoMigrate (DB_MIGRATOR=gorm): runs gormDB.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) ​

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 single db.Transaction, rolling back everything if any seeder fails.

Built-in seeders in internal/database/seeders/:

  • DatabaseSeeder — default root seeder (db:seed with no --class flag).
  • TodoSeeder — idempotent FirstOrCreate seeder inserting 3 initial todos.
  • RegisterAll() — blank-import anchor ensuring init() functions run.

Released under the Apache-2.0 License.