From 557ae44dbd10935792d82b54090979d80f5f6676 Mon Sep 17 00:00:00 2001 From: Jon Langevin Date: Mon, 27 Apr 2026 14:19:30 -0400 Subject: [PATCH] fix(cli): print explicit migration dirty status --- pkg/cli/root.go | 32 +++++++++++++++++++------------- pkg/cli/root_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/pkg/cli/root.go b/pkg/cli/root.go index 66085e7..f300c63 100644 --- a/pkg/cli/root.go +++ b/pkg/cli/root.go @@ -6,6 +6,7 @@ package cli import ( "context" "fmt" + "io" "os" "strings" @@ -158,19 +159,7 @@ func newStatusCmd() *cobra.Command { if err != nil { return fmt.Errorf("migrate status: %w", err) } - if st.Current == "" { - fmt.Println("No migrations applied.") - } else { - fmt.Printf("Current: %s\n", st.Current) - } - if len(st.Pending) > 0 { - fmt.Printf("Pending: %v\n", st.Pending) - } else { - fmt.Println("No pending migrations.") - } - if st.Dirty { - fmt.Println("WARNING: database is in dirty state!") - } + writeStatus(cmd.OutOrStdout(), st) return nil }, } @@ -178,6 +167,23 @@ func newStatusCmd() *cobra.Command { return cmd } +func writeStatus(out io.Writer, st interfaces.MigrationStatus) { + if st.Current == "" { + fmt.Fprintln(out, "No migrations applied.") + } else { + fmt.Fprintf(out, "Current: %s\n", st.Current) + } + if len(st.Pending) > 0 { + fmt.Fprintf(out, "Pending: %v\n", st.Pending) + } else { + fmt.Fprintln(out, "No pending migrations.") + } + fmt.Fprintf(out, "Dirty: %t\n", st.Dirty) + if st.Dirty { + fmt.Fprintln(out, "WARNING: database is in dirty state!") + } +} + func newGotoCmd() *cobra.Command { cmd := &cobra.Command{ Use: "goto ", diff --git a/pkg/cli/root_test.go b/pkg/cli/root_test.go index 2f77330..e49d176 100644 --- a/pkg/cli/root_test.go +++ b/pkg/cli/root_test.go @@ -1,6 +1,7 @@ package cli import ( + "bytes" "database/sql" "os" "path/filepath" @@ -8,6 +9,7 @@ import ( "testing" "github.com/GoCodeAlone/workflow-plugin-migrations/pkg/testharness" + "github.com/GoCodeAlone/workflow/interfaces" _ "github.com/jackc/pgx/v5/stdlib" ) @@ -40,6 +42,29 @@ func TestRootIncludesRepairDirtyCommand(t *testing.T) { } } +func TestWriteStatusIncludesExplicitDirtyFlag(t *testing.T) { + var out bytes.Buffer + writeStatus(&out, interfaces.MigrationStatus{ + Current: "202604270001", + Dirty: false, + }) + + got := out.String() + if !strings.Contains(got, "Dirty: false") { + t.Fatalf("status output = %q; want explicit clean dirty flag", got) + } + + out.Reset() + writeStatus(&out, interfaces.MigrationStatus{ + Current: "202604270001", + Dirty: true, + }) + got = out.String() + if !strings.Contains(got, "Dirty: true") || !strings.Contains(got, "WARNING: database is in dirty state!") { + t.Fatalf("status output = %q; want explicit dirty flag and warning", got) + } +} + func TestRootIncludesValidateUpgradeCommand(t *testing.T) { root := NewRoot()