Add kagent db migrate CLI for out-of-band migrations#2168
Conversation
Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
…s-improvements-pt-3 Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
d104f2f to
03bf283
Compare
…s-improvements-pt-4 Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
There was a problem hiding this comment.
Pull request overview
Adds an out-of-band database migration CLI (kagent db migrate) that uses the same migration orchestration and locking behavior as server startup, enabling operators to apply/inspect/rollback migrations without booting the controller.
Changes:
- Export
migrations.WithMigratorto open/close a migrator with the orchestrator’s schema/tracking-table/lock configuration. - Introduce
go/core/pkg/cli/dbandgo/core/pkg/cli/db/migrateimplementingup/down/goto/force/status/version, including JSON status output and dirty-state refusal semantics. - Wire the
dbcommand subtree into the mainkagentCLI and add unit + container-backed tests for the new migrate commands.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| go/core/pkg/migrations/runner.go | Adds exported WithMigrator helper to share orchestrator migrator setup with CLI tooling. |
| go/core/pkg/cli/db/migrate/migrate.go | Implements the kagent db migrate command group and subcommands, output, and validation logic. |
| go/core/pkg/cli/db/migrate/migrate_test.go | Adds unit + Postgres container-backed tests covering the CLI behavior and JSON shape. |
| go/core/pkg/cli/db/db.go | Adds kagent db parent command and hides root persistent flags in help output for the subtree. |
| go/core/cli/cmd/kagent/main.go | Wires the new db command into the root CLI and selects built-in migration sources. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
…s-improvements-pt-4 Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
| func newDBCommand() *cobra.Command { | ||
| vectorEnabled := true | ||
| var envWarning string | ||
| if v := os.Getenv("DATABASE_VECTOR_ENABLED"); v != "" { |
There was a problem hiding this comment.
Can we somehow read this from the live cluster rather than requiring information from the user. We can use this as a fallback of course
There was a problem hiding this comment.
Good question, I will look into that.
There was a problem hiding this comment.
I have a way to look up the kagent's config. The only issue is that if the kubeconfig is pointing to a Kagent which is attached to a different postgres than you are connecting to via --db-url, the looked-up value of VECTOR_ENABLED could silently differ from what's in postgres. I can't detect a mismatch, but I can print it in the cli output as stderr:
❯ go run ./core/cli/cmd/kagent db migrate status
resolved vector track from cluster context "kind-cluster": configmap kagent/kagent-controller has DATABASE_VECTOR_ENABLED=false (set DATABASE_VECTOR_ENABLED to override)
9 migration(s) applied, 0 pending
core: 6 applied (at v6), 0 pending
vector: 3 applied (at v3), 0 pending
…rate CLI Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
| func migrationSources(cfg *config.Config) dbmigrate.SourcesFunc { | ||
| return func(ctx context.Context) ([]migrations.Source, error) { | ||
| vectorEnabled := true | ||
| if v := os.Getenv("DATABASE_VECTOR_ENABLED"); v != "" { |
There was a problem hiding this comment.
Why do you have this and the check inside of clusterVectorEnabled
There was a problem hiding this comment.
They are technically different sources (env var override for the cli vs the deployed controller's configmap key). The env var wins when both are set. I've made that more clear in the code.
Signed-off-by: Jeremy Alvis <jeremy.alvis@solo.io>
Description
Adds a
kagent db migratecommand group for running and inspecting database migrations out-of-band from server startup.go/core/pkg/cli/dbandgo/core/pkg/cli/db/migrate;NewCommand(sources ...migrations.Source)takes the source list explicitly so downstream consumers can wire their own sources.--db-url, falling back to the controller'sPOSTGRES_DATABASE_URLenv var. The vector track is gated on the controller'sDATABASE_VECTOR_ENABLEDenv var (default enabled).migrations.WithMigratorexport: the CLI opens migrators through the orchestrator's own schema handling, tracking-table config, and advisory-lock identity, so a CLI invocation racing a booting server serializes.up,down, andgotorefuse a dirty source and report theforceinvocation that clears it; the CLI never auto-recovers (startup remains the automatic tier).status/versionreport dirty state instead of refusing.force 0clears the version record entirely (recovery when a track's first migration fails dirty); any other value must be a shipped migration version.downon an empty track reports "no migrations to roll back" instead of golang-migrate's raw "file does not exist".status --output jsonhas a frozen field shape locked byTestStatusJSONShape.dbsubtree hides the root's server-oriented persistent flags (--kagent-url,-n,-o,--timeout, etc.); db commands talk to Postgres directly.Testing
Version:
Fresh install:
Down (Rollback):
Goto:
Force:
Goto + Up: