Migrate db and config to mcdsl

- db.Open: delegate to mcdsl/db.Open (WAL, FK, busy timeout, 0600)
- db.Migrate: convert function-based migrations to mcdsl/db.Migration
  SQL strings, delegate to mcdsl/db.Migrate
- db.Snapshot: delegate to mcdsl/db.Snapshot (adds 0600 permissions)
- config: replace local Duration with mcdsl/config.Duration alias,
  replace Load with mcdsl/config.Load[T] + Validator interface
- Remove direct modernc.org/sqlite and go-toml/v2 dependencies
  (now indirect via mcdsl)
- Update TestEnvOverrideInvalidDuration: mcdsl silently ignores
  invalid env duration values (behavioral change from migration)
- All existing tests pass

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-25 16:57:02 -07:00
parent 564e0a9c67
commit 1ad42dbbee
7 changed files with 116 additions and 245 deletions

View File

@@ -3,9 +3,8 @@ package db
import (
"database/sql"
"fmt"
"os"
_ "modernc.org/sqlite"
mcdsldb "git.wntrmute.dev/kyle/mcdsl/db"
)
// Store wraps a SQLite database connection for mc-proxy persistence.
@@ -14,36 +13,14 @@ type Store struct {
}
// Open opens (or creates) the SQLite database at path with WAL mode,
// foreign keys, and a busy timeout. The file is created with 0600 permissions.
// foreign keys, and a busy timeout. The file is created with 0600
// permissions.
func Open(path string) (*Store, error) {
// Ensure the file has restrictive permissions if it doesn't exist.
if _, err := os.Stat(path); os.IsNotExist(err) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, fmt.Errorf("creating database file: %w", err)
}
f.Close()
}
db, err := sql.Open("sqlite", path)
database, err := mcdsldb.Open(path)
if err != nil {
return nil, fmt.Errorf("opening database: %w", err)
}
// Apply connection pragmas.
pragmas := []string{
"PRAGMA journal_mode = WAL",
"PRAGMA foreign_keys = ON",
"PRAGMA busy_timeout = 5000",
}
for _, p := range pragmas {
if _, err := db.Exec(p); err != nil {
db.Close()
return nil, fmt.Errorf("setting pragma %q: %w", p, err)
}
}
return &Store{db: db}, nil
return &Store{db: database}, nil
}
// Close closes the database connection.
@@ -51,6 +28,11 @@ func (s *Store) Close() error {
return s.db.Close()
}
// DB returns the underlying *sql.DB for use with mcdsl functions.
func (s *Store) DB() *sql.DB {
return s.db
}
// IsEmpty returns true if the listeners table has no rows.
// Used to determine if the database needs seeding from config.
func (s *Store) IsEmpty() (bool, error) {