Files
archie/internal/app/archied/state_store_recovery_postgres.go
T
sam ae552db7e0 refactor(store)!: remove the SQLite task store, the PocketBase event store and the SQLite session store
The State Store, event capture and Gateway conversations already serve from
PostgreSQL. This deletes internal/store and internal/infrastructure/edastore,
the SQLite gateway session store and openProductionTaskStore, and the -db
recovery path on a legacy task file; backup, restore, validate and rollback
work on the database database_url names.

- The binding-secret cipher moves to internal/infrastructure/bindingcipher and
  ToolCall to internal/infrastructure/postgres.
- NewSessionStoreMemory is now a pure in-memory store that passes the shared
  SessionStore conformance suite.
- Tests run against PostgreSQL through internal/infrastructure/postgres/pgstore;
  legacyimport/legacyfixture writes the legacy files the import and the boot
  gate read.
- The cron dialect is owned in internal/domain/scheduling, so
  github.com/pocketbase/pocketbase and pocketbase/dbx leave go.mod.
  modernc.org/sqlite stays for the one-time import (legacyread).
- The UI and Messaging architecture gates ban the Postgres and legacy readers
  in place of internal/store.
2026-09-24 14:43:50 +10:00

110 lines
3.7 KiB
Go

package archied
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
)
// runPostgresRecovery performs one recovery operation on the PostgreSQL
// database the configuration names. There is no automatic schema rollback:
// goose Down drops tables, so the way back from a migration is restoring the
// snapshot taken before it, which loses every write made after that snapshot.
func runPostgresRecovery(ctx context.Context, options StateStoreRecoveryOptions) (string, error) {
cfg, err := bootConfig(ctx, options)
if err != nil {
return "", err
}
url := cfg.DatabaseURL
if url == "" {
return "", errors.New("database_url is required: the configuration names no PostgreSQL database")
}
switch options.Operation {
case RecoveryBackup:
if options.Out == "" {
return "", errors.New("backup requires -out")
}
if err := postgres.Backup(ctx, url, options.Out); err != nil {
return "", err
}
return "backed up the whole database to " + options.Out, nil
case RecoveryRestore:
if options.From == "" {
return "", errors.New("restore requires -from")
}
if err := postgres.Restore(ctx, url, options.From); err != nil {
return "", err
}
return fmt.Sprintf("restored the whole database from %s; every write made after the snapshot is gone; start the archie services again", options.From), nil
case RecoveryValidate:
return validatePostgres(ctx, url, options)
case RecoveryRollback:
if options.Kind == "" {
return "", errors.New("rollback requires -kind")
}
return rollbackPostgres(ctx, url, options)
default:
return "", fmt.Errorf("unknown recovery command %q", options.Operation)
}
}
// openRecoveryPool opens the database without migrating it: recovery inspects
// or repairs the schema it finds, and refuses one newer than this binary.
func openRecoveryPool(ctx context.Context, url string) (*pgxpool.Pool, int64, error) {
pool, err := postgres.Open(ctx, url)
if err != nil {
return nil, 0, err
}
version, err := postgres.SchemaVersion(ctx, pool)
if err == nil {
err = postgres.CheckSchemaVersion(version)
}
if err != nil {
pool.Close()
return nil, 0, err
}
return pool, version, nil
}
// validatePostgres answers whether archied would start against the database:
// the stored resources pass the writer's validation and boot's gate. An
// unmigrated database holds nothing to check; the State Store migrates and
// seeds it on its next start, so only boot's config gate applies.
func validatePostgres(ctx context.Context, url string, options StateStoreRecoveryOptions) (string, error) {
pool, version, err := openRecoveryPool(ctx, url)
if err != nil {
return "", err
}
defer pool.Close()
server, err := openStateStoreControlPlane(postgres.NewResources(pool))
if err != nil {
return "", err
}
checked, err := checkBootGate(ctx, server, version > 0, options)
if err != nil {
return "", err
}
return fmt.Sprintf("the database is valid at schema version %d; %d stored resources validate", version, checked), nil
}
// rollbackPostgres replays an earlier revision holding the State Store's
// ownership claim, so it refuses while a State Store serves and none can
// start until it finishes.
func rollbackPostgres(ctx context.Context, url string, options StateStoreRecoveryOptions) (string, error) {
pool, _, err := openRecoveryPool(ctx, url)
if err != nil {
return "", err
}
defer pool.Close()
claim, err := postgres.AcquireOwnership(ctx, pool, postgres.OwnerStateStore)
if err != nil {
return "", fmt.Errorf("rollback refused, stop the State Store first: %w", err)
}
defer func() { _ = claim.Release(context.WithoutCancel(ctx)) }()
return replayRevision(ctx, postgres.NewResources(pool), options)
}