Files
egutierrez aceb10b672 feat(registry): expose Conn() and Path() on registry.DB
Allows external readers (registry_mcp app) to issue raw aggregations
(e.g. fn_list_domains) and inspect the active db path without
duplicating the connection setup logic.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 13:31:08 +02:00

71 lines
1.7 KiB
Go

package registry
import (
"database/sql"
"fmt"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
)
// DB wraps a SQLite connection for the registry.
type DB struct {
conn *sql.DB
path string
}
// Open opens or creates the registry database at the given path.
func Open(path string) (*DB, error) {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, fmt.Errorf("creating db directory: %w", err)
}
conn, err := sql.Open("sqlite3", path+"?_foreign_keys=on")
if err != nil {
return nil, fmt.Errorf("opening database: %w", err)
}
// WAL mode: enables concurrent reads while writing.
if _, err := conn.Exec("PRAGMA journal_mode=WAL"); err != nil {
conn.Close()
return nil, fmt.Errorf("setting WAL mode: %w", err)
}
if err := migrate(conn); err != nil {
conn.Close()
return nil, fmt.Errorf("running migrations: %w", err)
}
return &DB{conn: conn, path: path}, nil
}
// Close closes the database connection.
func (db *DB) Close() error {
return db.conn.Close()
}
// WalCheckpoint flushes the WAL to the main database file so external
// readers (e.g. Metabase via bind mount) see the latest data.
func (db *DB) WalCheckpoint() {
db.conn.Exec("PRAGMA wal_checkpoint(TRUNCATE)")
}
// Drop removes the database file. Used by `fn index` to regenerate.
func (db *DB) Drop() error {
db.Close()
return os.Remove(db.path)
}
// Conn returns the underlying *sql.DB for callers that need raw SQL access
// (e.g. registry_mcp for aggregations not covered by Search/Get helpers).
func (db *DB) Conn() *sql.DB {
return db.conn
}
// Path returns the filesystem path of the database file.
func (db *DB) Path() string {
return db.path
}