Implements the Pansy v1 scaffold (epic #20, phase 0). #1 Backend: Go module (pure-Go, builds with CGO_ENABLED=0), env config, gin server with slog + recovery + /api/v1/healthz, modernc SQLite store with WAL/busy_timeout/foreign_keys pragmas, embedded numbered-migration runner, full initial schema (users, sessions, gardens, garden_shares, garden_objects, plants, plantings), domain structs + sentinel errors, graceful shutdown. #2 Frontend: Vite + React 19 + TS (strict) + Tailwind 4 + TanStack Router/Query, typed /api/v1 fetch wrapper (ApiError carries status + body so later issues can read 409 conflict rows), dev proxy /api -> :8080, responsive app shell with stub pages for all five routes. #3 Single binary: //go:embed of the web build with a committed placeholder, SPA fallback (deep links, immutable asset caching, JSON 404 for unmatched /api and missing assets), Makefile (web/build/dev/test), README quickstart + env var table. Verified: go build/vet/test clean (CGO off); binary migrates idempotently and serves healthz; web tsc + build clean; integrated binary serves the SPA, deep links, and correct cache headers; app mounts and navigates all five routes at desktop and 375px widths. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
69 lines
2.6 KiB
Go
69 lines
2.6 KiB
Go
// Package store provides pansy's durable persistence layer: a pure-Go SQLite
|
|
// database (modernc.org/sqlite — no cgo, so the binary stays static) with a
|
|
// tiny embedded migration runner. Entity query methods are added per feature
|
|
// issue; this file only handles opening and the connection pragmas.
|
|
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
_ "modernc.org/sqlite" // pure-Go driver, registered as "sqlite"
|
|
)
|
|
|
|
// DB is a handle to the SQLite database backing pansy. Safe for concurrent use:
|
|
// WAL mode allows many readers with one writer, and busy_timeout makes a
|
|
// contended writer wait rather than fail with SQLITE_BUSY.
|
|
type DB struct {
|
|
sql *sql.DB
|
|
}
|
|
|
|
// Open opens (creating if absent) the SQLite database at path and returns a DB.
|
|
// The pragmas — busy_timeout, WAL journal, and foreign-key enforcement — are
|
|
// applied per connection via the DSN so every pooled connection inherits them.
|
|
// A path of ":memory:" yields an ephemeral in-memory database (useful in tests);
|
|
// the pool is pinned to a single connection so all queries see the same DB.
|
|
// The caller owns the returned DB and must Close it.
|
|
func Open(path string) (*DB, error) {
|
|
dsn, memory := buildDSN(path)
|
|
sqldb, err := sql.Open("sqlite", dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: open %q: %w", path, err)
|
|
}
|
|
if memory {
|
|
// Each new connection to a bare ":memory:" DSN gets its OWN empty database,
|
|
// so a pooled second connection wouldn't see the migrated schema. Pin the
|
|
// pool to one connection to keep an in-memory DB coherent.
|
|
sqldb.SetMaxOpenConns(1)
|
|
}
|
|
if err := sqldb.Ping(); err != nil {
|
|
sqldb.Close()
|
|
return nil, fmt.Errorf("store: ping %q: %w", path, err)
|
|
}
|
|
return &DB{sql: sqldb}, nil
|
|
}
|
|
|
|
// buildDSN turns a filesystem path (or ":memory:") into a modernc DSN carrying
|
|
// the pragmas we want on every connection, and reports whether it is in-memory.
|
|
// A path that already looks like a DSN (has a scheme or query) is passed through
|
|
// untouched.
|
|
func buildDSN(path string) (dsn string, memory bool) {
|
|
memory = path == ":memory:" || strings.Contains(path, ":memory:") || strings.Contains(path, "mode=memory")
|
|
if strings.HasPrefix(path, "file:") || strings.Contains(path, "?") {
|
|
return path, memory
|
|
}
|
|
pragmas := url.Values{}
|
|
pragmas.Add("_pragma", "busy_timeout(5000)")
|
|
pragmas.Add("_pragma", "journal_mode(WAL)")
|
|
pragmas.Add("_pragma", "foreign_keys(1)")
|
|
return "file:" + path + "?" + pragmas.Encode(), memory
|
|
}
|
|
|
|
// Close closes the underlying database.
|
|
func (d *DB) Close() error { return d.sql.Close() }
|
|
|
|
// SQL exposes the underlying *sql.DB for the per-entity query files.
|
|
func (d *DB) SQL() *sql.DB { return d.sql }
|