Files
garage/backend/internal/db/db.go
T
2026-08-06 18:26:45 +12:00

100 lines
3.0 KiB
Go

// Package db wraps the DB connection and applies the schema on startup so
// there's no separate migrate step for the operator to remember. Supports
// Postgres (production) and SQLite (local dev/testing without a real
// Postgres instance) via DB_DRIVER.
package db
import (
"fmt"
"os"
"path/filepath"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq" // postgres driver
_ "modernc.org/sqlite" // pure-Go sqlite driver, no CGO
"vlog/migrations"
)
// Connect opens the DB connection for the given driver ("postgres" or
// "sqlite") and applies the schema.
func Connect(driver, databaseURL string) (*sqlx.DB, error) {
switch driver {
case "postgres":
return connectPostgres(databaseURL)
case "sqlite":
return connectSQLite(databaseURL)
default:
return nil, fmt.Errorf("invalid DB_DRIVER %q (must be postgres or sqlite)", driver)
}
}
func connectPostgres(databaseURL string) (*sqlx.DB, error) {
conn, err := sqlx.Connect("postgres", databaseURL)
if err != nil {
return nil, fmt.Errorf("connect to postgres: %w", err)
}
sourceDriver, err := iofs.New(migrations.PostgresFS, "postgres")
if err != nil {
conn.Close()
return nil, fmt.Errorf("load embedded migrations: %w", err)
}
dbDriver, err := postgres.WithInstance(conn.DB, &postgres.Config{})
if err != nil {
conn.Close()
return nil, fmt.Errorf("init postgres migrate driver: %w", err)
}
m, err := migrate.NewWithInstance("iofs", sourceDriver, "postgres", dbDriver)
if err != nil {
conn.Close()
return nil, fmt.Errorf("init migrate instance: %w", err)
}
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
conn.Close()
return nil, fmt.Errorf("run migrations: %w", err)
}
return conn, nil
}
// connectSQLite treats databaseURL as a filesystem path to the DB file
// (e.g. "./data/vlog.db"). The schema is applied idempotently on every
// startup rather than through golang-migrate — see migrations/sqlite/schema.sql.
func connectSQLite(path string) (*sqlx.DB, error) {
if dir := filepath.Dir(path); dir != "." && dir != "" {
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, fmt.Errorf("create sqlite db directory: %w", err)
}
}
conn, err := sqlx.Connect("sqlite", path)
if err != nil {
return nil, fmt.Errorf("connect to sqlite: %w", err)
}
// SQLite handles one writer at a time anyway; capping the pool at a
// single connection sidesteps per-connection PRAGMA/locking headaches
// entirely. Fine for the local dev/testing use case this is for.
conn.SetMaxOpenConns(1)
if _, err := conn.Exec(`PRAGMA foreign_keys = ON`); err != nil {
conn.Close()
return nil, fmt.Errorf("enable sqlite foreign keys: %w", err)
}
if _, err := conn.Exec(migrations.SQLiteSchemaSQL); err != nil {
conn.Close()
return nil, fmt.Errorf("apply sqlite schema: %w", err)
}
if err := ensureSQLiteColumns(conn); err != nil {
conn.Close()
return nil, fmt.Errorf("apply sqlite column additions: %w", err)
}
return conn, nil
}