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
136 lines
3.6 KiB
Go
136 lines
3.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"fmt"
|
|
"io/fs"
|
|
"log/slog"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var migrationsFS embed.FS
|
|
|
|
// migration is one numbered SQL file: version parsed from the filename prefix.
|
|
type migration struct {
|
|
version int
|
|
name string
|
|
sql string
|
|
}
|
|
|
|
// Migrate applies every embedded migration whose version has not yet been
|
|
// recorded, in ascending order, each in its own transaction. It is idempotent:
|
|
// a second run with no new files is a no-op. Migration files are named
|
|
// NNNN_description.sql (e.g. 0001_init.sql); NNNN is the version.
|
|
func (d *DB) Migrate(ctx context.Context) error {
|
|
if _, err := d.sql.ExecContext(ctx,
|
|
`CREATE TABLE IF NOT EXISTS schema_migrations (version INTEGER PRIMARY KEY)`,
|
|
); err != nil {
|
|
return fmt.Errorf("store: create schema_migrations: %w", err)
|
|
}
|
|
|
|
applied, err := d.appliedVersions(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
migrations, err := loadMigrations()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, m := range migrations {
|
|
if applied[m.version] {
|
|
continue
|
|
}
|
|
if err := d.applyMigration(ctx, m); err != nil {
|
|
return err
|
|
}
|
|
slog.Info("store: applied migration", "version", m.version, "name", m.name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *DB) appliedVersions(ctx context.Context) (map[int]bool, error) {
|
|
rows, err := d.sql.QueryContext(ctx, `SELECT version FROM schema_migrations`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: read schema_migrations: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
applied := map[int]bool{}
|
|
for rows.Next() {
|
|
var v int
|
|
if err := rows.Scan(&v); err != nil {
|
|
return nil, fmt.Errorf("store: scan schema_migrations: %w", err)
|
|
}
|
|
applied[v] = true
|
|
}
|
|
return applied, rows.Err()
|
|
}
|
|
|
|
func (d *DB) applyMigration(ctx context.Context, m migration) error {
|
|
tx, err := d.sql.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("store: begin migration %d: %w", m.version, err)
|
|
}
|
|
defer tx.Rollback() //nolint:errcheck // no-op after a successful commit
|
|
|
|
if _, err := tx.ExecContext(ctx, m.sql); err != nil {
|
|
return fmt.Errorf("store: apply migration %d (%s): %w", m.version, m.name, err)
|
|
}
|
|
if _, err := tx.ExecContext(ctx,
|
|
`INSERT INTO schema_migrations (version) VALUES (?)`, m.version,
|
|
); err != nil {
|
|
return fmt.Errorf("store: record migration %d: %w", m.version, err)
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// loadMigrations reads and parses every embedded migration file, sorted by
|
|
// version ascending.
|
|
func loadMigrations() ([]migration, error) {
|
|
entries, err := fs.ReadDir(migrationsFS, "migrations")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: read migrations dir: %w", err)
|
|
}
|
|
|
|
var migrations []migration
|
|
for _, e := range entries {
|
|
if e.IsDir() || !strings.HasSuffix(e.Name(), ".sql") {
|
|
continue
|
|
}
|
|
version, err := parseVersion(e.Name())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
body, err := fs.ReadFile(migrationsFS, "migrations/"+e.Name())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: read migration %s: %w", e.Name(), err)
|
|
}
|
|
migrations = append(migrations, migration{version: version, name: e.Name(), sql: string(body)})
|
|
}
|
|
|
|
sort.Slice(migrations, func(i, j int) bool {
|
|
return migrations[i].version < migrations[j].version
|
|
})
|
|
return migrations, nil
|
|
}
|
|
|
|
// parseVersion extracts the leading integer from a migration filename such as
|
|
// "0001_init.sql".
|
|
func parseVersion(name string) (int, error) {
|
|
prefix, _, ok := strings.Cut(name, "_")
|
|
if !ok {
|
|
return 0, fmt.Errorf("store: migration %q missing NNNN_ prefix", name)
|
|
}
|
|
v, err := strconv.Atoi(prefix)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("store: migration %q has non-numeric version: %w", name, err)
|
|
}
|
|
return v, nil
|
|
}
|