Phase 0: turn Supermarket into a Dockerized local web app
Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
// Package db opens the app's SQLite database and keeps its schema current.
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
_ "modernc.org/sqlite" // registers the "sqlite" driver (pure Go, no cgo)
|
||||
)
|
||||
|
||||
// migrations are applied in order, once each. Never edit or reorder one that
|
||||
// has shipped: add a new one instead.
|
||||
var migrations = []string{
|
||||
// 1: settings, inventory, the content pipeline's state, and the Hermes bridge
|
||||
`
|
||||
CREATE TABLE settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE inventory (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE COLLATE NOCASE,
|
||||
relationship TEXT NOT NULL CHECK (relationship IN ('partner', 'competitor', 'watch')),
|
||||
data TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
|
||||
);
|
||||
|
||||
-- When each source was last checked (was state.json)
|
||||
CREATE TABLE fetch_state (
|
||||
key TEXT PRIMARY KEY,
|
||||
last_checked TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- Content items the user dismissed (was dismissed.json)
|
||||
CREATE TABLE dismissed (
|
||||
item_id TEXT PRIMARY KEY,
|
||||
dismissed_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
|
||||
);
|
||||
|
||||
-- Generated posts waiting to be published (was posts.json). posted_to is a
|
||||
-- JSON list of platforms already published to, so a partial failure can be
|
||||
-- retried without posting twice.
|
||||
CREATE TABLE posts (
|
||||
id TEXT PRIMARY KEY,
|
||||
inventory_name TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
source_url TEXT NOT NULL DEFAULT '',
|
||||
generated_at TEXT NOT NULL,
|
||||
posted_to TEXT NOT NULL DEFAULT '[]'
|
||||
);
|
||||
|
||||
-- Each time Hermes reaches the app through MCP
|
||||
CREATE TABLE bridge_checkins (
|
||||
id INTEGER PRIMARY KEY,
|
||||
at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
||||
agent TEXT NOT NULL,
|
||||
mcphub_tools TEXT NOT NULL DEFAULT '[]',
|
||||
note TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
`,
|
||||
}
|
||||
|
||||
// DB wraps the database with the settings helpers every package uses.
|
||||
type DB struct {
|
||||
*sql.DB
|
||||
}
|
||||
|
||||
// Open opens (creating if needed) the database at path and migrates it.
|
||||
func Open(path string) (*DB, error) {
|
||||
// WAL lets the web UI read while a background job writes; busy_timeout
|
||||
// waits out short write locks instead of failing.
|
||||
dsn := "file:" + path + "?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)&_pragma=foreign_keys(ON)"
|
||||
sqldb, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// SQLite allows one writer; a single connection avoids "database is
|
||||
// locked" between our own goroutines.
|
||||
sqldb.SetMaxOpenConns(1)
|
||||
if err := sqldb.Ping(); err != nil {
|
||||
sqldb.Close()
|
||||
return nil, fmt.Errorf("opening %s: %w", path, err)
|
||||
}
|
||||
d := &DB{sqldb}
|
||||
if err := d.migrate(); err != nil {
|
||||
sqldb.Close()
|
||||
return nil, err
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *DB) migrate() error {
|
||||
var version int
|
||||
if err := d.QueryRow(`PRAGMA user_version`).Scan(&version); err != nil {
|
||||
return err
|
||||
}
|
||||
if version > len(migrations) {
|
||||
return fmt.Errorf("the database is from a newer Supermarket (schema %d, this version knows %d); update Supermarket", version, len(migrations))
|
||||
}
|
||||
for i := version; i < len(migrations); i++ {
|
||||
tx, err := d.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(migrations[i]); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("migration %d: %w", i+1, err)
|
||||
}
|
||||
// PRAGMA can't take a bound parameter; i+1 is our own integer.
|
||||
if _, err := tx.Exec(fmt.Sprintf(`PRAGMA user_version = %d`, i+1)); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJSON reads the setting key into out. It reports false, with out
|
||||
// untouched, if the setting doesn't exist.
|
||||
func (d *DB) GetJSON(key string, out any) (bool, error) {
|
||||
var raw string
|
||||
err := d.QueryRow(`SELECT value FROM settings WHERE key = ?`, key).Scan(&raw)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(raw), out); err != nil {
|
||||
return false, fmt.Errorf("setting %s: %w", key, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutJSON stores v as the setting key.
|
||||
func (d *DB) PutJSON(key string, v any) error {
|
||||
raw, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = d.Exec(`INSERT INTO settings (key, value) VALUES (?, ?)
|
||||
ON CONFLICT (key) DO UPDATE SET value = excluded.value`, key, string(raw))
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user