chore(state-store): remove the one-time SQLite import and db_path

Every install serves from Postgres, so the legacyread/legacyimport packages,
the archie-state-store import command, the boot gate that refused to serve
over unimported SQLite files, and db_path (which only located those files)
go. Migration 0010 drops the import_completion table.
This commit is contained in:
sam committed 2026-09-25 00:40:48 +10:00
1 parent d61e49b2c9
commit 8c55b5d4e4
65 files changed
+138 -3350

No files matched your search

+1 -1
View File
@@ -413,7 +413,7 @@ func (b *boot) openStateStoreAdapter() error {
// database_url. The daemon reads it (session curator) and the Gateway serves
// it; only the Gateway claims serve ownership (claimGatewayOwnership).
func (b *boot) openChatSessions(ctx context.Context) error {
pool, err := openServicePool(ctx, b.cfg.DatabaseURL, b.cfg.DBPath, "the conversation store")
pool, err := openServicePool(ctx, b.cfg.DatabaseURL, "the conversation store")
if err != nil {
return fmt.Errorf("open conversation store: %w", err)
}
-6
View File
@@ -145,12 +145,6 @@ func makeUpdateService(s chatSetup) *releaseupdate.Service {
return updates
}
// conversationDBPath is the legacy SQLite conversation database the one-time
// import reads.
func conversationDBPath(taskDBPath string) string {
return taskDBPath + "-conversations.sqlite"
}
func makeChatLLMResponder(
ctx context.Context,
channel string,
@@ -2,7 +2,6 @@ package archied
import (
"log/slog"
"path/filepath"
"testing"
"github.com/samcharles93/archie-core/internal/config"
@@ -13,7 +12,6 @@ import (
func TestRemoteChatCompositionPreservesChannelTurnLedger(t *testing.T) {
cfg := config.Config{
DBPath: filepath.Join(t.TempDir(), "tasks.db"),
DatabaseURL: pgtest.URL(t),
Services: config.Services{config.ServiceNameGateway: {Target: "127.0.0.1:1"}},
}
@@ -103,8 +103,8 @@ func TestStateStoreBootsWithStaleDatabaseOwnedValues(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
body := fmt.Sprintf(
"bot_user = 'archie'\ndb_path = %q\ndatabase_url = %q\n%s[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n",
filepath.Join(dir, "state.db"), pgtest.URL(t), tt.body,
"bot_user = 'archie'\ndatabase_url = %q\n%s[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n",
pgtest.URL(t), tt.body,
)
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
-167
View File
@@ -1,167 +0,0 @@
package archied
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/legacyimport/legacyfixture"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
)
// seedLegacyConversation writes one session into the legacy SQLite
// conversation file beside dbPath, which is enough for the cutover gate to
// see legacy data.
func seedLegacyConversation(t *testing.T, dbPath string) {
t.Helper()
legacyfixture.Exec(t, conversationDBPath(dbPath), legacyfixture.GatewayDDL)
}
func testPool(t *testing.T, url string) *pgxpool.Pool {
t.Helper()
pool, err := postgres.Open(t.Context(), url)
if err != nil {
t.Fatalf("open pool: %v", err)
}
t.Cleanup(pool.Close)
if err := postgres.Migrate(t.Context(), pool, postgres.Migrations()); err != nil {
t.Fatalf("migrate: %v", err)
}
return pool
}
// TestCutoverGate pins D1.3 in both serving processes: a fresh install boots
// and records completion, legacy data without a completed import refuses with
// the operator's instruction, and a completed import boots over legacy files.
func TestCutoverGate(t *testing.T) {
processes := []struct {
name string
open func(*boot, context.Context) error
}{
{"state store", func(b *boot, ctx context.Context) error { return b.openStateStorePool(ctx) }},
{"gateway", func(b *boot, ctx context.Context) error { return b.openStores(ctx) }},
}
tests := []struct {
name string
legacy bool
complete bool
wantErr string
}{
{name: "fresh install"},
{name: "legacy data without import", legacy: true, wantErr: "run `archie-state-store import`"},
{name: "legacy data with completed import", legacy: true, complete: true},
}
for _, p := range processes {
for _, tt := range tests {
t.Run(p.name+"/"+tt.name, func(t *testing.T) {
url := pgtest.URL(t)
dbPath := filepath.Join(t.TempDir(), "archie.db")
if tt.legacy {
seedLegacyConversation(t, dbPath)
}
pool := testPool(t, url)
if tt.complete {
if _, err := pool.Exec(t.Context(),
"INSERT INTO import_completion (id, sources, report) VALUES (1, 's', 'r')"); err != nil {
t.Fatalf("seed completion: %v", err)
}
}
b := newBootstrap()
b.cfg = config.Config{DatabaseURL: url, DBPath: dbPath}
err := p.open(b, t.Context())
b.cleanup()
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("open = %v, want refusal containing %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatalf("open: %v", err)
}
done, err := postgres.ImportComplete(t.Context(), pool)
if err != nil {
t.Fatalf("ImportComplete: %v", err)
}
if !done {
t.Fatal("serving database has no completion record; a later legacy file would block it")
}
})
}
}
}
// TestFreshInstallIgnoresLaterLegacyFile pins the fresh-install record: once a
// database started with no legacy data, a stray legacy file does not block it.
func TestFreshInstallIgnoresLaterLegacyFile(t *testing.T) {
url := pgtest.URL(t)
dbPath := filepath.Join(t.TempDir(), "archie.db")
first := newBootstrap()
first.cfg = config.Config{DatabaseURL: url, DBPath: dbPath}
if err := first.openStateStorePool(t.Context()); err != nil {
t.Fatalf("fresh start: %v", err)
}
first.cleanup()
seedLegacyConversation(t, dbPath)
if _, err := os.Stat(conversationDBPath(dbPath)); err != nil {
t.Fatalf("legacy file not written: %v", err)
}
second := newBootstrap()
second.cfg = first.cfg
if err := second.openStateStorePool(t.Context()); err != nil {
t.Fatalf("restart with a stray legacy file: %v", err)
}
second.cleanup()
}
// TestGatewayRefusesASecondOwner pins D4.1: one Gateway serves a database, a
// second refuses while the first holds its claim, and one starts again once
// the first has shut down.
func TestGatewayRefusesASecondOwner(t *testing.T) {
cfg := config.Config{DatabaseURL: pgtest.URL(t), DBPath: filepath.Join(t.TempDir(), "archie.db")}
start := func() (*boot, error) {
b := newBootstrap()
b.cfg = cfg
if err := b.openChatSessions(t.Context()); err != nil {
b.cleanup()
return nil, err
}
if err := b.claimGatewayOwnership(t.Context()); err != nil {
b.cleanup()
return nil, err
}
return b, nil
}
first, err := start()
if err != nil {
t.Fatalf("first gateway: %v", err)
}
if _, err := start(); !errors.Is(err, postgres.ErrOwned) {
first.cleanup()
t.Fatalf("second gateway = %v, want ErrOwned", err)
}
first.cleanup()
third, err := start()
if err != nil {
t.Fatalf("gateway after the owner shut down: %v", err)
}
third.cleanup()
}
// TestConversationStoreFailsClosedWithoutURL: the conversation store is
// Postgres-only, so a missing database_url stops the process.
func TestConversationStoreFailsClosedWithoutURL(t *testing.T) {
b := newBootstrap()
defer b.cleanup()
err := b.openChatSessions(t.Context())
if err == nil || !strings.Contains(err.Error(), "database_url is required") {
t.Fatalf("openChatSessions = %v, want it to name the missing database_url", err)
}
}
-4
View File
@@ -283,10 +283,6 @@ func resolveForge(cfg config.Forge, secrets *secret.Registry, log *slog.Logger)
return client, token
}
func taskDBPath(configuredPath string) string {
return configuredPath + "-tasks.sqlite"
}
func manualRequeueTask(ctx context.Context, st storecontract.TaskStore, taskID int64) error {
task, err := st.TaskByID(ctx, taskID)
if err != nil {
+2 -4
View File
@@ -2,7 +2,6 @@ package archied
import (
"context"
"path/filepath"
"testing"
"github.com/samcharles93/archie-core/internal/config"
@@ -12,14 +11,14 @@ import (
)
func TestDiskProbePath_PrefersStateDir(t *testing.T) {
cfg := config.Config{StateDir: "/var/lib/archie", DBPath: "/data/archie/tasks.db", WorkDir: "/srv/archie"}
cfg := config.Config{StateDir: "/var/lib/archie", WorkDir: "/srv/archie"}
if got := diskProbePath(cfg); got != "/var/lib/archie" {
t.Fatalf("diskProbePath = %q, want /var/lib/archie", got)
}
}
func TestDiskProbePath_FallsBackToWorkDir(t *testing.T) {
cfg := config.Config{DBPath: "/data/archie/tasks.db", WorkDir: "/srv/archie"}
cfg := config.Config{WorkDir: "/srv/archie"}
if got := diskProbePath(cfg); got != "/srv/archie" {
t.Fatalf("diskProbePath = %q, want /srv/archie", got)
}
@@ -54,7 +53,6 @@ func TestSetupReadinessProbes_WiresEverySubsystem(t *testing.T) {
cfg := config.Config{
BotUser: "archie-bot",
DBPath: filepath.Join(t.TempDir(), "tasks.db"),
PollInterval: config.Duration(60_000_000_000), // 60s, satisfies validate
Dispatch: config.Dispatch{Trigger: "assignee"},
Forge: config.Forge{Type: "none"},
+1 -2
View File
@@ -4,7 +4,6 @@ import (
"context"
"log/slog"
"os"
"path/filepath"
"testing"
"github.com/samcharles93/archie-core/internal/config"
@@ -33,7 +32,7 @@ func newTestBoot(t *testing.T, tasks gateway.TaskCreator) *boot {
log: slog.New(slog.NewTextHandler(os.Stderr, nil)),
bus: bus,
doc: &configuration.Document{},
cfg: config.Config{DBPath: filepath.Join(t.TempDir(), "archie.db")},
cfg: config.Config{},
chatTasks: tasks,
defaultChatIdentity: "primary",
}
-5
View File
@@ -43,11 +43,6 @@ func configuredIdentityNames(cfg config.Config) []string {
return names
}
// edaDBPath derives the event-capture database from the configured db_path,
// beside the task database rather than inside it: the two stores have separate
// lifecycles and only meet at binding_dispatches.task_id.
func edaDBPath(configuredPath string) string { return configuredPath + "-eda.sqlite" }
// StateStoreOptions contains process inputs for the standalone State Store.
type StateStoreOptions struct {
Config string
@@ -1,56 +0,0 @@
package archied
import (
"context"
"errors"
"fmt"
"github.com/samcharles93/archie-core/internal/infrastructure/legacyimport"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
)
// legacySources names an install's three legacy SQLite files from its
// configured db_path, the same derivation the legacy openers use, so an
// operator cannot import one store and forget another.
func legacySources(dbPath string) legacyimport.Sources {
return legacyimport.Sources{
StateStore: taskDBPath(dbPath),
EDA: edaDBPath(dbPath),
Gateway: conversationDBPath(dbPath),
}
}
// importLegacy runs the one-time import of the legacy files into Postgres.
// It is an offline command of the State Store binary, not of archied: offline
// maintenance of a store belongs to the process that owns it.
func importLegacy(ctx context.Context, options StateStoreRecoveryOptions) (string, error) {
dbPath, url := options.DBPath, options.DatabaseURL
if dbPath == "" || url == "" {
cfg, err := bootConfig(ctx, options)
if err != nil {
return "", err
}
if dbPath == "" {
dbPath = cfg.DBPath
}
if url == "" {
url = cfg.DatabaseURL
}
}
if dbPath == "" {
return "", errors.New("import requires db_path (-db-path or the configuration)")
}
if url == "" {
return "", errors.New("import requires database_url (-database-url or the configuration)")
}
pool, err := postgres.Open(ctx, url)
if err != nil {
return "", err
}
defer pool.Close()
report, err := legacyimport.Import(ctx, pool, legacySources(dbPath))
if err != nil {
return "", err
}
return fmt.Sprintf("imported %s into Postgres and recorded the completion: %s", dbPath, report), nil
}
+7 -37
View File
@@ -3,24 +3,22 @@ package archied
import (
"context"
"fmt"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/samcharles93/archie-core/internal/infrastructure/legacyimport"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
)
// openStateStorePool opens the State Store process's one process-scoped
// PostgreSQL pool, applies the schema and claims serve ownership of the
// database for the process's life, so a second State Store against the same
// database refuses to start. A missing or invalid database_url, a migration
// failure or unimported legacy data is a startup failure.
// database refuses to start. A missing or invalid database_url or a migration
// failure is a startup failure.
//
// The pool is stored on boot.pg and closed at shutdown; nothing opens a second
// pool per subsystem.
func (b *boot) openStateStorePool(ctx context.Context) error {
pool, err := openServicePool(ctx, b.cfg.DatabaseURL, b.cfg.DBPath, "the State Store")
pool, err := openServicePool(ctx, b.cfg.DatabaseURL, "the State Store")
if err != nil {
b.log.Error("open state store postgres pool", "err", err)
return err
@@ -40,12 +38,11 @@ func (b *boot) openStateStorePool(ctx context.Context) error {
return nil
}
// openServicePool opens and migrates a serving process's pool and refuses to
// serve over legacy data the one-time import has not moved. service names the
// process in the error an operator reads.
func openServicePool(ctx context.Context, url, dbPath, service string) (*pgxpool.Pool, error) {
// openServicePool opens and migrates a serving process's pool. service names
// the process in the error an operator reads.
func openServicePool(ctx context.Context, url, service string) (*pgxpool.Pool, error) {
if url == "" {
return nil, fmt.Errorf("database_url is required: %s is Postgres-only and refuses to fall back to SQLite", service)
return nil, fmt.Errorf("database_url is required: %s is Postgres-only", service)
}
pool, err := postgres.Open(ctx, url)
if err != nil {
@@ -55,32 +52,5 @@ func openServicePool(ctx context.Context, url, dbPath, service string) (*pgxpool
pool.Close()
return nil, err
}
if err := refuseUnimportedLegacy(ctx, pool, dbPath); err != nil {
pool.Close()
return nil, err
}
return pool, nil
}
// refuseUnimportedLegacy is the cutover gate. A database with a completion
// record serves. Otherwise non-empty legacy SQLite files mean an upgrade whose
// import has not run, and serving would hide that data behind empty tables,
// so it refuses. With no legacy data it is a fresh install: the completion
// record is written so a legacy file that appears later cannot block it.
func refuseUnimportedLegacy(ctx context.Context, pool *pgxpool.Pool, dbPath string) error {
done, err := postgres.ImportComplete(ctx, pool)
if err != nil || done {
return err
}
sources := legacySources(dbPath)
has, err := legacyimport.HasLegacyData(ctx, sources)
if err != nil {
return fmt.Errorf("check for legacy SQLite data: %w", err)
}
if has {
return fmt.Errorf("legacy SQLite data found (%s) but this Postgres database has no completed import: "+
"stop every archie process, run `archie-state-store import`, then start archie again",
strings.Join([]string{sources.StateStore, sources.EDA, sources.Gateway}, ", "))
}
return postgres.RecordFreshInstall(ctx, pool)
}
@@ -47,7 +47,6 @@ const (
RecoveryRestore = "restore"
RecoveryValidate = "validate"
RecoveryRollback = "rollback"
RecoveryImport = "import"
)
// StateStoreRecoveryOptions are the process inputs for one offline recovery
@@ -70,21 +69,11 @@ type StateStoreRecoveryOptions struct {
// Revision is the revision rollback replays. Zero means the newest
// revision older than the one the resource carries now.
Revision int64
// DBPath and DatabaseURL are import's source and target: the configured
// db_path every legacy file is named from, and the Postgres URL. Either
// left empty is read from the configuration.
DBPath string
DatabaseURL string
}
// RunStateStoreRecovery performs one offline operation on the task database and
// returns the line the command reports on stdout.
func RunStateStoreRecovery(ctx context.Context, options StateStoreRecoveryOptions) (string, error) {
// import names its legacy sources from db_path and its target from
// database_url itself.
if options.Operation == RecoveryImport {
return importLegacy(ctx, options)
}
return runPostgresRecovery(ctx, options)
}
@@ -25,8 +25,8 @@ func pgRecoveryFixture(t *testing.T) (configPath string, pool *pgxpool.Pool, url
url = pgtest.URL(t)
dir := t.TempDir()
configPath = filepath.Join(dir, "config.toml")
body := fmt.Sprintf("bot_user = 'archie'\ndb_path = %q\ndatabase_url = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n",
filepath.Join(dir, "archie.db"), url)
body := fmt.Sprintf("bot_user = 'archie'\ndatabase_url = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n",
url)
if err := os.WriteFile(configPath, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
@@ -57,7 +57,7 @@ func TestRunStateStoreAcceptsEnvironmentToken(t *testing.T) {
t.Setenv("STATE_STORE_TOKEN", "environment-token")
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
data := fmt.Sprintf("bot_user = 'archie'\ndb_path = %q\ndatabase_url = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n[[repos]]\nowner = 'acme'\nname = 'widget'\n", filepath.Join(dir, "state.db"), pgtest.URL(t))
data := fmt.Sprintf("bot_user = 'archie'\ndatabase_url = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n[[repos]]\nowner = 'acme'\nname = 'widget'\n", pgtest.URL(t))
if err := os.WriteFile(path, []byte(data), 0o600); err != nil {
t.Fatal(err)
}
+1 -2
View File
@@ -167,7 +167,6 @@ func TestStateStoreDepsServePlaybookDispatcher(t *testing.T) {
// the file rather than an in-memory or per-process store.
func TestStateStoreDataSurvivesRestart(t *testing.T) {
cfg := config.Config{
DBPath: filepath.Join(t.TempDir(), "archie"),
DatabaseURL: pgtest.URL(t),
}
@@ -208,7 +207,7 @@ func TestStateStoreDataSurvivesRestart(t *testing.T) {
// this would mean archie.db is opened by two processes at once.
func TestOpenStoresNeverOwnsTaskDB(t *testing.T) {
b := newBootstrap()
b.cfg = config.Config{DBPath: filepath.Join(t.TempDir(), "archie"), DatabaseURL: pgtest.URL(t)}
b.cfg = config.Config{DatabaseURL: pgtest.URL(t)}
if err := b.openStores(t.Context()); err != nil {
t.Fatalf("openStores: %v", err)
}