Files
archie/internal/store/config_snapshot.go
T
sam 3699f51f9b refactor(store): move the producer-owned contracts to internal/contracts/store/v1
The deletion gate for the UI Service (archie-core-8cda.5.6) is objective:
go list -deps from cmd/archie-ui must show no SQL/store implementation.
internal/store's hand-written contract surfaces (TaskStore and its
component interfaces, CaptureStore, MappingStore, BindingStore,
BindingDispatcher, BindingTaskCreator, ConfigSnapshotStore) move to
internal/contracts/store/v1 (package storev1), alongside the value types
they carry (CapturedEvent, ConfigSnapshot, WorkflowStat, StageStat,
DayTokens) and the sentinel errors -- whose canonical message strings are
the wire contract and are byte-identical after the move.

internal/store keeps type aliases, so daemon-side callers (archied, the
daemon, staterpc's server side, taskactions) are unaffected. The
UI-facing packages switch to storev1 directly: webui, archieui,
captureintake, gatewayrpc, and staterpc's client/values -- which drops
internal/store and modernc.org/sqlite from the archie-ui binary's import
closure.

overlay.DeniedKeys moves to internal/infrastructure/configuration for the
same reason: webui referenced the constant and that dragged the overlay
package's SQLite store in.

The chat-contract and remaining runtime-handle decoupling continues under
the same bead.
2026-09-10 03:03:33 +10:00

70 lines
2.1 KiB
Go

package store
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
const configSnapshotSchema = `
CREATE TABLE IF NOT EXISTS config_snapshot (
id INTEGER PRIMARY KEY CHECK (id = 1),
schema TEXT NOT NULL DEFAULT '',
document TEXT NOT NULL DEFAULT '',
published_at TEXT NOT NULL
);
`
// configSnapshotTimeLayout follows the rest of the store's timestamp columns:
// fixed-width, so string and chronological order agree.
const configSnapshotTimeLayout = time.RFC3339
// PutConfigSnapshot replaces the published snapshot. One running
// configuration means one row, so a republish overwrites rather than
// appending a history nothing reads.
func (s *Store) PutConfigSnapshot(ctx context.Context, snapshot ConfigSnapshot) error {
published := snapshot.PublishedAt
if published.IsZero() {
published = time.Now()
}
_, err := s.db.ExecContext(ctx, `
INSERT INTO config_snapshot (id, schema, document, published_at)
VALUES (1, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
schema=excluded.schema,
document=excluded.document,
published_at=excluded.published_at`,
snapshot.Schema, string(snapshot.Document), published.UTC().Format(configSnapshotTimeLayout))
if err != nil {
return fmt.Errorf("store: put config snapshot: %w", err)
}
return nil
}
// ConfigSnapshot returns the published snapshot. found is false, with a nil
// error, before anything has published one -- the normal state of a store
// whose daemon has not booted yet.
func (s *Store) ConfigSnapshot(ctx context.Context) (ConfigSnapshot, bool, error) {
var (
snapshot ConfigSnapshot
document string
published string
)
err := s.db.QueryRowContext(ctx, `
SELECT schema, document, published_at FROM config_snapshot WHERE id = 1`).
Scan(&snapshot.Schema, &document, &published)
if errors.Is(err, sql.ErrNoRows) {
return ConfigSnapshot{}, false, nil
}
if err != nil {
return ConfigSnapshot{}, false, fmt.Errorf("store: read config snapshot: %w", err)
}
snapshot.Document = []byte(document)
if at, err := time.Parse(configSnapshotTimeLayout, published); err == nil {
snapshot.PublishedAt = at
}
return snapshot, true, nil
}