2026-07-24 21:08:07 +02:00
|
|
|
// Package store is geniusrun's SQLite persistence layer: activities, laps,
|
2026-07-17 18:33:06 +02:00
|
|
|
// per-second samples, workout kind rule config, and classification history.
|
|
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2026-07-25 19:24:19 +02:00
|
|
|
_ "embed"
|
2026-07-17 18:33:06 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-25 19:24:19 +02:00
|
|
|
//go:embed schema.sql
|
|
|
|
|
var schemaSQL string
|
2026-07-17 18:33:06 +02:00
|
|
|
|
2026-07-24 21:08:07 +02:00
|
|
|
// DB wraps a *sql.DB opened against a geniusrun SQLite database file, with
|
2026-07-25 19:24:19 +02:00
|
|
|
// the schema already applied.
|
2026-07-17 18:33:06 +02:00
|
|
|
type DB struct {
|
|
|
|
|
*sql.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open opens (creating if needed) the SQLite database at path and applies
|
2026-07-25 19:24:19 +02:00
|
|
|
// schema.sql if it hasn't been applied yet. There is no migration history --
|
|
|
|
|
// this is a pre-production app with no compatibility obligation to older
|
|
|
|
|
// database files. Edit schema.sql directly to change the schema.
|
2026-07-17 18:33:06 +02:00
|
|
|
func Open(path string) (*DB, error) {
|
2026-07-25 10:33:35 +02:00
|
|
|
sqlDB, err := sql.Open("sqlite", path+"?_pragma=foreign_keys(1)")
|
2026-07-17 18:33:06 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("open sqlite database: %w", err)
|
|
|
|
|
}
|
|
|
|
|
// SQLite only supports one writer at a time; a single connection avoids
|
|
|
|
|
// "database is locked" errors under any concurrent access from the app.
|
|
|
|
|
sqlDB.SetMaxOpenConns(1)
|
|
|
|
|
|
|
|
|
|
db := &DB{DB: sqlDB}
|
2026-07-25 19:24:19 +02:00
|
|
|
if err := db.applySchema(); err != nil {
|
2026-07-17 18:33:06 +02:00
|
|
|
sqlDB.Close()
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return db, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 19:24:19 +02:00
|
|
|
// applySchema runs schema.sql once, the first time this database file is
|
|
|
|
|
// opened -- detected by checking whether the users table already exists.
|
|
|
|
|
func (db *DB) applySchema() error {
|
|
|
|
|
var alreadyApplied int
|
|
|
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='users'`).Scan(&alreadyApplied); err != nil {
|
|
|
|
|
return fmt.Errorf("check existing schema: %w", err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
2026-07-25 19:24:19 +02:00
|
|
|
if alreadyApplied > 0 {
|
|
|
|
|
return nil
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-25 19:24:19 +02:00
|
|
|
tx, err := db.Begin()
|
2026-07-17 18:33:06 +02:00
|
|
|
if err != nil {
|
2026-07-25 19:24:19 +02:00
|
|
|
return fmt.Errorf("begin schema tx: %w", err)
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
2026-07-25 19:24:19 +02:00
|
|
|
defer tx.Rollback()
|
2026-07-25 10:33:35 +02:00
|
|
|
|
2026-07-25 19:24:19 +02:00
|
|
|
if _, err := tx.Exec(schemaSQL); err != nil {
|
|
|
|
|
return fmt.Errorf("apply schema: %w", err)
|
2026-07-25 10:20:59 +02:00
|
|
|
}
|
2026-07-25 19:24:19 +02:00
|
|
|
return tx.Commit()
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|