feat(state-store): one-time import of the legacy SQLite stores into Postgres
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.
This commit is contained in:
1 parent
201b96e6fe
commit
42dcc80754
19 files changed
+1743
-21
No files matched your search
@@ -0,0 +1,49 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func run() int {
|
||||
flag.StringVar(&options.Token, "token", "", "bearer token required for a non-loopback listener (defaults to [services.state].target_token)")
|
||||
flag.StringVar(&options.ReadyAddr, "ready-addr", "", "optional readiness HTTP listen address (e.g. 127.0.0.1:9091)")
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "usage: archie-state-store [flags] # serve the State Store gRPC contract\n\noffline recovery: archie-state-store <backup|restore|validate|rollback> -h\n\n")
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "usage: archie-state-store [flags] # serve the State Store gRPC contract\n\noffline recovery: archie-state-store <backup|restore|validate|rollback|import> -h\n\n")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
@@ -31,6 +31,9 @@ owns it, while backup takes a consistent snapshot of a serving store.
|
||||
configuration the daemon would boot with
|
||||
rollback -db FILE -kind KIND replay an earlier revision of a stored
|
||||
[-revision N] resource through the ordinary replace
|
||||
import [-db-path PATH] copy the legacy SQLite stores named from
|
||||
[-database-url URL] db_path into an empty Postgres database,
|
||||
[-config FILE] once; refuses while any Archie service runs
|
||||
`
|
||||
|
||||
// runRecovery parses and performs one offline recovery command, reporting the
|
||||
@@ -44,7 +47,8 @@ func runRecovery(args []string, stdout, stderr io.Writer) int {
|
||||
command := args[0]
|
||||
options := archied.StateStoreRecoveryOptions{Operation: command}
|
||||
switch command {
|
||||
case archied.RecoveryBackup, archied.RecoveryRestore, archied.RecoveryValidate, archied.RecoveryRollback:
|
||||
case archied.RecoveryBackup, archied.RecoveryRestore, archied.RecoveryValidate, archied.RecoveryRollback,
|
||||
archied.RecoveryImport:
|
||||
default:
|
||||
fmt.Fprintf(stderr, "archie-state-store: unknown command %q\n\n", command)
|
||||
fmt.Fprint(stderr, recoveryUsage)
|
||||
@@ -59,9 +63,14 @@ func runRecovery(args []string, stdout, stderr io.Writer) int {
|
||||
flags.StringVar(&options.From, "from", "", "snapshot file restore reads")
|
||||
flags.StringVar(&options.Kind, "kind", "", "control-plane resource kind rollback replays")
|
||||
flags.Int64Var(&options.Revision, "revision", 0, "revision rollback replays (default: the newest one older than the current value)")
|
||||
// Only validate asks a question about the process rather than the file, so
|
||||
// only validate takes the configuration the process would boot with.
|
||||
if command == archied.RecoveryValidate {
|
||||
// validate asks a question about the process rather than the file, and
|
||||
// import reads db_path and database_url from it, so only those two take
|
||||
// the configuration the process would boot with.
|
||||
if command == archied.RecoveryImport {
|
||||
flags.StringVar(&options.DBPath, "db-path", "", "configured db_path the legacy files are named from (default: the configuration's)")
|
||||
flags.StringVar(&options.DatabaseURL, "database-url", "", "Postgres URL to import into (default: the configuration's database_url)")
|
||||
}
|
||||
if command == archied.RecoveryValidate || command == archied.RecoveryImport {
|
||||
flags.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory the daemon boots with")
|
||||
flags.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
|
||||
}
|
||||
|
||||
@@ -356,7 +356,7 @@ func TestRecoveryValidateRunsTheGateBootRuns(t *testing.T) {
|
||||
// path in the same binary exits 0 for the same request, so a script probing the
|
||||
// recovery surface must not read a requested help as a failure.
|
||||
func TestRecoveryHelpExitsZero(t *testing.T) {
|
||||
commands := []string{archied.RecoveryBackup, archied.RecoveryRestore, archied.RecoveryValidate, archied.RecoveryRollback}
|
||||
commands := []string{archied.RecoveryBackup, archied.RecoveryRestore, archied.RecoveryValidate, archied.RecoveryRollback, archied.RecoveryImport}
|
||||
for _, command := range commands {
|
||||
code, _, stderr := runRecoveryCmd(t, command, "-h")
|
||||
if code != 0 {
|
||||
|
||||
Reference in new issue
Block a user