Files
flowsight/backend/internal/store/db.go
T
asepharyana 2b0c692ac5
CI / go test + web typecheck (push) Canceled after 0s
CI / build + deploy (Nix) — flowsight (push) Canceled after 0s
fix: journaled migrations (schema_migrations), rerun-safe on existing prod DB
2026-09-15 14:52:54 +07:00

101 lines
2.4 KiB
Go

// Package store persists snapshots, derived tables, routines, alerts, reports,
// the accuracy ledger, and the credit ledger on SQLite (modernc.org/sqlite,
// pure Go — no CGO so demo machines without gcc still build).
package store
import (
"database/sql"
"embed"
"fmt"
"sort"
_ "modernc.org/sqlite"
)
//go:embed migrations/*.sql
var migrationsFS embed.FS
// DB wraps database/sql with FlowSight helpers.
type DB struct {
*sql.DB
Path string
}
// Open connects to path (created if missing) and applies numbered migrations.
func Open(path string) (*DB, error) {
sqldb, err := sql.Open("sqlite", path)
if err != nil {
return nil, fmt.Errorf("store: open %s: %w", path, err)
}
sqldb.SetMaxOpenConns(1)
db := &DB{DB: sqldb, Path: path}
if err := db.migrate(); err != nil {
sqldb.Close()
return nil, err
}
return db, nil
}
func (db *DB) migrate() error {
entries, err := migrationsFS.ReadDir("migrations")
if err != nil {
return err
}
var names []string
for _, e := range entries {
if !e.IsDir() {
names = append(names, "migrations/"+e.Name())
}
}
sort.Strings(names)
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS schema_migrations(name TEXT PRIMARY KEY)`); err != nil {
return fmt.Errorf("store: schema_migrations: %w", err)
}
for _, n := range names {
var applied int
if err := db.QueryRow(`SELECT COUNT(*) FROM schema_migrations WHERE name=?`, n).Scan(&applied); err != nil {
return err
}
if applied > 0 {
continue
}
b, err := migrationsFS.ReadFile(n)
if err != nil {
return err
}
tx, err := db.Begin()
if err != nil {
return err
}
if _, err := tx.Exec(string(b)); err != nil {
_ = tx.Rollback()
return fmt.Errorf("store: migration %s: %w", n, err)
}
if _, err := tx.Exec(`INSERT INTO schema_migrations(name) VALUES(?)`, n); err != nil {
_ = tx.Rollback()
return err
}
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
// Meta helpers persist incremental cursors (quarterly_since, news_since,
// filings_since) and dedup markers so restarts resume incrementally.
// GetMeta returns a meta value or "".
func (db *DB) GetMeta(key string) string {
var v string
_ = db.QueryRow(`SELECT value FROM meta WHERE key=?`, key).Scan(&v)
return v
}
// SetMeta upserts a meta value.
func (db *DB) SetMeta(key, value string) error {
_, err := db.Exec(`INSERT INTO meta(key,value) VALUES(?,?)
ON CONFLICT(key) DO UPDATE SET value=excluded.value`, key, value)
return err
}