Files
mc-proxy/internal/db/db.go
Kyle Isom 1ad42dbbee 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>
2026-03-25 16:57:02 -07:00

46 lines
1.0 KiB
Go

package db
import (
"database/sql"
"fmt"
mcdsldb "git.wntrmute.dev/kyle/mcdsl/db"
)
// Store wraps a SQLite database connection for mc-proxy persistence.
type Store struct {
db *sql.DB
}
// 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.
func Open(path string) (*Store, error) {
database, err := mcdsldb.Open(path)
if err != nil {
return nil, fmt.Errorf("opening database: %w", err)
}
return &Store{db: database}, nil
}
// Close closes the database connection.
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) {
var count int
err := s.db.QueryRow("SELECT COUNT(*) FROM listeners").Scan(&count)
if err != nil {
return false, err
}
return count == 0, nil
}