archie-state-store import copies the State Store task database, the PocketBase event store and the Gateway conversation store, all named from db_path, into Postgres in one transaction. The bead asked for an archied command; offline maintenance of a store belongs to the binary that owns it, so it sits beside backup/restore/validate/rollback instead. It refuses rather than guesses: while the state-store or gateway ownership claim is held, when a domain it imports already holds rows (an allowlist of that domain's tables, checked after Migrate), on a completed prior import, on FK orphans, alias case collisions, duplicate bindings, events whose (at, id) order microsecond storage would change, schema gaps, and any value the target column cannot hold, naming table, row, value and rule. Historical ids are kept and every identity sequence is set from AUTOINCREMENT's high-water mark. After loading it compares key sets, event order, resource blobs and binding secrets byte for byte and the next id of every sequence, checks the source files were not written while read, and only then writes an import_completion row (migration 0005) in the same transaction. postgres.ImportComplete and legacyimport.HasLegacyData are the two halves of the boot gate: legacy data without a completed import must not serve.
50 lines
1.7 KiB
Go
50 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
|
)
|
|
|
|
func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) }
|
|
|
|
// TestRecoveryImportMovesTheInstallOnce drives the import the way an operator
|
|
// does: the configured db_path locates every legacy file, and a second run
|
|
// against the same database is refused because the first one completed.
|
|
func TestRecoveryImportMovesTheInstallOnce(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "archie.db")
|
|
st := openTaskStore(t, dbPath+"-tasks.sqlite")
|
|
seedTask(t, st, "carried over")
|
|
if err := st.Close(); err != nil {
|
|
t.Fatalf("close task store: %v", err)
|
|
}
|
|
url := pgtest.URL(t)
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "import", "-db-path", dbPath, "-database-url", url)
|
|
if code != 0 {
|
|
t.Fatalf("import exited %d; stderr = %q", code, stderr)
|
|
}
|
|
if !strings.Contains(stdout, "tasks=1") {
|
|
t.Errorf("import summary %q does not report the imported task", stdout)
|
|
}
|
|
|
|
pool, err := postgres.Open(t.Context(), url)
|
|
if err != nil {
|
|
t.Fatalf("postgres.Open: %v", err)
|
|
}
|
|
defer pool.Close()
|
|
var title string
|
|
if err := pool.QueryRow(t.Context(), "SELECT title FROM tasks").Scan(&title); err != nil || title != "carried over" {
|
|
t.Fatalf("imported task title = %q, %v; want %q", title, err, "carried over")
|
|
}
|
|
|
|
code, _, stderr = runRecoveryCmd(t, "import", "-db-path", dbPath, "-database-url", url)
|
|
if code != 1 || !strings.Contains(stderr, "already holds a completed import") {
|
|
t.Fatalf("second import exited %d, stderr %q; want 1 naming the completed import", code, stderr)
|
|
}
|
|
}
|