Pre-production app, no need to preserve incremental migration history: replace the 26 migration files with one current-state schema.sql (the schema.sql comments are now the living documentation), simplify db.go to apply it once instead of tracking/rebuilding through schema_migrations, and make user_id NOT NULL everywhere now that there's no staged migration to accommodate a nullable backfill window. This removes the reason ClaimLegacyOwner/GENIUSRUN_LEGACY_OWNER_OIDC_SUB existed (binding a pre-existing singleton-schema database to one account across a staged migration), so that whole path is gone too -- the existing dev database was wiped and reseeded fresh under the new schema. Add cmd/dumpschema, which regenerates docs/DATABASE.md straight from the live schema (via store.Open + sqlite_master introspection) so the database documentation can never drift out of sync with reality. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
// Package store is geniusrun's SQLite persistence layer: activities, laps,
|
|
// per-second samples, workout kind rule config, and classification history.
|
|
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
_ "embed"
|
|
"fmt"
|
|
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
//go:embed schema.sql
|
|
var schemaSQL string
|
|
|
|
// DB wraps a *sql.DB opened against a geniusrun SQLite database file, with
|
|
// the schema already applied.
|
|
type DB struct {
|
|
*sql.DB
|
|
}
|
|
|
|
// Open opens (creating if needed) the SQLite database at path and applies
|
|
// 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.
|
|
func Open(path string) (*DB, error) {
|
|
sqlDB, err := sql.Open("sqlite", path+"?_pragma=foreign_keys(1)")
|
|
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}
|
|
if err := db.applySchema(); err != nil {
|
|
sqlDB.Close()
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
if alreadyApplied > 0 {
|
|
return nil
|
|
}
|
|
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return fmt.Errorf("begin schema tx: %w", err)
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
if _, err := tx.Exec(schemaSQL); err != nil {
|
|
return fmt.Errorf("apply schema: %w", err)
|
|
}
|
|
return tx.Commit()
|
|
}
|