beads/cmd/bd/migrate_force_test.go
matt wilkie ef3ffd45b3
feat(migrate): add --force flag as CLI twin of BD_ALLOW_REMOTE_MIGRATE (#4654)
* feat(migrate): add --force flag as CLI twin of BD_ALLOW_REMOTE_MIGRATE

The remote-migrate gate (#4259) refuses to auto-apply pending schema
migrations to a remote-backed database because two clones migrating
independently fork the schema unrecoverably. Until now the only unlock
was the env var BD_ALLOW_REMOTE_MIGRATE=1, which is awkward at the CLI
and can leak into child processes (git hooks, dolt subprocesses). The
--force flag is the natural CLI form of the designated-migrator escape
hatch: it is set before both autoMigrateOnVersionBump and the main store
open in the root PersistentPreRunE via a package-level bool, so it never
leaks out of the process. The env var remains supported for scripted/CI
use and is documented as a parenthetical alongside the new primary form.

Agent-Signature: claude-sonnet-4-6-high on behalf of matt wilkie

* fix(embeddeddolt): point lenient-gate guidance at bd migrate --force

The soft-fail (warn-and-continue) gate warning for read-only and
working-set-reconcile opens still named the env-var escape hatch; align
it with the new primary form introduced for the strict gate.

Agent-Signature: claude-fable-5-high on behalf of matt wilkie

* fix(migrate): reject --force with preview flags; harden override lifecycle

Review findings on the --force flag (cross-vendor: codex gpt-5.5 + claude
opus):

- --force combined with --dry-run/--inspect must fail before the store
  opens: with the gate overridden, the open itself applies pending
  migrations, so the preview flag would be honored only after the
  destructive work it exists to prevent (codex P2).
- Set-or-clear the override unconditionally so it is self-clearing under
  any future in-process root-command reuse (reviewer nit).
- Lock the agent-safety contract against the new flag form: AgentDirective
  must not embed 'bd migrate --force' any more than the env form (nit).

Agent-Signature: claude-fable-5-high on behalf of matt wilkie
2026-07-07 15:58:18 -07:00

57 lines
1.8 KiB
Go

package main
import "testing"
// TestIsForcedMigrate covers the root-PersistentPreRunE helpers behind
// `bd migrate --force`: only the migrate and migrate-schema commands report
// forced, and --force conflicts with the preview flags (--dry-run/--inspect)
// because the gate-overridden store open would apply pending migrations
// before the preview ever ran.
func TestIsForcedMigrate(t *testing.T) {
t.Cleanup(func() {
_ = migrateCmd.Flags().Set("force", "false")
_ = migrateCmd.Flags().Set("dry-run", "false")
_ = migrateCmd.Flags().Set("inspect", "false")
_ = migrateSchemaCmd.Flags().Set("force", "false")
})
set := func(name, v string) {
t.Helper()
if err := migrateCmd.Flags().Set(name, v); err != nil {
t.Fatalf("set --%s=%s: %v", name, v, err)
}
}
if isForcedMigrate(migrateCmd) {
t.Error("force unset: want false")
}
set("force", "true")
if !isForcedMigrate(migrateCmd) {
t.Error("force set on migrate: want true")
}
if isForcedMigrate(rootCmd) {
t.Error("non-migrate command: want false even while migrate's flag is set")
}
if got := forcedMigratePreviewFlag(migrateCmd); got != "" {
t.Errorf("no preview flags set: want \"\", got %q", got)
}
set("dry-run", "true")
if got := forcedMigratePreviewFlag(migrateCmd); got != "dry-run" {
t.Errorf("dry-run set: want \"dry-run\", got %q", got)
}
set("dry-run", "false")
set("inspect", "true")
if got := forcedMigratePreviewFlag(migrateCmd); got != "inspect" {
t.Errorf("inspect set: want \"inspect\", got %q", got)
}
if err := migrateSchemaCmd.Flags().Set("force", "true"); err != nil {
t.Fatalf("set migrate schema --force: %v", err)
}
if !isForcedMigrate(migrateSchemaCmd) {
t.Error("force set on migrate schema: want true")
}
if got := forcedMigratePreviewFlag(migrateSchemaCmd); got != "" {
t.Errorf("migrate schema has no preview flags: want \"\", got %q", got)
}
}